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

Auto create configured schemas if they don't exist #192

Merged
merged 2 commits into from
Feb 20, 2018
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ You can adjust defaults by passing arguments to `node-pg-migrate`:

* `config-file` (`f`) - The file with migration JSON config (defaults to undefined)
* `schema` (`s`) - The schema on which migration will be run (defaults to `public`)
* `create-schema` - Create the configured schema if it doesn't exist (defaults to `false`)
* `database-url-var` (`d`) - Name of env variable with database url string (defaults to `DATABASE_URL`)
* `migrations-dir` (`m`) - The directory containing your migration files (defaults to `migrations`)
* `migrations-schema` - The schema storing table which migrations have been run (defaults to same value as `schema`)
* `create-migrations-schema` - Create the configured migrations schema if it doesn't exist (defaults to `false`)
* `migrations-table` (`t`) - The table storing which migrations have been run (defaults to `pgmigrations`)
* `ignore-pattern` - Regex pattern for file names to ignore (e.g. `ignore_file|\..*|.*\.spec\.js`)
* `migration-file-language` (`j`) - Language of the migration file to create (`js` or `ts`)
Expand Down
18 changes: 18 additions & 0 deletions bin/node-pg-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ try {
}

const schemaArg = 'schema';
const createSchemaArg = 'create-schema';
const databaseUrlVarArg = 'database-url-var';
const migrationsDirArg = 'migrations-dir';
const migrationsTableArg = 'migrations-table';
const migrationsSchemaArg = 'migrations-schema';
const createMigrationsSchemaArg = 'create-migrations-schema';
const migrationFileLanguageArg = 'migration-file-language';
const checkOrderArg = 'check-order';
const configValueArg = 'config-value';
Expand Down Expand Up @@ -71,13 +73,25 @@ const argv = yargs
type: 'string',
})

.option(createSchemaArg, {
default: false,
describe: 'Creates the configured schema if it doesn\'t exist',
type: 'boolean',
})

.option(migrationsSchemaArg, {
default: undefined,
defaultDescription: 'Same as "schema"',
describe: 'The schema storing table which migrations have been run',
type: 'string',
})

.option(createMigrationsSchemaArg, {
default: false,
describe: 'Creates the configured migration schema if it doesn\'t exist',
type: 'boolean',
})

.option(dryRunArg, {
default: false,
describe: 'Prints the SQL but doesn\'t run it',
Expand Down Expand Up @@ -153,7 +167,9 @@ let MIGRATIONS_DIR = argv[migrationsDirArg];
let DATABASE_URL = process.env[argv[databaseUrlVarArg]];
let IGNORE_PATTERN = argv[ignorePatternArg];
let SCHEMA = argv[schemaArg];
let CREATE_SCHEMA = argv[createSchemaArg];
let MIGRATIONS_SCHEMA = argv[migrationsSchemaArg];
let CREATE_MIGRATIONS_SCHEMA = argv[createMigrationsSchemaArg];
let MIGRATIONS_TABLE = argv[migrationsTableArg];
let MIGRATIONS_FILE_LANGUAGE = argv[migrationFileLanguageArg];
let CHECK_ORDER = argv[checkOrderArg];
Expand All @@ -163,8 +179,10 @@ const TIMESTAMP = argv[timestampArg];
function readJson(json) {
if (typeof json === 'object') {
SCHEMA = json[schemaArg] || SCHEMA;
CREATE_SCHEMA = json[createSchemaArg] || CREATE_SCHEMA;
MIGRATIONS_DIR = json[migrationsDirArg] || MIGRATIONS_DIR;
MIGRATIONS_SCHEMA = json[migrationsSchemaArg] || MIGRATIONS_SCHEMA;
CREATE_MIGRATIONS_SCHEMA = json[createMigrationsSchemaArg] || CREATE_MIGRATIONS_SCHEMA;
MIGRATIONS_TABLE = json[migrationsTableArg] || MIGRATIONS_TABLE;
MIGRATIONS_FILE_LANGUAGE = json[migrationFileLanguageArg] || MIGRATIONS_FILE_LANGUAGE;
CHECK_ORDER = typeof json[checkOrderArg] !== 'undefined' ? json[checkOrderArg] : CHECK_ORDER;
Expand Down
16 changes: 11 additions & 5 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ const getMigrationsToRun = (options, runNames, migrations) => {
export default (options) => {
const db = Db(options.database_url);
return Promise.resolve()
.then(() => (
options.schema
? db.query(`SET SCHEMA '${options.schema}'`)
: null
))
.then(() => {
if (options.schema) {
if (options.create_schema) {
db.query(`CREATE SCHEMA IF NOT EXISTS '${options.schema}'`);
}
db.query(`SET SCHEMA '${options.schema}'`);
}
if (options.migrations_schema && options.create_migrations_schema) {
db.query(`CREATE SCHEMA IF NOT EXISTS '${options.migrations_schema}'`);
}
})
.then(() =>
Promise
.all([
Expand Down