ALB Studio

Migrations

Declare your tables as numbered versions that ALBCore runs once, in order.

Docs updated · What changed

On this page

Each plugin declares its tables as numbered versions. ALBCore stores the version each plugin has reached in alb_schema_versions and runs only the missing steps, in order, each in its own transaction.

ExampleLink to this section

Kotlin
db.migrate("MyPlugin") {
    version(1, "Create players table") {
        execute("""CREATE TABLE ${table("level_players")} (
            uuid CHAR(36) NOT NULL PRIMARY KEY,
            level INT NOT NULL DEFAULT 1,
            xp BIGINT NOT NULL DEFAULT 0)""")
    }
    version(2, "Add prestige") {
        execute("ALTER TABLE ${table("level_players")} ADD COLUMN prestige INT NOT NULL DEFAULT 0")
    }
    version(3, "Backend-specific index") {
        mysql("CREATE INDEX ${table("lp_level")} ON ${table("level_players")} (level) USING BTREE")
        sqlite("CREATE INDEX ${table("lp_level")} ON ${table("level_players")} (level)")
    }
}

RulesLink to this section

  • Call migrate in onEnable; it blocks until done. If it throws MigrationException, disable your plugin.
  • Never change or renumber a version that has run on a server. Add a new version instead.
  • MySQL commits CREATE/ALTER immediately, so a failed step can be half applied there. Keep one DDL statement per version. SQLite rolls back the whole step.
  • Servers sharing one MySQL database take a lock while migrating, so they never run the same step twice.

Type a class, method, config key or topic. Use the arrow keys and Enter to open a result.