Skip to content

Commit f2aa6b3

Browse files
committed
Improve the documentation for table recreation
1 parent 71e09a3 commit f2aa6b3

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

GRDB/Documentation.docc/Migrations.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,39 @@ migrator.registerMigration("Add NOT NULL check on author.name") { db in
133133

134134
The detailed sequence of operations for recreating a database table from a migration is:
135135

136-
1. When relevant, remember the format of all indexes, triggers, and views associated with table X. This information will be needed in steps 6 and 7 below. One way to do this is to run a query like the following: `SELECT type, sql FROM sqlite_schema WHERE tbl_name='X'`.
136+
1. When relevant, remember the format of all indexes, triggers, and views associated with table `X`. This information will be needed in steps 6 below. One way to do this is to run the following statement and examine the output in the console:
137137

138-
2. Use `CREATE TABLE` to construct a new table "new_X" that is in the desired revised format of table X. Make sure that the name "new_X" does not collide with any existing table name, of course.
138+
```swift
139+
try db.dumpSQL("SELECT type, sql FROM sqlite_schema WHERE tbl_name='X'")
140+
```
141+
142+
2. Construct a new table `new_X` that is in the desired revised format of table `X`. Make sure that the name `new_X` does not collide with any existing table name, of course.
143+
144+
```swift
145+
try db.create(table: "new_X") { t in ... }
146+
```
139147

140-
3. Transfer content from X into new_X using a statement like: `INSERT INTO new_X SELECT ... FROM X`.
148+
3. Transfer content from `X` into `new_X` using a statement like:
141149

142-
4. Drop the old table X: `DROP TABLE X`.
150+
```swift
151+
try db.execute(sql: "INSERT INTO new_X SELECT ... FROM X")
152+
```
143153

144-
5. Change the name of new_X to X using: `ALTER TABLE new_X RENAME TO X`.
154+
4. Drop the old table `X`:
155+
156+
```swift
157+
try db.drop(table: "X")
158+
```
159+
160+
5. Change the name of `new_X` to `X` using:
161+
162+
```swift
163+
try db.rename(table: "new_X", to: "X")
164+
```
145165

146-
6. When relevant, use `CREATE INDEX`, `CREATE TRIGGER`, and `CREATE VIEW` to reconstruct indexes, triggers, and views associated with table X. Perhaps use the old format of the triggers, indexes, and views saved from step 3 above as a guide, making changes as appropriate for the alteration.
166+
6. When relevant, reconstruct indexes, triggers, and views associated with table `X`.
147167

148-
7. If any views refer to table X in a way that is affected by the schema change, then drop those views using `DROP VIEW` and recreate them with whatever changes are necessary to accommodate the schema change using `CREATE VIEW`.
168+
7. If any views refer to table `X` in a way that is affected by the schema change, then drop those views using `DROP VIEW` and recreate them with whatever changes are necessary to accommodate the schema change using `CREATE VIEW`.
149169

150170
> Important: When recreating a table, be sure to follow the above procedure exactly, in the given order, or you might corrupt triggers, views, and foreign key constraints.
151171
>

0 commit comments

Comments
 (0)