4141 - [ Updating Rows] ( #updating-rows )
4242 - [ Deleting Rows] ( #deleting-rows )
4343 - [ Transactions and Savepoints] ( #transactions-and-savepoints )
44+ - [ Querying the Schema] ( #querying-the-schema )
4445 - [ Altering the Schema] ( #altering-the-schema )
4546 - [ Renaming Tables] ( #renaming-tables )
47+ - [ Dropping Tables] ( #dropping-tables )
4648 - [ Adding Columns] ( #adding-columns )
4749 - [ Added Column Constraints] ( #added-column-constraints )
50+ - [ Schema Changer] ( #schemachanger )
51+ - [ Renaming Columns] ( #renaming-columns )
52+ - [ Dropping Columns] ( #dropping-columns )
53+ - [ Renaming/dropping tables] ( #renamingdropping-tables )
4854 - [ Indexes] ( #indexes )
4955 - [ Creating Indexes] ( #creating-indexes )
5056 - [ Dropping Indexes] ( #dropping-indexes )
51- - [ Dropping Tables] ( #dropping-tables )
5257 - [ Migrations and Schema Versioning] ( #migrations-and-schema-versioning )
5358 - [ Custom Types] ( #custom-types )
5459 - [ Date-Time Values] ( #date-time-values )
@@ -1409,7 +1414,6 @@ for column in columns {
14091414SQLite.swift comes with several functions (in addition to `Table.create `) for
14101415altering a database schema in a type- safe manner.
14111416
1412-
14131417### Renaming Tables
14141418
14151419We can build an `ALTER TABLE … RENAME TO` statement by calling the `rename`
@@ -1420,6 +1424,24 @@ try db.run(users.rename(Table("users_old")))
14201424// ALTER TABLE "users" RENAME TO "users_old"
14211425```
14221426
1427+ ### Dropping Tables
1428+
1429+ We can build
1430+ [`DROP TABLE` statements](https :// www.sqlite.org/lang_droptable.html)
1431+ by calling the `dropTable` function on a `SchemaType`.
1432+
1433+ ```swift
1434+ try db.run (users.drop ())
1435+ // DROP TABLE "users"
1436+ ```
1437+
1438+ The `drop` function has one additional parameter, `ifExists`, which (when
1439+ `true `) adds an `IF EXISTS` clause to the statement.
1440+
1441+ ```swift
1442+ try db.run (users.drop (ifExists : true ))
1443+ // DROP TABLE IF EXISTS "users"
1444+ ```
14231445
14241446### Adding Columns
14251447
@@ -1484,57 +1506,54 @@ tables](#creating-a-table).
14841506 // ALTER TABLE "posts" ADD COLUMN "user_id" INTEGER REFERENCES "users" ("id")
14851507 ```
14861508
1487- ### Renaming Columns
1509+ ### SchemaChanger
14881510
1489- We can rename columns with the help of the `SchemaChanger` class:
1511+ Version 0.14.0 introduces `SchemaChanger`, an alternative API to perform more complex
1512+ migrations such as renaming columns. These operations work with all versions of
1513+ SQLite but use SQL statements such as `ALTER TABLE RENAME COLUMN` when available.
1514+
1515+ #### Adding Columns
14901516
14911517```swift
1518+ let newColumn = ColumnDefinition (
1519+ name : " new_text_column" ,
1520+ type : .TEXT ,
1521+ nullable : true ,
1522+ defaultValue : .stringLiteral (" foo" )
1523+ )
1524+
14921525let schemaChanger = SchemaChanger (connection : db)
1526+
14931527try schemaChanger.alter (table : " users" ) { table in
1494- table.rename ( column : " old_name " , to : " new_name " )
1528+ table.add (newColumn )
14951529}
14961530```
14971531
1498- ### Dropping Columns
1532+ #### Renaming Columns
14991533
15001534```swift
15011535let schemaChanger = SchemaChanger (connection : db)
15021536try schemaChanger.alter (table : " users" ) { table in
1503- table.drop (column : " email " )
1537+ table.rename (column : " old_name " , to : " new_name " )
15041538}
15051539```
15061540
1507- These operations will work with all versions of SQLite and use modern SQL
1508- operations such as `DROP COLUMN` when available.
1509-
1510- ### Adding Columns (SchemaChanger)
1511-
1512- The `SchemaChanger` provides an alternative API to add new columns:
1541+ #### Dropping Columns
15131542
15141543```swift
1515- let newColumn = ColumnDefinition (
1516- name : " new_text_column" ,
1517- type : .TEXT ,
1518- nullable : true ,
1519- defaultValue : .stringLiteral (" foo" )
1520- )
1521-
15221544let schemaChanger = SchemaChanger (connection : db)
1523-
15241545try schemaChanger.alter (table : " users" ) { table in
1525- table.add (newColumn )
1546+ table.drop ( column : " email " )
15261547}
15271548```
15281549
1529- ### Renaming/ dropping Tables (SchemaChanger)
1530-
1531- The `SchemaChanger` provides an alternative API to rename and drop tables:
1550+ #### Renaming/ dropping Tables
15321551
15331552```swift
15341553let schemaChanger = SchemaChanger (connection : db)
15351554
15361555try schemaChanger.rename (table : " users" , to : " users_new" )
1537- try schemaChanger.drop (table : " emails" )
1556+ try schemaChanger.drop (table : " emails" , ifExists : false )
15381557```
15391558
15401559### Indexes
@@ -1592,25 +1611,6 @@ try db.run(users.dropIndex(email, ifExists: true))
15921611// DROP INDEX IF EXISTS "index_users_on_email"
15931612```
15941613
1595- ### Dropping Tables
1596-
1597- We can build
1598- [`DROP TABLE` statements](https :// www.sqlite.org/lang_droptable.html)
1599- by calling the `dropTable` function on a `SchemaType`.
1600-
1601- ```swift
1602- try db.run (users.drop ())
1603- // DROP TABLE "users"
1604- ```
1605-
1606- The `drop` function has one additional parameter, `ifExists`, which (when
1607- `true `) adds an `IF EXISTS` clause to the statement.
1608-
1609- ```swift
1610- try db.run (users.drop (ifExists : true ))
1611- // DROP TABLE IF EXISTS "users"
1612- ```
1613-
16141614### Migrations and Schema Versioning
16151615
16161616You can use the convenience property on `Connection` to query and set the
0 commit comments