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

feat: adds glob support for dir and ignorePattern #1274

Merged
merged 10 commits into from
Sep 23, 2024
14 changes: 11 additions & 3 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const schemaArg = 'schema';
const createSchemaArg = 'create-schema';
const databaseUrlVarArg = 'database-url-var';
const migrationsDirArg = 'migrations-dir';
const useGlobArg = 'use-glob';
const migrationsTableArg = 'migrations-table';
const migrationsSchemaArg = 'migrations-schema';
const createMigrationsSchemaArg = 'create-migrations-schema';
Expand Down Expand Up @@ -84,9 +85,14 @@ const parser = yargs(process.argv.slice(2))
[migrationsDirArg]: {
alias: 'm',
defaultDescription: '"migrations"',
describe: 'The directory containing your migration files',
describe: `The directory name or glob pattern containing your migration files (resolved from cwd()). When using glob pattern, "${useGlobArg}" must be used as well`,
type: 'string',
},
[useGlobArg]: {
defaultDescription: 'false',
describe: `Use glob to find migration files. This will use "${migrationsDirArg}" _and_ "${ignorePatternArg}" to glob-search for migration files.`,
type: 'boolean',
},
[migrationsTableArg]: {
alias: 't',
defaultDescription: '"pgmigrations"',
Expand Down Expand Up @@ -128,7 +134,7 @@ const parser = yargs(process.argv.slice(2))
},
[ignorePatternArg]: {
defaultDescription: '"\\..*"',
describe: 'Regex pattern for file names to ignore',
describe: `Regex or glob pattern for migration files to be ignored. When using glob pattern, "${useGlobArg}" must be used as well`,
type: 'string',
},
[decamelizeArg]: {
Expand Down Expand Up @@ -253,6 +259,7 @@ if (dotenv) {
}

let MIGRATIONS_DIR = argv[migrationsDirArg];
let USE_GLOB = argv[useGlobArg];
let DB_CONNECTION: string | ConnectionParameters | ClientConfig | undefined =
process.env[argv[databaseUrlVarArg]];
let IGNORE_PATTERN = argv[ignorePatternArg];
Expand Down Expand Up @@ -469,11 +476,11 @@ const action = argv._.shift();

// defaults
MIGRATIONS_DIR ??= join(cwd(), 'migrations');
USE_GLOB ??= false;
MIGRATIONS_FILE_LANGUAGE ??= 'js';
MIGRATIONS_FILENAME_FORMAT ??= 'timestamp';
MIGRATIONS_TABLE ??= 'pgmigrations';
SCHEMA ??= ['public'];
IGNORE_PATTERN ??= '\\..*';
CHECK_ORDER ??= true;
VERBOSE ??= true;

Expand Down Expand Up @@ -583,6 +590,7 @@ if (action === 'create') {
},
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
dir: MIGRATIONS_DIR!,
useGlob: USE_GLOB,
ignorePattern: IGNORE_PATTERN,
schema: SCHEMA,
createSchema: CREATE_SCHEMA,
Expand Down
49 changes: 25 additions & 24 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ which takes options argument with the following structure (similar to [command l
> [!NOTE]
> If you use `dbClient`, you should not use `databaseUrl` at the same time and vice versa.

| Option | Type | Description |
| ------------------------ | ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `databaseUrl` | `string or object` | Connection string or client config which is passed to [new pg.Client](https://node-postgres.com/api/client#constructor) |
| `dbClient` | `pg.Client` | Instance of [new pg.Client](https://node-postgres.com/api/client). Instance should be connected to DB, and after finishing migration, user is responsible to close connection |
| `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 or array[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 |
| `direction` | `enum` | `up` or `down` |
| `count` | `number` | Amount of migration to run |
| `timestamp` | `boolean` | Treats `count` as timestamp |
| `ignorePattern` | `string` | Regex pattern for file names to ignore (ignores files starting with `.` by default) |
| `file` | `string` | Run-only migration with this name |
| `singleTransaction` | `boolean` | Combines all pending migrations into a single transaction so that if any migration fails, all will be rolled back (defaults to `true`) |
| `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 |
| `fake` | `boolean` | Mark migrations as run without actually performing them (use with caution!) |
| `dryRun` | `boolean` | |
| `log` | `function` | Redirect log messages to this function, rather than `console` |
| `logger` | `object with debug/info/warn/error methods` | Redirect messages to this logger object, rather than `console` |
| `verbose` | `boolean` | Print all debug messages like DB queries run (if you switch it on, it will disable `logger.debug` method) |
| `decamelize` | `boolean` | Runs [`decamelize`](https://github.com/salsita/node-pg-migrate/blob/main/src/utils/decamelize.ts) on table/column/etc. names |
| Option | Type | Description |
| ------------------------ | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `databaseUrl` | `string or object` | Connection string or client config which is passed to [new pg.Client](https://node-postgres.com/api/client#constructor) |
| `dbClient` | `pg.Client` | Instance of [new pg.Client](https://node-postgres.com/api/client). Instance should be connected to DB, and after finishing migration, user is responsible to close connection |
| `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 or array[string]` | The schema on which migration will be run (defaults to `public`) |
| `dir` | `string or array[string]` | The directory containing your migration files. This path is resolved from `cwd()`. Alternatively, provide a [glob](https://www.npmjs.com/package/glob) pattern or an array of glob patterns and set `useGlob = true`. Note: enabling glob will read both, `dir` _and_ `ignorePattern` as glob patterns |
| `useGlob` | `boolean` | Use [glob](https://www.npmjs.com/package/glob) to find migration files. This will use `dir` _and_ `ignorePattern` to glob-search for migration files. Note: enabling glob will read both, `dir` _and_ `ignorePattern` as glob patterns |
| `checkOrder` | `boolean` | Check order of migrations before running them |
| `direction` | `enum` | `up` or `down` |
| `count` | `number` | Amount of migration to run |
| `timestamp` | `boolean` | Treats `count` as timestamp |
| `ignorePattern` | `string or array[string]` | Regex pattern for file names to ignore (ignores files starting with `.` by default). Alternatively, provide a [glob](https://www.npmjs.com/package/glob) pattern or an array of glob patterns and set `isGlob = true`. Note: enabling glob will read both, `dir` _and_ `ignorePattern` as glob patterns |
| `file` | `string` | Run-only migration with this name |
| `singleTransaction` | `boolean` | Combines all pending migrations into a single transaction so that if any migration fails, all will be rolled back (defaults to `true`) |
| `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 |
| `fake` | `boolean` | Mark migrations as run without actually performing them (use with caution!) |
| `dryRun` | `boolean` | |
| `log` | `function` | Redirect log messages to this function, rather than `console` |
| `logger` | `object with debug/info/warn/error methods` | Redirect messages to this logger object, rather than `console` |
| `verbose` | `boolean` | Print all debug messages like DB queries run (if you switch it on, it will disable `logger.debug` method) |
| `decamelize` | `boolean` | Runs [`decamelize`](https://github.com/salsita/node-pg-migrate/blob/main/src/utils/decamelize.ts) on table/column/etc. names |
Loading