Description
Loading / Dumping Schema
A scenario I come across fairly often is the need to drop old migrations. For example, on a large codebase there might be dozens or hundreds of migrations. In these cases, I find it helpful to just delete old ones (taking care to make sure all environments including dev environments). After doing this, when new developers come on the team, they would just create an empty database from a structure.sql file (like in Rails) to get setup.
What do you think about adding the ability for node-db-migrate to dump a schema file to a directory specified in database.json after every migration? This script would:
- create a database dump
- add sql statements to insert migration rows (so future
db-migrate up
commands would work). This would copy the migrations table from the dev database
Here's a simple example of this kind of script (minus the migrations piece):
require('dotenv').load({silent: true})
var child_process = require('child_process');
var dumpOptions = [
'-s',
'-x',
'-c',
'-O',
'-f',
'db/structure.sql',
process.env.DATABASE_URL
]
var loadOptions = [
'-q',
'-f',
'db/structure.sql',
process.env.TEST_DATABASE_URL,
]
var stdOptions = { stdio: [ 0, 'inherit', 'inherit' ] }
function prepareDatabase() {
return new Promise(function (resolve, reject) {
pgDump = child_process.spawn('pg_dump', dumpOptions, stdOptions);
pgDump.on('exit', function (code, err) {
if (err) reject(err)
psql = child_process.spawn('psql', loadOptions, stdOptions);
psql.on('exit', function (code, err) {
if (err) reject(err)
resolve(code)
})
});
})
}
module.exports = prepareDatabase
if (require.main === module) {
prepareDatabase().then(function (code) {
console.log('Exited with ' + code);
})
}
Thoughts?
Auto-migrate more than one env
Another helpful feature could be to specify multiple environments. So when I run a migration I could, specify more than one environment (or maybe it just defaults to always trying to load test
??).
The use case here is that when I'm testing, and I migrate, I'll always want to prepare the test database before running tests again.
Abort on pending migrations
When running tests, it would be nice to be able to quickly diff the test database's migrations with those in the dev database. This would be less necessary if running migrations auto-migrated the test database, but still useful as a quick check.
I imagine this would be available to developers as a module, rather than through the CLI.
Thoughts on any / all of the above? If I wrote these features, what are the chances they'd be merged?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.