Socket
Socket
Sign inDemoInstall

node-pg-migrate

Package Overview
Dependencies
Maintainers
3
Versions
176
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-pg-migrate - npm Package Compare versions

Comparing version 7.0.0-alpha.1 to 7.0.0-alpha.2

dist/utils/createSchemalize.d.ts

386

bin/node-pg-migrate.js
#!/usr/bin/env node
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __getOwnPropNames = Object.getOwnPropertyNames;
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {

@@ -10,3 +10,3 @@ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;

var _connectionparameters = require('pg/lib/connection-parameters'); var _connectionparameters2 = _interopRequireDefault(_connectionparameters);
var _yargs = require('yargs'); var _yargs2 = _interopRequireDefault(_yargs);
var _yargs = require('yargs/yargs'); var _yargs2 = _interopRequireDefault(_yargs);
var _dist = require('../dist'); var _dist2 = _interopRequireDefault(_dist);

@@ -23,3 +23,6 @@ var require_node_pg_migrate = __commonJS({

} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") {
if (
// @ts-expect-error: TS doesn't know about code property
(err == null ? void 0 : err.code) !== "MODULE_NOT_FOUND"
) {
throw err;

@@ -54,110 +57,139 @@ }

const envPathArg = "envPath";
const { argv } = _yargs2.default.usage("Usage: $0 [up|down|create|redo] [migrationName] [options]").option("d", {
alias: databaseUrlVarArg,
default: "DATABASE_URL",
describe: "Name of env variable where is set the databaseUrl",
type: "string"
}).option("m", {
alias: migrationsDirArg,
defaultDescription: '"migrations"',
describe: "The directory containing your migration files",
type: "string"
}).option("t", {
alias: migrationsTableArg,
defaultDescription: '"pgmigrations"',
describe: "The table storing which migrations have been run",
type: "string"
}).option("s", {
alias: schemaArg,
defaultDescription: '"public"',
describe: "The schema on which migration will be run (defaults to `public`)",
type: "string",
array: true
}).option(createSchemaArg, {
defaultDescription: "false",
describe: "Creates the configured schema if it doesn't exist",
type: "boolean"
}).option(migrationsSchemaArg, {
defaultDescription: 'Same as "schema"',
describe: "The schema storing table which migrations have been run",
type: "string"
}).option(createMigrationsSchemaArg, {
defaultDescription: "false",
describe: "Creates the configured migration schema if it doesn't exist",
type: "boolean"
}).option(checkOrderArg, {
defaultDescription: "true",
describe: "Check order of migrations before running them",
type: "boolean"
}).option(verboseArg, {
defaultDescription: "true",
describe: "Print debug messages - all DB statements run",
type: "boolean"
}).option(ignorePatternArg, {
defaultDescription: '"\\..*"',
describe: "Regex pattern for file names to ignore",
type: "string"
}).option(decamelizeArg, {
defaultDescription: "false",
describe: "Runs decamelize on table/columns/etc names",
type: "boolean"
}).option(configValueArg, {
default: "db",
describe: "Name of config section with db options",
type: "string"
}).option("f", {
alias: configFileArg,
describe: "Name of config file with db options",
type: "string"
}).option("j", {
alias: migrationFileLanguageArg,
defaultDescription: 'last one used or "js" if there is no migration yet',
choices: ["js", "ts", "sql"],
describe: "Language of the migration file (Only valid with the create action)",
type: "string"
}).option(migrationFilenameFormatArg, {
defaultDescription: '"timestamp"',
choices: ["timestamp", "utc"],
describe: "Prefix type of migration filename (Only valid with the create action)",
type: "string"
}).option(templateFileNameArg, {
describe: "Path to template for creating migrations",
type: "string"
}).option(tsconfigArg, {
describe: "Path to tsconfig.json file",
type: "string"
}).option(envPathArg, {
describe: "Path to the .env file that should be used for configuration",
type: "string"
}).option(dryRunArg, {
default: false,
describe: "Prints the SQL but doesn't run it",
type: "boolean"
}).option(fakeArg, {
default: false,
describe: "Marks migrations as run",
type: "boolean"
}).option(singleTransactionArg, {
default: true,
describe: "Combines all pending migrations into a single database transaction so that if any migration fails, all will be rolled back",
type: "boolean"
}).option(lockArg, {
default: true,
describe: "When false, disables locking mechanism and checks",
type: "boolean"
}).option(rejectUnauthorizedArg, {
defaultDescription: "false",
describe: "Sets rejectUnauthorized SSL option",
type: "boolean"
}).option(timestampArg, {
default: false,
describe: "Treats number argument to up/down migration as timestamp",
type: "boolean"
const parser = _yargs2.default.call(void 0, process.argv.slice(2)).usage("Usage: $0 [up|down|create|redo] [migrationName] [options]").options({
[databaseUrlVarArg]: {
alias: "d",
default: "DATABASE_URL",
description: "Name of env variable where is set the databaseUrl",
type: "string"
},
[migrationsDirArg]: {
alias: "m",
defaultDescription: '"migrations"',
describe: "The directory containing your migration files",
type: "string"
},
[migrationsTableArg]: {
alias: "t",
defaultDescription: '"pgmigrations"',
describe: "The table storing which migrations have been run",
type: "string"
},
[schemaArg]: {
alias: "s",
defaultDescription: '"public"',
describe: "The schema on which migration will be run (defaults to `public`)",
type: "string",
array: true
},
[createSchemaArg]: {
defaultDescription: "false",
describe: "Creates the configured schema if it doesn't exist",
type: "boolean"
},
[migrationsSchemaArg]: {
defaultDescription: 'Same as "schema"',
describe: "The schema storing table which migrations have been run",
type: "string"
},
[createMigrationsSchemaArg]: {
defaultDescription: "false",
describe: "Creates the configured migration schema if it doesn't exist",
type: "boolean"
},
[checkOrderArg]: {
defaultDescription: "true",
describe: "Check order of migrations before running them",
type: "boolean"
},
[verboseArg]: {
defaultDescription: "true",
describe: "Print debug messages - all DB statements run",
type: "boolean"
},
[ignorePatternArg]: {
defaultDescription: '"\\..*"',
describe: "Regex pattern for file names to ignore",
type: "string"
},
[decamelizeArg]: {
defaultDescription: "false",
describe: "Runs decamelize on table/columns/etc names",
type: "boolean"
},
[configValueArg]: {
default: "db",
describe: "Name of config section with db options",
type: "string"
},
[configFileArg]: {
alias: "f",
describe: "Name of config file with db options",
type: "string"
},
[migrationFileLanguageArg]: {
alias: "j",
defaultDescription: 'last one used or "js" if there is no migration yet',
choices: ["js", "ts", "sql"],
describe: "Language of the migration file (Only valid with the create action)",
type: "string"
},
[migrationFilenameFormatArg]: {
defaultDescription: '"timestamp"',
choices: ["timestamp", "utc"],
describe: "Prefix type of migration filename (Only valid with the create action)",
type: "string"
},
[templateFileNameArg]: {
describe: "Path to template for creating migrations",
type: "string"
},
[tsconfigArg]: {
describe: "Path to tsconfig.json file",
type: "string"
},
[envPathArg]: {
describe: "Path to the .env file that should be used for configuration",
type: "string"
},
[dryRunArg]: {
default: false,
describe: "Prints the SQL but doesn't run it",
type: "boolean"
},
[fakeArg]: {
default: false,
describe: "Marks migrations as run",
type: "boolean"
},
[singleTransactionArg]: {
default: true,
describe: "Combines all pending migrations into a single database transaction so that if any migration fails, all will be rolled back",
type: "boolean"
},
[lockArg]: {
default: true,
describe: "When false, disables locking mechanism and checks",
type: "boolean"
},
[rejectUnauthorizedArg]: {
defaultDescription: "false",
describe: "Sets rejectUnauthorized SSL option",
type: "boolean"
},
[timestampArg]: {
default: false,
describe: "Treats number argument to up/down migration as timestamp",
type: "boolean"
}
}).version().alias("version", "i").help();
const argv = parser.parseSync();
if (argv.help || argv._.length === 0) {
_yargs2.default.showHelp();
parser.showHelp();
process.exit(1);
}
const envPath = argv[envPathArg];
const dotenvConfig = { silent: true };
const dotenvConfig = {
// TODO @Shinigami92 2024-04-05: Does the silent option even still exists and do anything?
silent: true
};
if (envPath) {

@@ -215,23 +247,83 @@ dotenvConfig.path = envPath;

}
function applyIf(arg, key, obj, condition) {
if (arg !== void 0 && !(key in obj)) {
return arg;
}
const val = obj[key];
return condition(val) ? val : arg;
}
function isString(val) {
return typeof val === "string";
}
function isBoolean(val) {
return typeof val === "boolean";
}
function isClientConfig(val) {
return typeof val === "object" && val !== null && ("host" in val && // @ts-expect-error: this is a TS 4.8 bug
!!val.host || "port" in val && // @ts-expect-error: this is a TS 4.8 bug
!!val.port || "name" in val && // @ts-expect-error: this is a TS 4.8 bug
!!val.name || "database" in val && // @ts-expect-error: this is a TS 4.8 bug
!!val.database);
}
function readJson(json) {
if (typeof json === "object") {
SCHEMA = typeof SCHEMA !== "undefined" ? SCHEMA : json[schemaArg];
CREATE_SCHEMA = typeof CREATE_SCHEMA !== "undefined" ? CREATE_SCHEMA : json[createSchemaArg];
MIGRATIONS_DIR = typeof MIGRATIONS_DIR !== "undefined" ? MIGRATIONS_DIR : json[migrationsDirArg];
MIGRATIONS_SCHEMA = typeof MIGRATIONS_SCHEMA !== "undefined" ? MIGRATIONS_SCHEMA : json[migrationsSchemaArg];
CREATE_MIGRATIONS_SCHEMA = typeof CREATE_MIGRATIONS_SCHEMA !== "undefined" ? CREATE_MIGRATIONS_SCHEMA : json[createMigrationsSchemaArg];
MIGRATIONS_TABLE = typeof MIGRATIONS_TABLE !== "undefined" ? MIGRATIONS_TABLE : json[migrationsTableArg];
MIGRATIONS_FILE_LANGUAGE = typeof MIGRATIONS_FILE_LANGUAGE !== "undefined" ? MIGRATIONS_FILE_LANGUAGE : json[migrationFileLanguageArg];
MIGRATIONS_FILENAME_FORMAT = typeof MIGRATIONS_FILENAME_FORMAT !== "undefined" ? MIGRATIONS_FILENAME_FORMAT : json[migrationFilenameFormatArg];
TEMPLATE_FILE_NAME = typeof TEMPLATE_FILE_NAME !== "undefined" ? TEMPLATE_FILE_NAME : json[templateFileNameArg];
IGNORE_PATTERN = typeof IGNORE_PATTERN !== "undefined" ? IGNORE_PATTERN : json[ignorePatternArg];
CHECK_ORDER = typeof CHECK_ORDER !== "undefined" ? CHECK_ORDER : json[checkOrderArg];
VERBOSE = typeof VERBOSE !== "undefined" ? VERBOSE : json[verboseArg];
DECAMELIZE = typeof DECAMELIZE !== "undefined" ? DECAMELIZE : json[decamelizeArg];
DB_CONNECTION = typeof DB_CONNECTION !== "undefined" ? DB_CONNECTION : process.env[json[databaseUrlVarArg]];
tsconfigPath = typeof tsconfigPath !== "undefined" ? tsconfigPath : json[tsconfigArg];
if (json.url) {
DB_CONNECTION = typeof DB_CONNECTION !== "undefined" ? DB_CONNECTION : json.url;
} else if (json.host || json.port || json.name || json.database) {
DB_CONNECTION = typeof DB_CONNECTION !== "undefined" ? DB_CONNECTION : {
if (typeof json === "object" && json !== null) {
SCHEMA = applyIf(
SCHEMA,
schemaArg,
json,
(val) => Array.isArray(val)
);
CREATE_SCHEMA = applyIf(CREATE_SCHEMA, createSchemaArg, json, isBoolean);
MIGRATIONS_DIR = applyIf(MIGRATIONS_DIR, migrationsDirArg, json, isString);
MIGRATIONS_SCHEMA = applyIf(
MIGRATIONS_SCHEMA,
migrationsSchemaArg,
json,
isString
);
CREATE_MIGRATIONS_SCHEMA = applyIf(
CREATE_MIGRATIONS_SCHEMA,
createMigrationsSchemaArg,
json,
isBoolean
);
MIGRATIONS_TABLE = applyIf(
MIGRATIONS_TABLE,
migrationsTableArg,
json,
isString
);
MIGRATIONS_FILE_LANGUAGE = applyIf(
MIGRATIONS_FILE_LANGUAGE,
migrationFileLanguageArg,
json,
(val) => val === "js" || val === "ts" || val === "sql"
);
MIGRATIONS_FILENAME_FORMAT = applyIf(
MIGRATIONS_FILENAME_FORMAT,
migrationFilenameFormatArg,
json,
(val) => val === "timestamp" || val === "utc"
);
TEMPLATE_FILE_NAME = applyIf(
TEMPLATE_FILE_NAME,
templateFileNameArg,
json,
isString
);
IGNORE_PATTERN = applyIf(IGNORE_PATTERN, ignorePatternArg, json, isString);
CHECK_ORDER = applyIf(CHECK_ORDER, checkOrderArg, json, isBoolean);
VERBOSE = applyIf(VERBOSE, verboseArg, json, isBoolean);
DECAMELIZE = applyIf(DECAMELIZE, decamelizeArg, json, isBoolean);
DB_CONNECTION = applyIf(
DB_CONNECTION,
databaseUrlVarArg,
json,
(val) => typeof val === "string" || typeof val === "object"
);
tsconfigPath = applyIf(tsconfigPath, tsconfigArg, json, isString);
if ("url" in json && json.url && isString(json.url)) {
_nullishCoalesce(DB_CONNECTION, () => ( (DB_CONNECTION = json.url)));
} else if (isClientConfig(json)) {
_nullishCoalesce(DB_CONNECTION, () => ( (DB_CONNECTION = {
user: json.user,

@@ -243,10 +335,10 @@ host: json.host || "localhost",

ssl: json.ssl
};
})));
}
} else {
DB_CONNECTION = typeof DB_CONNECTION !== "undefined" ? DB_CONNECTION : json;
_nullishCoalesce(DB_CONNECTION, () => ( (DB_CONNECTION = json)));
}
}
const oldSuppressWarning = process.env.SUPPRESS_NO_CONFIG_WARNING;
process.env.SUPPRESS_NO_CONFIG_WARNING = 1;
process.env.SUPPRESS_NO_CONFIG_WARNING = "yes";
const config = tryRequire("config");

@@ -265,8 +357,10 @@ if (config && config.has(argv[configValueArg])) {

const action = argv._.shift();
MIGRATIONS_DIR = typeof MIGRATIONS_DIR === "undefined" ? `${process.cwd()}/migrations` : MIGRATIONS_DIR;
MIGRATIONS_TABLE = typeof MIGRATIONS_TABLE === "undefined" ? "pgmigrations" : MIGRATIONS_TABLE;
SCHEMA = typeof SCHEMA === "undefined" ? "public" : SCHEMA;
IGNORE_PATTERN = typeof IGNORE_PATTERN === "undefined" ? "\\..*" : IGNORE_PATTERN;
CHECK_ORDER = typeof CHECK_ORDER === "undefined" ? true : CHECK_ORDER;
VERBOSE = typeof VERBOSE === "undefined" ? true : VERBOSE;
_nullishCoalesce(MIGRATIONS_DIR, () => ( (MIGRATIONS_DIR = `${process.cwd()}/migrations`)));
_nullishCoalesce(MIGRATIONS_FILE_LANGUAGE, () => ( (MIGRATIONS_FILE_LANGUAGE = "js")));
_nullishCoalesce(MIGRATIONS_FILENAME_FORMAT, () => ( (MIGRATIONS_FILENAME_FORMAT = "timestamp")));
_nullishCoalesce(MIGRATIONS_TABLE, () => ( (MIGRATIONS_TABLE = "pgmigrations")));
_nullishCoalesce(SCHEMA, () => ( (SCHEMA = ["public"])));
_nullishCoalesce(IGNORE_PATTERN, () => ( (IGNORE_PATTERN = "\\..*")));
_nullishCoalesce(CHECK_ORDER, () => ( (CHECK_ORDER = true)));
_nullishCoalesce(VERBOSE, () => ( (VERBOSE = true)));
if (action === "create") {

@@ -277,3 +371,3 @@ let newMigrationName = argv._.length ? argv._.join("-") : "";

console.error("'migrationName' is required.");
_yargs2.default.showHelp();
parser.showHelp();
process.exit(1);

@@ -317,8 +411,9 @@ }

}
const updownArg = argv._.length ? argv._[0] : null;
const upDownArg = argv._.length ? argv._[0] : null;
let numMigrations;
let migrationName;
if (updownArg !== null) {
if (parseInt(updownArg, 10) == updownArg) {
numMigrations = parseInt(updownArg, 10);
if (upDownArg !== null) {
const parsedUpDownArg = parseInt(`${upDownArg}`, 10);
if (parsedUpDownArg == upDownArg) {
numMigrations = parsedUpDownArg;
} else {

@@ -336,3 +431,10 @@ migrationName = argv._.join("-").replace(/_ /g, "-");

...databaseUrl,
...typeof rejectUnauthorized === "boolean" ? { ssl: { ...databaseUrl.ssl, rejectUnauthorized } } : void 0
...typeof rejectUnauthorized === "boolean" ? {
ssl: {
// TODO @Shinigami92 2024-04-05: Fix ssl could be boolean
// @ts-expect-error: ignore possible boolean for now
...databaseUrl.ssl,
rejectUnauthorized
}
} : void 0
},

@@ -370,3 +472,3 @@ dir: MIGRATIONS_DIR,

console.error("Invalid Action: Must be [up|down|create|redo].");
_yargs2.default.showHelp();
parser.showHelp();
process.exit(1);

@@ -373,0 +475,0 @@ }

#!/usr/bin/env node
import type { DotenvConfigOptions } from 'dotenv';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { format } from 'node:util';
import type { ClientConfig } from 'pg';
import ConnectionParameters from 'pg/lib/connection-parameters';
import yargs from 'yargs';
import yargs from 'yargs/yargs';
import { default as migrationRunner, Migration } from '../dist';
import type { RunnerOption } from '../src';
import type { FilenameFormat } from '../src/migration';

@@ -15,7 +19,10 @@ process.on('uncaughtException', (err) => {

function tryRequire(moduleName: string): unknown {
function tryRequire<TModule = unknown>(moduleName: string): TModule | null {
try {
return require(moduleName);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
if (
// @ts-expect-error: TS doesn't know about code property
err?.code !== 'MODULE_NOT_FOUND'
) {
throw err;

@@ -53,158 +60,137 @@ }

const { argv } = yargs
const parser = yargs(process.argv.slice(2))
.usage('Usage: $0 [up|down|create|redo] [migrationName] [options]')
.option('d', {
alias: databaseUrlVarArg,
default: 'DATABASE_URL',
describe: 'Name of env variable where is set the databaseUrl',
type: 'string',
.options({
[databaseUrlVarArg]: {
alias: 'd',
default: 'DATABASE_URL',
description: 'Name of env variable where is set the databaseUrl',
type: 'string',
},
[migrationsDirArg]: {
alias: 'm',
defaultDescription: '"migrations"',
describe: 'The directory containing your migration files',
type: 'string',
},
[migrationsTableArg]: {
alias: 't',
defaultDescription: '"pgmigrations"',
describe: 'The table storing which migrations have been run',
type: 'string',
},
[schemaArg]: {
alias: 's',
defaultDescription: '"public"',
describe:
'The schema on which migration will be run (defaults to `public`)',
type: 'string',
array: true,
},
[createSchemaArg]: {
defaultDescription: 'false',
describe: "Creates the configured schema if it doesn't exist",
type: 'boolean',
},
[migrationsSchemaArg]: {
defaultDescription: 'Same as "schema"',
describe: 'The schema storing table which migrations have been run',
type: 'string',
},
[createMigrationsSchemaArg]: {
defaultDescription: 'false',
describe: "Creates the configured migration schema if it doesn't exist",
type: 'boolean',
},
[checkOrderArg]: {
defaultDescription: 'true',
describe: 'Check order of migrations before running them',
type: 'boolean',
},
[verboseArg]: {
defaultDescription: 'true',
describe: 'Print debug messages - all DB statements run',
type: 'boolean',
},
[ignorePatternArg]: {
defaultDescription: '"\\..*"',
describe: 'Regex pattern for file names to ignore',
type: 'string',
},
[decamelizeArg]: {
defaultDescription: 'false',
describe: 'Runs decamelize on table/columns/etc names',
type: 'boolean',
},
[configValueArg]: {
default: 'db',
describe: 'Name of config section with db options',
type: 'string',
},
[configFileArg]: {
alias: 'f',
describe: 'Name of config file with db options',
type: 'string',
},
[migrationFileLanguageArg]: {
alias: 'j',
defaultDescription: 'last one used or "js" if there is no migration yet',
choices: ['js', 'ts', 'sql'],
describe:
'Language of the migration file (Only valid with the create action)',
type: 'string',
},
[migrationFilenameFormatArg]: {
defaultDescription: '"timestamp"',
choices: ['timestamp', 'utc'],
describe:
'Prefix type of migration filename (Only valid with the create action)',
type: 'string',
},
[templateFileNameArg]: {
describe: 'Path to template for creating migrations',
type: 'string',
},
[tsconfigArg]: {
describe: 'Path to tsconfig.json file',
type: 'string',
},
[envPathArg]: {
describe: 'Path to the .env file that should be used for configuration',
type: 'string',
},
[dryRunArg]: {
default: false,
describe: "Prints the SQL but doesn't run it",
type: 'boolean',
},
[fakeArg]: {
default: false,
describe: 'Marks migrations as run',
type: 'boolean',
},
[singleTransactionArg]: {
default: true,
describe:
'Combines all pending migrations into a single database transaction so that if any migration fails, all will be rolled back',
type: 'boolean',
},
[lockArg]: {
default: true,
describe: 'When false, disables locking mechanism and checks',
type: 'boolean',
},
[rejectUnauthorizedArg]: {
defaultDescription: 'false',
describe: 'Sets rejectUnauthorized SSL option',
type: 'boolean',
},
[timestampArg]: {
default: false,
describe: 'Treats number argument to up/down migration as timestamp',
type: 'boolean',
},
})
.option('m', {
alias: migrationsDirArg,
defaultDescription: '"migrations"',
describe: 'The directory containing your migration files',
type: 'string',
})
.option('t', {
alias: migrationsTableArg,
defaultDescription: '"pgmigrations"',
describe: 'The table storing which migrations have been run',
type: 'string',
})
.option('s', {
alias: schemaArg,
defaultDescription: '"public"',
describe:
'The schema on which migration will be run (defaults to `public`)',
type: 'string',
array: true,
})
.option(createSchemaArg, {
defaultDescription: 'false',
describe: "Creates the configured schema if it doesn't exist",
type: 'boolean',
})
.option(migrationsSchemaArg, {
defaultDescription: 'Same as "schema"',
describe: 'The schema storing table which migrations have been run',
type: 'string',
})
.option(createMigrationsSchemaArg, {
defaultDescription: 'false',
describe: "Creates the configured migration schema if it doesn't exist",
type: 'boolean',
})
.option(checkOrderArg, {
defaultDescription: 'true',
describe: 'Check order of migrations before running them',
type: 'boolean',
})
.option(verboseArg, {
defaultDescription: 'true',
describe: 'Print debug messages - all DB statements run',
type: 'boolean',
})
.option(ignorePatternArg, {
defaultDescription: '"\\..*"',
describe: 'Regex pattern for file names to ignore',
type: 'string',
})
.option(decamelizeArg, {
defaultDescription: 'false',
describe: 'Runs decamelize on table/columns/etc names',
type: 'boolean',
})
.option(configValueArg, {
default: 'db',
describe: 'Name of config section with db options',
type: 'string',
})
.option('f', {
alias: configFileArg,
describe: 'Name of config file with db options',
type: 'string',
})
.option('j', {
alias: migrationFileLanguageArg,
defaultDescription: 'last one used or "js" if there is no migration yet',
choices: ['js', 'ts', 'sql'],
describe:
'Language of the migration file (Only valid with the create action)',
type: 'string',
})
.option(migrationFilenameFormatArg, {
defaultDescription: '"timestamp"',
choices: ['timestamp', 'utc'],
describe:
'Prefix type of migration filename (Only valid with the create action)',
type: 'string',
})
.option(templateFileNameArg, {
describe: 'Path to template for creating migrations',
type: 'string',
})
.option(tsconfigArg, {
describe: 'Path to tsconfig.json file',
type: 'string',
})
.option(envPathArg, {
describe: 'Path to the .env file that should be used for configuration',
type: 'string',
})
.option(dryRunArg, {
default: false,
describe: "Prints the SQL but doesn't run it",
type: 'boolean',
})
.option(fakeArg, {
default: false,
describe: 'Marks migrations as run',
type: 'boolean',
})
.option(singleTransactionArg, {
default: true,
describe:
'Combines all pending migrations into a single database transaction so that if any migration fails, all will be rolled back',
type: 'boolean',
})
.option(lockArg, {
default: true,
describe: 'When false, disables locking mechanism and checks',
type: 'boolean',
})
.option(rejectUnauthorizedArg, {
defaultDescription: 'false',
describe: 'Sets rejectUnauthorized SSL option',
type: 'boolean',
})
.option(timestampArg, {
default: false,
describe: 'Treats number argument to up/down migration as timestamp',
type: 'boolean',
})
.version()

@@ -214,4 +200,6 @@ .alias('version', 'i')

const argv = parser.parseSync();
if (argv.help || argv._.length === 0) {
yargs.showHelp();
parser.showHelp();
process.exit(1);

@@ -224,3 +212,6 @@ }

// Create default dotenv config
const dotenvConfig = { silent: true };
const dotenvConfig: DotenvConfigOptions & { silent: boolean } = {
// TODO @Shinigami92 2024-04-05: Does the silent option even still exists and do anything?
silent: true,
};

@@ -232,7 +223,8 @@ // If the path has been configured, add it to the config, otherwise don't change the default dotenv path

const dotenv = tryRequire('dotenv');
const dotenv = tryRequire<typeof import('dotenv')>('dotenv');
if (dotenv) {
// Load config from ".env" file
const myEnv = dotenv.config(dotenvConfig);
const dotenvExpand = tryRequire('dotenv-expand');
const dotenvExpand =
tryRequire<typeof import('dotenv-expand')>('dotenv-expand');
if (dotenvExpand && dotenvExpand.expand) {

@@ -244,3 +236,4 @@ dotenvExpand.expand(myEnv);

let MIGRATIONS_DIR = argv[migrationsDirArg];
let DB_CONNECTION = process.env[argv[databaseUrlVarArg]];
let DB_CONNECTION: string | ConnectionParameters | ClientConfig | undefined =
process.env[argv[databaseUrlVarArg]];
let IGNORE_PATTERN = argv[ignorePatternArg];

@@ -252,4 +245,8 @@ let SCHEMA = argv[schemaArg];

let MIGRATIONS_TABLE = argv[migrationsTableArg];
let MIGRATIONS_FILE_LANGUAGE = argv[migrationFileLanguageArg];
let MIGRATIONS_FILENAME_FORMAT = argv[migrationFilenameFormatArg];
let MIGRATIONS_FILE_LANGUAGE: 'js' | 'ts' | 'sql' | undefined = argv[
migrationFileLanguageArg
] as 'js' | 'ts' | 'sql' | undefined;
let MIGRATIONS_FILENAME_FORMAT: `${FilenameFormat}` | undefined = argv[
migrationFilenameFormatArg
] as `${FilenameFormat}` | undefined;
let TEMPLATE_FILE_NAME = argv[templateFileNameArg];

@@ -264,3 +261,3 @@ let CHECK_ORDER = argv[checkOrderArg];

let tsconfig;
const json5 = tryRequire('json5');
const json5 = tryRequire<typeof import('json5')>('json5');

@@ -276,3 +273,3 @@ try {

const tsnode = tryRequire('ts-node');
const tsnode = tryRequire<typeof import('ts-node')>('ts-node');
if (!tsnode) {

@@ -293,70 +290,117 @@ console.error("For TypeScript support, please install 'ts-node' module");

function readJson(json) {
if (typeof json === 'object') {
SCHEMA = typeof SCHEMA !== 'undefined' ? SCHEMA : json[schemaArg];
CREATE_SCHEMA =
typeof CREATE_SCHEMA !== 'undefined'
? CREATE_SCHEMA
: json[createSchemaArg];
MIGRATIONS_DIR =
typeof MIGRATIONS_DIR !== 'undefined'
? MIGRATIONS_DIR
: json[migrationsDirArg];
MIGRATIONS_SCHEMA =
typeof MIGRATIONS_SCHEMA !== 'undefined'
? MIGRATIONS_SCHEMA
: json[migrationsSchemaArg];
CREATE_MIGRATIONS_SCHEMA =
typeof CREATE_MIGRATIONS_SCHEMA !== 'undefined'
? CREATE_MIGRATIONS_SCHEMA
: json[createMigrationsSchemaArg];
MIGRATIONS_TABLE =
typeof MIGRATIONS_TABLE !== 'undefined'
? MIGRATIONS_TABLE
: json[migrationsTableArg];
MIGRATIONS_FILE_LANGUAGE =
typeof MIGRATIONS_FILE_LANGUAGE !== 'undefined'
? MIGRATIONS_FILE_LANGUAGE
: json[migrationFileLanguageArg];
MIGRATIONS_FILENAME_FORMAT =
typeof MIGRATIONS_FILENAME_FORMAT !== 'undefined'
? MIGRATIONS_FILENAME_FORMAT
: json[migrationFilenameFormatArg];
TEMPLATE_FILE_NAME =
typeof TEMPLATE_FILE_NAME !== 'undefined'
? TEMPLATE_FILE_NAME
: json[templateFileNameArg];
IGNORE_PATTERN =
typeof IGNORE_PATTERN !== 'undefined'
? IGNORE_PATTERN
: json[ignorePatternArg];
CHECK_ORDER =
typeof CHECK_ORDER !== 'undefined' ? CHECK_ORDER : json[checkOrderArg];
VERBOSE = typeof VERBOSE !== 'undefined' ? VERBOSE : json[verboseArg];
DECAMELIZE =
typeof DECAMELIZE !== 'undefined' ? DECAMELIZE : json[decamelizeArg];
DB_CONNECTION =
typeof DB_CONNECTION !== 'undefined'
? DB_CONNECTION
: process.env[json[databaseUrlVarArg]];
tsconfigPath =
typeof tsconfigPath !== 'undefined' ? tsconfigPath : json[tsconfigArg];
if (json.url) {
DB_CONNECTION =
typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : json.url;
} else if (json.host || json.port || json.name || json.database) {
DB_CONNECTION =
typeof DB_CONNECTION !== 'undefined'
? DB_CONNECTION
: {
user: json.user,
host: json.host || 'localhost',
database: json.name || json.database,
password: json.password,
port: json.port || 5432,
ssl: json.ssl,
};
function applyIf<TArg, TKey extends string = string>(
arg: TArg,
key: TKey,
obj: { [k in TKey]?: unknown },
condition: (val: (typeof obj)[TKey]) => val is TArg
): TArg {
if (arg !== undefined && !(key in obj)) {
return arg;
}
const val = obj[key];
return condition(val) ? val : arg;
}
function isString(val: unknown): val is string {
return typeof val === 'string';
}
function isBoolean(val: unknown): val is boolean {
return typeof val === 'boolean';
}
function isClientConfig(val: unknown): val is ClientConfig & { name?: string } {
return (
typeof val === 'object' &&
val !== null &&
(('host' in val &&
// @ts-expect-error: this is a TS 4.8 bug
!!val.host) ||
('port' in val &&
// @ts-expect-error: this is a TS 4.8 bug
!!val.port) ||
('name' in val &&
// @ts-expect-error: this is a TS 4.8 bug
!!val.name) ||
('database' in val &&
// @ts-expect-error: this is a TS 4.8 bug
!!val.database))
);
}
function readJson(json: unknown): void {
if (typeof json === 'object' && json !== null) {
SCHEMA = applyIf(SCHEMA, schemaArg, json, (val): val is string[] =>
Array.isArray(val)
);
CREATE_SCHEMA = applyIf(CREATE_SCHEMA, createSchemaArg, json, isBoolean);
MIGRATIONS_DIR = applyIf(MIGRATIONS_DIR, migrationsDirArg, json, isString);
MIGRATIONS_SCHEMA = applyIf(
MIGRATIONS_SCHEMA,
migrationsSchemaArg,
json,
isString
);
CREATE_MIGRATIONS_SCHEMA = applyIf(
CREATE_MIGRATIONS_SCHEMA,
createMigrationsSchemaArg,
json,
isBoolean
);
MIGRATIONS_TABLE = applyIf(
MIGRATIONS_TABLE,
migrationsTableArg,
json,
isString
);
MIGRATIONS_FILE_LANGUAGE = applyIf(
MIGRATIONS_FILE_LANGUAGE,
migrationFileLanguageArg,
json,
(val): val is 'js' | 'ts' | 'sql' =>
val === 'js' || val === 'ts' || val === 'sql'
);
MIGRATIONS_FILENAME_FORMAT = applyIf(
MIGRATIONS_FILENAME_FORMAT,
migrationFilenameFormatArg,
json,
(val): val is `${FilenameFormat}` => val === 'timestamp' || val === 'utc'
);
TEMPLATE_FILE_NAME = applyIf(
TEMPLATE_FILE_NAME,
templateFileNameArg,
json,
isString
);
IGNORE_PATTERN = applyIf(IGNORE_PATTERN, ignorePatternArg, json, isString);
CHECK_ORDER = applyIf(CHECK_ORDER, checkOrderArg, json, isBoolean);
VERBOSE = applyIf(VERBOSE, verboseArg, json, isBoolean);
DECAMELIZE = applyIf(DECAMELIZE, decamelizeArg, json, isBoolean);
DB_CONNECTION = applyIf(
DB_CONNECTION,
databaseUrlVarArg,
json,
(val): val is string | ConnectionParameters | ClientConfig =>
typeof val === 'string' || typeof val === 'object'
);
tsconfigPath = applyIf(tsconfigPath, tsconfigArg, json, isString);
// @ts-expect-error: this is a TS 4.8 bug
if ('url' in json && json.url && isString(json.url)) {
// @ts-expect-error: this is a TS 4.8 bug
DB_CONNECTION ??= json.url;
} else if (isClientConfig(json)) {
DB_CONNECTION ??= {
user: json.user,
host: json.host || 'localhost',
database: json.name || json.database,
password: json.password,
port: json.port || 5432,
ssl: json.ssl,
};
}
} else {
DB_CONNECTION = typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : json;
DB_CONNECTION ??= json as string | ConnectionParameters | ClientConfig;
}

@@ -367,4 +411,4 @@ }

const oldSuppressWarning = process.env.SUPPRESS_NO_CONFIG_WARNING;
process.env.SUPPRESS_NO_CONFIG_WARNING = 1;
const config = tryRequire('config');
process.env.SUPPRESS_NO_CONFIG_WARNING = 'yes';
const config = tryRequire<typeof import('config')>('config');
if (config && config.has(argv[configValueArg])) {

@@ -388,13 +432,10 @@ const db = config.get(argv[configValueArg]);

// defaults
MIGRATIONS_DIR =
typeof MIGRATIONS_DIR === 'undefined'
? `${process.cwd()}/migrations`
: MIGRATIONS_DIR;
MIGRATIONS_TABLE =
typeof MIGRATIONS_TABLE === 'undefined' ? 'pgmigrations' : MIGRATIONS_TABLE;
SCHEMA = typeof SCHEMA === 'undefined' ? 'public' : SCHEMA;
IGNORE_PATTERN =
typeof IGNORE_PATTERN === 'undefined' ? '\\..*' : IGNORE_PATTERN;
CHECK_ORDER = typeof CHECK_ORDER === 'undefined' ? true : CHECK_ORDER;
VERBOSE = typeof VERBOSE === 'undefined' ? true : VERBOSE;
MIGRATIONS_DIR ??= `${process.cwd()}/migrations`;
MIGRATIONS_FILE_LANGUAGE ??= 'js';
MIGRATIONS_FILENAME_FORMAT ??= 'timestamp';
MIGRATIONS_TABLE ??= 'pgmigrations';
SCHEMA ??= ['public'];
IGNORE_PATTERN ??= '\\..*';
CHECK_ORDER ??= true;
VERBOSE ??= true;

@@ -409,3 +450,3 @@ if (action === 'create') {

console.error("'migrationName' is required.");
yargs.showHelp();
parser.showHelp();
process.exit(1);

@@ -459,10 +500,11 @@ }

const updownArg = argv._.length ? argv._[0] : null;
let numMigrations;
let migrationName;
const upDownArg = argv._.length ? argv._[0] : null;
let numMigrations: number;
let migrationName: string;
if (updownArg !== null) {
if (upDownArg !== null) {
const parsedUpDownArg = parseInt(`${upDownArg}`, 10);
// eslint-disable-next-line eqeqeq
if (parseInt(updownArg, 10) == updownArg) {
numMigrations = parseInt(updownArg, 10);
if (parsedUpDownArg == upDownArg) {
numMigrations = parsedUpDownArg;
} else {

@@ -478,5 +520,10 @@ migrationName = argv._.join('-').replace(/_ /g, '-');

const options = (direction, _count, _timestamp) => {
const options: (
direction: 'up' | 'down',
count?: number,
timestamp?: boolean
) => RunnerOption = (direction, _count, _timestamp) => {
const count = _count === undefined ? numMigrations : _count;
const timestamp = _timestamp === undefined ? TIMESTAMP : _timestamp;
return {

@@ -487,6 +534,13 @@ dryRun,

...(typeof rejectUnauthorized === 'boolean'
? { ssl: { ...databaseUrl.ssl, rejectUnauthorized } }
? {
ssl: {
// TODO @Shinigami92 2024-04-05: Fix ssl could be boolean
// @ts-expect-error: ignore possible boolean for now
...databaseUrl.ssl,
rejectUnauthorized,
},
}
: undefined),
},
dir: MIGRATIONS_DIR,
dir: MIGRATIONS_DIR!,
ignorePattern: IGNORE_PATTERN,

@@ -497,3 +551,3 @@ schema: SCHEMA,

createMigrationsSchema: CREATE_MIGRATIONS_SCHEMA,
migrationsTable: MIGRATIONS_TABLE,
migrationsTable: MIGRATIONS_TABLE!,
count,

@@ -529,3 +583,3 @@ timestamp,

console.error('Invalid Action: Must be [up|down|create|redo].');
yargs.showHelp();
parser.showHelp();
process.exit(1);

@@ -532,0 +586,0 @@ }

@@ -38,4 +38,4 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _domains = require('./operations/domains'); var domains = _interopRequireWildcard(_domains);

typeShorthands,
schemalize: _utils.createSchemalize.call(void 0, shouldDecamelize, false),
literal: _utils.createSchemalize.call(void 0, shouldDecamelize, true),
schemalize: _utils.createSchemalize.call(void 0, { shouldDecamelize, shouldQuote: false }),
literal: _utils.createSchemalize.call(void 0, { shouldDecamelize, shouldQuote: true }),
logger

@@ -42,0 +42,0 @@ };

@@ -23,3 +23,3 @@ import type { QueryResult } from 'pg';

export declare type CreateOptions = {
filenameFormat?: FilenameFormat;
filenameFormat?: FilenameFormat | `${FilenameFormat}`;
} & (CreateOptionsTemplate | CreateOptionsDefault);

@@ -26,0 +26,0 @@ export declare const loadMigrationFiles: (dir: string, ignorePattern?: string) => Promise<string[]>;

@@ -61,6 +61,6 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _path = require('path'); var _path2 = _interopRequireDefault(_path);

const { migrationsTable } = options;
const fullTableName = _utils.createSchemalize.call(void 0,
Boolean(options.decamelize),
true
)({
const fullTableName = _utils.createSchemalize.call(void 0, {
shouldDecamelize: Boolean(options.decamelize),
shouldQuote: true
})({
schema,

@@ -93,6 +93,6 @@ name: migrationsTable

const { migrationsTable } = options;
const fullTableName = _utils.createSchemalize.call(void 0,
Boolean(options.decamelize),
true
)({
const fullTableName = _utils.createSchemalize.call(void 0, {
shouldDecamelize: Boolean(options.decamelize),
shouldQuote: true
})({
schema,

@@ -99,0 +99,0 @@ name: migrationsTable

{
"name": "node-pg-migrate",
"version": "7.0.0-alpha.1",
"version": "7.0.0-alpha.2",
"description": "Postgresql database migration management tool for node.js",

@@ -86,2 +86,3 @@ "bin": {

"@eslint-types/typescript-eslint": "7.2.0",
"@types/config": "3.3.4",
"@types/mkdirp": "1.0.2",

@@ -88,0 +89,0 @@ "@types/node": "18.19.29",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc