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: support migrations in subdirectories #1226

Closed
wants to merge 4 commits 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
23 changes: 21 additions & 2 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const schemaArg = 'schema';
const createSchemaArg = 'create-schema';
const databaseUrlVarArg = 'database-url-var';
const migrationsDirArg = 'migrations-dir';
const migrationsSubdirsArg = 'migrations-subdirs';
const migrationsTableArg = 'migrations-table';
const migrationsSchemaArg = 'migrations-schema';
const createMigrationsSchemaArg = 'create-migrations-schema';
Expand Down Expand Up @@ -80,6 +81,11 @@ const parser = yargs(process.argv.slice(2))
describe: 'The directory containing your migration files',
type: 'string',
},
[migrationsSubdirsArg]: {
defaultDescription: 'false',
describe: 'Search for migrations in subdirectories',
type: 'boolean',
},
[migrationsTableArg]: {
alias: 't',
defaultDescription: '"pgmigrations"',
Expand Down Expand Up @@ -149,7 +155,7 @@ const parser = yargs(process.argv.slice(2))
},
[migrationFilenameFormatArg]: {
defaultDescription: '"timestamp"',
choices: ['timestamp', 'utc'],
choices: ['timestamp', 'utc', 'year/utc', 'year/timestamp'],
describe:
'Prefix type of migration filename (Only valid with the create action)',
type: 'string',
Expand Down Expand Up @@ -236,6 +242,7 @@ if (dotenv) {
}

let MIGRATIONS_DIR = argv[migrationsDirArg];
let MIGRATIONS_SUBDIRS = argv[migrationsSubdirsArg];
let DB_CONNECTION: string | ConnectionParameters | ClientConfig | undefined =
process.env[argv[databaseUrlVarArg]];
let IGNORE_PATTERN = argv[ignorePatternArg];
Expand Down Expand Up @@ -352,6 +359,12 @@ function readJson(json: unknown): void {
);
CREATE_SCHEMA = applyIf(CREATE_SCHEMA, createSchemaArg, json, isBoolean);
MIGRATIONS_DIR = applyIf(MIGRATIONS_DIR, migrationsDirArg, json, isString);
MIGRATIONS_SUBDIRS = applyIf(
MIGRATIONS_SUBDIRS,
migrationsSubdirsArg,
json,
isBoolean
);
MIGRATIONS_SCHEMA = applyIf(
MIGRATIONS_SCHEMA,
migrationsSchemaArg,
Expand Down Expand Up @@ -381,7 +394,11 @@ function readJson(json: unknown): void {
MIGRATIONS_FILENAME_FORMAT,
migrationFilenameFormatArg,
json,
(val): val is `${FilenameFormat}` => val === 'timestamp' || val === 'utc'
(val): val is `${FilenameFormat}` =>
val === 'timestamp' ||
val === 'utc' ||
val === 'year/utc' ||
val === 'year/timestamp'
);
TEMPLATE_FILE_NAME = applyIf(
TEMPLATE_FILE_NAME,
Expand Down Expand Up @@ -444,6 +461,7 @@ const action = argv._.shift();

// defaults
MIGRATIONS_DIR ??= join(cwd(), 'migrations');
MIGRATIONS_SUBDIRS ??= false;
MIGRATIONS_FILE_LANGUAGE ??= 'js';
MIGRATIONS_FILENAME_FORMAT ??= 'timestamp';
MIGRATIONS_TABLE ??= 'pgmigrations';
Expand Down Expand Up @@ -558,6 +576,7 @@ if (action === 'create') {
},
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
dir: MIGRATIONS_DIR!,
migrationSubdirs: MIGRATIONS_SUBDIRS,
ignorePattern: IGNORE_PATTERN,
schema: SCHEMA,
createSchema: CREATE_SCHEMA,
Expand Down
51 changes: 26 additions & 25 deletions docs/src/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,32 @@ More on that below.

You can adjust defaults by passing arguments to `node-pg-migrate`:

| Argument | Aliases | Default | Description |
| --------------------------- | ------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `config-file` | `f` | `undefined` | The file with migration JSON config |
| `config-value` | | `db` | Name of config section with db options |
| `schema` | `s` | `public` | The schema(s) on which migration will be run, used to set `search_path` |
| `create-schema` | | `false` | Create the configured schema if it doesn't exist |
| `database-url-var` | `d` | `DATABASE_URL` | Name of env variable with database url string |
| `migrations-dir` | `m` | `migrations` | The directory containing your migration files |
| `migrations-schema` | | same value as `schema` | The schema storing table which migrations have been run |
| `create-migrations-schema` | | `false` | Create the configured migrations schema if it doesn't exist |
| `migrations-table` | `t` | `pgmigrations` | The table storing which migrations have been run |
| `ignore-pattern` | | `undefined` | Regex pattern for file names to ignore |
| `migration-filename-format` | | `utc` | Choose prefix of file, `utc` (`20200605075829074`) or `timestamp` (`1591343909074`) |
| `migration-file-language` | `j` | `js` | Language of the migration file to create (`js`, `ts` or `sql`) |
| `template-file-name` | | `undefined` | Utilize a custom migration template file with language inferred from its extension. The file should export the up method, accepting a MigrationBuilder instance. |
| `tsconfig` | | `undefined` | Path to tsconfig.json. Used to setup transpiling of TS migration files. (Also sets `migration-file-language` to typescript, if not overridden) |
| `envPath` | | `same level where it's invoked` | Retrieve the path to a .env file. This feature proves handy when dealing with nested projects or when referencing a global .env file. |
| `timestamp` | | `false` | Treats number argument to up/down migration as timestamp (running up migrations less or equal to timestamp or down migrations greater or equal to timestamp) |
| `check-order` | | `true` | Check order of migrations before running them, to switch it off supply `--no-check-order` |
| `single-transaction` | | `true` | Combines all pending migrations into a single transaction so that if any migration fails, all will be rolled back, to switch it off supply `--no-single-transaction` |
| `no-lock` | | `false` | Disables locking mechanism and checks |
| `fake` | | `false` | Mark migrations as run without actually performing them, (use with caution!) |
| `decamelize` | | `false` | Runs `decamelize` on table/column/etc. names |
| `verbose` | | `true` | Print all debug messages like DB queries run, to switch it off supply `--no-verbose` |
| `reject-unauthorized` | | `undefined` | Sets ssl `rejectUnauthorized` parameter. Use for e.g. self-signed certificates on the server. [see](https://node-postgres.com/announcements#2020-02-25) |
| Argument | Aliases | Default | Description |
| --------------------------- | ------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `config-file` | `f` | `undefined` | The file with migration JSON config |
| `config-value` | | `db` | Name of config section with db options |
| `schema` | `s` | `public` | The schema(s) on which migration will be run, used to set `search_path` |
| `create-schema` | | `false` | Create the configured schema if it doesn't exist |
| `database-url-var` | `d` | `DATABASE_URL` | Name of env variable with database url string |
| `migrations-dir` | `m` | `migrations` | The directory containing your migration files |
| `migrations-subdirs` | | `false` | Search for migration files in subdirectories |
| `migrations-schema` | | same value as `schema` | The schema storing table which migrations have been run |
| `create-migrations-schema` | | `false` | Create the configured migrations schema if it doesn't exist |
| `migrations-table` | `t` | `pgmigrations` | The table storing which migrations have been run |
| `ignore-pattern` | | `undefined` | Regex pattern for file names to ignore |
| `migration-filename-format` | | `utc` | Choose prefix of file, `utc` (`20200605075829074`) or `timestamp` (`1591343909074`), or either format prefixed with `year/` (`2020/20200605075829074` or `2020/1591343909074`) |
| `migration-file-language` | `j` | `js` | Language of the migration file to create (`js`, `ts` or `sql`) |
| `template-file-name` | | `undefined` | Utilize a custom migration template file with language inferred from its extension. The file should export the up method, accepting a MigrationBuilder instance. |
| `tsconfig` | | `undefined` | Path to tsconfig.json. Used to setup transpiling of TS migration files. (Also sets `migration-file-language` to typescript, if not overridden) |
| `envPath` | | `same level where it's invoked` | Retrieve the path to a .env file. This feature proves handy when dealing with nested projects or when referencing a global .env file. |
| `timestamp` | | `false` | Treats number argument to up/down migration as timestamp (running up migrations less or equal to timestamp or down migrations greater or equal to timestamp) |
| `check-order` | | `true` | Check order of migrations before running them, to switch it off supply `--no-check-order` |
| `single-transaction` | | `true` | Combines all pending migrations into a single transaction so that if any migration fails, all will be rolled back, to switch it off supply `--no-single-transaction` |
| `no-lock` | | `false` | Disables locking mechanism and checks |
| `fake` | | `false` | Mark migrations as run without actually performing them, (use with caution!) |
| `decamelize` | | `false` | Runs `decamelize` on table/column/etc. names |
| `verbose` | | `true` | Print all debug messages like DB queries run, to switch it off supply `--no-verbose` |
| `reject-unauthorized` | | `undefined` | Sets ssl `rejectUnauthorized` parameter. Use for e.g. self-signed certificates on the server. [see](https://node-postgres.com/announcements#2020-02-25) |

For SSL connection to DB you can set `PGSSLMODE` environment variable to value
from [list](https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNECT-SSLMODE) other
Expand Down
Loading
Loading