Migration

public class Migration

A class to help with migration between two versions of a table.

Usage Example:

The suggested usage is to keep versions of the table classes somewhere in the application code:

public class MyTable_v0: Table {
    let a = Column("a", ...)
    let b = Column("b", ...)
    let tableName = "MyTable"
}
public class MyTable_v1: Table {
    let b = Column("b", ...)
    let c = Column("c", ...)
    let tableName = "MyTable"
}

And use a typealias to refer to the current version of the table class in the application:

typealias MyTable = MyTable_v0
let t = MyTable()
let q = Select(from: t)
...

The migration code from v0 to v1 should be something like this:

let t0 = MyTable_v0()
let t1 = MyTable_v1()
let migration0 = Migration(from: t0, to: t1, using: connection)
migration0.alterTableAdd(column: t1.c) { result in ... }

And raw alternations, if needed:

let dropColumnQuery = "ALTER TABLE " + t1.tableName + " DROP COLUMN " + t0.a.name
connection.execute(dropColumnQuery) { result in ... }

Initializer

  • Initialize an instance of Migration with the tables you are migrating between and the connection to the database, where the tables are located.

    Usage Example:

    let connection = getConnection()
    let oldTable = MyTable_v0()
    let newTable = MyTable_v1()
    let migration = Migration(from: oldTable, to: newTable, using: connection)
    

    Declaration

    Swift

    public init(from: Table, to: Table, using connection: Connection)

    Parameters

    from

    The version of the table to migrate from.

    to

    The version of the table to migrate to.

Table Alteration

  • Builds an executes an SQL query to alter the name of the old table to the name in the new version of the table.

    Usage Example:

    In this example, a Migration instance is initialized. The alterTableName function is called to initiate the SQL query, which changes the name. The “responseHandler” is a closure which handles the response from the database.

    let migration = Migration(from: oldTable, to: newTable, using: connection)
    migration.alterTableName(responseHandler)
    

    Declaration

    Swift

    public func alterTableName(onCompletion: @escaping ((QueryResult) -> ()))

    Parameters

    onCompletion

    The function to be called when the execution of the query has completed.

  • Create and execute an SQL query to add a Column to the new table in the Migration instance.

    Usage Example:

    In this example, Migration and Column instances are initialized. The alterTableAdd function is called to add a new column to newTable within the Migration instance and a closure responseHandler is passed in to handle the response from the database.

    let migration = Migration(from: oldTable, to: newTable, using: connection)
    let toDo_title = Column("toDo_title", String.self, notNull: true)
    migration.alterTableAdd(column: toDo_title, onCompletion: responseHandler)
    

    Declaration

    Swift

    public func alterTableAdd(column: Column, onCompletion: @escaping ((QueryResult) -> ()))

    Parameters

    column

    The column to add. This should be a column in the new version of the table.

    onCompletion

    The function to be called when the execution of the query has completed.