You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: GRDB/Documentation.docc/Migrations.md
+27-7
Original file line number
Diff line number
Diff line change
@@ -133,19 +133,39 @@ migrator.registerMigration("Add NOT NULL check on author.name") { db in
133
133
134
134
The detailed sequence of operations for recreating a database table from a migration is:
135
135
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:
137
137
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 isin 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
+
```
139
147
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:
141
149
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
+
```
143
153
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
+
```
145
165
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`.
147
167
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`.
149
169
150
170
> 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.
0 commit comments