@@ -380,15 +380,34 @@ We can make the necessary database schema adjustments with the following migrati
380
380
``` swift
381
381
struct UserNameMigration : AsyncMigration {
382
382
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
+
383
395
try await database.schema (" users" )
384
396
.deleteField (" name" )
385
- .field (" first_name" , .string )
386
- .field (" last_name" , .string )
387
397
.update ()
388
398
}
389
399
390
400
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 ()
392
411
}
393
412
}
394
413
```
0 commit comments