CityRP Documentation Repository

Migrations

Database schema migrations runner. Runs ordered SQL migrations against DB_MAIN exactly once per database, tracked via a schema_migrations table created on demand. Migration files live in gamemode/core/migrations/, named NNNN_description.lua, each returning a table with up(done) and down(done) functions.

Functions

ServerMigrations.EnsureTable(cb)Source ☍

Ensure the schema_migrations tracking table exists.

Parameters

  • cbfun(ok: boolean)?Called once the table has been created (or already existed).
ServerMigrations.GetApplied(cb)Source ☍

Get applied migrations, keyed by id.

Parameters

  • cbfun(ok: boolean, applied: table<number, table>)Called with the read result; applied maps id to {id=, name=, applied_at=}.
ServerMigrations.Load()Source ☍

Load all migration files from core/migrations, keyed by id.

ServerMigrations.Redo(id, cb)Source ☍

Down then up a single migration id (or the latest applied id if omitted).

Parameters

  • idnumber?Migration id to redo; defaults to the latest applied id.
  • cbfun(ok: boolean)?Called with the overall result.
ServerMigrations.RunDown(count, cb)Source ☍

Revert the last count applied migrations (default 1), most-recent first. Each migration's down(done) callback must call done(ok) itself; a failed revert stops the run and leaves its schema_migrations row in place.

Parameters

  • countnumber?How many migrations to revert. Defaults to 1.
  • cbfun(ok: boolean)?Called with the overall result.
ServerMigrations.RunPending(cb)Source ☍

Run all pending migrations in ascending id order. Each migration's up(done) callback must call done(ok) itself; a migration that fails is not recorded as applied and stops the run.

Parameters

  • cbfun(ok: boolean, didApplyAny: boolean)Called with the overall result.
ServerMigrations.RunSequence(queries, done)Source ☍

Run a sequence of raw SQL statements in order. Stops on the first failed statement and does not call done(true).

Parameters

  • queriesstring[]Array of SQL strings.
  • donefun(ok: boolean)Called with the result. Legacy callers that ignore the argument still work when ok is true.
ServerMigrations.Status(cb)Source ☍

Get a status snapshot of applied and pending migrations.

Parameters

  • cbfun(status: table)Called with {applied = {...}, pending = {...}}, or {applied = {}, pending = {}, error = true} on read failure.

Tables

ServerMigrations.filesSource ☍

Fields

ServerMigrationsSource ☍