Skip to content

Commit 288b3ac

Browse files
authored
Make the sample migration not destroy data (#962)
1 parent ed73175 commit 288b3ac

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

docs/fluent/schema.md

+22-3
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,34 @@ We can make the necessary database schema adjustments with the following migrati
380380
```swift
381381
struct UserNameMigration: AsyncMigration {
382382
func prepare(on database: Database) async throws {
383+
try await database.schema("users")
384+
.field("first_name", .string, .required)
385+
.field("last_name", .string, .required)
386+
.update()
387+
388+
// It is not currently possible to express this update without using custom SQL.
389+
// This also doesn't try to deal with splitting the name into first and last,
390+
// as that requires database-specific syntax.
391+
try await User.query(on: database)
392+
.set(["first_name": .sql(embed: "name"))
393+
.run()
394+
383395
try await database.schema("users")
384396
.deleteField("name")
385-
.field("first_name", .string)
386-
.field("last_name", .string)
387397
.update()
388398
}
389399

390400
func revert(on database: Database) async throws {
391-
try await database.schema("users").delete()
401+
try await database.schema("users")
402+
.field("name", .string, .required)
403+
.update()
404+
try await User.query(on: database)
405+
.set(["name": .sql(embed: "concat(first_name, ' ', last_name)"))
406+
.run()
407+
try await database.schema("users")
408+
.deleteField("first_name")
409+
.deleteField("last_name")
410+
.update()
392411
}
393412
}
394413
```

0 commit comments

Comments
 (0)