Migrations Library

The migrations library is a simple helper for running functions once per database schema.

The entire system is based around a single table, migrations, which has 2 fields, migration_id and created_at. The migrations table itself is the only one which is created with an if-not-exists check, all other migrations can be created as if they will be only ran once.

Migrations files are lua files, with the name of sv_<file creation unix timestamp>_<name>.lua, in the <GMFOLDER>/gamemode/core/migrations folder, or an individual plugin's <PLUGIN>/migrations folder. So, for example, a full timestamp might be <GMFOLDER>/gamemode/core/migrations/sv_12359324_create-users-table.lua. This will resolve to a migration ID of 12359324_create-users-table.

Migrations are ran from oldest to newest (as defined by the migraiton timestamp), only if they have not been ran before.

The migrations file themselves are simple, with only a call to migrations.register, with one argument, a table with members up and down, both functions which take one argument, the function done. As an example, here.

cityrp.migrations.register({
    up = function(done)
        print("making changes to the schema")
        return done()
    end,
    down = function(done)
        print("reverting changes, if possible.")
        return done()
    end
})

Or, as a more complicated example, we have a function which takes callbacks.

cityrp.migrations.register({
    up = function(done)
        print("starting the migration")
        functionWhichTakesACallback(function()
            print("we've done the thing, now let's mark ourselves as done!")
            done()
        end)
    end,
    down = function(done)
        print("reverting changes, if possible.")
        return done()
    end
})