Skip to content

Commit 0eb58d8

Browse files
committed
Doc updates
1 parent fc8f8df commit 0eb58d8

File tree

7 files changed

+78
-50
lines changed

7 files changed

+78
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.14.0 (tbd), [diff][diff-0.14.0]
22
========================================
3+
For breaking changes, see [Upgrading.md](Documentation/Upgrading.md).
34

45
* Support more complex schema changes and queries ([#1073][], [#1146][] [#1148][])
56
* Support `ATTACH`/`DETACH` ([#30][], [#1142][])

Documentation/Index.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@
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 {
14091414
SQLite.swift comes with several functions (in addition to `Table.create`) for
14101415
altering a database schema in a type-safe manner.
14111416

1412-
14131417
### Renaming Tables
14141418

14151419
We 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+
14921525
let schemaChanger = SchemaChanger(connection: db)
1526+
14931527
try 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
15011535
let schemaChanger = SchemaChanger(connection: db)
15021536
try 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-
15221544
let schemaChanger = SchemaChanger(connection: db)
1523-
15241545
try 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
15341553
let schemaChanger = SchemaChanger(connection: db)
15351554

15361555
try 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

16161616
You can use the convenience property on `Connection` to query and set the

Documentation/Release.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [ ] Make sure current master branch has a green build
44
* [ ] Make sure `SQLite.playground` runs without errors
55
* [ ] Make sure `CHANGELOG.md` is up-to-date
6+
* [ ] Add content to `Documentation/Upgrading.md` if needed
67
* [ ] Update the version number in `SQLite.swift.podspec`
78
* [ ] Run `pod lib lint` locally
89
* [ ] Update the version numbers mentioned in `README.md`, `Documentation/Index.md`

Documentation/Upgrading.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Upgrading
2+
3+
## 0.13 → 0.14
4+
5+
- `Expression.asSQL()` is no longer available. Expressions now implement `CustomStringConvertible`,
6+
where `description` returns the SQL.
7+
- `Statement.prepareRowIterator()` is now longer available. Instead, use the methods
8+
of the same name on `Connection`.
9+
- `Blob` no longer wraps byte arrays and now uses `NSData`, which enables memory and
10+
performance improvements.
11+
- `Connection.registerTokenizer` is no longer available to register custom FTS4 tokenizers.

SQLite.playground/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ for user in try Array(rowIterator) {
5252

5353
/// also with `map()`
5454
let mapRowIterator = try db.prepareRowIterator(users)
55+
5556
let userIds = try mapRowIterator.map { $0[id] }
5657

5758
/// using `failableNext()` on `RowIterator`

Sources/SQLite/Schema/SchemaChanger.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public class SchemaChanger: CustomStringConvertible {
141141
}
142142
}
143143

144-
public func drop(table: String) throws {
145-
try dropTable(table)
144+
public func drop(table: String, ifExists: Bool = true) throws {
145+
try dropTable(table, ifExists: ifExists)
146146
}
147147

148148
// Beginning with release 3.25.0 (2018-09-15), references to the table within trigger bodies and
@@ -192,7 +192,7 @@ public class SchemaChanger: CustomStringConvertible {
192192

193193
private func moveTable(from: String, to: String, options: Options = .default, operation: Operation? = nil) throws {
194194
try copyTable(from: from, to: to, options: options, operation: operation)
195-
try dropTable(from)
195+
try dropTable(from, ifExists: true)
196196
}
197197

198198
private func copyTable(from: String, to: String, options: Options = .default, operation: Operation?) throws {
@@ -225,8 +225,8 @@ public class SchemaChanger: CustomStringConvertible {
225225
}
226226
}
227227

228-
private func dropTable(_ table: String) throws {
229-
try connection.run("DROP TABLE IF EXISTS \(table.quote())")
228+
private func dropTable(_ table: String, ifExists: Bool) throws {
229+
try connection.run("DROP TABLE \(ifExists ? "IF EXISTS" : "") \(table.quote())")
230230
}
231231

232232
private func copyTableContents(from: TableDefinition, to: TableDefinition) throws {

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ class SchemaChangerTests: SQLiteTestCase {
135135
}
136136
}
137137

138+
func test_drop_table_if_exists_true() throws {
139+
try schemaChanger.drop(table: "xxx", ifExists: true)
140+
}
141+
142+
func test_drop_table_if_exists_false() throws {
143+
XCTAssertThrowsError(try schemaChanger.drop(table: "xxx", ifExists: false)) { error in
144+
if case Result.error(let message, _, _) = error {
145+
XCTAssertEqual(message, "no such table: xxx")
146+
} else {
147+
XCTFail("unexpected error \(error)")
148+
}
149+
}
150+
}
151+
138152
func test_rename_table() throws {
139153
try schemaChanger.rename(table: "users", to: "users_new")
140154
let users_new = Table("users_new")

0 commit comments

Comments
 (0)