TableAlteration

public final class TableAlteration

The TableAlteration class lets you alter database tables.

You don’t create instances of this class. Instead, you use the Database alter(table:) method:

try db.alter(table: "player") { t in // t is TableAlteration
    t.add(column: ...)
}

See https://www.sqlite.org/lang_altertable.html

  • Appends a column to the table.

    try db.alter(table: "player") { t in
        t.add(column: "url", .text)
    }
    

    See https://www.sqlite.org/lang_altertable.html

    Declaration

    Swift

    @discardableResult
    public func add(column name: String, _ type: Database.ColumnType? = nil) -> ColumnDefinition

    Parameters

    name

    the column name.

    type

    the column type.

    Return Value

    An ColumnDefinition that allows you to refine the column definition.

  • Appends a table column defined with raw SQL.

    // ALTER TABLE player ADD COLUMN name TEXT
    try db.alter(table: "player") { t in
        t.addColumn(sql: "name TEXT")
    }
    

    Declaration

    Swift

    public func addColumn(sql: String)
  • Appends a table column defined with an SQL literal.

    Literals allow you to safely embed raw values in your SQL, without any risk of syntax errors or SQL injection:

    // ALTER TABLE player ADD COLUMN name TEXT DEFAULT 'Anonymous'
    let defaultName = "Anonymous"
    try db.alter(table: "player") { t in
        t.addColumn(literal: "name TEXT DEFAULT \(defaultName)")
    }
    

    Declaration

    Swift

    public func addColumn(literal: SQL)
  • Renames a column in a table.

    try db.alter(table: "player") { t in
        t.rename(column: "url", to: "homeURL")
    }
    

    See https://www.sqlite.org/lang_altertable.html

  • Renames a column in a table.

    try db.alter(table: "player") { t in
        t.rename(column: "url", to: "homeURL")
    }
    

    See https://www.sqlite.org/lang_altertable.html

    Declaration

    Swift

    @available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    public func rename(column name: String, to newName: String)

    Parameters

    name

    the column name to rename.

    newName

    the new name of the column.