Sqlite.Migrate
Apply or preview migrations to a SQLite database.
The permissions needed to run migrations.
secureContextis used to hash migrations and create hash chains.
A single migration represented as a name and set of SQL statements to be applied to the database.
All strings in statements must contain a single SQL statement. Additional
statements in the same string are silently ignored, will not be executed when the
migration happens, and do not throw errors. This is the default behavior of the
Sqlite.execute function which this package uses to apply the given migrations
to the database. However, these additional statements will be used to calculate the
hash of this migration. As a result, once applied to a database, these no-op statements
will need to be kept around or else corrupt the hash chain.
While this is not necessarily going to corrupt your database, it may make it difficult to understand the state of a database by looking at its migrations or may cause your runtime code to expect a specific database schema that isn't accurate. You've been warned!
For example, if you want to create two tables in a single migration, it might look something like this:
{ name = "Add tables"
, statements =
[ "CREATE TABLE IF NOT EXISTS menu (id INTEGER)"
, "CREATE TABLE IF NOT EXISTS dishes (id INTEGER)"
]
}
Do not do this:
{ name = "Add tables"
, statements =
[ "CREATE TABLE IF NOT EXISTS menu (id INTEGER); CREATE TABLE IF NOT EXISTS dishes (id INTEGER)"
]
}
Run migrations
Run a set of given migrations (Array Migration) in order on the given
database. This will:
- Open a savepoint, making this function safe to call from inside an existing SQLite transaction.
- Create a new
__gren_sqlite_migratetable if it does not already exist. - Retrieve any existing migrations from the
__gren_sqlite_migratetable. - Check the given migrations to ensure there aren't any issues that will cause problems or corruption.
- Check the given migration hash chain against the databases hash chain, ensuring everything checks out and there are no deviations. This also builds a list of migrations that still need to be applied.
- Applies the still pending migrations and updates the
__gren_sqlite_migratetable with a record of those applied.
While the SQL savepoint helps ensure the SQL runs without issue (and properly rolls back all changes if necessary), it's highly recommended that you run this function completely separate from other SQL.
Preview migrations
The result of running the preview function.
appliedare the migrations the database already records as applied, in order.pendingare the migrations that would be applied ifmigratewere to be called with the same arguments, in order.
Run the migration pipeline up to (but not including) applying any pending
migrations. Instead of running those migrations, produce a Preview record.
Errors
Errors that can happen when running or previewing a migration.
SqliteErroris an error with the Gren SQLite library. This is most likely to happen due to problems with the given SQL statements in a migration.LocalMigrationsEmptyErroris when theArray Migrationsasked for by thepreviewandmigratefunctions is empty ([]).MoreRemoteThanLocalMigrationsErroris when there are more applied migrations in the database than there are in a set of migrations. This doesn't make sense! The local migrations given to themigrateorpreviewfunctions must always be equal to or greater than the migrations already applied to the database.EmptyMigrationNameErroris when a migration's name is empty or contains only whitespace. Each migration must have a non-empty name.ConflictingMigrationNameErroris when two or more migrations share the same name (after trimming surrounding whitespace). Each migration name must be unique.EmptyMigrationStatementsErroris when the givenstatementsin aMigrationare empty ([]).MigrationStatementMentionsMigrationTableErroris when one or more of thestatementsin a migration contain an exact reference to the__gren_sqlite_migratetable. While unlikely to happen, this prevents accidentally altering the migrations ledger data which would cause problems!MigrationHashesDoNotMatchErroris when a migration hash does not match a hash in the database. This points towards a migration having been altered from when it was first applied to the database.MigrationChainDivergedErroris when the calculated hash chain in a set of migrations does not align with the hash chain in the database. This points to the order of migrations previously applied being changed.MigrationHashDecodingFailedErroris when there is a problem with decoding the base64 encoded strings for migration and chain hashes in the database. This points towards data corruption, where the data on the database may have been changed outside of the functions in this module.