Testing database schema migrations #1368
-
Hello! I have run into an issue where dates greater than 9999 have entered my database. I've written a migration to fix the issue, and now I'm in the testing phase. I'm using Swift Package Manager with a testTarget to copy over a test
This passes, but only once. The reason it fails after the first time is because the database file seems to be fixed after the first run. Running a clean in Xcode fixes the issue, but again, only for one run. I'm not sure if this is an SPM bug, but I'm wondering if there's another way to provide an 'in-memory database based on an existing database file' in GRDB. Is that possible? Am I holding it wrong? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @dawilliams-gpsw, Migrations modify their input database. If you want to run your test several times, you need to leave the original database intact, and therefore to migrate a copy of the original. One way to do it to copy the database file to some temporary location on disk, with Another way to do it is to open a connection to the original file, and backup this connection into a temporary in-memory connection: let url = Bundle.module.url(...)!
// Make an in-memory copy
let dbQueue = try DatabaseQueue()
let originalDbQueue = try DatabaseQueue(path: url.path)
try originalDbQueue.backup(to: dbQueue)
// Proceed with the test
let dbClient = DatabaseClient(dbQueue, ...) |
Beta Was this translation helpful? Give feedback.
Hello @dawilliams-gpsw,
Migrations modify their input database. If you want to run your test several times, you need to leave the original database intact, and therefore to migrate a copy of the original.
One way to do it to copy the database file to some temporary location on disk, with
FileManager
, and run your test on a database connection to the new file.Another way to do it is to open a connection to the original file, and backup this connection into a temporary in-memory connection: