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: ...)
}
-
Appends a column to the table.
try db.alter(table: "player") { t in t.add(column: "url", .text) }
-
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") }
-
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)") }
-
Renames a column in a table.
try db.alter(table: "player") { t in t.rename(column: "url", to: "homeURL") }
-
Drops a column from the table.
try db.alter(table: "player") { t in t.drop(column: "age") }