Skip to content

Testing database schema migrations #1368

Answered by groue
dawilliams-gpsw asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dawilliams-gpsw
Comment options

Answer selected by dawilliams-gpsw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants