Skip to content

Commit

Permalink
using camel case in API (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolezel committed Feb 20, 2018
1 parent 1bf2f03 commit d6ab0b9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ You can use babel or typescript for transpiling migration files. It requires a l
Alongside with command line, you can use `node-pg-migrate` also programmatically. It exports runner function,
which takes options argument with following structure (similar to [command line arguments](#configuration)):
* `database_url` _[string or object]_ - Connection string or client config which is passed to [new pg.Client](https://node-postgres.com/api/client#new-client-config-object-)
* `migrations_table` _[string]_ - The table storing which migrations have been run
* `migrations_schema` _[string]_ - The schema storing table which migrations have been run (defaults to same value as `schema`)
* `databaseUrl` _[string or object]_ - Connection string or client config which is passed to [new pg.Client](https://node-postgres.com/api/client#new-client-config-object-)
* `migrationsTable` _[string]_ - The table storing which migrations have been run
* `migrationsSchema` _[string]_ - The schema storing table which migrations have been run (defaults to same value as `schema`)
* `schema` _[string]_ - The schema on which migration will be run (defaults to `public`)
* `dir` _[string]_ - The directory containing your migration files
* `checkOrder` _[boolean]_ - Check order of migrations before running them
Expand All @@ -160,6 +160,8 @@ which takes options argument with following structure (similar to [command line
* `ignorePattern` _[string]_ - Regex pattern for file names to ignore
* `file` _[string]_ - Run only migration with this name
* `typeShorthands` _[object]_ - Object with column type shorthands
* `createSchema` _[boolean]_ - Creates the configured schema if it doesn't exist
* `createMigrationsSchema` _[boolean]_ - Creates the configured migration schema if it doesn't exist
* `noLock` _[boolean]_ - Disables locking mechanism and checks
* `dryRun` _[boolean]_
Expand Down
18 changes: 9 additions & 9 deletions bin/node-pg-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const argv = yargs
.option('d', {
alias: databaseUrlVarArg,
default: 'DATABASE_URL',
describe: 'Name of env variable where is set the database_url',
describe: 'Name of env variable where is set the databaseUrl',
type: 'string',
})

Expand Down Expand Up @@ -244,10 +244,10 @@ if (action === 'create') {
process.exit(1);
}
unlockRunner({
database_url: DATABASE_URL,
databaseUrl: DATABASE_URL,
schema: SCHEMA,
migrations_schema: MIGRATIONS_SCHEMA,
migrations_table: MIGRATIONS_TABLE,
migrationsSchema: MIGRATIONS_SCHEMA,
migrationsTable: MIGRATIONS_TABLE,
})
.then(() => {
console.log('Unlocked!');
Expand Down Expand Up @@ -287,19 +287,19 @@ if (action === 'create') {

const options = (direction, count = numMigrations, timestamp = TIMESTAMP) => ({
dryRun,
database_url: DATABASE_URL,
databaseUrl: DATABASE_URL,
dir: MIGRATIONS_DIR,
ignorePattern: IGNORE_PATTERN,
schema: SCHEMA,
migrations_schema: MIGRATIONS_SCHEMA,
migrations_table: MIGRATIONS_TABLE,
migrationsSchema: MIGRATIONS_SCHEMA,
migrationsTable: MIGRATIONS_TABLE,
count,
timestamp,
file: migrationName,
checkOrder: CHECK_ORDER,
typeShorthands: TYPE_SHORTHANDS,
create_schema: CREATE_SCHEMA,
create_migrations_schema: CREATE_MIGRATIONS_SCHEMA,
createSchema: CREATE_SCHEMA,
createMigrationsSchema: CREATE_MIGRATIONS_SCHEMA,
direction,
noLock,
});
Expand Down
14 changes: 8 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ export interface ClientConfig extends ConnectionConfig {
}

export interface RunnerOption {
database_url: string | ClientConfig
migrations_table: string
migrations_schema?: string
databaseUrl: string | ClientConfig
migrationsTable: string
migrationsSchema?: string
schema?: string
dir: string
checkOrder?: boolean
Expand All @@ -406,15 +406,17 @@ export interface RunnerOption {
file?: string
dryRun?: boolean
typeShorthands?: { [name: string]: ColumnDefinition }
createSchema?: boolean
createMigrationsSchema?: boolean
noLock?: boolean
}

export default function (options: RunnerOption): Promise<void>

export interface UnlockRunnerOption {
database_url: string | ClientConfig
migrations_table: string
migrations_schema?: string
databaseUrl: string | ClientConfig
migrationsTable: string
migrationsSchema?: string
schema?: string
}

Expand Down
4 changes: 2 additions & 2 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class Migration {
switch (action) {
case this.down:
this.log(`### MIGRATION ${this.name} (DOWN) ###`);
sqlSteps.push(`DELETE FROM "${schema}"."${this.options.migrations_table}" WHERE name='${this.name}';`);
sqlSteps.push(`DELETE FROM "${schema}"."${this.options.migrationsTable}" WHERE name='${this.name}';`);
break;
case this.up:
this.log(`### MIGRATION ${this.name} (UP) ###`);
sqlSteps.push(`INSERT INTO "${schema}"."${this.options.migrations_table}" (name, run_on) VALUES ('${this.name}', NOW());`);
sqlSteps.push(`INSERT INTO "${schema}"."${this.options.migrationsTable}" (name, run_on) VALUES ('${this.name}', NOW());`);
break;
default:
throw new Error('Unknown direction');
Expand Down
24 changes: 12 additions & 12 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const lock = (db, options) => {
db
.select(`SELECT obj_description(c.oid) as "comment"
FROM pg_class c join pg_namespace n ON (c.relnamespace = n.oid)
WHERE c.relname = '${options.migrations_table}' and c.relkind = 'r' and n.nspname = '${schema}'`)
WHERE c.relname = '${options.migrationsTable}' and c.relkind = 'r' and n.nspname = '${schema}'`)
.then((rows) => {
if (rows.length > 1) {
throw new Error('More then one migration table');
Expand All @@ -73,28 +73,28 @@ const lock = (db, options) => {
});

return db.query('BEGIN')
.then(() => db.query(`LOCK "${schema}"."${options.migrations_table}" IN ACCESS EXCLUSIVE MODE`))
.then(() => db.query(`LOCK "${schema}"."${options.migrationsTable}" IN ACCESS EXCLUSIVE MODE`))
.then(getCurrentLockName)
.then((currentLockName) => {
if (currentLockName) {
throw new Error('Another migration is already running');
}
})
.then(() => db.query(`COMMENT ON TABLE "${schema}"."${options.migrations_table}" IS '${lockName}'`))
.then(() => db.query(`COMMENT ON TABLE "${schema}"."${options.migrationsTable}" IS '${lockName}'`))
.then(() => db.query('COMMIT'));
};

const unlock = (db, options) => {
const schema = getMigrationTableSchema(options);
return db.query(`COMMENT ON TABLE "${schema}"."${options.migrations_table}" IS NULL`);
return db.query(`COMMENT ON TABLE "${schema}"."${options.migrationsTable}" IS NULL`);
};

const getRunMigrations = (db, options) => {
const schema = getMigrationTableSchema(options);
return db.select(`SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}' AND table_name = '${options.migrations_table}'`)
return db.select(`SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}' AND table_name = '${options.migrationsTable}'`)
.then(migrationTables =>
(migrationTables && migrationTables.length === 1)
|| db.query(`CREATE TABLE "${schema}"."${options.migrations_table}" ( id SERIAL, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`)
|| db.query(`CREATE TABLE "${schema}"."${options.migrationsTable}" ( id SERIAL, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`)
)
.then(() => (
!options.noLock
Expand All @@ -107,7 +107,7 @@ const getRunMigrations = (db, options) => {
: null
))
.then(() =>
db.column(`SELECT ${nameColumn} FROM "${schema}"."${options.migrations_table}" ORDER BY ${runOnColumn}`, nameColumn)
db.column(`SELECT ${nameColumn} FROM "${schema}"."${options.migrationsTable}" ORDER BY ${runOnColumn}`, nameColumn)
)
.catch((err) => {
throw new Error(`Unable to fetch migrations: ${err.stack}`);
Expand Down Expand Up @@ -142,18 +142,18 @@ const getMigrationsToRun = (options, runNames, migrations) => {
};

export default (options) => {
const db = Db(options.database_url);
const db = Db(options.databaseUrl);
return Promise.resolve()
.then(() => {
let promise = Promise.resolve();
if (options.schema) {
if (options.create_schema) {
if (options.createSchema) {
promise = promise.then(() => db.query(`CREATE SCHEMA IF NOT EXISTS '${options.schema}'`));
}
promise = promise.then(() => db.query(`SET SCHEMA '${options.schema}'`));
}
if (options.migrations_schema && options.create_migrations_schema) {
promise = promise.then(() => db.query(`CREATE SCHEMA IF NOT EXISTS '${options.migrations_schema}'`));
if (options.migrationsSchema && options.createMigrationsSchema) {
promise = promise.then(() => db.query(`CREATE SCHEMA IF NOT EXISTS '${options.migrationsSchema}'`));
}
return promise;
})
Expand Down Expand Up @@ -207,7 +207,7 @@ export default (options) => {
};

export const unlockRunner = (options) => {
const db = Db(options.database_url);
const db = Db(options.databaseUrl);
return unlock(db, options)
.then(...finallyPromise(db.close));
};
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const opTemplate = (strings, ...keys) => {
return result.join('');
};

export const getMigrationTableSchema = options => options.migrations_schema || options.schema || 'public';
export const getMigrationTableSchema = options => options.migrationsSchema || options.schema || 'public';

export const finallyPromise = func => [
func,
Expand Down
2 changes: 1 addition & 1 deletion test/migration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const actionsPromise = require(`./${promiseMigration}`); // eslint-disable-line
describe('lib/migration', () => {
const dbMock = {};
const log = () => null;
const options = { migrations_table: migrationsTable };
const options = { migrationsTable };
let migration;

beforeEach(() => {
Expand Down

0 comments on commit d6ab0b9

Please sign in to comment.