Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose schemaTable option from postgrator #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions command-line-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const optionDefinitions = [
name: 'migration-directory', description: "A directory to run migration files from. Default: 'migrations''",
alias: 'm', type: String, typeLabel: '{underline directory}', defaultValue: 'migrations',
},
{
name: 'schema-table', description: "The name of the table where the version of the database will be stored. Default: 'schemaversion'",
alias: 'b', type: String, typeLabel: '{underline name}', defaultValue: 'schemaversion',
},
{
name: 'secure', description: 'Secure connection (Azure). Default: false',
alias: 's', type: Boolean,
Expand Down
7 changes: 4 additions & 3 deletions postgrator-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function getPostgratorConfigFromCommandLineArgs(commandLineArgs) {
database: commandLineArgs.database,
username: commandLineArgs.username,
password: commandLineArgs.password,
schemaTable: commandLineArgs['schema-table'],
options: { encrypt: commandLineArgs.secure || false },
};
}
Expand Down Expand Up @@ -137,7 +138,7 @@ function areConflictingMigrations(migrationA, migrationB) {
&& migrationA.filename !== migrationB.filename;
}

function migrate(postgrator, to, detectVersionConflicts, migrationDirectory) {
function migrate(postgrator, to, detectVersionConflicts, migrationDirectory, schemaTable) {
let toVersion;

return postgrator.getMigrations()
Expand All @@ -150,7 +151,7 @@ function migrate(postgrator, to, detectVersionConflicts, migrationDirectory) {
.then((migrateToVersion) => {
toVersion = migrateToVersion;
return postgrator.getDatabaseVersion().catch(() => {
logMessage('table schemaversion does not exist - creating it.');
logMessage(`table ${schemaTable} does not exist - creating it.`);
return 0;
});
})
Expand Down Expand Up @@ -282,7 +283,7 @@ function run(commandLineArgs, callback) {
(migration) => logMessage(`running ${migration.filename}`)
);

const migratePromise = migrate(postgrator, migrateTo, detectVersionConflicts, postgratorConfig.migrationDirectory);
const migratePromise = migrate(postgrator, migrateTo, detectVersionConflicts, postgratorConfig.migrationDirectory, postgratorConfig.schemaTable);

promiseToCallback(migratePromise, (err, migrations) => {
// connection is closed, or will close in the case of SQL Server
Expand Down
34 changes: 33 additions & 1 deletion test/postgrator-cli-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ function removeVersionTable(options, callback) {
console.log(`\n----- ${config.driver} removing tables -----`);
const Postgrator = require('../node_modules/postgrator/postgrator.js');
const pg = new Postgrator(config);
const versionTable = options['schema-table'];

promiseToCallback(pg.runQuery('DROP TABLE IF EXISTS schemaversion, animal, person'), (err) => {
promiseToCallback(pg.runQuery(`DROP TABLE IF EXISTS ${versionTable}, animal, person`), (err) => {
assert.ifError(err);
callback(err);
});
Expand Down Expand Up @@ -465,6 +466,36 @@ function buildTestsForOptions(options) {
return callback();
});
});

tests.push((callback) => {
options['schema-table'] = 'myschemaversion';
removeVersionTable(options, (err) => {
assert.ifError(err);
return callback();
});
});

tests.push((callback) => {
console.log('\n----- testing migration to 001 with custom version table-----');
options['schema-table'] = 'myschemaversion';
options.to = 1;
postgratorCli.run(options, (err, migrations) => {
assert.ifError(err);
assert.equal(migrations.length, 1);
assert.equal(migrations[0].version, 1);
return callback();
});
});

tests.push(resetMigrations);

tests.push((callback) => {
options['schema-table'] = 'myschemaversion';
removeVersionTable(options, (err) => {
assert.ifError(err);
return callback();
});
});
}

const options = {
Expand All @@ -476,6 +507,7 @@ const options = {
username: 'postgrator',
password: 'postgrator',
'migration-directory': 'test/migrations',
'schema-table': 'schemaversion',
};

// Command line parameters
Expand Down