diff --git a/.eslintrc.js b/.eslintrc.js
index 81a92c2e..8d75d872 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -36,4 +36,4 @@ module.exports = {
},
},
],
-}
+};
diff --git a/README.md b/README.md
index ff6c0e1f..5b521a59 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ exports.up = (pgm) => {
notNull: true,
default: pgm.func('current_timestamp'),
},
- })
+ });
pgm.createTable('posts', {
id: 'id',
userId: {
@@ -56,9 +56,9 @@ exports.up = (pgm) => {
notNull: true,
default: pgm.func('current_timestamp'),
},
- })
- pgm.createIndex('posts', 'userId')
-}
+ });
+ pgm.createIndex('posts', 'userId');
+};
```
Save migration file.
@@ -76,8 +76,8 @@ Run `npm run migrate create posts lead`, edit `xxx_posts_lead.js`:
exports.up = (pgm) => {
pgm.addColumns('posts', {
lead: { type: 'text', notNull: true },
- })
-}
+ });
+};
```
Run `npm run migrate up` and there will be new column in `posts` table :tada: :tada:
diff --git a/bin/node-pg-migrate b/bin/node-pg-migrate
index a8f1d5b5..959f262a 100755
--- a/bin/node-pg-migrate
+++ b/bin/node-pg-migrate
@@ -1,56 +1,56 @@
#!/usr/bin/env node
/* eslint-disable strict, no-console */
-'use strict'
+'use strict';
-const fs = require('fs')
-const util = require('util')
-const path = require('path')
-const yargs = require('yargs')
-const ConnectionParameters = require('pg/lib/connection-parameters')
-const { default: migrationRunner, Migration } = require('../dist')
+const fs = require('fs');
+const util = require('util');
+const path = require('path');
+const yargs = require('yargs');
+const ConnectionParameters = require('pg/lib/connection-parameters');
+const { default: migrationRunner, Migration } = require('../dist');
process.on('uncaughtException', (err) => {
- console.error(err)
- process.exit(1)
-})
+ console.error(err);
+ process.exit(1);
+});
function tryRequire(moduleName) {
try {
// eslint-disable-next-line import/no-dynamic-require,global-require,security/detect-non-literal-require
- return require(moduleName)
+ return require(moduleName);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
- throw err
+ throw err;
}
- return null
+ return null;
}
}
-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 migrationFilenameFormatArg = 'migration-filename-format'
-const templateFileNameArg = 'template-file-name'
-const checkOrderArg = 'check-order'
-const configValueArg = 'config-value'
-const configFileArg = 'config-file'
-const ignorePatternArg = 'ignore-pattern'
-const singleTransactionArg = 'single-transaction'
-const lockArg = 'lock'
-const timestampArg = 'timestamp'
-const dryRunArg = 'dry-run'
-const fakeArg = 'fake'
-const decamelizeArg = 'decamelize'
-const tsconfigArg = 'tsconfig'
-const verboseArg = 'verbose'
-const rejectUnauthorizedArg = 'reject-unauthorized'
-const envPathArg = 'envPath'
+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 migrationFilenameFormatArg = 'migration-filename-format';
+const templateFileNameArg = 'template-file-name';
+const checkOrderArg = 'check-order';
+const configValueArg = 'config-value';
+const configFileArg = 'config-file';
+const ignorePatternArg = 'ignore-pattern';
+const singleTransactionArg = 'single-transaction';
+const lockArg = 'lock';
+const timestampArg = 'timestamp';
+const dryRunArg = 'dry-run';
+const fakeArg = 'fake';
+const decamelizeArg = 'decamelize';
+const tsconfigArg = 'tsconfig';
+const verboseArg = 'verbose';
+const rejectUnauthorizedArg = 'reject-unauthorized';
+const envPathArg = 'envPath';
const { argv } = yargs
.usage('Usage: $0 [up|down|create|redo] [migrationName] [options]')
@@ -79,7 +79,8 @@ const { argv } = yargs
.option('s', {
alias: schemaArg,
defaultDescription: '"public"',
- describe: 'The schema on which migration will be run (defaults to `public`)',
+ describe:
+ 'The schema on which migration will be run (defaults to `public`)',
type: 'string',
array: true,
})
@@ -142,14 +143,16 @@ const { argv } = yargs
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)',
+ 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)',
+ describe:
+ 'Prefix type of migration filename (Only valid with the create action)',
type: 'string',
})
@@ -207,100 +210,134 @@ const { argv } = yargs
.version()
.alias('version', 'i')
- .help()
+ .help();
if (argv.help || argv._.length === 0) {
- yargs.showHelp()
- process.exit(1)
+ yargs.showHelp();
+ process.exit(1);
}
/* Load env before accessing process.env */
-const envPath = argv[envPathArg]
+const envPath = argv[envPathArg];
// Create default dotenv config
-const dotenvConfig = { silent: true }
+const dotenvConfig = { silent: true };
// If the path has been configured, add it to the config, otherwise don't change the default dotenv path
if (envPath) {
- dotenvConfig.path = envPath
+ dotenvConfig.path = envPath;
}
-const dotenv = tryRequire('dotenv')
+const dotenv = tryRequire('dotenv');
if (dotenv) {
// Load config from ".env" file
- const myEnv = dotenv.config(dotenvConfig)
- const dotenvExpand = tryRequire('dotenv-expand')
+ const myEnv = dotenv.config(dotenvConfig);
+ const dotenvExpand = tryRequire('dotenv-expand');
if (dotenvExpand && dotenvExpand.expand) {
- dotenvExpand.expand(myEnv)
+ dotenvExpand.expand(myEnv);
}
}
-let MIGRATIONS_DIR = argv[migrationsDirArg]
-let DB_CONNECTION = 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 MIGRATIONS_FILENAME_FORMAT = argv[migrationFilenameFormatArg]
-let TEMPLATE_FILE_NAME = argv[templateFileNameArg]
-let CHECK_ORDER = argv[checkOrderArg]
-let VERBOSE = argv[verboseArg]
-let DECAMELIZE = argv[decamelizeArg]
-let tsconfigPath = argv[tsconfigArg]
+let MIGRATIONS_DIR = argv[migrationsDirArg];
+let DB_CONNECTION = 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 MIGRATIONS_FILENAME_FORMAT = argv[migrationFilenameFormatArg];
+let TEMPLATE_FILE_NAME = argv[templateFileNameArg];
+let CHECK_ORDER = argv[checkOrderArg];
+let VERBOSE = argv[verboseArg];
+let DECAMELIZE = argv[decamelizeArg];
+let tsconfigPath = argv[tsconfigArg];
function readTsconfig() {
if (tsconfigPath) {
- let tsconfig
- const json5 = tryRequire('json5')
+ let tsconfig;
+ const json5 = tryRequire('json5');
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
- const config = fs.readFileSync(path.resolve(process.cwd(), tsconfigPath), { encoding: 'utf-8' })
- tsconfig = json5 ? json5.parse(config) : JSON.parse(config)
+ const config = fs.readFileSync(
+ path.resolve(process.cwd(), tsconfigPath),
+ { encoding: 'utf-8' }
+ );
+ tsconfig = json5 ? json5.parse(config) : JSON.parse(config);
} catch (err) {
- console.error("Can't load tsconfig.json:", err)
+ console.error("Can't load tsconfig.json:", err);
}
- const tsnode = tryRequire('ts-node')
+ const tsnode = tryRequire('ts-node');
if (!tsnode) {
- console.error("For TypeScript support, please install 'ts-node' module")
+ console.error("For TypeScript support, please install 'ts-node' module");
}
if (tsconfig && tsnode) {
- tsnode.register(tsconfig)
+ tsnode.register(tsconfig);
if (!MIGRATIONS_FILE_LANGUAGE) {
- MIGRATIONS_FILE_LANGUAGE = 'ts'
+ MIGRATIONS_FILE_LANGUAGE = 'ts';
}
} else {
- process.exit(1)
+ process.exit(1);
}
}
}
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]
+ 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]
+ 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]
+ 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]
+ 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
+ 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'
@@ -312,51 +349,56 @@ function readJson(json) {
password: json.password,
port: json.port || 5432,
ssl: json.ssl,
- }
+ };
}
} else {
- DB_CONNECTION = typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : json
+ DB_CONNECTION = typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : json;
}
}
// Load config (and suppress the no-config-warning)
-const oldSuppressWarning = process.env.SUPPRESS_NO_CONFIG_WARNING
-process.env.SUPPRESS_NO_CONFIG_WARNING = 1
-const config = tryRequire('config')
+const oldSuppressWarning = process.env.SUPPRESS_NO_CONFIG_WARNING;
+process.env.SUPPRESS_NO_CONFIG_WARNING = 1;
+const config = tryRequire('config');
if (config && config.has(argv[configValueArg])) {
- const db = config.get(argv[configValueArg])
- readJson(db)
+ const db = config.get(argv[configValueArg]);
+ readJson(db);
}
-process.env.SUPPRESS_NO_CONFIG_WARNING = oldSuppressWarning
+process.env.SUPPRESS_NO_CONFIG_WARNING = oldSuppressWarning;
-const configFileName = argv[configFileArg]
+const configFileName = argv[configFileArg];
if (configFileName) {
// eslint-disable-next-line global-require,import/no-dynamic-require,security/detect-non-literal-require
- const jsonConfig = require(path.resolve(configFileName))
- readJson(jsonConfig)
+ const jsonConfig = require(path.resolve(configFileName));
+ readJson(jsonConfig);
}
-readTsconfig()
+readTsconfig();
-const action = argv._.shift()
+const action = argv._.shift();
// 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 =
+ 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;
if (action === 'create') {
// replaces spaces with dashes - should help fix some errors
- let newMigrationName = argv._.length ? argv._.join('-') : ''
+ let newMigrationName = argv._.length ? argv._.join('-') : '';
// forces use of dashes in names - keep thing clean
- newMigrationName = newMigrationName.replace(/[_ ]+/g, '-')
+ newMigrationName = newMigrationName.replace(/[_ ]+/g, '-');
if (!newMigrationName) {
- console.error("'migrationName' is required.")
- yargs.showHelp()
- process.exit(1)
+ console.error("'migrationName' is required.");
+ yargs.showHelp();
+ process.exit(1);
}
Migration.create(newMigrationName, MIGRATIONS_DIR, {
@@ -369,60 +411,67 @@ if (action === 'create') {
}),
})
.then((migrationPath) => {
- console.log(util.format('Created migration -- %s', migrationPath))
- process.exit(0)
+ console.log(util.format('Created migration -- %s', migrationPath));
+ process.exit(0);
})
.catch((err) => {
- console.error(err)
- process.exit(1)
- })
+ console.error(err);
+ process.exit(1);
+ });
} else if (action === 'up' || action === 'down' || action === 'redo') {
if (!DB_CONNECTION) {
- const cp = new ConnectionParameters()
+ const cp = new ConnectionParameters();
if (!cp.host && !cp.port && !cp.database) {
- console.error(`The $${argv[databaseUrlVarArg]} environment variable is not set.`)
- process.exit(1)
+ console.error(
+ `The $${argv[databaseUrlVarArg]} environment variable is not set.`
+ );
+ process.exit(1);
}
- DB_CONNECTION = cp
+ DB_CONNECTION = cp;
}
- const dryRun = argv[dryRunArg]
+ const dryRun = argv[dryRunArg];
if (dryRun) {
- console.log('dry run')
+ console.log('dry run');
}
- const singleTransaction = argv[singleTransactionArg]
- const fake = argv[fakeArg]
- const TIMESTAMP = argv[timestampArg]
- const rejectUnauthorized = argv[rejectUnauthorizedArg]
- const noLock = !argv[lockArg]
+ const singleTransaction = argv[singleTransactionArg];
+ const fake = argv[fakeArg];
+ const TIMESTAMP = argv[timestampArg];
+ const rejectUnauthorized = argv[rejectUnauthorizedArg];
+ const noLock = !argv[lockArg];
if (noLock) {
- console.log('no lock')
+ console.log('no lock');
}
- const updownArg = argv._.length ? argv._[0] : null
- let numMigrations
- let migrationName
+ const updownArg = argv._.length ? argv._[0] : null;
+ let numMigrations;
+ let migrationName;
if (updownArg !== null) {
// eslint-disable-next-line eqeqeq
if (parseInt(updownArg, 10) == updownArg) {
- numMigrations = parseInt(updownArg, 10)
+ numMigrations = parseInt(updownArg, 10);
} else {
- migrationName = argv._.join('-').replace(/_ /g, '-')
+ migrationName = argv._.join('-').replace(/_ /g, '-');
}
}
- const databaseUrl = typeof DB_CONNECTION === 'string' ? { connectionString: DB_CONNECTION } : DB_CONNECTION
+ const databaseUrl =
+ typeof DB_CONNECTION === 'string'
+ ? { connectionString: DB_CONNECTION }
+ : DB_CONNECTION;
const options = (direction, _count, _timestamp) => {
- const count = _count === undefined ? numMigrations : _count
- const timestamp = _timestamp === undefined ? TIMESTAMP : _timestamp
+ const count = _count === undefined ? numMigrations : _count;
+ const timestamp = _timestamp === undefined ? TIMESTAMP : _timestamp;
return {
dryRun,
databaseUrl: {
...databaseUrl,
- ...(typeof rejectUnauthorized === 'boolean' ? { ssl: { ...databaseUrl.ssl, rejectUnauthorized } } : undefined),
+ ...(typeof rejectUnauthorized === 'boolean'
+ ? { ssl: { ...databaseUrl.ssl, rejectUnauthorized } }
+ : undefined),
},
dir: MIGRATIONS_DIR,
ignorePattern: IGNORE_PATTERN,
@@ -441,28 +490,30 @@ if (action === 'create') {
noLock,
fake,
decamelize: DECAMELIZE,
- }
- }
+ };
+ };
const promise =
action === 'redo'
- ? migrationRunner(options('down')).then(() => migrationRunner(options('up', Infinity, false)))
- : migrationRunner(options(action))
+ ? migrationRunner(options('down')).then(() =>
+ migrationRunner(options('up', Infinity, false))
+ )
+ : migrationRunner(options(action));
promise
.then(() => {
- console.log('Migrations complete!')
- process.exit(0)
+ console.log('Migrations complete!');
+ process.exit(0);
})
.catch((err) => {
- console.error(err)
- process.exit(1)
- })
+ console.error(err);
+ process.exit(1);
+ });
} else {
- console.error('Invalid Action: Must be [up|down|create|redo].')
- yargs.showHelp()
- process.exit(1)
+ console.error('Invalid Action: Must be [up|down|create|redo].');
+ yargs.showHelp();
+ process.exit(1);
}
if (argv['force-exit']) {
- console.log('Forcing exit')
- process.exit(0)
+ console.log('Forcing exit');
+ process.exit(0);
}
diff --git a/docs/README.md b/docs/README.md
index d54a70b3..278557df 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -43,7 +43,7 @@ exports.up = (pgm) => {
notNull: true,
default: pgm.func('current_timestamp'),
},
- })
+ });
pgm.createTable('posts', {
id: 'id',
userId: {
@@ -58,9 +58,9 @@ exports.up = (pgm) => {
notNull: true,
default: pgm.func('current_timestamp'),
},
- })
- pgm.createIndex('posts', 'userId')
-}
+ });
+ pgm.createIndex('posts', 'userId');
+};
```
Save migration file.
@@ -78,8 +78,8 @@ Run `npm run migrate create posts lead`, edit `xxx_posts_lead.js`:
exports.up = (pgm) => {
pgm.addColumns('posts', {
lead: { type: 'text', notNull: true },
- })
-}
+ });
+};
```
Run `npm run migrate up` and there will be new column in `posts` table :tada: :tada:
diff --git a/docs/index.html b/docs/index.html
index f161c43b..1d8b8ec4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -1,10 +1,16 @@
-
+
- node-pg-migrate - Postgresql database migration management tool for node.js
+
+ node-pg-migrate - Postgresql database migration management tool for
+ node.js
+
-
+
diff --git a/docs/migrations.md b/docs/migrations.md
index 122c8fa9..14b71990 100644
--- a/docs/migrations.md
+++ b/docs/migrations.md
@@ -3,11 +3,11 @@
When you run `node-pg-migrate create` a new migration file is created that looks like this:
```javascript
-exports.shorthands = undefined
+exports.shorthands = undefined;
-exports.up = function up(pgm) {}
+exports.up = function up(pgm) {};
-exports.down = function down(pgm) {}
+exports.down = function down(pgm) {};
```
`pgm` is a helper object that provides migration operations and `run` is the callback to call when you are done.
@@ -32,9 +32,9 @@ In some cases, you may want to perform some async operation during a migration,
```javascript
exports.up = function up(pgm, run) {
doSomethingAsync(function () {
- run()
- })
-}
+ run();
+ });
+};
```
Another way how to perform some async operation is to return [Promise](https://promisesaplus.com/) from `up` or `down` function. Example:
@@ -43,9 +43,9 @@ Another way how to perform some async operation is to return [Promise](https://p
exports.up = function (pgm) {
return new Promise((resolve) => {
// doSomethingAsync
- resolve()
- })
-}
+ resolve();
+ });
+};
```
or
@@ -53,7 +53,7 @@ or
```javascript
exports.up = async (pgm) => {
// doSomethingAsync
-}
+};
```
### Using schemas
@@ -73,7 +73,7 @@ CREATE TABLE "my_schema"."my_table_name" (
#### Type
```ts
-type Name = string | { schema: string; name: string }
+type Name = string | { schema: string; name: string };
```
### Locking
diff --git a/mocha.bootstrap.js b/mocha.bootstrap.js
index f86f2683..bcc07973 100644
--- a/mocha.bootstrap.js
+++ b/mocha.bootstrap.js
@@ -1,15 +1,15 @@
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
-const chai = require('chai')
-const sinonChai = require('sinon-chai')
-const chaiAsPromised = require('chai-as-promised')
+const chai = require('chai');
+const sinonChai = require('sinon-chai');
+const chaiAsPromised = require('chai-as-promised');
-const config = require('./tsconfig-test.json')
+const config = require('./tsconfig-test.json');
-config.compilerOptions.module = 'commonjs'
-config.transpileOnly = true
+config.compilerOptions.module = 'commonjs';
+config.transpileOnly = true;
// eslint-disable-next-line import/no-extraneous-dependencies
-require('ts-node').register(config)
+require('ts-node').register(config);
-chai.use(sinonChai)
-chai.use(chaiAsPromised)
+chai.use(sinonChai);
+chai.use(chaiAsPromised);
diff --git a/src/db.ts b/src/db.ts
index 5ca78969..e9d5a341 100644
--- a/src/db.ts
+++ b/src/db.ts
@@ -1,21 +1,37 @@
/*
This file just manages the database connection and provides a query method
*/
-import { inspect } from 'util'
-import { ClientBase, Client, ClientConfig, QueryArrayResult, QueryResult, QueryArrayConfig, QueryConfig } from 'pg'
-import { Logger, DB } from './types'
+import {
+ Client,
+ ClientBase,
+ ClientConfig,
+ QueryArrayConfig,
+ QueryArrayResult,
+ QueryConfig,
+ QueryResult,
+} from 'pg';
+import { inspect } from 'util';
+import { DB, Logger } from './types';
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface DBConnection extends DB {
- createConnection(): Promise
+ createConnection(): Promise;
- column(columnName: string, queryConfig: QueryArrayConfig, values?: any[]): Promise
- column(columnName: string, queryConfig: QueryConfig): Promise
- column(columnName: string, queryTextOrConfig: string | QueryConfig, values?: any[]): Promise
+ column(
+ columnName: string,
+ queryConfig: QueryArrayConfig,
+ values?: any[]
+ ): Promise;
+ column(columnName: string, queryConfig: QueryConfig): Promise;
+ column(
+ columnName: string,
+ queryTextOrConfig: string | QueryConfig,
+ values?: any[]
+ ): Promise;
- connected: () => boolean
- addBeforeCloseListener: (listener: any) => number
- close(): Promise
+ connected: () => boolean;
+ addBeforeCloseListener: (listener: any) => number;
+ close(): Promise;
}
enum ConnectionStatus {
@@ -24,82 +40,95 @@ enum ConnectionStatus {
ERROR = 'ERROR',
}
-const db = (connection: ClientBase | string | ClientConfig, logger: Logger = console): DBConnection => {
+const db = (
+ connection: ClientBase | string | ClientConfig,
+ logger: Logger = console
+): DBConnection => {
const isExternalClient =
- typeof connection === 'object' && 'query' in connection && typeof connection.query === 'function'
- let connectionStatus = ConnectionStatus.DISCONNECTED
+ typeof connection === 'object' &&
+ 'query' in connection &&
+ typeof connection.query === 'function';
+ let connectionStatus = ConnectionStatus.DISCONNECTED;
- const client: Client = isExternalClient ? (connection as Client) : new Client(connection as string | ClientConfig)
+ const client: Client = isExternalClient
+ ? (connection as Client)
+ : new Client(connection as string | ClientConfig);
- const beforeCloseListeners: any[] = []
+ const beforeCloseListeners: any[] = [];
const createConnection: () => Promise = () =>
new Promise((resolve, reject) => {
if (isExternalClient || connectionStatus === ConnectionStatus.CONNECTED) {
- resolve()
+ resolve();
} else if (connectionStatus === ConnectionStatus.ERROR) {
- reject(new Error('Connection already failed, do not try to connect again'))
+ reject(
+ new Error('Connection already failed, do not try to connect again')
+ );
} else {
client.connect((err) => {
if (err) {
- connectionStatus = ConnectionStatus.ERROR
- logger.error(`could not connect to postgres: ${inspect(err)}`)
- return reject(err)
+ connectionStatus = ConnectionStatus.ERROR;
+ logger.error(`could not connect to postgres: ${inspect(err)}`);
+ return reject(err);
}
- connectionStatus = ConnectionStatus.CONNECTED
- return resolve()
- })
+ connectionStatus = ConnectionStatus.CONNECTED;
+ return resolve();
+ });
}
- })
+ });
const query: DBConnection['query'] = async (
queryTextOrConfig: string | QueryConfig | QueryArrayConfig,
- values?: any[],
+ values?: any[]
): Promise => {
- await createConnection()
+ await createConnection();
try {
- return await client.query(queryTextOrConfig, values)
+ return await client.query(queryTextOrConfig, values);
} catch (err: any) {
- const { message, position }: { message: string; position: number } = err
- const string: string = typeof queryTextOrConfig === 'string' ? queryTextOrConfig : queryTextOrConfig.text
+ const { message, position }: { message: string; position: number } = err;
+ const string: string =
+ typeof queryTextOrConfig === 'string'
+ ? queryTextOrConfig
+ : queryTextOrConfig.text;
if (message && position >= 1) {
- const endLineWrapIndexOf = string.indexOf('\n', position)
- const endLineWrapPos = endLineWrapIndexOf >= 0 ? endLineWrapIndexOf : string.length
- const stringStart = string.substring(0, endLineWrapPos)
- const stringEnd = string.substr(endLineWrapPos)
- const startLineWrapPos = stringStart.lastIndexOf('\n') + 1
- const padding = ' '.repeat(position - startLineWrapPos - 1)
+ const endLineWrapIndexOf = string.indexOf('\n', position);
+ const endLineWrapPos =
+ endLineWrapIndexOf >= 0 ? endLineWrapIndexOf : string.length;
+ const stringStart = string.substring(0, endLineWrapPos);
+ const stringEnd = string.substr(endLineWrapPos);
+ const startLineWrapPos = stringStart.lastIndexOf('\n') + 1;
+ const padding = ' '.repeat(position - startLineWrapPos - 1);
logger.error(`Error executing:
${stringStart}
${padding}^^^^${stringEnd}
${message}
-`)
+`);
} else {
logger.error(`Error executing:
${string}
${err}
-`)
+`);
}
- throw err
+ throw err;
}
- }
+ };
const select: DBConnection['select'] = async (
queryTextOrConfig: string | QueryConfig | QueryArrayConfig,
- values?: any[],
+ values?: any[]
) => {
- const { rows } = await query(queryTextOrConfig, values)
- return rows
- }
+ const { rows } = await query(queryTextOrConfig, values);
+ return rows;
+ };
const column: DBConnection['column'] = async (
columnName: string,
queryTextOrConfig: string | QueryConfig | QueryArrayConfig,
- values?: any[],
+ values?: any[]
) => {
- const rows = await select(queryTextOrConfig, values)
- return rows.map((r: { [key: string]: any }) => r[columnName])
- }
+ const rows = await select(queryTextOrConfig, values);
+ return rows.map((r: { [key: string]: any }) => r[columnName]);
+ };
return {
createConnection,
@@ -111,15 +140,18 @@ ${err}
addBeforeCloseListener: (listener) => beforeCloseListeners.push(listener),
close: async () => {
await beforeCloseListeners.reduce(
- (promise, listener) => promise.then(listener).catch((err: any) => logger.error(err.stack || err)),
- Promise.resolve(),
- )
+ (promise, listener) =>
+ promise
+ .then(listener)
+ .catch((err: any) => logger.error(err.stack || err)),
+ Promise.resolve()
+ );
if (!isExternalClient) {
- connectionStatus = ConnectionStatus.DISCONNECTED
- client.end()
+ connectionStatus = ConnectionStatus.DISCONNECTED;
+ client.end();
}
},
- }
-}
+ };
+};
-export default db
+export default db;
diff --git a/src/index.ts b/src/index.ts
index 34053e4a..381e97b1 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,122 +1,148 @@
-import runner from './runner'
-import { Migration } from './migration'
-import { RunnerOption, MigrationBuilder, PgType } from './types'
-import PgLiteral from './operations/PgLiteral'
-import {
- PgLiteralValue,
- Value,
- Name,
- Type,
- IfExistsOption,
- IfNotExistsOption,
- CascadeOption,
- DropOptions,
-} from './operations/generalTypes'
+import { Migration } from './migration';
import {
+ AlterDomain,
CreateDomain,
+ DomainOptionsAlter,
+ DomainOptionsCreate,
DropDomain,
- AlterDomain,
RenameDomain,
- DomainOptionsCreate,
- DomainOptionsAlter,
-} from './operations/domainsTypes'
-import { CreateExtension, DropExtension, Extension, CreateExtensionOptions } from './operations/extensionsTypes'
+} from './operations/domainsTypes';
+import {
+ CreateExtension,
+ CreateExtensionOptions,
+ DropExtension,
+ Extension,
+} from './operations/extensionsTypes';
import {
CreateFunction,
DropFunction,
- RenameFunction,
- FunctionParam,
FunctionOptions,
-} from './operations/functionsTypes'
-import { DropIndex, CreateIndex, CreateIndexOptions, DropIndexOptions } from './operations/indexesTypes'
+ FunctionParam,
+ RenameFunction,
+} from './operations/functionsTypes';
+import {
+ CascadeOption,
+ DropOptions,
+ IfExistsOption,
+ IfNotExistsOption,
+ Name,
+ PgLiteralValue,
+ Type,
+ Value,
+} from './operations/generalTypes';
+import {
+ CreateIndex,
+ CreateIndexOptions,
+ DropIndex,
+ DropIndexOptions,
+} from './operations/indexesTypes';
import {
+ AddToOperatorFamily,
CreateOperator,
- DropOperator,
CreateOperatorClass,
- DropOperatorClass,
+ CreateOperatorClassOptions,
CreateOperatorFamily,
- DropOperatorFamily,
- AddToOperatorFamily,
- RenameOperatorFamily,
- RenameOperatorClass,
- RemoveFromOperatorFamily,
CreateOperatorOptions,
+ DropOperator,
+ DropOperatorClass,
+ DropOperatorFamily,
DropOperatorOptions,
OperatorListDefinition,
- CreateOperatorClassOptions,
-} from './operations/operatorsTypes'
-import { Sql } from './operations/othersTypes'
+ RemoveFromOperatorFamily,
+ RenameOperatorClass,
+ RenameOperatorFamily,
+} from './operations/operatorsTypes';
+import { Sql } from './operations/othersTypes';
+import PgLiteral from './operations/PgLiteral';
import {
- CreatePolicy,
- DropPolicy,
AlterPolicy,
- RenamePolicy,
+ CreatePolicy,
CreatePolicyOptions,
+ DropPolicy,
PolicyOptions,
-} from './operations/policiesTypes'
-import { CreateRole, DropRole, AlterRole, RenameRole, RoleOptions } from './operations/rolesTypes'
+ RenamePolicy,
+} from './operations/policiesTypes';
+import {
+ AlterRole,
+ CreateRole,
+ DropRole,
+ RenameRole,
+ RoleOptions,
+} from './operations/rolesTypes';
+import {
+ CreateSchema,
+ CreateSchemaOptions,
+ DropSchema,
+ RenameSchema,
+} from './operations/schemasTypes';
import {
+ AlterSequence,
CreateSequence,
DropSequence,
- AlterSequence,
RenameSequence,
- SequenceOptionsCreate,
SequenceOptionsAlter,
-} from './operations/sequencesTypes'
-import { CreateSchema, DropSchema, RenameSchema, CreateSchemaOptions } from './operations/schemasTypes'
+ SequenceOptionsCreate,
+} from './operations/sequencesTypes';
import {
- CreateTable,
- DropTable,
- AlterTable,
- RenameTable,
AddColumns,
- DropColumns,
AlterColumn,
- RenameColumn,
+ AlterColumnOptions,
+ AlterTable,
+ AlterTableOptions,
+ ColumnDefinition,
+ ColumnDefinitions,
+ ConstraintOptions,
CreateConstraint,
+ CreateTable,
+ DropColumns,
DropConstraint,
+ DropTable,
+ RenameColumn,
RenameConstraint,
- ColumnDefinition,
- ColumnDefinitions,
+ RenameTable,
TableOptions,
- AlterTableOptions,
- AlterColumnOptions,
- ConstraintOptions,
-} from './operations/tablesTypes'
-import { CreateTrigger, DropTrigger, RenameTrigger, TriggerOptions } from './operations/triggersTypes'
+} from './operations/tablesTypes';
import {
+ CreateTrigger,
+ DropTrigger,
+ RenameTrigger,
+ TriggerOptions,
+} from './operations/triggersTypes';
+import {
+ AddTypeAttribute,
+ AddTypeValue,
+ AddTypeValueOptions,
CreateType,
DropType,
- RenameType,
- AddTypeAttribute,
DropTypeAttribute,
- SetTypeAttribute,
- AddTypeValue,
+ RenameType,
RenameTypeAttribute,
RenameTypeValue,
- AddTypeValueOptions,
-} from './operations/typesTypes'
-import {
- CreateView,
- DropView,
- AlterView,
- AlterViewColumn,
- RenameView,
- CreateViewOptions,
- AlterViewOptions,
- AlterViewColumnOptions,
-} from './operations/viewsTypes'
+ SetTypeAttribute,
+} from './operations/typesTypes';
import {
+ AlterMaterializedView,
+ AlterMaterializedViewOptions,
CreateMaterializedView,
+ CreateMaterializedViewOptions,
DropMaterializedView,
- AlterMaterializedView,
- RenameMaterializedView,
- RenameMaterializedViewColumn,
RefreshMaterializedView,
- CreateMaterializedViewOptions,
- AlterMaterializedViewOptions,
RefreshMaterializedViewOptions,
-} from './operations/viewsMaterializedTypes'
+ RenameMaterializedView,
+ RenameMaterializedViewColumn,
+} from './operations/viewsMaterializedTypes';
+import {
+ AlterView,
+ AlterViewColumn,
+ AlterViewColumnOptions,
+ AlterViewOptions,
+ CreateView,
+ CreateViewOptions,
+ DropView,
+ RenameView,
+} from './operations/viewsTypes';
+import runner from './runner';
+import { MigrationBuilder, PgType, RunnerOption } from './types';
export {
PgLiteral,
@@ -235,6 +261,6 @@ export {
CreateMaterializedViewOptions,
AlterMaterializedViewOptions,
RefreshMaterializedViewOptions,
-}
+};
-export default runner
+export default runner;
diff --git a/src/migration-builder.ts b/src/migration-builder.ts
index 9a36a4e8..ab18ffcf 100644
--- a/src/migration-builder.ts
+++ b/src/migration-builder.ts
@@ -9,201 +9,315 @@
and it makes inference of down migrations possible.
*/
-import { createSchemalize } from './utils'
-import { ColumnDefinitions } from './operations/tablesTypes'
-import { DB, MigrationBuilder, MigrationOptions, Logger } from './types'
-
-import * as domains from './operations/domains'
-import * as extensions from './operations/extensions'
-import * as functions from './operations/functions'
-import * as indexes from './operations/indexes'
-import * as operators from './operations/operators'
-import * as other from './operations/other'
-import * as policies from './operations/policies'
-import * as roles from './operations/roles'
-import * as schemas from './operations/schemas'
-import * as sequences from './operations/sequences'
-import * as tables from './operations/tables'
-import * as triggers from './operations/triggers'
-import * as types from './operations/types'
-import * as views from './operations/views'
-import * as mViews from './operations/viewsMaterialized'
-import PgLiteral from './operations/PgLiteral'
+import * as domains from './operations/domains';
+import * as extensions from './operations/extensions';
+import * as functions from './operations/functions';
+import * as indexes from './operations/indexes';
+import * as operators from './operations/operators';
+import * as other from './operations/other';
+import PgLiteral from './operations/PgLiteral';
+import * as policies from './operations/policies';
+import * as roles from './operations/roles';
+import * as schemas from './operations/schemas';
+import * as sequences from './operations/sequences';
+import * as tables from './operations/tables';
+import { ColumnDefinitions } from './operations/tablesTypes';
+import * as triggers from './operations/triggers';
+import * as types from './operations/types';
+import * as views from './operations/views';
+import * as mViews from './operations/viewsMaterialized';
+import { DB, Logger, MigrationBuilder, MigrationOptions } from './types';
+import { createSchemalize } from './utils';
export default class MigrationBuilderImpl implements MigrationBuilder {
- public readonly createExtension: (...args: Parameters) => void
+ public readonly createExtension: (
+ ...args: Parameters
+ ) => void;
- public readonly dropExtension: (...args: Parameters) => void
+ public readonly dropExtension: (
+ ...args: Parameters
+ ) => void;
- public readonly addExtension: (...args: Parameters) => void
+ public readonly addExtension: (
+ ...args: Parameters
+ ) => void;
- public readonly createTable: (...args: Parameters) => void
+ public readonly createTable: (
+ ...args: Parameters
+ ) => void;
- public readonly dropTable: (...args: Parameters) => void
+ public readonly dropTable: (...args: Parameters) => void;
- public readonly renameTable: (...args: Parameters) => void
+ public readonly renameTable: (
+ ...args: Parameters
+ ) => void;
- public readonly alterTable: (...args: Parameters) => void
+ public readonly alterTable: (...args: Parameters) => void;
- public readonly addColumns: (...args: Parameters) => void
+ public readonly addColumns: (...args: Parameters) => void;
- public readonly dropColumns: (...args: Parameters) => void
+ public readonly dropColumns: (
+ ...args: Parameters
+ ) => void;
- public readonly renameColumn: (...args: Parameters) => void
+ public readonly renameColumn: (
+ ...args: Parameters
+ ) => void;
- public readonly alterColumn: (...args: Parameters) => void
+ public readonly alterColumn: (
+ ...args: Parameters
+ ) => void;
- public readonly addColumn: (...args: Parameters) => void
+ public readonly addColumn: (...args: Parameters) => void;
- public readonly dropColumn: (...args: Parameters) => void
+ public readonly dropColumn: (...args: Parameters) => void;
- public readonly addConstraint: (...args: Parameters) => void
+ public readonly addConstraint: (
+ ...args: Parameters
+ ) => void;
- public readonly dropConstraint: (...args: Parameters) => void
+ public readonly dropConstraint: (
+ ...args: Parameters
+ ) => void;
- public readonly renameConstraint: (...args: Parameters) => void
+ public readonly renameConstraint: (
+ ...args: Parameters
+ ) => void;
- public readonly createConstraint: (...args: Parameters) => void
+ public readonly createConstraint: (
+ ...args: Parameters
+ ) => void;
- public readonly createType: (...args: Parameters) => void
+ public readonly createType: (...args: Parameters) => void;
- public readonly dropType: (...args: Parameters) => void
+ public readonly dropType: (...args: Parameters) => void;
- public readonly addType: (...args: Parameters) => void
+ public readonly addType: (...args: Parameters) => void;
- public readonly renameType: (...args: Parameters) => void
+ public readonly renameType: (...args: Parameters) => void;
- public readonly renameTypeAttribute: (...args: Parameters) => void
+ public readonly renameTypeAttribute: (
+ ...args: Parameters
+ ) => void;
- public readonly renameTypeValue: (...args: Parameters) => void
+ public readonly renameTypeValue: (
+ ...args: Parameters
+ ) => void;
- public readonly addTypeAttribute: (...args: Parameters) => void
+ public readonly addTypeAttribute: (
+ ...args: Parameters
+ ) => void;
- public readonly dropTypeAttribute: (...args: Parameters) => void
+ public readonly dropTypeAttribute: (
+ ...args: Parameters
+ ) => void;
- public readonly setTypeAttribute: (...args: Parameters) => void
+ public readonly setTypeAttribute: (
+ ...args: Parameters
+ ) => void;
- public readonly addTypeValue: (...args: Parameters) => void
+ public readonly addTypeValue: (
+ ...args: Parameters
+ ) => void;
- public readonly createIndex: (...args: Parameters) => void
+ public readonly createIndex: (
+ ...args: Parameters
+ ) => void;
- public readonly dropIndex: (...args: Parameters) => void
+ public readonly dropIndex: (...args: Parameters) => void;
- public readonly addIndex: (...args: Parameters) => void
+ public readonly addIndex: (...args: Parameters) => void;
- public readonly createRole: (...args: Parameters) => void
+ public readonly createRole: (...args: Parameters) => void;
- public readonly dropRole: (...args: Parameters) => void
+ public readonly dropRole: (...args: Parameters) => void;
- public readonly alterRole: (...args: Parameters) => void
+ public readonly alterRole: (...args: Parameters) => void;
- public readonly renameRole: (...args: Parameters) => void
+ public readonly renameRole: (...args: Parameters) => void;
- public readonly createFunction: (...args: Parameters) => void
+ public readonly createFunction: (
+ ...args: Parameters
+ ) => void;
- public readonly dropFunction: (...args: Parameters) => void
+ public readonly dropFunction: (
+ ...args: Parameters
+ ) => void;
- public readonly renameFunction: (...args: Parameters) => void
+ public readonly renameFunction: (
+ ...args: Parameters
+ ) => void;
- public readonly createTrigger: (...args: Parameters) => void
+ public readonly createTrigger: (
+ ...args: Parameters
+ ) => void;
- public readonly dropTrigger: (...args: Parameters) => void
+ public readonly dropTrigger: (
+ ...args: Parameters
+ ) => void;
- public readonly renameTrigger: (...args: Parameters) => void
+ public readonly renameTrigger: (
+ ...args: Parameters
+ ) => void;
- public readonly createSchema: (...args: Parameters) => void
+ public readonly createSchema: (
+ ...args: Parameters
+ ) => void;
- public readonly dropSchema: (...args: Parameters) => void
+ public readonly dropSchema: (...args: Parameters) => void;
- public readonly renameSchema: (...args: Parameters) => void
+ public readonly renameSchema: (
+ ...args: Parameters
+ ) => void;
- public readonly createDomain: (...args: Parameters) => void
+ public readonly createDomain: (
+ ...args: Parameters
+ ) => void;
- public readonly dropDomain: (...args: Parameters) => void
+ public readonly dropDomain: (...args: Parameters) => void;
- public readonly alterDomain: (...args: Parameters) => void
+ public readonly alterDomain: (
+ ...args: Parameters
+ ) => void;
- public readonly renameDomain: (...args: Parameters) => void
+ public readonly renameDomain: (
+ ...args: Parameters
+ ) => void;
- public readonly createSequence: (...args: Parameters) => void
+ public readonly createSequence: (
+ ...args: Parameters
+ ) => void;
- public readonly dropSequence: (...args: Parameters) => void
+ public readonly dropSequence: (
+ ...args: Parameters
+ ) => void;
- public readonly alterSequence: (...args: Parameters) => void
+ public readonly alterSequence: (
+ ...args: Parameters
+ ) => void;
- public readonly renameSequence: (...args: Parameters) => void
+ public readonly renameSequence: (
+ ...args: Parameters
+ ) => void;
- public readonly createOperator: (...args: Parameters) => void
+ public readonly createOperator: (
+ ...args: Parameters
+ ) => void;
- public readonly dropOperator: (...args: Parameters) => void
+ public readonly dropOperator: (
+ ...args: Parameters
+ ) => void;
- public readonly createOperatorClass: (...args: Parameters) => void
+ public readonly createOperatorClass: (
+ ...args: Parameters
+ ) => void;
- public readonly dropOperatorClass: (...args: Parameters) => void
+ public readonly dropOperatorClass: (
+ ...args: Parameters
+ ) => void;
- public readonly renameOperatorClass: (...args: Parameters) => void
+ public readonly renameOperatorClass: (
+ ...args: Parameters
+ ) => void;
- public readonly createOperatorFamily: (...args: Parameters) => void
+ public readonly createOperatorFamily: (
+ ...args: Parameters
+ ) => void;
- public readonly dropOperatorFamily: (...args: Parameters) => void
+ public readonly dropOperatorFamily: (
+ ...args: Parameters
+ ) => void;
- public readonly renameOperatorFamily: (...args: Parameters) => void
+ public readonly renameOperatorFamily: (
+ ...args: Parameters
+ ) => void;
- public readonly addToOperatorFamily: (...args: Parameters) => void
+ public readonly addToOperatorFamily: (
+ ...args: Parameters
+ ) => void;
- public readonly removeFromOperatorFamily: (...args: Parameters) => void
+ public readonly removeFromOperatorFamily: (
+ ...args: Parameters
+ ) => void;
- public readonly createPolicy: (...args: Parameters) => void
+ public readonly createPolicy: (
+ ...args: Parameters
+ ) => void;
- public readonly dropPolicy: (...args: Parameters) => void
+ public readonly dropPolicy: (
+ ...args: Parameters
+ ) => void;
- public readonly alterPolicy: (...args: Parameters) => void
+ public readonly alterPolicy: (
+ ...args: Parameters
+ ) => void;
- public readonly renamePolicy: (...args: Parameters) => void
+ public readonly renamePolicy: (
+ ...args: Parameters
+ ) => void;
- public readonly createView: (...args: Parameters) => void
+ public readonly createView: (...args: Parameters) => void;
- public readonly dropView: (...args: Parameters) => void
+ public readonly dropView: (...args: Parameters) => void;
- public readonly alterView: (...args: Parameters) => void
+ public readonly alterView: (...args: Parameters) => void;
- public readonly alterViewColumn: (...args: Parameters) => void
+ public readonly alterViewColumn: (
+ ...args: Parameters
+ ) => void;
- public readonly renameView: (...args: Parameters) => void
+ public readonly renameView: (...args: Parameters) => void;
- public readonly createMaterializedView: (...args: Parameters) => void
+ public readonly createMaterializedView: (
+ ...args: Parameters
+ ) => void;
- public readonly dropMaterializedView: (...args: Parameters) => void
+ public readonly dropMaterializedView: (
+ ...args: Parameters
+ ) => void;
- public readonly alterMaterializedView: (...args: Parameters) => void
+ public readonly alterMaterializedView: (
+ ...args: Parameters
+ ) => void;
- public readonly renameMaterializedView: (...args: Parameters) => void
+ public readonly renameMaterializedView: (
+ ...args: Parameters
+ ) => void;
- public readonly renameMaterializedViewColumn: (...args: Parameters) => void
+ public readonly renameMaterializedViewColumn: (
+ ...args: Parameters
+ ) => void;
- public readonly refreshMaterializedView: (...args: Parameters) => void
+ public readonly refreshMaterializedView: (
+ ...args: Parameters
+ ) => void;
- public readonly sql: (...args: Parameters) => void
+ public readonly sql: (...args: Parameters) => void;
- public readonly func: (sql: string) => PgLiteral
+ public readonly func: (sql: string) => PgLiteral;
- public readonly db: DB
+ public readonly db: DB;
- private _steps: string[]
+ private _steps: string[];
- private _REVERSE_MODE: boolean
+ private _REVERSE_MODE: boolean;
- private _useTransaction: boolean
+ private _useTransaction: boolean;
- constructor(db: DB, typeShorthands: ColumnDefinitions | undefined, shouldDecamelize: boolean, logger: Logger) {
- this._steps = []
- this._REVERSE_MODE = false
+ constructor(
+ db: DB,
+ typeShorthands: ColumnDefinitions | undefined,
+ shouldDecamelize: boolean,
+ logger: Logger
+ ) {
+ this._steps = [];
+ this._REVERSE_MODE = false;
// by default, all migrations are wrapped in a transaction
- this._useTransaction = true
+ this._useTransaction = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
- type OperationFn = (...args: any[]) => string | string[]
- type Operation = OperationFn & { reverse?: OperationFn }
+ type OperationFn = (...args: any[]) => string | string[];
+ type Operation = OperationFn & { reverse?: OperationFn };
// this function wraps each operation within a function that either
// calls the operation or its reverse, and appends the result (array of sql statements)
@@ -213,122 +327,130 @@ export default class MigrationBuilderImpl implements MigrationBuilder {
(...args: Parameters) => {
if (this._REVERSE_MODE) {
if (typeof operation.reverse !== 'function') {
- const name = `pgm.${operation.name}()`
- throw new Error(`Impossible to automatically infer down migration for "${name}"`)
+ const name = `pgm.${operation.name}()`;
+ throw new Error(
+ `Impossible to automatically infer down migration for "${name}"`
+ );
}
- this._steps = this._steps.concat(operation.reverse(...args))
+ this._steps = this._steps.concat(operation.reverse(...args));
} else {
- this._steps = this._steps.concat(operation(...args))
+ this._steps = this._steps.concat(operation(...args));
}
- }
+ };
const options: MigrationOptions = {
typeShorthands,
schemalize: createSchemalize(shouldDecamelize, false),
literal: createSchemalize(shouldDecamelize, true),
logger,
- }
+ };
// defines the methods that are accessible via pgm in each migrations
// there are some convenience aliases to make usage easier
- this.createExtension = wrap(extensions.createExtension(options))
- this.dropExtension = wrap(extensions.dropExtension(options))
- this.addExtension = this.createExtension
-
- this.createTable = wrap(tables.createTable(options))
- this.dropTable = wrap(tables.dropTable(options))
- this.renameTable = wrap(tables.renameTable(options))
- this.alterTable = wrap(tables.alterTable(options))
-
- this.addColumns = wrap(tables.addColumns(options))
- this.dropColumns = wrap(tables.dropColumns(options))
- this.renameColumn = wrap(tables.renameColumn(options))
- this.alterColumn = wrap(tables.alterColumn(options))
- this.addColumn = this.addColumns
- this.dropColumn = this.dropColumns
-
- this.addConstraint = wrap(tables.addConstraint(options))
- this.dropConstraint = wrap(tables.dropConstraint(options))
- this.renameConstraint = wrap(tables.renameConstraint(options))
- this.createConstraint = this.addConstraint
-
- this.createType = wrap(types.createType(options))
- this.dropType = wrap(types.dropType(options))
- this.addType = this.createType
- this.renameType = wrap(types.renameType(options))
- this.renameTypeAttribute = wrap(types.renameTypeAttribute(options))
- this.renameTypeValue = wrap(types.renameTypeValue(options))
- this.addTypeAttribute = wrap(types.addTypeAttribute(options))
- this.dropTypeAttribute = wrap(types.dropTypeAttribute(options))
- this.setTypeAttribute = wrap(types.setTypeAttribute(options))
- this.addTypeValue = wrap(types.addTypeValue(options))
-
- this.createIndex = wrap(indexes.createIndex(options))
- this.dropIndex = wrap(indexes.dropIndex(options))
- this.addIndex = this.createIndex
-
- this.createRole = wrap(roles.createRole(options))
- this.dropRole = wrap(roles.dropRole(options))
- this.alterRole = wrap(roles.alterRole(options))
- this.renameRole = wrap(roles.renameRole(options))
-
- this.createFunction = wrap(functions.createFunction(options))
- this.dropFunction = wrap(functions.dropFunction(options))
- this.renameFunction = wrap(functions.renameFunction(options))
-
- this.createTrigger = wrap(triggers.createTrigger(options))
- this.dropTrigger = wrap(triggers.dropTrigger(options))
- this.renameTrigger = wrap(triggers.renameTrigger(options))
-
- this.createSchema = wrap(schemas.createSchema(options))
- this.dropSchema = wrap(schemas.dropSchema(options))
- this.renameSchema = wrap(schemas.renameSchema(options))
-
- this.createDomain = wrap(domains.createDomain(options))
- this.dropDomain = wrap(domains.dropDomain(options))
- this.alterDomain = wrap(domains.alterDomain(options))
- this.renameDomain = wrap(domains.renameDomain(options))
-
- this.createSequence = wrap(sequences.createSequence(options))
- this.dropSequence = wrap(sequences.dropSequence(options))
- this.alterSequence = wrap(sequences.alterSequence(options))
- this.renameSequence = wrap(sequences.renameSequence(options))
-
- this.createOperator = wrap(operators.createOperator(options))
- this.dropOperator = wrap(operators.dropOperator(options))
- this.createOperatorClass = wrap(operators.createOperatorClass(options))
- this.dropOperatorClass = wrap(operators.dropOperatorClass(options))
- this.renameOperatorClass = wrap(operators.renameOperatorClass(options))
- this.createOperatorFamily = wrap(operators.createOperatorFamily(options))
- this.dropOperatorFamily = wrap(operators.dropOperatorFamily(options))
- this.renameOperatorFamily = wrap(operators.renameOperatorFamily(options))
- this.addToOperatorFamily = wrap(operators.addToOperatorFamily(options))
- this.removeFromOperatorFamily = wrap(operators.removeFromOperatorFamily(options))
-
- this.createPolicy = wrap(policies.createPolicy(options))
- this.dropPolicy = wrap(policies.dropPolicy(options))
- this.alterPolicy = wrap(policies.alterPolicy(options))
- this.renamePolicy = wrap(policies.renamePolicy(options))
-
- this.createView = wrap(views.createView(options))
- this.dropView = wrap(views.dropView(options))
- this.alterView = wrap(views.alterView(options))
- this.alterViewColumn = wrap(views.alterViewColumn(options))
- this.renameView = wrap(views.renameView(options))
-
- this.createMaterializedView = wrap(mViews.createMaterializedView(options))
- this.dropMaterializedView = wrap(mViews.dropMaterializedView(options))
- this.alterMaterializedView = wrap(mViews.alterMaterializedView(options))
- this.renameMaterializedView = wrap(mViews.renameMaterializedView(options))
- this.renameMaterializedViewColumn = wrap(mViews.renameMaterializedViewColumn(options))
- this.refreshMaterializedView = wrap(mViews.refreshMaterializedView(options))
-
- this.sql = wrap(other.sql(options))
+ this.createExtension = wrap(extensions.createExtension(options));
+ this.dropExtension = wrap(extensions.dropExtension(options));
+ this.addExtension = this.createExtension;
+
+ this.createTable = wrap(tables.createTable(options));
+ this.dropTable = wrap(tables.dropTable(options));
+ this.renameTable = wrap(tables.renameTable(options));
+ this.alterTable = wrap(tables.alterTable(options));
+
+ this.addColumns = wrap(tables.addColumns(options));
+ this.dropColumns = wrap(tables.dropColumns(options));
+ this.renameColumn = wrap(tables.renameColumn(options));
+ this.alterColumn = wrap(tables.alterColumn(options));
+ this.addColumn = this.addColumns;
+ this.dropColumn = this.dropColumns;
+
+ this.addConstraint = wrap(tables.addConstraint(options));
+ this.dropConstraint = wrap(tables.dropConstraint(options));
+ this.renameConstraint = wrap(tables.renameConstraint(options));
+ this.createConstraint = this.addConstraint;
+
+ this.createType = wrap(types.createType(options));
+ this.dropType = wrap(types.dropType(options));
+ this.addType = this.createType;
+ this.renameType = wrap(types.renameType(options));
+ this.renameTypeAttribute = wrap(types.renameTypeAttribute(options));
+ this.renameTypeValue = wrap(types.renameTypeValue(options));
+ this.addTypeAttribute = wrap(types.addTypeAttribute(options));
+ this.dropTypeAttribute = wrap(types.dropTypeAttribute(options));
+ this.setTypeAttribute = wrap(types.setTypeAttribute(options));
+ this.addTypeValue = wrap(types.addTypeValue(options));
+
+ this.createIndex = wrap(indexes.createIndex(options));
+ this.dropIndex = wrap(indexes.dropIndex(options));
+ this.addIndex = this.createIndex;
+
+ this.createRole = wrap(roles.createRole(options));
+ this.dropRole = wrap(roles.dropRole(options));
+ this.alterRole = wrap(roles.alterRole(options));
+ this.renameRole = wrap(roles.renameRole(options));
+
+ this.createFunction = wrap(functions.createFunction(options));
+ this.dropFunction = wrap(functions.dropFunction(options));
+ this.renameFunction = wrap(functions.renameFunction(options));
+
+ this.createTrigger = wrap(triggers.createTrigger(options));
+ this.dropTrigger = wrap(triggers.dropTrigger(options));
+ this.renameTrigger = wrap(triggers.renameTrigger(options));
+
+ this.createSchema = wrap(schemas.createSchema(options));
+ this.dropSchema = wrap(schemas.dropSchema(options));
+ this.renameSchema = wrap(schemas.renameSchema(options));
+
+ this.createDomain = wrap(domains.createDomain(options));
+ this.dropDomain = wrap(domains.dropDomain(options));
+ this.alterDomain = wrap(domains.alterDomain(options));
+ this.renameDomain = wrap(domains.renameDomain(options));
+
+ this.createSequence = wrap(sequences.createSequence(options));
+ this.dropSequence = wrap(sequences.dropSequence(options));
+ this.alterSequence = wrap(sequences.alterSequence(options));
+ this.renameSequence = wrap(sequences.renameSequence(options));
+
+ this.createOperator = wrap(operators.createOperator(options));
+ this.dropOperator = wrap(operators.dropOperator(options));
+ this.createOperatorClass = wrap(operators.createOperatorClass(options));
+ this.dropOperatorClass = wrap(operators.dropOperatorClass(options));
+ this.renameOperatorClass = wrap(operators.renameOperatorClass(options));
+ this.createOperatorFamily = wrap(operators.createOperatorFamily(options));
+ this.dropOperatorFamily = wrap(operators.dropOperatorFamily(options));
+ this.renameOperatorFamily = wrap(operators.renameOperatorFamily(options));
+ this.addToOperatorFamily = wrap(operators.addToOperatorFamily(options));
+ this.removeFromOperatorFamily = wrap(
+ operators.removeFromOperatorFamily(options)
+ );
+
+ this.createPolicy = wrap(policies.createPolicy(options));
+ this.dropPolicy = wrap(policies.dropPolicy(options));
+ this.alterPolicy = wrap(policies.alterPolicy(options));
+ this.renamePolicy = wrap(policies.renamePolicy(options));
+
+ this.createView = wrap(views.createView(options));
+ this.dropView = wrap(views.dropView(options));
+ this.alterView = wrap(views.alterView(options));
+ this.alterViewColumn = wrap(views.alterViewColumn(options));
+ this.renameView = wrap(views.renameView(options));
+
+ this.createMaterializedView = wrap(mViews.createMaterializedView(options));
+ this.dropMaterializedView = wrap(mViews.dropMaterializedView(options));
+ this.alterMaterializedView = wrap(mViews.alterMaterializedView(options));
+ this.renameMaterializedView = wrap(mViews.renameMaterializedView(options));
+ this.renameMaterializedViewColumn = wrap(
+ mViews.renameMaterializedViewColumn(options)
+ );
+ this.refreshMaterializedView = wrap(
+ mViews.refreshMaterializedView(options)
+ );
+
+ this.sql = wrap(other.sql(options));
// Other utilities which may be useful
// .func creates a string which will not be escaped
// common uses are for PG functions, ex: { ... default: pgm.func('NOW()') }
- this.func = PgLiteral.create
+ this.func = PgLiteral.create;
// expose DB so we can access database within transaction
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -336,38 +458,38 @@ export default class MigrationBuilderImpl implements MigrationBuilder {
(operation: (...args: T) => R) =>
(...args: T) => {
if (this._REVERSE_MODE) {
- throw new Error('Impossible to automatically infer down migration')
+ throw new Error('Impossible to automatically infer down migration');
}
- return operation(...args)
- }
+ return operation(...args);
+ };
/* eslint-enable @typescript-eslint/no-explicit-any */
this.db = {
query: wrapDB(db.query),
select: wrapDB(db.select),
- }
+ };
}
enableReverseMode(): this {
- this._REVERSE_MODE = true
- return this
+ this._REVERSE_MODE = true;
+ return this;
}
noTransaction(): this {
- this._useTransaction = false
- return this
+ this._useTransaction = false;
+ return this;
}
isUsingTransaction(): boolean {
- return this._useTransaction
+ return this._useTransaction;
}
getSql(): string {
- return `${this.getSqlSteps().join('\n')}\n`
+ return `${this.getSqlSteps().join('\n')}\n`;
}
getSqlSteps(): string[] {
// in reverse mode, we flip the order of the statements
- return this._REVERSE_MODE ? this._steps.slice().reverse() : this._steps
+ return this._REVERSE_MODE ? this._steps.slice().reverse() : this._steps;
}
}
/* eslint-enable security/detect-non-literal-fs-filename */
diff --git a/src/migration.ts b/src/migration.ts
index eef6419c..ef07e144 100644
--- a/src/migration.ts
+++ b/src/migration.ts
@@ -6,21 +6,27 @@
*/
-import fs from 'fs'
-import mkdirp from 'mkdirp'
-import path from 'path'
-import { DBConnection } from './db'
-import MigrationBuilder from './migration-builder'
-import { MigrationAction, MigrationBuilderActions, MigrationDirection, RunnerOption, Logger } from './types'
-import { getMigrationTableSchema } from './utils'
-import { ColumnDefinitions } from './operations/tablesTypes'
-
-const { readdir } = fs.promises
+import fs from 'fs';
+import mkdirp from 'mkdirp';
+import path from 'path';
+import { DBConnection } from './db';
+import MigrationBuilder from './migration-builder';
+import { ColumnDefinitions } from './operations/tablesTypes';
+import {
+ Logger,
+ MigrationAction,
+ MigrationBuilderActions,
+ MigrationDirection,
+ RunnerOption,
+} from './types';
+import { getMigrationTableSchema } from './utils';
+
+const { readdir } = fs.promises;
export interface RunMigration {
- readonly path: string
- readonly name: string
- readonly timestamp: number
+ readonly path: string;
+ readonly name: string;
+ readonly timestamp: number;
}
export enum FilenameFormat {
@@ -29,66 +35,78 @@ export enum FilenameFormat {
}
export interface CreateOptionsTemplate {
- templateFileName: string
+ templateFileName: string;
}
export interface CreateOptionsDefault {
- language?: 'js' | 'ts' | 'sql'
- ignorePattern?: string
+ language?: 'js' | 'ts' | 'sql';
+ ignorePattern?: string;
}
export type CreateOptions = {
- filenameFormat?: FilenameFormat
-} & (CreateOptionsTemplate | CreateOptionsDefault)
+ filenameFormat?: FilenameFormat;
+} & (CreateOptionsTemplate | CreateOptionsDefault);
-const SEPARATOR = '_'
+const SEPARATOR = '_';
-export const loadMigrationFiles = async (dir: string, ignorePattern?: string) => {
- const dirContent = await readdir(`${dir}/`, { withFileTypes: true })
+export const loadMigrationFiles = async (
+ dir: string,
+ ignorePattern?: string
+) => {
+ const dirContent = await readdir(`${dir}/`, { withFileTypes: true });
const files = dirContent
.map((file) => (file.isFile() || file.isSymbolicLink() ? file.name : null))
.filter((file): file is string => Boolean(file))
- .sort()
- const filter = new RegExp(`^(${ignorePattern})$`) // eslint-disable-line security/detect-non-literal-regexp
- return ignorePattern === undefined ? files : files.filter((i) => !filter.test(i))
-}
+ .sort();
+ const filter = new RegExp(`^(${ignorePattern})$`); // eslint-disable-line security/detect-non-literal-regexp
+ return ignorePattern === undefined
+ ? files
+ : files.filter((i) => !filter.test(i));
+};
-const getSuffixFromFileName = (fileName: string) => path.extname(fileName).substr(1)
+const getSuffixFromFileName = (fileName: string) =>
+ path.extname(fileName).substr(1);
const getLastSuffix = async (dir: string, ignorePattern?: string) => {
try {
- const files = await loadMigrationFiles(dir, ignorePattern)
- return files.length > 0 ? getSuffixFromFileName(files[files.length - 1]) : undefined
+ const files = await loadMigrationFiles(dir, ignorePattern);
+ return files.length > 0
+ ? getSuffixFromFileName(files[files.length - 1])
+ : undefined;
} catch (err) {
- return undefined
+ return undefined;
}
-}
+};
export const getTimestamp = (logger: Logger, filename: string): number => {
- const prefix = filename.split(SEPARATOR)[0]
+ const prefix = filename.split(SEPARATOR)[0];
if (prefix && /^\d+$/.test(prefix)) {
if (prefix.length === 13) {
// timestamp: 1391877300255
- return Number(prefix)
+ return Number(prefix);
}
if (prefix && prefix.length === 17) {
// utc: 20200513070724505
- const year = prefix.substr(0, 4)
- const month = prefix.substr(4, 2)
- const date = prefix.substr(6, 2)
- const hours = prefix.substr(8, 2)
- const minutes = prefix.substr(10, 2)
- const seconds = prefix.substr(12, 2)
- const ms = prefix.substr(14)
- return new Date(`${year}-${month}-${date}T${hours}:${minutes}:${seconds}.${ms}Z`).valueOf()
+ const year = prefix.substr(0, 4);
+ const month = prefix.substr(4, 2);
+ const date = prefix.substr(6, 2);
+ const hours = prefix.substr(8, 2);
+ const minutes = prefix.substr(10, 2);
+ const seconds = prefix.substr(12, 2);
+ const ms = prefix.substr(14);
+ return new Date(
+ `${year}-${month}-${date}T${hours}:${minutes}:${seconds}.${ms}Z`
+ ).valueOf();
}
}
- logger.error(`Can't determine timestamp for ${prefix}`)
- return Number(prefix) || 0
-}
+ logger.error(`Can't determine timestamp for ${prefix}`);
+ return Number(prefix) || 0;
+};
-const resolveSuffix = async (directory: string, { language, ignorePattern }: CreateOptionsDefault) =>
- language || (await getLastSuffix(directory, ignorePattern)) || 'js'
+const resolveSuffix = async (
+ directory: string,
+ { language, ignorePattern }: CreateOptionsDefault
+) => language || (await getLastSuffix(directory, ignorePattern)) || 'js';
export class Migration implements RunMigration {
// class method that creates a new migration file by cloning the migration template
@@ -97,32 +115,44 @@ export class Migration implements RunMigration {
directory: string,
_language?: 'js' | 'ts' | 'sql' | CreateOptions,
_ignorePattern?: string,
- _filenameFormat?: FilenameFormat,
+ _filenameFormat?: FilenameFormat
) {
if (typeof _language === 'string') {
// eslint-disable-next-line no-console
- console.warn('This usage is deprecated. Please use this method with options object argument')
+ console.warn(
+ 'This usage is deprecated. Please use this method with options object argument'
+ );
}
const options =
typeof _language === 'object'
? _language
- : { language: _language, ignorePattern: _ignorePattern, filenameFormat: _filenameFormat }
- const { filenameFormat = FilenameFormat.timestamp } = options
+ : {
+ language: _language,
+ ignorePattern: _ignorePattern,
+ filenameFormat: _filenameFormat,
+ };
+ const { filenameFormat = FilenameFormat.timestamp } = options;
// ensure the migrations directory exists
- mkdirp.sync(directory)
+ mkdirp.sync(directory);
- const now = new Date()
- const time = filenameFormat === FilenameFormat.utc ? now.toISOString().replace(/[^\d]/g, '') : now.valueOf()
+ const now = new Date();
+ const time =
+ filenameFormat === FilenameFormat.utc
+ ? now.toISOString().replace(/[^\d]/g, '')
+ : now.valueOf();
const templateFileName =
'templateFileName' in options
? path.resolve(process.cwd(), options.templateFileName)
- : path.resolve(__dirname, `../templates/migration-template.${await resolveSuffix(directory, options)}`)
- const suffix = getSuffixFromFileName(templateFileName)
+ : path.resolve(
+ __dirname,
+ `../templates/migration-template.${await resolveSuffix(directory, options)}`
+ );
+ const suffix = getSuffixFromFileName(templateFileName);
// file name looks like migrations/1391877300255_migration-title.js
- const newFile = `${directory}/${time}${SEPARATOR}${name}.${suffix}`
+ const newFile = `${directory}/${time}${SEPARATOR}${name}.${suffix}`;
// copy the default migration template to the new file location
await new Promise((resolve, reject) => {
@@ -131,29 +161,29 @@ export class Migration implements RunMigration {
// eslint-disable-next-line security/detect-non-literal-fs-filename
.pipe(fs.createWriteStream(newFile))
.on('close', resolve)
- .on('error', reject)
- })
+ .on('error', reject);
+ });
- return newFile
+ return newFile;
}
- public readonly db: DBConnection
+ public readonly db: DBConnection;
- public readonly path: string
+ public readonly path: string;
- public readonly name: string
+ public readonly name: string;
- public readonly timestamp: number
+ public readonly timestamp: number;
- public up?: false | MigrationAction
+ public up?: false | MigrationAction;
- public down?: false | MigrationAction
+ public down?: false | MigrationAction;
- public readonly options: RunnerOption
+ public readonly options: RunnerOption;
- public readonly typeShorthands?: ColumnDefinitions
+ public readonly typeShorthands?: ColumnDefinitions;
- public readonly logger: Logger
+ public readonly logger: Logger;
constructor(
db: DBConnection,
@@ -161,104 +191,114 @@ export class Migration implements RunMigration {
{ up, down }: MigrationBuilderActions,
options: RunnerOption,
typeShorthands?: ColumnDefinitions,
- logger: Logger = console,
+ logger: Logger = console
) {
- this.db = db
- this.path = migrationPath
- this.name = path.basename(migrationPath, path.extname(migrationPath))
- this.timestamp = getTimestamp(logger, this.name)
- this.up = up
- this.down = down
- this.options = options
- this.typeShorthands = typeShorthands
- this.logger = logger
+ this.db = db;
+ this.path = migrationPath;
+ this.name = path.basename(migrationPath, path.extname(migrationPath));
+ this.timestamp = getTimestamp(logger, this.name);
+ this.up = up;
+ this.down = down;
+ this.options = options;
+ this.typeShorthands = typeShorthands;
+ this.logger = logger;
}
_getMarkAsRun(action: MigrationAction) {
- const schema = getMigrationTableSchema(this.options)
- const { migrationsTable } = this.options
- const { name } = this
+ const schema = getMigrationTableSchema(this.options);
+ const { migrationsTable } = this.options;
+ const { name } = this;
switch (action) {
case this.down:
- this.logger.info(`### MIGRATION ${this.name} (DOWN) ###`)
- return `DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';`
+ this.logger.info(`### MIGRATION ${this.name} (DOWN) ###`);
+ return `DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';`;
case this.up:
- this.logger.info(`### MIGRATION ${this.name} (UP) ###`)
- return `INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());`
+ this.logger.info(`### MIGRATION ${this.name} (UP) ###`);
+ return `INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());`;
default:
- throw new Error('Unknown direction')
+ throw new Error('Unknown direction');
}
}
async _apply(action: MigrationAction, pgm: MigrationBuilder) {
if (action.length === 2) {
await new Promise((resolve) => {
- action(pgm, resolve)
- })
+ action(pgm, resolve);
+ });
} else {
- await action(pgm)
+ await action(pgm);
}
- const sqlSteps = pgm.getSqlSteps()
+ const sqlSteps = pgm.getSqlSteps();
- sqlSteps.push(this._getMarkAsRun(action))
+ sqlSteps.push(this._getMarkAsRun(action));
if (!this.options.singleTransaction && pgm.isUsingTransaction()) {
// if not in singleTransaction mode we need to create our own transaction
- sqlSteps.unshift('BEGIN;')
- sqlSteps.push('COMMIT;')
+ sqlSteps.unshift('BEGIN;');
+ sqlSteps.push('COMMIT;');
} else if (this.options.singleTransaction && !pgm.isUsingTransaction()) {
// in singleTransaction mode we are already wrapped in a global transaction
- this.logger.warn('#> WARNING: Need to break single transaction! <')
- sqlSteps.unshift('COMMIT;')
- sqlSteps.push('BEGIN;')
+ this.logger.warn('#> WARNING: Need to break single transaction! <');
+ sqlSteps.unshift('COMMIT;');
+ sqlSteps.push('BEGIN;');
} else if (!this.options.singleTransaction || !pgm.isUsingTransaction()) {
- this.logger.warn('#> WARNING: This migration is not wrapped in a transaction! <')
+ this.logger.warn(
+ '#> WARNING: This migration is not wrapped in a transaction! <'
+ );
}
if (typeof this.logger.debug === 'function') {
- this.logger.debug(`${sqlSteps.join('\n')}\n\n`)
+ this.logger.debug(`${sqlSteps.join('\n')}\n\n`);
}
return sqlSteps.reduce(
- (promise: Promise, sql) => promise.then((): unknown => this.options.dryRun || this.db.query(sql)),
- Promise.resolve(),
- )
+ (promise: Promise, sql) =>
+ promise.then((): unknown => this.options.dryRun || this.db.query(sql)),
+ Promise.resolve()
+ );
}
_getAction(direction: MigrationDirection) {
if (direction === 'down' && this.down === undefined) {
- this.down = this.up
+ this.down = this.up;
}
- const action: MigrationAction | false | undefined = this[direction]
+ const action: MigrationAction | false | undefined = this[direction];
if (action === false) {
- throw new Error(`User has disabled ${direction} migration on file: ${this.name}`)
+ throw new Error(
+ `User has disabled ${direction} migration on file: ${this.name}`
+ );
}
if (typeof action !== 'function') {
throw new Error(
- `Unknown value for direction: ${direction}. Is the migration ${this.name} exporting a '${direction}' function?`,
- )
+ `Unknown value for direction: ${direction}. Is the migration ${this.name} exporting a '${direction}' function?`
+ );
}
- return action
+ return action;
}
apply(direction: MigrationDirection) {
- const pgm = new MigrationBuilder(this.db, this.typeShorthands, Boolean(this.options.decamelize), this.logger)
- const action = this._getAction(direction)
+ const pgm = new MigrationBuilder(
+ this.db,
+ this.typeShorthands,
+ Boolean(this.options.decamelize),
+ this.logger
+ );
+ const action = this._getAction(direction);
if (this.down === this.up) {
// automatically infer the down migration by running the up migration in reverse mode...
- pgm.enableReverseMode()
+ pgm.enableReverseMode();
}
- return this._apply(action, pgm)
+ return this._apply(action, pgm);
}
markAsRun(direction: MigrationDirection) {
- return this.db.query(this._getMarkAsRun(this._getAction(direction)))
+ return this.db.query(this._getMarkAsRun(this._getAction(direction)));
}
}
diff --git a/src/operations/PgLiteral.ts b/src/operations/PgLiteral.ts
index 59775232..9cbb1d7c 100644
--- a/src/operations/PgLiteral.ts
+++ b/src/operations/PgLiteral.ts
@@ -2,15 +2,15 @@
// exposed in the migrations via pgm.func
export default class PgLiteral {
static create(str: string): PgLiteral {
- return new PgLiteral(str)
+ return new PgLiteral(str);
}
- public readonly literal = true
+ public readonly literal = true;
// eslint-disable-next-line no-useless-constructor
constructor(public readonly value: string) {}
toString(): string {
- return this.value
+ return this.value;
}
}
diff --git a/src/operations/domains.ts b/src/operations/domains.ts
index 583df4ec..8888c04e 100644
--- a/src/operations/domains.ts
+++ b/src/operations/domains.ts
@@ -1,83 +1,106 @@
-import { MigrationOptions } from '../types'
-import { applyType, escapeValue } from '../utils'
-import { CreateDomain, DropDomain, AlterDomain, RenameDomain } from './domainsTypes'
+import { MigrationOptions } from '../types';
+import { applyType, escapeValue } from '../utils';
+import {
+ AlterDomain,
+ CreateDomain,
+ DropDomain,
+ RenameDomain,
+} from './domainsTypes';
-export { CreateDomain, DropDomain, AlterDomain, RenameDomain }
+export { CreateDomain, DropDomain, AlterDomain, RenameDomain };
export function dropDomain(mOptions: MigrationOptions) {
const _drop: DropDomain = (domainName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const domainNameStr = mOptions.literal(domainName)
- return `DROP DOMAIN${ifExistsStr} ${domainNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const domainNameStr = mOptions.literal(domainName);
+ return `DROP DOMAIN${ifExistsStr} ${domainNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createDomain(mOptions: MigrationOptions) {
const _create: CreateDomain = (domainName, type, options = {}) => {
- const { default: defaultValue, collation, notNull, check, constraintName } = options
- const constraints = []
+ const {
+ default: defaultValue,
+ collation,
+ notNull,
+ check,
+ constraintName,
+ } = options;
+ const constraints = [];
if (collation) {
- constraints.push(`COLLATE ${collation}`)
+ constraints.push(`COLLATE ${collation}`);
}
if (defaultValue !== undefined) {
- constraints.push(`DEFAULT ${escapeValue(defaultValue)}`)
+ constraints.push(`DEFAULT ${escapeValue(defaultValue)}`);
}
if (notNull && check) {
- throw new Error('"notNull" and "check" can\'t be specified together')
+ throw new Error('"notNull" and "check" can\'t be specified together');
} else if (notNull || check) {
if (constraintName) {
- constraints.push(`CONSTRAINT ${mOptions.literal(constraintName)}`)
+ constraints.push(`CONSTRAINT ${mOptions.literal(constraintName)}`);
}
if (notNull) {
- constraints.push('NOT NULL')
+ constraints.push('NOT NULL');
} else if (check) {
- constraints.push(`CHECK (${check})`)
+ constraints.push(`CHECK (${check})`);
}
}
- const constraintsStr = constraints.length ? ` ${constraints.join(' ')}` : ''
+ const constraintsStr = constraints.length
+ ? ` ${constraints.join(' ')}`
+ : '';
- const typeStr = applyType(type, mOptions.typeShorthands).type
- const domainNameStr = mOptions.literal(domainName)
+ const typeStr = applyType(type, mOptions.typeShorthands).type;
+ const domainNameStr = mOptions.literal(domainName);
- return `CREATE DOMAIN ${domainNameStr} AS ${typeStr}${constraintsStr};`
- }
- _create.reverse = (domainName, type, options) => dropDomain(mOptions)(domainName, options)
- return _create
+ return `CREATE DOMAIN ${domainNameStr} AS ${typeStr}${constraintsStr};`;
+ };
+ _create.reverse = (domainName, type, options) =>
+ dropDomain(mOptions)(domainName, options);
+ return _create;
}
export function alterDomain(mOptions: MigrationOptions) {
const _alter: AlterDomain = (domainName, options) => {
- const { default: defaultValue, notNull, allowNull, check, constraintName } = options
- const actions = []
+ const {
+ default: defaultValue,
+ notNull,
+ allowNull,
+ check,
+ constraintName,
+ } = options;
+ const actions = [];
if (defaultValue === null) {
- actions.push('DROP DEFAULT')
+ actions.push('DROP DEFAULT');
} else if (defaultValue !== undefined) {
- actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`)
+ actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`);
}
if (notNull) {
- actions.push('SET NOT NULL')
+ actions.push('SET NOT NULL');
} else if (notNull === false || allowNull) {
- actions.push('DROP NOT NULL')
+ actions.push('DROP NOT NULL');
}
if (check) {
- actions.push(`${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}CHECK (${check})`)
+ actions.push(
+ `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}CHECK (${check})`
+ );
}
- return `${actions.map((action) => `ALTER DOMAIN ${mOptions.literal(domainName)} ${action}`).join(';\n')};`
- }
- return _alter
+ return `${actions.map((action) => `ALTER DOMAIN ${mOptions.literal(domainName)} ${action}`).join(';\n')};`;
+ };
+ return _alter;
}
export function renameDomain(mOptions: MigrationOptions) {
const _rename: RenameDomain = (domainName, newDomainName) => {
- const domainNameStr = mOptions.literal(domainName)
- const newDomainNameStr = mOptions.literal(newDomainName)
- return `ALTER DOMAIN ${domainNameStr} RENAME TO ${newDomainNameStr};`
- }
- _rename.reverse = (domainName, newDomainName) => _rename(newDomainName, domainName)
- return _rename
+ const domainNameStr = mOptions.literal(domainName);
+ const newDomainNameStr = mOptions.literal(newDomainName);
+ return `ALTER DOMAIN ${domainNameStr} RENAME TO ${newDomainNameStr};`;
+ };
+ _rename.reverse = (domainName, newDomainName) =>
+ _rename(newDomainName, domainName);
+ return _rename;
}
diff --git a/src/operations/domainsTypes.ts b/src/operations/domainsTypes.ts
index b4f9e7c0..d0b4569e 100644
--- a/src/operations/domainsTypes.ts
+++ b/src/operations/domainsTypes.ts
@@ -1,27 +1,36 @@
-import { Value, Name, Type, DropOptions } from './generalTypes'
+import { DropOptions, Name, Type, Value } from './generalTypes';
export interface DomainOptions {
- default?: Value
- notNull?: boolean
- check?: string
- constraintName?: string
+ default?: Value;
+ notNull?: boolean;
+ check?: string;
+ constraintName?: string;
}
export interface DomainOptionsCreate extends DomainOptions {
- collation?: string
+ collation?: string;
}
export interface DomainOptionsAlter extends DomainOptions {
- allowNull?: boolean
+ allowNull?: boolean;
}
type CreateDomainFn = (
domainName: Name,
type: Type,
- domainOptions?: DomainOptionsCreate & DropOptions,
-) => string | string[]
-export type CreateDomain = CreateDomainFn & { reverse: CreateDomainFn }
-export type DropDomain = (domainName: Name, dropOptions?: DropOptions) => string | string[]
-export type AlterDomain = (domainName: Name, domainOptions: DomainOptionsAlter) => string | string[]
-type RenameDomainFn = (oldDomainName: Name, newDomainName: Name) => string | string[]
-export type RenameDomain = RenameDomainFn & { reverse: RenameDomainFn }
+ domainOptions?: DomainOptionsCreate & DropOptions
+) => string | string[];
+export type CreateDomain = CreateDomainFn & { reverse: CreateDomainFn };
+export type DropDomain = (
+ domainName: Name,
+ dropOptions?: DropOptions
+) => string | string[];
+export type AlterDomain = (
+ domainName: Name,
+ domainOptions: DomainOptionsAlter
+) => string | string[];
+type RenameDomainFn = (
+ oldDomainName: Name,
+ newDomainName: Name
+) => string | string[];
+export type RenameDomain = RenameDomainFn & { reverse: RenameDomainFn };
diff --git a/src/operations/extensions.ts b/src/operations/extensions.ts
index bd4358af..378e8b79 100644
--- a/src/operations/extensions.ts
+++ b/src/operations/extensions.ts
@@ -1,33 +1,33 @@
-import { MigrationOptions } from '../types'
-import { CreateExtension, DropExtension } from './extensionsTypes'
+import { MigrationOptions } from '../types';
+import { CreateExtension, DropExtension } from './extensionsTypes';
-export { CreateExtension, DropExtension }
+export { CreateExtension, DropExtension };
export function dropExtension(mOptions: MigrationOptions) {
const _drop: DropExtension = (_extensions, options = {}) => {
- const { ifExists, cascade } = options
- const extensions = Array.isArray(_extensions) ? _extensions : [_extensions]
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
+ const { ifExists, cascade } = options;
+ const extensions = Array.isArray(_extensions) ? _extensions : [_extensions];
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
return extensions.map((extension) => {
- const extensionStr = mOptions.literal(extension)
- return `DROP EXTENSION${ifExistsStr} ${extensionStr}${cascadeStr};`
- })
- }
- return _drop
+ const extensionStr = mOptions.literal(extension);
+ return `DROP EXTENSION${ifExistsStr} ${extensionStr}${cascadeStr};`;
+ });
+ };
+ return _drop;
}
export function createExtension(mOptions: MigrationOptions) {
const _create: CreateExtension = (_extensions, options = {}) => {
- const { ifNotExists, schema } = options
- const extensions = Array.isArray(_extensions) ? _extensions : [_extensions]
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const schemaStr = schema ? ` SCHEMA ${mOptions.literal(schema)}` : ''
+ const { ifNotExists, schema } = options;
+ const extensions = Array.isArray(_extensions) ? _extensions : [_extensions];
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const schemaStr = schema ? ` SCHEMA ${mOptions.literal(schema)}` : '';
return extensions.map((extension) => {
- const extensionStr = mOptions.literal(extension)
- return `CREATE EXTENSION${ifNotExistsStr} ${extensionStr}${schemaStr};`
- })
- }
- _create.reverse = dropExtension(mOptions)
- return _create
+ const extensionStr = mOptions.literal(extension);
+ return `CREATE EXTENSION${ifNotExistsStr} ${extensionStr}${schemaStr};`;
+ });
+ };
+ _create.reverse = dropExtension(mOptions);
+ return _create;
}
diff --git a/src/operations/extensionsTypes.ts b/src/operations/extensionsTypes.ts
index ec18600f..a5b333e2 100644
--- a/src/operations/extensionsTypes.ts
+++ b/src/operations/extensionsTypes.ts
@@ -1,4 +1,4 @@
-import { LiteralUnion, DropOptions, IfNotExistsOption } from './generalTypes'
+import { DropOptions, IfNotExistsOption, LiteralUnion } from './generalTypes';
export type Extension =
| 'adminpack'
@@ -45,19 +45,21 @@ export type Extension =
| 'tsm_system_time'
| 'unaccent'
| 'uuid-ossp'
- | 'xml2'
+ | 'xml2';
export interface CreateExtensionOptions extends IfNotExistsOption {
- schema?: string
+ schema?: string;
}
-type StringExtension = LiteralUnion
+type StringExtension = LiteralUnion;
type CreateExtensionFn = (
extension: StringExtension | Array,
- options?: CreateExtensionOptions & DropOptions,
-) => string | string[]
-export type CreateExtension = CreateExtensionFn & { reverse: CreateExtensionFn }
+ options?: CreateExtensionOptions & DropOptions
+) => string | string[];
+export type CreateExtension = CreateExtensionFn & {
+ reverse: CreateExtensionFn;
+};
export type DropExtension = (
extension: StringExtension | Array,
- dropOptions?: DropOptions,
-) => string | string[]
+ dropOptions?: DropOptions
+) => string | string[];
diff --git a/src/operations/functions.ts b/src/operations/functions.ts
index 44ff6bf9..4a5a9cb0 100644
--- a/src/operations/functions.ts
+++ b/src/operations/functions.ts
@@ -1,66 +1,89 @@
-import { MigrationOptions } from '../types'
-import { escapeValue, formatParams } from '../utils'
-import { CreateFunction, DropFunction, RenameFunction } from './functionsTypes'
+import { MigrationOptions } from '../types';
+import { escapeValue, formatParams } from '../utils';
+import { CreateFunction, DropFunction, RenameFunction } from './functionsTypes';
-export { CreateFunction, DropFunction, RenameFunction }
+export { CreateFunction, DropFunction, RenameFunction };
export function dropFunction(mOptions: MigrationOptions) {
- const _drop: DropFunction = (functionName, functionParams = [], options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const paramsStr = formatParams(functionParams, mOptions)
- const functionNameStr = mOptions.literal(functionName)
- return `DROP FUNCTION${ifExistsStr} ${functionNameStr}${paramsStr}${cascadeStr};`
- }
- return _drop
+ const _drop: DropFunction = (
+ functionName,
+ functionParams = [],
+ options = {}
+ ) => {
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const paramsStr = formatParams(functionParams, mOptions);
+ const functionNameStr = mOptions.literal(functionName);
+ return `DROP FUNCTION${ifExistsStr} ${functionNameStr}${paramsStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createFunction(mOptions: MigrationOptions) {
- // eslint-disable-next-line default-param-last
- const _create: CreateFunction = (functionName, functionParams = [], functionOptions, definition) => {
- const { replace, returns = 'void', language, window, behavior = 'VOLATILE', onNull, parallel } = functionOptions
- const options = []
+ const _create: CreateFunction = (
+ functionName,
+ // eslint-disable-next-line default-param-last
+ functionParams = [],
+ functionOptions,
+ definition
+ ) => {
+ const {
+ replace,
+ returns = 'void',
+ language,
+ window,
+ behavior = 'VOLATILE',
+ onNull,
+ parallel,
+ } = functionOptions;
+ const options = [];
if (behavior) {
- options.push(behavior)
+ options.push(behavior);
}
if (language) {
- options.push(`LANGUAGE ${language}`)
+ options.push(`LANGUAGE ${language}`);
} else {
- throw new Error(`Language for function ${functionName} have to be specified`)
+ throw new Error(
+ `Language for function ${functionName} have to be specified`
+ );
}
if (window) {
- options.push('WINDOW')
+ options.push('WINDOW');
}
if (onNull) {
- options.push('RETURNS NULL ON NULL INPUT')
+ options.push('RETURNS NULL ON NULL INPUT');
}
if (parallel) {
- options.push(`PARALLEL ${parallel}`)
+ options.push(`PARALLEL ${parallel}`);
}
- const replaceStr = replace ? ' OR REPLACE' : ''
- const paramsStr = formatParams(functionParams, mOptions)
- const functionNameStr = mOptions.literal(functionName)
+ const replaceStr = replace ? ' OR REPLACE' : '';
+ const paramsStr = formatParams(functionParams, mOptions);
+ const functionNameStr = mOptions.literal(functionName);
return `CREATE${replaceStr} FUNCTION ${functionNameStr}${paramsStr}
RETURNS ${returns}
AS ${escapeValue(definition)}
- ${options.join('\n ')};`
- }
- _create.reverse = dropFunction(mOptions)
- return _create
+ ${options.join('\n ')};`;
+ };
+ _create.reverse = dropFunction(mOptions);
+ return _create;
}
export function renameFunction(mOptions: MigrationOptions) {
- // eslint-disable-next-line default-param-last
- const _rename: RenameFunction = (oldFunctionName, functionParams = [], newFunctionName) => {
- const paramsStr = formatParams(functionParams, mOptions)
- const oldFunctionNameStr = mOptions.literal(oldFunctionName)
- const newFunctionNameStr = mOptions.literal(newFunctionName)
- return `ALTER FUNCTION ${oldFunctionNameStr}${paramsStr} RENAME TO ${newFunctionNameStr};`
- }
+ const _rename: RenameFunction = (
+ oldFunctionName,
+ // eslint-disable-next-line default-param-last
+ functionParams = [],
+ newFunctionName
+ ) => {
+ const paramsStr = formatParams(functionParams, mOptions);
+ const oldFunctionNameStr = mOptions.literal(oldFunctionName);
+ const newFunctionNameStr = mOptions.literal(newFunctionName);
+ return `ALTER FUNCTION ${oldFunctionNameStr}${paramsStr} RENAME TO ${newFunctionNameStr};`;
+ };
_rename.reverse = (oldFunctionName, functionParams, newFunctionName) =>
- _rename(newFunctionName, functionParams, oldFunctionName)
- return _rename
+ _rename(newFunctionName, functionParams, oldFunctionName);
+ return _rename;
}
diff --git a/src/operations/functionsTypes.ts b/src/operations/functionsTypes.ts
index 3113f0e0..2e2d6905 100644
--- a/src/operations/functionsTypes.ts
+++ b/src/operations/functionsTypes.ts
@@ -1,39 +1,39 @@
-import { Name, Value, DropOptions } from './generalTypes'
+import { DropOptions, Name, Value } from './generalTypes';
export interface FunctionParamType {
- mode?: 'IN' | 'OUT' | 'INOUT' | 'VARIADIC'
- name?: string
- type: string
- default?: Value
+ mode?: 'IN' | 'OUT' | 'INOUT' | 'VARIADIC';
+ name?: string;
+ type: string;
+ default?: Value;
}
-export type FunctionParam = string | FunctionParamType
+export type FunctionParam = string | FunctionParamType;
export interface FunctionOptions {
- returns?: string
- language: string
- replace?: boolean
- window?: boolean
- behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'
- onNull?: boolean
- parallel?: 'UNSAFE' | 'RESTRICTED' | 'SAFE'
+ returns?: string;
+ language: string;
+ replace?: boolean;
+ window?: boolean;
+ behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE';
+ onNull?: boolean;
+ parallel?: 'UNSAFE' | 'RESTRICTED' | 'SAFE';
}
type CreateFunctionFn = (
functionName: Name,
functionParams: FunctionParam[],
functionOptions: FunctionOptions & DropOptions,
- definition: Value,
-) => string | string[]
-export type CreateFunction = CreateFunctionFn & { reverse: CreateFunctionFn }
+ definition: Value
+) => string | string[];
+export type CreateFunction = CreateFunctionFn & { reverse: CreateFunctionFn };
export type DropFunction = (
functionName: Name,
functionParams: FunctionParam[],
- dropOptions?: DropOptions,
-) => string | string[]
+ dropOptions?: DropOptions
+) => string | string[];
type RenameFunctionFn = (
oldFunctionName: Name,
functionParams: FunctionParam[],
- newFunctionName: Name,
-) => string | string[]
-export type RenameFunction = RenameFunctionFn & { reverse: RenameFunctionFn }
+ newFunctionName: Name
+) => string | string[];
+export type RenameFunction = RenameFunctionFn & { reverse: RenameFunctionFn };
diff --git a/src/operations/generalTypes.ts b/src/operations/generalTypes.ts
index 618c5b99..69d2b6a9 100644
--- a/src/operations/generalTypes.ts
+++ b/src/operations/generalTypes.ts
@@ -1,28 +1,37 @@
-import PgLiteral from './PgLiteral'
+import PgLiteral from './PgLiteral';
// eslint-disable-next-line camelcase
-export type LiteralUnion = T | (string & { zz_IGNORE_ME?: never })
-export type PublicPart = { [K in keyof T]: T[K] }
+export type LiteralUnion =
+ | T
+ | (string & { zz_IGNORE_ME?: never });
+export type PublicPart = { [K in keyof T]: T[K] };
-export type Nullable = { [P in keyof T]: T[P] | null }
+export type Nullable = { [P in keyof T]: T[P] | null };
-export type PgLiteralValue = PublicPart
-export type Value = null | boolean | string | number | PgLiteral | PgLiteralValue | Value[]
+export type PgLiteralValue = PublicPart;
+export type Value =
+ | null
+ | boolean
+ | string
+ | number
+ | PgLiteral
+ | PgLiteralValue
+ | Value[];
-export type Type = string | { type: string }
+export type Type = string | { type: string };
-export type Name = string | { schema?: string; name: string }
+export type Name = string | { schema?: string; name: string };
export interface IfNotExistsOption {
- ifNotExists?: boolean
+ ifNotExists?: boolean;
}
export interface IfExistsOption {
- ifExists?: boolean
+ ifExists?: boolean;
}
export interface CascadeOption {
- cascade?: boolean
+ cascade?: boolean;
}
-export type DropOptions = IfExistsOption & CascadeOption
+export type DropOptions = IfExistsOption & CascadeOption;
diff --git a/src/operations/indexes.ts b/src/operations/indexes.ts
index 6d9cb4d9..b0fe0a67 100644
--- a/src/operations/indexes.ts
+++ b/src/operations/indexes.ts
@@ -1,37 +1,50 @@
-import { MigrationOptions, Literal } from '../types'
-import { Name } from './generalTypes'
-import { DropIndex, CreateIndex, CreateIndexOptions, DropIndexOptions, IndexColumn } from './indexesTypes'
+import { Literal, MigrationOptions } from '../types';
+import { Name } from './generalTypes';
+import {
+ CreateIndex,
+ CreateIndexOptions,
+ DropIndex,
+ DropIndexOptions,
+ IndexColumn,
+} from './indexesTypes';
-export { CreateIndex, DropIndex }
+export { CreateIndex, DropIndex };
function generateIndexName(
table: Name,
columns: (string | IndexColumn)[],
options: CreateIndexOptions | DropIndexOptions,
- schemalize: Literal,
+ schemalize: Literal
) {
if (options.name) {
- return typeof table === 'object' ? { schema: table.schema, name: options.name } : options.name
+ return typeof table === 'object'
+ ? { schema: table.schema, name: options.name }
+ : options.name;
}
- const cols = columns.map((col) => schemalize(typeof col === 'string' ? col : col.name)).join('_')
- const uniq = 'unique' in options && options.unique ? '_unique' : ''
+ const cols = columns
+ .map((col) => schemalize(typeof col === 'string' ? col : col.name))
+ .join('_');
+ const uniq = 'unique' in options && options.unique ? '_unique' : '';
return typeof table === 'object'
? {
schema: table.schema,
name: `${table.name}_${cols}${uniq}_index`,
}
- : `${table}_${cols}${uniq}_index`
+ : `${table}_${cols}${uniq}_index`;
}
function generateColumnString(column: Name, mOptions: MigrationOptions) {
- const name = mOptions.schemalize(column)
- const isSpecial = /[. ()]/.test(name)
+ const name = mOptions.schemalize(column);
+ const isSpecial = /[. ()]/.test(name);
return isSpecial
? name // expression
- : mOptions.literal(name) // single column
+ : mOptions.literal(name); // single column
}
-function generateColumnsString(columns: (string | IndexColumn)[], mOptions: MigrationOptions) {
+function generateColumnsString(
+ columns: (string | IndexColumn)[],
+ mOptions: MigrationOptions
+) {
return columns
.map((column) =>
typeof column === 'string'
@@ -42,24 +55,31 @@ function generateColumnsString(columns: (string | IndexColumn)[], mOptions: Migr
column.sort,
]
.filter((s) => typeof s === 'string' && s !== '')
- .join(' '),
+ .join(' ')
)
- .join(', ')
+ .join(', ');
}
export function dropIndex(mOptions: MigrationOptions) {
const _drop: DropIndex = (tableName, rawColumns, options = {}) => {
- const { concurrently, ifExists, cascade } = options
- const columns = Array.isArray(rawColumns) ? rawColumns.slice() : [rawColumns]
- const concurrentlyStr = concurrently ? ' CONCURRENTLY' : ''
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const indexName = generateIndexName(tableName, columns, options, mOptions.schemalize)
- const cascadeStr = cascade ? ' CASCADE' : ''
- const indexNameStr = mOptions.literal(indexName)
+ const { concurrently, ifExists, cascade } = options;
+ const columns = Array.isArray(rawColumns)
+ ? rawColumns.slice()
+ : [rawColumns];
+ const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const indexName = generateIndexName(
+ tableName,
+ columns,
+ options,
+ mOptions.schemalize
+ );
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const indexNameStr = mOptions.literal(indexName);
- return `DROP INDEX${concurrentlyStr}${ifExistsStr} ${indexNameStr}${cascadeStr};`
- }
- return _drop
+ return `DROP INDEX${concurrentlyStr}${ifExistsStr} ${indexNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createIndex(mOptions: MigrationOptions) {
@@ -75,43 +95,50 @@ export function createIndex(mOptions: MigrationOptions) {
ifNotExists - optionally create index
options.method - [ btree | hash | gist | spgist | gin ]
*/
- const columns = Array.isArray(rawColumns) ? rawColumns.slice() : [rawColumns]
+ const columns = Array.isArray(rawColumns)
+ ? rawColumns.slice()
+ : [rawColumns];
if (options.opclass) {
mOptions.logger.warn(
- "Using opclass is deprecated. You should use it as part of column definition e.g. pgm.createIndex('table', [['column', 'opclass', 'ASC']])",
- )
- const lastIndex = columns.length - 1
- const lastColumn = columns[lastIndex]
+ "Using opclass is deprecated. You should use it as part of column definition e.g. pgm.createIndex('table', [['column', 'opclass', 'ASC']])"
+ );
+ const lastIndex = columns.length - 1;
+ const lastColumn = columns[lastIndex];
if (typeof lastColumn === 'string') {
- columns[lastIndex] = { name: lastColumn, opclass: options.opclass }
+ columns[lastIndex] = { name: lastColumn, opclass: options.opclass };
} else if (lastColumn.opclass) {
- throw new Error("There is already defined opclass on column, can't override it with global one")
+ throw new Error(
+ "There is already defined opclass on column, can't override it with global one"
+ );
} else {
- columns[lastIndex] = { ...lastColumn, opclass: options.opclass }
+ columns[lastIndex] = { ...lastColumn, opclass: options.opclass };
}
}
const indexName = generateIndexName(
typeof tableName === 'object' ? tableName.name : tableName,
columns,
options,
- mOptions.schemalize,
- )
- const columnsString = generateColumnsString(columns, mOptions)
- const unique = options.unique ? ' UNIQUE' : ''
- const concurrently = options.concurrently ? ' CONCURRENTLY' : ''
- const ifNotExistsStr = options.ifNotExists ? ' IF NOT EXISTS' : ''
- const method = options.method ? ` USING ${options.method}` : ''
- const where = options.where ? ` WHERE ${options.where}` : ''
+ mOptions.schemalize
+ );
+ const columnsString = generateColumnsString(columns, mOptions);
+ const unique = options.unique ? ' UNIQUE' : '';
+ const concurrently = options.concurrently ? ' CONCURRENTLY' : '';
+ const ifNotExistsStr = options.ifNotExists ? ' IF NOT EXISTS' : '';
+ const method = options.method ? ` USING ${options.method}` : '';
+ const where = options.where ? ` WHERE ${options.where}` : '';
const include = options.include
- ? ` INCLUDE (${(Array.isArray(options.include) ? options.include : [options.include])
+ ? ` INCLUDE (${(Array.isArray(options.include)
+ ? options.include
+ : [options.include]
+ )
.map(mOptions.literal)
.join(', ')})`
- : ''
- const indexNameStr = mOptions.literal(indexName)
- const tableNameStr = mOptions.literal(tableName)
+ : '';
+ const indexNameStr = mOptions.literal(indexName);
+ const tableNameStr = mOptions.literal(tableName);
- return `CREATE${unique} INDEX${concurrently}${ifNotExistsStr} ${indexNameStr} ON ${tableNameStr}${method} (${columnsString})${include}${where};`
- }
- _create.reverse = dropIndex(mOptions)
- return _create
+ return `CREATE${unique} INDEX${concurrently}${ifNotExistsStr} ${indexNameStr} ON ${tableNameStr}${method} (${columnsString})${include}${where};`;
+ };
+ _create.reverse = dropIndex(mOptions);
+ return _create;
}
diff --git a/src/operations/indexesTypes.ts b/src/operations/indexesTypes.ts
index bb4bf823..414e2694 100644
--- a/src/operations/indexesTypes.ts
+++ b/src/operations/indexesTypes.ts
@@ -1,39 +1,39 @@
-import { Name, DropOptions } from './generalTypes'
+import { DropOptions, Name } from './generalTypes';
export interface IndexColumn {
- name: string
- opclass?: Name
- sort?: 'ASC' | 'DESC'
+ name: string;
+ opclass?: Name;
+ sort?: 'ASC' | 'DESC';
}
export interface CreateIndexOptions {
- name?: string
- unique?: boolean
- where?: string
- concurrently?: boolean
- ifNotExists?: boolean
+ name?: string;
+ unique?: boolean;
+ where?: string;
+ concurrently?: boolean;
+ ifNotExists?: boolean;
/**
* @deprecated should be parameter of IndexColumn
*/
- opclass?: Name
- method?: 'btree' | 'hash' | 'gist' | 'spgist' | 'gin'
- include?: string | string[]
+ opclass?: Name;
+ method?: 'btree' | 'hash' | 'gist' | 'spgist' | 'gin';
+ include?: string | string[];
}
export interface DropIndexOptions extends DropOptions {
- unique?: boolean
- name?: string
- concurrently?: boolean
+ unique?: boolean;
+ name?: string;
+ concurrently?: boolean;
}
type CreateIndexFn = (
tableName: Name,
columns: string | (string | IndexColumn)[],
- options?: CreateIndexOptions & DropIndexOptions,
-) => string | string[]
-export type CreateIndex = CreateIndexFn & { reverse: CreateIndexFn }
+ options?: CreateIndexOptions & DropIndexOptions
+) => string | string[];
+export type CreateIndex = CreateIndexFn & { reverse: CreateIndexFn };
export type DropIndex = (
tableName: Name,
columns: string | (string | IndexColumn)[],
- options?: DropIndexOptions,
-) => string | string[]
+ options?: DropIndexOptions
+) => string | string[];
diff --git a/src/operations/operators.ts b/src/operations/operators.ts
index bddd7b1a..4d9cd1a7 100644
--- a/src/operations/operators.ts
+++ b/src/operations/operators.ts
@@ -1,18 +1,18 @@
-import { MigrationOptions } from '../types'
-import { applyType, formatParams } from '../utils'
+import { MigrationOptions } from '../types';
+import { applyType, formatParams } from '../utils';
import {
- OperatorListDefinition,
+ AddToOperatorFamily,
CreateOperator,
- DropOperator,
CreateOperatorClass,
- DropOperatorClass,
- RenameOperatorClass,
CreateOperatorFamily,
+ DropOperator,
+ DropOperatorClass,
DropOperatorFamily,
- AddToOperatorFamily,
- RenameOperatorFamily,
+ OperatorListDefinition,
RemoveFromOperatorFamily,
-} from './operatorsTypes'
+ RenameOperatorClass,
+ RenameOperatorFamily,
+} from './operatorsTypes';
export {
CreateOperator,
@@ -25,174 +25,228 @@ export {
AddToOperatorFamily,
RenameOperatorFamily,
RemoveFromOperatorFamily,
-}
+};
export function dropOperator(mOptions: MigrationOptions) {
const _drop: DropOperator = (operatorName, options = {}) => {
- const { ifExists, cascade, left, right } = options
+ const { ifExists, cascade, left, right } = options;
- const operatorNameStr = mOptions.schemalize(operatorName)
- const leftStr = mOptions.literal(left || 'none')
- const rightStr = mOptions.literal(right || 'none')
+ const operatorNameStr = mOptions.schemalize(operatorName);
+ const leftStr = mOptions.literal(left || 'none');
+ const rightStr = mOptions.literal(right || 'none');
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
- return `DROP OPERATOR${ifExistsStr} ${operatorNameStr}(${leftStr}, ${rightStr})${cascadeStr};`
- }
- return _drop
+ return `DROP OPERATOR${ifExistsStr} ${operatorNameStr}(${leftStr}, ${rightStr})${cascadeStr};`;
+ };
+ return _drop;
}
export function createOperator(mOptions: MigrationOptions) {
const _create: CreateOperator = (operatorName, options) => {
- const { procedure, left, right, commutator, negator, restrict, join, hashes, merges } = options || {}
-
- const defs = []
- defs.push(`PROCEDURE = ${mOptions.literal(procedure)}`)
+ const {
+ procedure,
+ left,
+ right,
+ commutator,
+ negator,
+ restrict,
+ join,
+ hashes,
+ merges,
+ } = options || {};
+
+ const defs = [];
+ defs.push(`PROCEDURE = ${mOptions.literal(procedure)}`);
if (left) {
- defs.push(`LEFTARG = ${mOptions.literal(left)}`)
+ defs.push(`LEFTARG = ${mOptions.literal(left)}`);
}
if (right) {
- defs.push(`RIGHTARG = ${mOptions.literal(right)}`)
+ defs.push(`RIGHTARG = ${mOptions.literal(right)}`);
}
if (commutator) {
- defs.push(`COMMUTATOR = ${mOptions.schemalize(commutator)}`)
+ defs.push(`COMMUTATOR = ${mOptions.schemalize(commutator)}`);
}
if (negator) {
- defs.push(`NEGATOR = ${mOptions.schemalize(negator)}`)
+ defs.push(`NEGATOR = ${mOptions.schemalize(negator)}`);
}
if (restrict) {
- defs.push(`RESTRICT = ${mOptions.literal(restrict)}`)
+ defs.push(`RESTRICT = ${mOptions.literal(restrict)}`);
}
if (join) {
- defs.push(`JOIN = ${mOptions.literal(join)}`)
+ defs.push(`JOIN = ${mOptions.literal(join)}`);
}
if (hashes) {
- defs.push('HASHES')
+ defs.push('HASHES');
}
if (merges) {
- defs.push('MERGES')
+ defs.push('MERGES');
}
- const operatorNameStr = mOptions.schemalize(operatorName)
- return `CREATE OPERATOR ${operatorNameStr} (${defs.join(', ')});`
- }
- _create.reverse = dropOperator(mOptions)
- return _create
+ const operatorNameStr = mOptions.schemalize(operatorName);
+ return `CREATE OPERATOR ${operatorNameStr} (${defs.join(', ')});`;
+ };
+ _create.reverse = dropOperator(mOptions);
+ return _create;
}
export function dropOperatorFamily(mOptions: MigrationOptions) {
- const _drop: DropOperatorFamily = (operatorFamilyName, indexMethod, options = {}) => {
- const { ifExists, cascade } = options
- const operatorFamilyNameStr = mOptions.literal(operatorFamilyName)
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- return `DROP OPERATOR FAMILY ${ifExistsStr} ${operatorFamilyNameStr} USING ${indexMethod}${cascadeStr};`
- }
- return _drop
+ const _drop: DropOperatorFamily = (
+ operatorFamilyName,
+ indexMethod,
+ options = {}
+ ) => {
+ const { ifExists, cascade } = options;
+ const operatorFamilyNameStr = mOptions.literal(operatorFamilyName);
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ return `DROP OPERATOR FAMILY ${ifExistsStr} ${operatorFamilyNameStr} USING ${indexMethod}${cascadeStr};`;
+ };
+ return _drop;
}
export function createOperatorFamily(mOptions: MigrationOptions) {
const _create: CreateOperatorFamily = (operatorFamilyName, indexMethod) => {
- const operatorFamilyNameStr = mOptions.literal(operatorFamilyName)
- return `CREATE OPERATOR FAMILY ${operatorFamilyNameStr} USING ${indexMethod};`
- }
- _create.reverse = dropOperatorFamily(mOptions)
- return _create
+ const operatorFamilyNameStr = mOptions.literal(operatorFamilyName);
+ return `CREATE OPERATOR FAMILY ${operatorFamilyNameStr} USING ${indexMethod};`;
+ };
+ _create.reverse = dropOperatorFamily(mOptions);
+ return _create;
}
const operatorMap =
(mOptions: MigrationOptions) =>
({ type, number, name, params = [] }: OperatorListDefinition) => {
- const nameStr = mOptions.literal(name)
+ const nameStr = mOptions.literal(name);
if (String(type).toLowerCase() === 'function') {
if (params.length > 2) {
- throw new Error("Operator can't have more than 2 parameters")
+ throw new Error("Operator can't have more than 2 parameters");
}
- const paramsStr = params.length > 0 ? formatParams(params, mOptions) : ''
+ const paramsStr = params.length > 0 ? formatParams(params, mOptions) : '';
- return `OPERATOR ${number} ${nameStr}${paramsStr}`
+ return `OPERATOR ${number} ${nameStr}${paramsStr}`;
}
if (String(type).toLowerCase() === 'operator') {
- const paramsStr = formatParams(params, mOptions)
- return `FUNCTION ${number} ${nameStr}${paramsStr}`
+ const paramsStr = formatParams(params, mOptions);
+ return `FUNCTION ${number} ${nameStr}${paramsStr}`;
}
- throw new Error('Operator "type" must be either "function" or "operator"')
- }
+ throw new Error('Operator "type" must be either "function" or "operator"');
+ };
export const removeFromOperatorFamily = (mOptions: MigrationOptions) => {
- const method: RemoveFromOperatorFamily = (operatorFamilyName, indexMethod, operatorList) => {
- const operatorFamilyNameStr = mOptions.literal(operatorFamilyName)
- const operatorListStr = operatorList.map(operatorMap(mOptions)).join(',\n ')
+ const method: RemoveFromOperatorFamily = (
+ operatorFamilyName,
+ indexMethod,
+ operatorList
+ ) => {
+ const operatorFamilyNameStr = mOptions.literal(operatorFamilyName);
+ const operatorListStr = operatorList
+ .map(operatorMap(mOptions))
+ .join(',\n ');
return `ALTER OPERATOR FAMILY ${operatorFamilyNameStr} USING ${indexMethod} DROP
- ${operatorListStr};`
- }
- return method
-}
+ ${operatorListStr};`;
+ };
+ return method;
+};
export const addToOperatorFamily = (mOptions: MigrationOptions) => {
- const method: AddToOperatorFamily = (operatorFamilyName, indexMethod, operatorList) => {
- const operatorFamilyNameStr = mOptions.literal(operatorFamilyName)
- const operatorListStr = operatorList.map(operatorMap(mOptions)).join(',\n ')
+ const method: AddToOperatorFamily = (
+ operatorFamilyName,
+ indexMethod,
+ operatorList
+ ) => {
+ const operatorFamilyNameStr = mOptions.literal(operatorFamilyName);
+ const operatorListStr = operatorList
+ .map(operatorMap(mOptions))
+ .join(',\n ');
return `ALTER OPERATOR FAMILY ${operatorFamilyNameStr} USING ${indexMethod} ADD
- ${operatorListStr};`
- }
- method.reverse = removeFromOperatorFamily(mOptions)
- return method
-}
+ ${operatorListStr};`;
+ };
+ method.reverse = removeFromOperatorFamily(mOptions);
+ return method;
+};
export function renameOperatorFamily(mOptions: MigrationOptions) {
- const _rename: RenameOperatorFamily = (oldOperatorFamilyName, indexMethod, newOperatorFamilyName) => {
- const oldOperatorFamilyNameStr = mOptions.literal(oldOperatorFamilyName)
- const newOperatorFamilyNameStr = mOptions.literal(newOperatorFamilyName)
-
- return `ALTER OPERATOR FAMILY ${oldOperatorFamilyNameStr} USING ${indexMethod} RENAME TO ${newOperatorFamilyNameStr};`
- }
- _rename.reverse = (oldOperatorFamilyName, indexMethod, newOperatorFamilyName) =>
- _rename(newOperatorFamilyName, indexMethod, oldOperatorFamilyName)
- return _rename
+ const _rename: RenameOperatorFamily = (
+ oldOperatorFamilyName,
+ indexMethod,
+ newOperatorFamilyName
+ ) => {
+ const oldOperatorFamilyNameStr = mOptions.literal(oldOperatorFamilyName);
+ const newOperatorFamilyNameStr = mOptions.literal(newOperatorFamilyName);
+
+ return `ALTER OPERATOR FAMILY ${oldOperatorFamilyNameStr} USING ${indexMethod} RENAME TO ${newOperatorFamilyNameStr};`;
+ };
+ _rename.reverse = (
+ oldOperatorFamilyName,
+ indexMethod,
+ newOperatorFamilyName
+ ) => _rename(newOperatorFamilyName, indexMethod, oldOperatorFamilyName);
+ return _rename;
}
export function dropOperatorClass(mOptions: MigrationOptions) {
- const _drop: DropOperatorClass = (operatorClassName, indexMethod, options = {}) => {
- const { ifExists, cascade } = options
- const operatorClassNameStr = mOptions.literal(operatorClassName)
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
-
- return `DROP OPERATOR CLASS ${ifExistsStr} ${operatorClassNameStr} USING ${indexMethod}${cascadeStr};`
- }
- return _drop
+ const _drop: DropOperatorClass = (
+ operatorClassName,
+ indexMethod,
+ options = {}
+ ) => {
+ const { ifExists, cascade } = options;
+ const operatorClassNameStr = mOptions.literal(operatorClassName);
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+
+ return `DROP OPERATOR CLASS ${ifExistsStr} ${operatorClassNameStr} USING ${indexMethod}${cascadeStr};`;
+ };
+ return _drop;
}
export function createOperatorClass(mOptions: MigrationOptions) {
- const _create: CreateOperatorClass = (operatorClassName, type, indexMethod, operatorList, options) => {
- const { default: isDefault, family } = options
- const operatorClassNameStr = mOptions.literal(operatorClassName)
- const defaultStr = isDefault ? ' DEFAULT' : ''
- const typeStr = mOptions.literal(applyType(type).type)
- const indexMethodStr = mOptions.literal(indexMethod)
- const familyStr = family ? ` FAMILY ${family}` : ''
- const operatorListStr = operatorList.map(operatorMap(mOptions)).join(',\n ')
+ const _create: CreateOperatorClass = (
+ operatorClassName,
+ type,
+ indexMethod,
+ operatorList,
+ options
+ ) => {
+ const { default: isDefault, family } = options;
+ const operatorClassNameStr = mOptions.literal(operatorClassName);
+ const defaultStr = isDefault ? ' DEFAULT' : '';
+ const typeStr = mOptions.literal(applyType(type).type);
+ const indexMethodStr = mOptions.literal(indexMethod);
+ const familyStr = family ? ` FAMILY ${family}` : '';
+ const operatorListStr = operatorList
+ .map(operatorMap(mOptions))
+ .join(',\n ');
return `CREATE OPERATOR CLASS ${operatorClassNameStr}${defaultStr} FOR TYPE ${typeStr} USING ${indexMethodStr} ${familyStr} AS
- ${operatorListStr};`
- }
- _create.reverse = (operatorClassName, type, indexMethod, operatorList, options) =>
- dropOperatorClass(mOptions)(operatorClassName, indexMethod, options)
- return _create
+ ${operatorListStr};`;
+ };
+ _create.reverse = (
+ operatorClassName,
+ type,
+ indexMethod,
+ operatorList,
+ options
+ ) => dropOperatorClass(mOptions)(operatorClassName, indexMethod, options);
+ return _create;
}
export function renameOperatorClass(mOptions: MigrationOptions) {
- const _rename: RenameOperatorClass = (oldOperatorClassName, indexMethod, newOperatorClassName) => {
- const oldOperatorClassNameStr = mOptions.literal(oldOperatorClassName)
- const newOperatorClassNameStr = mOptions.literal(newOperatorClassName)
-
- return `ALTER OPERATOR CLASS ${oldOperatorClassNameStr} USING ${indexMethod} RENAME TO ${newOperatorClassNameStr};`
- }
+ const _rename: RenameOperatorClass = (
+ oldOperatorClassName,
+ indexMethod,
+ newOperatorClassName
+ ) => {
+ const oldOperatorClassNameStr = mOptions.literal(oldOperatorClassName);
+ const newOperatorClassNameStr = mOptions.literal(newOperatorClassName);
+
+ return `ALTER OPERATOR CLASS ${oldOperatorClassNameStr} USING ${indexMethod} RENAME TO ${newOperatorClassNameStr};`;
+ };
_rename.reverse = (oldOperatorClassName, indexMethod, newOperatorClassName) =>
- _rename(newOperatorClassName, indexMethod, oldOperatorClassName)
- return _rename
+ _rename(newOperatorClassName, indexMethod, oldOperatorClassName);
+ return _rename;
}
diff --git a/src/operations/operatorsTypes.ts b/src/operations/operatorsTypes.ts
index 6a8a60e1..1e2726b6 100644
--- a/src/operations/operatorsTypes.ts
+++ b/src/operations/operatorsTypes.ts
@@ -1,78 +1,98 @@
-import { Name, Type, DropOptions } from './generalTypes'
-import { FunctionParam } from './functionsTypes'
+import { FunctionParam } from './functionsTypes';
+import { DropOptions, Name, Type } from './generalTypes';
export interface CreateOperatorOptions {
- procedure: Name
- left?: Name
- right?: Name
- commutator?: Name
- negator?: Name
- restrict?: Name
- join?: Name
- hashes?: boolean
- merges?: boolean
+ procedure: Name;
+ left?: Name;
+ right?: Name;
+ commutator?: Name;
+ negator?: Name;
+ restrict?: Name;
+ join?: Name;
+ hashes?: boolean;
+ merges?: boolean;
}
export interface DropOperatorOptions extends DropOptions {
- left?: Name
- right?: Name
+ left?: Name;
+ right?: Name;
}
export interface CreateOperatorClassOptions {
- default?: boolean
- family?: string
+ default?: boolean;
+ family?: string;
}
export interface OperatorListDefinition {
- type: 'function' | 'operator'
- number: number
- name: Name
- params?: FunctionParam[]
+ type: 'function' | 'operator';
+ number: number;
+ name: Name;
+ params?: FunctionParam[];
}
-type CreateOperatorFn = (operatorName: Name, options: CreateOperatorOptions & DropOperatorOptions) => string | string[]
-export type CreateOperator = CreateOperatorFn & { reverse: CreateOperatorFn }
-export type DropOperator = (operatorName: Name, dropOptions?: DropOperatorOptions) => string | string[]
+type CreateOperatorFn = (
+ operatorName: Name,
+ options: CreateOperatorOptions & DropOperatorOptions
+) => string | string[];
+export type CreateOperator = CreateOperatorFn & { reverse: CreateOperatorFn };
+export type DropOperator = (
+ operatorName: Name,
+ dropOptions?: DropOperatorOptions
+) => string | string[];
type CreateOperatorClassFn = (
operatorClassName: Name,
type: Type,
indexMethod: Name,
operatorList: OperatorListDefinition[],
- options: CreateOperatorClassOptions & DropOptions,
-) => string | string[]
-export type CreateOperatorClass = CreateOperatorClassFn & { reverse: CreateOperatorClassFn }
+ options: CreateOperatorClassOptions & DropOptions
+) => string | string[];
+export type CreateOperatorClass = CreateOperatorClassFn & {
+ reverse: CreateOperatorClassFn;
+};
export type DropOperatorClass = (
operatorClassName: Name,
indexMethod: Name,
- dropOptions?: DropOptions,
-) => string | string[]
+ dropOptions?: DropOptions
+) => string | string[];
type RenameOperatorClassFn = (
oldOperatorClassName: Name,
indexMethod: Name,
- newOperatorClassName: Name,
-) => string | string[]
-export type RenameOperatorClass = RenameOperatorClassFn & { reverse: RenameOperatorClassFn }
-type CreateOperatorFamilyFn = (operatorFamilyName: Name, indexMethod: Name, options?: DropOptions) => string | string[]
-export type CreateOperatorFamily = CreateOperatorFamilyFn & { reverse: CreateOperatorFamilyFn }
+ newOperatorClassName: Name
+) => string | string[];
+export type RenameOperatorClass = RenameOperatorClassFn & {
+ reverse: RenameOperatorClassFn;
+};
+type CreateOperatorFamilyFn = (
+ operatorFamilyName: Name,
+ indexMethod: Name,
+ options?: DropOptions
+) => string | string[];
+export type CreateOperatorFamily = CreateOperatorFamilyFn & {
+ reverse: CreateOperatorFamilyFn;
+};
export type DropOperatorFamily = (
operatorFamilyName: Name,
newSchemaName: Name,
- dropOptions?: DropOptions,
-) => string | string[]
+ dropOptions?: DropOptions
+) => string | string[];
type RenameOperatorFamilyFn = (
oldOperatorFamilyName: Name,
indexMethod: Name,
- newOperatorFamilyName: Name,
-) => string | string[]
-export type RenameOperatorFamily = RenameOperatorFamilyFn & { reverse: RenameOperatorFamilyFn }
+ newOperatorFamilyName: Name
+) => string | string[];
+export type RenameOperatorFamily = RenameOperatorFamilyFn & {
+ reverse: RenameOperatorFamilyFn;
+};
type AddToOperatorFamilyFn = (
operatorFamilyName: Name,
indexMethod: Name,
- operatorList: OperatorListDefinition[],
-) => string | string[]
-export type AddToOperatorFamily = AddToOperatorFamilyFn & { reverse: AddToOperatorFamilyFn }
+ operatorList: OperatorListDefinition[]
+) => string | string[];
+export type AddToOperatorFamily = AddToOperatorFamilyFn & {
+ reverse: AddToOperatorFamilyFn;
+};
export type RemoveFromOperatorFamily = (
operatorFamilyName: Name,
indexMethod: Name,
- operatorList: OperatorListDefinition[],
-) => string | string[]
+ operatorList: OperatorListDefinition[]
+) => string | string[];
diff --git a/src/operations/other.ts b/src/operations/other.ts
index ff110452..ae27add0 100644
--- a/src/operations/other.ts
+++ b/src/operations/other.ts
@@ -1,18 +1,18 @@
-import { MigrationOptions } from '../types'
-import { createTransformer } from '../utils'
-import { Sql } from './othersTypes'
+import { MigrationOptions } from '../types';
+import { createTransformer } from '../utils';
+import { Sql } from './othersTypes';
-export { Sql }
+export { Sql };
export function sql(mOptions: MigrationOptions): Sql {
- const t = createTransformer(mOptions.literal)
+ const t = createTransformer(mOptions.literal);
return (sqlStr, args) => {
// applies some very basic templating using the utils.p
- let s: string = t(sqlStr, args)
+ let s: string = t(sqlStr, args);
// add trailing ; if not present
if (s.lastIndexOf(';') !== s.length - 1) {
- s += ';'
+ s += ';';
}
- return s
- }
+ return s;
+ };
}
diff --git a/src/operations/othersTypes.ts b/src/operations/othersTypes.ts
index a67ac493..89edbf4e 100644
--- a/src/operations/othersTypes.ts
+++ b/src/operations/othersTypes.ts
@@ -1,4 +1,7 @@
-import { Value } from '..'
-import { Name } from './generalTypes'
+import { Value } from '..';
+import { Name } from './generalTypes';
-export type Sql = (sqlStr: string, args?: { [key: string]: Name | Value }) => string | string[]
+export type Sql = (
+ sqlStr: string,
+ args?: { [key: string]: Name | Value }
+) => string | string[];
diff --git a/src/operations/policies.ts b/src/operations/policies.ts
index be5ca145..6aabf655 100644
--- a/src/operations/policies.ts
+++ b/src/operations/policies.ts
@@ -1,32 +1,38 @@
-import { MigrationOptions } from '../types'
-import { PolicyOptions, CreatePolicy, DropPolicy, AlterPolicy, RenamePolicy } from './policiesTypes'
+import { MigrationOptions } from '../types';
+import {
+ AlterPolicy,
+ CreatePolicy,
+ DropPolicy,
+ PolicyOptions,
+ RenamePolicy,
+} from './policiesTypes';
-export { CreatePolicy, DropPolicy, AlterPolicy, RenamePolicy }
+export { CreatePolicy, DropPolicy, AlterPolicy, RenamePolicy };
const makeClauses = ({ role, using, check }: PolicyOptions) => {
- const roles = (Array.isArray(role) ? role : [role]).join(', ')
- const clauses: string[] = []
+ const roles = (Array.isArray(role) ? role : [role]).join(', ');
+ const clauses: string[] = [];
if (roles) {
- clauses.push(`TO ${roles}`)
+ clauses.push(`TO ${roles}`);
}
if (using) {
- clauses.push(`USING (${using})`)
+ clauses.push(`USING (${using})`);
}
if (check) {
- clauses.push(`WITH CHECK (${check})`)
+ clauses.push(`WITH CHECK (${check})`);
}
- return clauses
-}
+ return clauses;
+};
export function dropPolicy(mOptions: MigrationOptions) {
const _drop: DropPolicy = (tableName, policyName, options = {}) => {
- const { ifExists } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const policyNameStr = mOptions.literal(policyName)
- const tableNameStr = mOptions.literal(tableName)
- return `DROP POLICY${ifExistsStr} ${policyNameStr} ON ${tableNameStr};`
- }
- return _drop
+ const { ifExists } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const policyNameStr = mOptions.literal(policyName);
+ const tableNameStr = mOptions.literal(tableName);
+ return `DROP POLICY${ifExistsStr} ${policyNameStr} ON ${tableNameStr};`;
+ };
+ return _drop;
}
export function createPolicy(mOptions: MigrationOptions) {
@@ -34,34 +40,38 @@ export function createPolicy(mOptions: MigrationOptions) {
const createOptions = {
...options,
role: options.role || 'PUBLIC',
- }
- const clauses = [`FOR ${options.command || 'ALL'}`, ...makeClauses(createOptions)]
- const clausesStr = clauses.join(' ')
- const policyNameStr = mOptions.literal(policyName)
- const tableNameStr = mOptions.literal(tableName)
- return `CREATE POLICY ${policyNameStr} ON ${tableNameStr} ${clausesStr};`
- }
- _create.reverse = dropPolicy(mOptions)
- return _create
+ };
+ const clauses = [
+ `FOR ${options.command || 'ALL'}`,
+ ...makeClauses(createOptions),
+ ];
+ const clausesStr = clauses.join(' ');
+ const policyNameStr = mOptions.literal(policyName);
+ const tableNameStr = mOptions.literal(tableName);
+ return `CREATE POLICY ${policyNameStr} ON ${tableNameStr} ${clausesStr};`;
+ };
+ _create.reverse = dropPolicy(mOptions);
+ return _create;
}
export function alterPolicy(mOptions: MigrationOptions) {
const _alter: AlterPolicy = (tableName, policyName, options = {}) => {
- const clausesStr = makeClauses(options).join(' ')
- const policyNameStr = mOptions.literal(policyName)
- const tableNameStr = mOptions.literal(tableName)
- return `ALTER POLICY ${policyNameStr} ON ${tableNameStr} ${clausesStr};`
- }
- return _alter
+ const clausesStr = makeClauses(options).join(' ');
+ const policyNameStr = mOptions.literal(policyName);
+ const tableNameStr = mOptions.literal(tableName);
+ return `ALTER POLICY ${policyNameStr} ON ${tableNameStr} ${clausesStr};`;
+ };
+ return _alter;
}
export function renamePolicy(mOptions: MigrationOptions) {
const _rename: RenamePolicy = (tableName, policyName, newPolicyName) => {
- const policyNameStr = mOptions.literal(policyName)
- const newPolicyNameStr = mOptions.literal(newPolicyName)
- const tableNameStr = mOptions.literal(tableName)
- return `ALTER POLICY ${policyNameStr} ON ${tableNameStr} RENAME TO ${newPolicyNameStr};`
- }
- _rename.reverse = (tableName, policyName, newPolicyName) => _rename(tableName, newPolicyName, policyName)
- return _rename
+ const policyNameStr = mOptions.literal(policyName);
+ const newPolicyNameStr = mOptions.literal(newPolicyName);
+ const tableNameStr = mOptions.literal(tableName);
+ return `ALTER POLICY ${policyNameStr} ON ${tableNameStr} RENAME TO ${newPolicyNameStr};`;
+ };
+ _rename.reverse = (tableName, policyName, newPolicyName) =>
+ _rename(tableName, newPolicyName, policyName);
+ return _rename;
}
diff --git a/src/operations/policiesTypes.ts b/src/operations/policiesTypes.ts
index b4005a39..8f2de7c2 100644
--- a/src/operations/policiesTypes.ts
+++ b/src/operations/policiesTypes.ts
@@ -1,24 +1,36 @@
-import { Name, IfExistsOption } from './generalTypes'
+import { IfExistsOption, Name } from './generalTypes';
export interface PolicyOptions {
- role?: string | string[]
- using?: string
- check?: string
+ role?: string | string[];
+ using?: string;
+ check?: string;
}
interface CreatePolicyOptionsEn {
- command?: 'ALL' | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE'
+ command?: 'ALL' | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE';
}
-export type CreatePolicyOptions = CreatePolicyOptionsEn & PolicyOptions
+export type CreatePolicyOptions = CreatePolicyOptionsEn & PolicyOptions;
type CreatePolicyFn = (
tableName: Name,
policyName: string,
- options?: CreatePolicyOptions & IfExistsOption,
-) => string | string[]
-export type CreatePolicy = CreatePolicyFn & { reverse: CreatePolicyFn }
-export type DropPolicy = (tableName: Name, policyName: string, options?: IfExistsOption) => string | string[]
-export type AlterPolicy = (tableName: Name, policyName: string, options: PolicyOptions) => string | string[]
-type RenamePolicyFn = (tableName: Name, policyName: string, newPolicyName: string) => string | string[]
-export type RenamePolicy = RenamePolicyFn & { reverse: RenamePolicyFn }
+ options?: CreatePolicyOptions & IfExistsOption
+) => string | string[];
+export type CreatePolicy = CreatePolicyFn & { reverse: CreatePolicyFn };
+export type DropPolicy = (
+ tableName: Name,
+ policyName: string,
+ options?: IfExistsOption
+) => string | string[];
+export type AlterPolicy = (
+ tableName: Name,
+ policyName: string,
+ options: PolicyOptions
+) => string | string[];
+type RenamePolicyFn = (
+ tableName: Name,
+ policyName: string,
+ newPolicyName: string
+) => string | string[];
+export type RenamePolicy = RenamePolicyFn & { reverse: RenamePolicyFn };
diff --git a/src/operations/roles.ts b/src/operations/roles.ts
index 17b709b1..3e91229f 100644
--- a/src/operations/roles.ts
+++ b/src/operations/roles.ts
@@ -1,67 +1,82 @@
-import { MigrationOptions } from '../types'
-import { escapeValue } from '../utils'
-import { CreateRole, DropRole, AlterRole, RenameRole, RoleOptions } from './rolesTypes'
+import { MigrationOptions } from '../types';
+import { escapeValue } from '../utils';
+import {
+ AlterRole,
+ CreateRole,
+ DropRole,
+ RenameRole,
+ RoleOptions,
+} from './rolesTypes';
-export { CreateRole, DropRole, AlterRole, RenameRole }
+export { CreateRole, DropRole, AlterRole, RenameRole };
const formatRoleOptions = (roleOptions: RoleOptions = {}) => {
- const options = []
+ const options = [];
if (roleOptions.superuser !== undefined) {
- options.push(roleOptions.superuser ? 'SUPERUSER' : 'NOSUPERUSER')
+ options.push(roleOptions.superuser ? 'SUPERUSER' : 'NOSUPERUSER');
}
if (roleOptions.createdb !== undefined) {
- options.push(roleOptions.createdb ? 'CREATEDB' : 'NOCREATEDB')
+ options.push(roleOptions.createdb ? 'CREATEDB' : 'NOCREATEDB');
}
if (roleOptions.createrole !== undefined) {
- options.push(roleOptions.createrole ? 'CREATEROLE' : 'NOCREATEROLE')
+ options.push(roleOptions.createrole ? 'CREATEROLE' : 'NOCREATEROLE');
}
if (roleOptions.inherit !== undefined) {
- options.push(roleOptions.inherit ? 'INHERIT' : 'NOINHERIT')
+ options.push(roleOptions.inherit ? 'INHERIT' : 'NOINHERIT');
}
if (roleOptions.login !== undefined) {
- options.push(roleOptions.login ? 'LOGIN' : 'NOLOGIN')
+ options.push(roleOptions.login ? 'LOGIN' : 'NOLOGIN');
}
if (roleOptions.replication !== undefined) {
- options.push(roleOptions.replication ? 'REPLICATION' : 'NOREPLICATION')
+ options.push(roleOptions.replication ? 'REPLICATION' : 'NOREPLICATION');
}
if (roleOptions.bypassrls !== undefined) {
- options.push(roleOptions.bypassrls ? 'BYPASSRLS' : 'NOBYPASSRLS')
+ options.push(roleOptions.bypassrls ? 'BYPASSRLS' : 'NOBYPASSRLS');
}
if (roleOptions.limit) {
- options.push(`CONNECTION LIMIT ${Number(roleOptions.limit)}`)
+ options.push(`CONNECTION LIMIT ${Number(roleOptions.limit)}`);
}
if (roleOptions.password !== undefined) {
- const encrypted = roleOptions.encrypted === false ? 'UNENCRYPTED' : 'ENCRYPTED'
- options.push(`${encrypted} PASSWORD ${escapeValue(roleOptions.password)}`)
+ const encrypted =
+ roleOptions.encrypted === false ? 'UNENCRYPTED' : 'ENCRYPTED';
+ options.push(`${encrypted} PASSWORD ${escapeValue(roleOptions.password)}`);
}
if (roleOptions.valid !== undefined) {
- const valid = roleOptions.valid ? escapeValue(roleOptions.valid) : "'infinity'"
- options.push(`VALID UNTIL ${valid}`)
+ const valid = roleOptions.valid
+ ? escapeValue(roleOptions.valid)
+ : "'infinity'";
+ options.push(`VALID UNTIL ${valid}`);
}
if (roleOptions.inRole) {
- const inRole = Array.isArray(roleOptions.inRole) ? roleOptions.inRole.join(',') : roleOptions.inRole
- options.push(`IN ROLE ${inRole}`)
+ const inRole = Array.isArray(roleOptions.inRole)
+ ? roleOptions.inRole.join(',')
+ : roleOptions.inRole;
+ options.push(`IN ROLE ${inRole}`);
}
if (roleOptions.role) {
- const role = Array.isArray(roleOptions.role) ? roleOptions.role.join(',') : roleOptions.role
- options.push(`ROLE ${role}`)
+ const role = Array.isArray(roleOptions.role)
+ ? roleOptions.role.join(',')
+ : roleOptions.role;
+ options.push(`ROLE ${role}`);
}
if (roleOptions.admin) {
- const admin = Array.isArray(roleOptions.admin) ? roleOptions.admin.join(',') : roleOptions.admin
- options.push(`ADMIN ${admin}`)
+ const admin = Array.isArray(roleOptions.admin)
+ ? roleOptions.admin.join(',')
+ : roleOptions.admin;
+ options.push(`ADMIN ${admin}`);
}
- return options.join(' ')
-}
+ return options.join(' ');
+};
export function dropRole(mOptions: MigrationOptions) {
const _drop: DropRole = (roleName, options = {}) => {
- const { ifExists } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const roleNameStr = mOptions.literal(roleName)
- return `DROP ROLE${ifExistsStr} ${roleNameStr};`
- }
- return _drop
+ const { ifExists } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const roleNameStr = mOptions.literal(roleName);
+ return `DROP ROLE${ifExistsStr} ${roleNameStr};`;
+ };
+ return _drop;
}
export function createRole(mOptions: MigrationOptions) {
@@ -74,28 +89,31 @@ export function createRole(mOptions: MigrationOptions) {
inherit: roleOptions.inherit !== false,
login: roleOptions.login || false,
replication: roleOptions.replication || false,
- })
- const optionsStr = options ? ` WITH ${options}` : ''
- return `CREATE ROLE ${mOptions.literal(roleName)}${optionsStr};`
- }
- _create.reverse = dropRole(mOptions)
- return _create
+ });
+ const optionsStr = options ? ` WITH ${options}` : '';
+ return `CREATE ROLE ${mOptions.literal(roleName)}${optionsStr};`;
+ };
+ _create.reverse = dropRole(mOptions);
+ return _create;
}
export function alterRole(mOptions: MigrationOptions) {
const _alter: AlterRole = (roleName, roleOptions = {}) => {
- const options = formatRoleOptions(roleOptions)
- return options ? `ALTER ROLE ${mOptions.literal(roleName)} WITH ${options};` : ''
- }
- return _alter
+ const options = formatRoleOptions(roleOptions);
+ return options
+ ? `ALTER ROLE ${mOptions.literal(roleName)} WITH ${options};`
+ : '';
+ };
+ return _alter;
}
export function renameRole(mOptions: MigrationOptions) {
const _rename: RenameRole = (oldRoleName, newRoleName) => {
- const oldRoleNameStr = mOptions.literal(oldRoleName)
- const newRoleNameStr = mOptions.literal(newRoleName)
- return `ALTER ROLE ${oldRoleNameStr} RENAME TO ${newRoleNameStr};`
- }
- _rename.reverse = (oldRoleName, newRoleName) => _rename(newRoleName, oldRoleName)
- return _rename
+ const oldRoleNameStr = mOptions.literal(oldRoleName);
+ const newRoleNameStr = mOptions.literal(newRoleName);
+ return `ALTER ROLE ${oldRoleNameStr} RENAME TO ${newRoleNameStr};`;
+ };
+ _rename.reverse = (oldRoleName, newRoleName) =>
+ _rename(newRoleName, oldRoleName);
+ return _rename;
}
diff --git a/src/operations/rolesTypes.ts b/src/operations/rolesTypes.ts
index 3ef8e9ac..ebfddcc8 100644
--- a/src/operations/rolesTypes.ts
+++ b/src/operations/rolesTypes.ts
@@ -1,25 +1,34 @@
-import { Name, Value, IfExistsOption } from './generalTypes'
+import { IfExistsOption, Name, Value } from './generalTypes';
export interface RoleOptions {
- superuser?: boolean
- createdb?: boolean
- createrole?: boolean
- inherit?: boolean
- login?: boolean
- replication?: boolean
- bypassrls?: boolean
- limit?: number
- password?: Value
- encrypted?: boolean
- valid?: Value
- inRole?: string | string[]
- role?: string | string[]
- admin?: string | string[]
+ superuser?: boolean;
+ createdb?: boolean;
+ createrole?: boolean;
+ inherit?: boolean;
+ login?: boolean;
+ replication?: boolean;
+ bypassrls?: boolean;
+ limit?: number;
+ password?: Value;
+ encrypted?: boolean;
+ valid?: Value;
+ inRole?: string | string[];
+ role?: string | string[];
+ admin?: string | string[];
}
-type CreateRoleFn = (roleName: Name, roleOptions?: RoleOptions & IfExistsOption) => string | string[]
-export type CreateRole = CreateRoleFn & { reverse: CreateRoleFn }
-export type DropRole = (roleName: Name, options?: IfExistsOption) => string | string[]
-export type AlterRole = (roleName: Name, roleOptions: RoleOptions) => string | string[]
-type RenameRoleFn = (oldRoleName: Name, newRoleName: Name) => string | string[]
-export type RenameRole = RenameRoleFn & { reverse: RenameRoleFn }
+type CreateRoleFn = (
+ roleName: Name,
+ roleOptions?: RoleOptions & IfExistsOption
+) => string | string[];
+export type CreateRole = CreateRoleFn & { reverse: CreateRoleFn };
+export type DropRole = (
+ roleName: Name,
+ options?: IfExistsOption
+) => string | string[];
+export type AlterRole = (
+ roleName: Name,
+ roleOptions: RoleOptions
+) => string | string[];
+type RenameRoleFn = (oldRoleName: Name, newRoleName: Name) => string | string[];
+export type RenameRole = RenameRoleFn & { reverse: RenameRoleFn };
diff --git a/src/operations/schemas.ts b/src/operations/schemas.ts
index df68f239..84951bb3 100644
--- a/src/operations/schemas.ts
+++ b/src/operations/schemas.ts
@@ -1,37 +1,40 @@
-import { MigrationOptions } from '../types'
-import { CreateSchema, DropSchema, RenameSchema } from './schemasTypes'
+import { MigrationOptions } from '../types';
+import { CreateSchema, DropSchema, RenameSchema } from './schemasTypes';
-export { CreateSchema, DropSchema, RenameSchema }
+export { CreateSchema, DropSchema, RenameSchema };
export function dropSchema(mOptions: MigrationOptions) {
const _drop: DropSchema = (schemaName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const schemaNameStr = mOptions.literal(schemaName)
- return `DROP SCHEMA${ifExistsStr} ${schemaNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const schemaNameStr = mOptions.literal(schemaName);
+ return `DROP SCHEMA${ifExistsStr} ${schemaNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createSchema(mOptions: MigrationOptions) {
const _create: CreateSchema = (schemaName: string, options = {}) => {
- const { ifNotExists, authorization } = options
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const schemaNameStr = mOptions.literal(schemaName)
- const authorizationStr = authorization ? ` AUTHORIZATION ${authorization}` : ''
- return `CREATE SCHEMA${ifNotExistsStr} ${schemaNameStr}${authorizationStr};`
- }
- _create.reverse = dropSchema(mOptions)
- return _create
+ const { ifNotExists, authorization } = options;
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const schemaNameStr = mOptions.literal(schemaName);
+ const authorizationStr = authorization
+ ? ` AUTHORIZATION ${authorization}`
+ : '';
+ return `CREATE SCHEMA${ifNotExistsStr} ${schemaNameStr}${authorizationStr};`;
+ };
+ _create.reverse = dropSchema(mOptions);
+ return _create;
}
export function renameSchema(mOptions: MigrationOptions) {
const _rename: RenameSchema = (schemaName, newSchemaName) => {
- const schemaNameStr = mOptions.literal(schemaName)
- const newSchemaNameStr = mOptions.literal(newSchemaName)
- return `ALTER SCHEMA ${schemaNameStr} RENAME TO ${newSchemaNameStr};`
- }
- _rename.reverse = (schemaName, newSchemaName) => _rename(newSchemaName, schemaName)
- return _rename
+ const schemaNameStr = mOptions.literal(schemaName);
+ const newSchemaNameStr = mOptions.literal(newSchemaName);
+ return `ALTER SCHEMA ${schemaNameStr} RENAME TO ${newSchemaNameStr};`;
+ };
+ _rename.reverse = (schemaName, newSchemaName) =>
+ _rename(newSchemaName, schemaName);
+ return _rename;
}
diff --git a/src/operations/schemasTypes.ts b/src/operations/schemasTypes.ts
index 6e89f828..e34b8fd0 100644
--- a/src/operations/schemasTypes.ts
+++ b/src/operations/schemasTypes.ts
@@ -1,11 +1,20 @@
-import { DropOptions, IfNotExistsOption } from './generalTypes'
+import { DropOptions, IfNotExistsOption } from './generalTypes';
export interface CreateSchemaOptions extends IfNotExistsOption {
- authorization?: string
+ authorization?: string;
}
-type CreateSchemaFn = (schemaName: string, schemaOptions?: CreateSchemaOptions & DropOptions) => string | string[]
-export type CreateSchema = CreateSchemaFn & { reverse: CreateSchemaFn }
-export type DropSchema = (schemaName: string, dropOptions?: DropOptions) => string | string[]
-type RenameSchemaFn = (oldSchemaName: string, newSchemaName: string) => string | string[]
-export type RenameSchema = RenameSchemaFn & { reverse: RenameSchemaFn }
+type CreateSchemaFn = (
+ schemaName: string,
+ schemaOptions?: CreateSchemaOptions & DropOptions
+) => string | string[];
+export type CreateSchema = CreateSchemaFn & { reverse: CreateSchemaFn };
+export type DropSchema = (
+ schemaName: string,
+ dropOptions?: DropOptions
+) => string | string[];
+type RenameSchemaFn = (
+ oldSchemaName: string,
+ newSchemaName: string
+) => string | string[];
+export type RenameSchema = RenameSchemaFn & { reverse: RenameSchemaFn };
diff --git a/src/operations/sequences.ts b/src/operations/sequences.ts
index b9a4e772..14a94a76 100644
--- a/src/operations/sequences.ts
+++ b/src/operations/sequences.ts
@@ -1,95 +1,109 @@
-import { MigrationOptions } from '../types'
-import { applyType } from '../utils'
-import { SequenceOptions, CreateSequence, DropSequence, AlterSequence, RenameSequence } from './sequencesTypes'
-import { ColumnDefinitions } from './tablesTypes'
+import { MigrationOptions } from '../types';
+import { applyType } from '../utils';
+import {
+ AlterSequence,
+ CreateSequence,
+ DropSequence,
+ RenameSequence,
+ SequenceOptions,
+} from './sequencesTypes';
+import { ColumnDefinitions } from './tablesTypes';
-export { CreateSequence, DropSequence, AlterSequence, RenameSequence }
+export { CreateSequence, DropSequence, AlterSequence, RenameSequence };
-export const parseSequenceOptions = (typeShorthands: ColumnDefinitions | undefined, options: SequenceOptions) => {
- const { type, increment, minvalue, maxvalue, start, cache, cycle, owner } = options
- const clauses: string[] = []
+export const parseSequenceOptions = (
+ typeShorthands: ColumnDefinitions | undefined,
+ options: SequenceOptions
+) => {
+ const { type, increment, minvalue, maxvalue, start, cache, cycle, owner } =
+ options;
+ const clauses: string[] = [];
if (type) {
- clauses.push(`AS ${applyType(type, typeShorthands).type}`)
+ clauses.push(`AS ${applyType(type, typeShorthands).type}`);
}
if (increment) {
- clauses.push(`INCREMENT BY ${increment}`)
+ clauses.push(`INCREMENT BY ${increment}`);
}
if (minvalue) {
- clauses.push(`MINVALUE ${minvalue}`)
+ clauses.push(`MINVALUE ${minvalue}`);
} else if (minvalue === null || minvalue === false) {
- clauses.push('NO MINVALUE')
+ clauses.push('NO MINVALUE');
}
if (maxvalue) {
- clauses.push(`MAXVALUE ${maxvalue}`)
+ clauses.push(`MAXVALUE ${maxvalue}`);
} else if (maxvalue === null || maxvalue === false) {
- clauses.push('NO MAXVALUE')
+ clauses.push('NO MAXVALUE');
}
if (start) {
- clauses.push(`START WITH ${start}`)
+ clauses.push(`START WITH ${start}`);
}
if (cache) {
- clauses.push(`CACHE ${cache}`)
+ clauses.push(`CACHE ${cache}`);
}
if (cycle) {
- clauses.push('CYCLE')
+ clauses.push('CYCLE');
} else if (cycle === false) {
- clauses.push('NO CYCLE')
+ clauses.push('NO CYCLE');
}
if (owner) {
- clauses.push(`OWNED BY ${owner}`)
+ clauses.push(`OWNED BY ${owner}`);
} else if (owner === null || owner === false) {
- clauses.push('OWNED BY NONE')
+ clauses.push('OWNED BY NONE');
}
- return clauses
-}
+ return clauses;
+};
export function dropSequence(mOptions: MigrationOptions) {
const _drop: DropSequence = (sequenceName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const sequenceNameStr = mOptions.literal(sequenceName)
- return `DROP SEQUENCE${ifExistsStr} ${sequenceNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const sequenceNameStr = mOptions.literal(sequenceName);
+ return `DROP SEQUENCE${ifExistsStr} ${sequenceNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createSequence(mOptions: MigrationOptions) {
const _create: CreateSequence = (sequenceName, options = {}) => {
- const { temporary, ifNotExists } = options
- const temporaryStr = temporary ? ' TEMPORARY' : ''
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const sequenceNameStr = mOptions.literal(sequenceName)
- const clausesStr = parseSequenceOptions(mOptions.typeShorthands, options).join('\n ')
+ const { temporary, ifNotExists } = options;
+ const temporaryStr = temporary ? ' TEMPORARY' : '';
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const sequenceNameStr = mOptions.literal(sequenceName);
+ const clausesStr = parseSequenceOptions(
+ mOptions.typeShorthands,
+ options
+ ).join('\n ');
return `CREATE${temporaryStr} SEQUENCE${ifNotExistsStr} ${sequenceNameStr}
- ${clausesStr};`
- }
- _create.reverse = dropSequence(mOptions)
- return _create
+ ${clausesStr};`;
+ };
+ _create.reverse = dropSequence(mOptions);
+ return _create;
}
export function alterSequence(mOptions: MigrationOptions): AlterSequence {
return (sequenceName, options) => {
- const { restart } = options
- const clauses = parseSequenceOptions(mOptions.typeShorthands, options)
+ const { restart } = options;
+ const clauses = parseSequenceOptions(mOptions.typeShorthands, options);
if (restart) {
if (restart === true) {
- clauses.push('RESTART')
+ clauses.push('RESTART');
} else {
- clauses.push(`RESTART WITH ${restart}`)
+ clauses.push(`RESTART WITH ${restart}`);
}
}
return `ALTER SEQUENCE ${mOptions.literal(sequenceName)}
- ${clauses.join('\n ')};`
- }
+ ${clauses.join('\n ')};`;
+ };
}
export function renameSequence(mOptions: MigrationOptions) {
const _rename: RenameSequence = (sequenceName, newSequenceName) => {
- const sequenceNameStr = mOptions.literal(sequenceName)
- const newSequenceNameStr = mOptions.literal(newSequenceName)
- return `ALTER SEQUENCE ${sequenceNameStr} RENAME TO ${newSequenceNameStr};`
- }
- _rename.reverse = (sequenceName, newSequenceName) => _rename(newSequenceName, sequenceName)
- return _rename
+ const sequenceNameStr = mOptions.literal(sequenceName);
+ const newSequenceNameStr = mOptions.literal(newSequenceName);
+ return `ALTER SEQUENCE ${sequenceNameStr} RENAME TO ${newSequenceNameStr};`;
+ };
+ _rename.reverse = (sequenceName, newSequenceName) =>
+ _rename(newSequenceName, sequenceName);
+ return _rename;
}
diff --git a/src/operations/sequencesTypes.ts b/src/operations/sequencesTypes.ts
index 78e3c339..6568bcc2 100644
--- a/src/operations/sequencesTypes.ts
+++ b/src/operations/sequencesTypes.ts
@@ -1,27 +1,41 @@
-import { Name, Type, DropOptions, IfNotExistsOption } from './generalTypes'
+import { DropOptions, IfNotExistsOption, Name, Type } from './generalTypes';
export interface SequenceOptions {
- type?: Type
- increment?: number
- minvalue?: number | null | false
- maxvalue?: number | null | false
- start?: number
- cache?: number
- cycle?: boolean
- owner?: string | null | false
+ type?: Type;
+ increment?: number;
+ minvalue?: number | null | false;
+ maxvalue?: number | null | false;
+ start?: number;
+ cache?: number;
+ cycle?: boolean;
+ owner?: string | null | false;
}
-export interface SequenceOptionsCreate extends SequenceOptions, IfNotExistsOption {
- temporary?: boolean
+export interface SequenceOptionsCreate
+ extends SequenceOptions,
+ IfNotExistsOption {
+ temporary?: boolean;
}
export interface SequenceOptionsAlter extends SequenceOptions {
- restart?: number | true
+ restart?: number | true;
}
-type CreateSequenceFn = (sequenceName: Name, sequenceOptions?: SequenceOptionsCreate & DropOptions) => string | string[]
-export type CreateSequence = CreateSequenceFn & { reverse: CreateSequenceFn }
-export type DropSequence = (sequenceName: Name, dropOptions?: DropOptions) => string | string[]
-export type AlterSequence = (sequenceName: Name, sequenceOptions: SequenceOptionsAlter) => string | string[]
-type RenameSequenceFn = (oldSequenceName: Name, newSequenceName: Name) => string | string[]
-export type RenameSequence = RenameSequenceFn & { reverse: RenameSequenceFn }
+type CreateSequenceFn = (
+ sequenceName: Name,
+ sequenceOptions?: SequenceOptionsCreate & DropOptions
+) => string | string[];
+export type CreateSequence = CreateSequenceFn & { reverse: CreateSequenceFn };
+export type DropSequence = (
+ sequenceName: Name,
+ dropOptions?: DropOptions
+) => string | string[];
+export type AlterSequence = (
+ sequenceName: Name,
+ sequenceOptions: SequenceOptionsAlter
+) => string | string[];
+type RenameSequenceFn = (
+ oldSequenceName: Name,
+ newSequenceName: Name
+) => string | string[];
+export type RenameSequence = RenameSequenceFn & { reverse: RenameSequenceFn };
diff --git a/src/operations/tables.ts b/src/operations/tables.ts
index 498b208c..faae9475 100644
--- a/src/operations/tables.ts
+++ b/src/operations/tables.ts
@@ -1,27 +1,34 @@
-import { MigrationOptions, Literal } from '../types'
-import { applyType, applyTypeAdapters, escapeValue, formatLines, intersection, makeComment } from '../utils'
-import { parseSequenceOptions } from './sequences'
+import { Literal, MigrationOptions } from '../types';
+import {
+ applyType,
+ applyTypeAdapters,
+ escapeValue,
+ formatLines,
+ intersection,
+ makeComment,
+} from '../utils';
+import { FunctionParamType } from './functionsTypes';
+import { Name } from './generalTypes';
+import { parseSequenceOptions } from './sequences';
import {
- CreateTable,
- DropTable,
- AlterTable,
- RenameTable,
AddColumns,
- DropColumns,
AlterColumn,
- RenameColumn,
+ AlterTable,
+ ColumnDefinition,
+ ColumnDefinitions,
+ ConstraintOptions,
CreateConstraint,
+ CreateTable,
+ DropColumns,
DropConstraint,
- RenameConstraint,
- ReferencesOptions,
- ConstraintOptions,
- LikeOptions,
+ DropTable,
Like,
- ColumnDefinition,
- ColumnDefinitions,
-} from './tablesTypes'
-import { Name } from './generalTypes'
-import { FunctionParamType } from './functionsTypes'
+ LikeOptions,
+ ReferencesOptions,
+ RenameColumn,
+ RenameConstraint,
+ RenameTable,
+} from './tablesTypes';
export {
CreateTable,
@@ -35,49 +42,56 @@ export {
CreateConstraint,
DropConstraint,
RenameConstraint,
-}
+};
const parseReferences = (options: ReferencesOptions, literal: Literal) => {
- const { references, match, onDelete, onUpdate } = options
- const clauses: string[] = []
+ const { references, match, onDelete, onUpdate } = options;
+ const clauses: string[] = [];
clauses.push(
- typeof references === 'string' && (references.startsWith('"') || references.endsWith(')'))
+ typeof references === 'string' &&
+ (references.startsWith('"') || references.endsWith(')'))
? `REFERENCES ${references}`
- : `REFERENCES ${literal(references)}`,
- )
+ : `REFERENCES ${literal(references)}`
+ );
if (match) {
- clauses.push(`MATCH ${match}`)
+ clauses.push(`MATCH ${match}`);
}
if (onDelete) {
- clauses.push(`ON DELETE ${onDelete}`)
+ clauses.push(`ON DELETE ${onDelete}`);
}
if (onUpdate) {
- clauses.push(`ON UPDATE ${onUpdate}`)
+ clauses.push(`ON UPDATE ${onUpdate}`);
}
- return clauses.join(' ')
-}
+ return clauses.join(' ');
+};
const parseDeferrable = (options: { deferred?: boolean }) =>
- `DEFERRABLE INITIALLY ${options.deferred ? 'DEFERRED' : 'IMMEDIATE'}`
+ `DEFERRABLE INITIALLY ${options.deferred ? 'DEFERRED' : 'IMMEDIATE'}`;
const parseColumns = (
tableName: Name,
columns: ColumnDefinitions,
- mOptions: MigrationOptions,
+ mOptions: MigrationOptions
): {
- columns: string[]
- constraints: ConstraintOptions
- comments: string[]
+ columns: string[];
+ constraints: ConstraintOptions;
+ comments: string[];
} => {
- const extendingTypeShorthands = mOptions.typeShorthands
+ const extendingTypeShorthands = mOptions.typeShorthands;
let columnsWithOptions = Object.keys(columns).reduce<{
- [x: string]: ColumnDefinition & FunctionParamType
- }>((previous, column) => ({ ...previous, [column]: applyType(columns[column], extendingTypeShorthands) }), {})
+ [x: string]: ColumnDefinition & FunctionParamType;
+ }>(
+ (previous, column) => ({
+ ...previous,
+ [column]: applyType(columns[column], extendingTypeShorthands),
+ }),
+ {}
+ );
const primaryColumns = Object.entries(columnsWithOptions)
.filter(([, { primaryKey }]) => Boolean(primaryKey))
- .map(([columnName]) => columnName)
- const multiplePrimaryColumns = primaryColumns.length > 1
+ .map(([columnName]) => columnName);
+ const multiplePrimaryColumns = primaryColumns.length > 1;
if (multiplePrimaryColumns) {
columnsWithOptions = Object.entries(columnsWithOptions).reduce(
@@ -88,18 +102,22 @@ const parseColumns = (
primaryKey: false,
},
}),
- {},
- )
+ {}
+ );
}
const comments = Object.entries(columnsWithOptions)
.map(([columnName, { comment }]) => {
return (
typeof comment !== 'undefined' &&
- makeComment('COLUMN', `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`, comment)
- )
+ makeComment(
+ 'COLUMN',
+ `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`,
+ comment
+ )
+ );
})
- .filter((comment): comment is string => Boolean(comment))
+ .filter((comment): comment is string => Boolean(comment));
return {
columns: Object.entries(columnsWithOptions).map(([columnName, options]) => {
@@ -116,355 +134,475 @@ const parseColumns = (
referencesConstraintComment,
deferrable,
expressionGenerated,
- }: ColumnDefinition = options
- const sequenceGenerated = options.sequenceGenerated === undefined ? options.generated : options.sequenceGenerated
- const constraints: string[] = []
+ }: ColumnDefinition = options;
+ const sequenceGenerated =
+ options.sequenceGenerated === undefined
+ ? options.generated
+ : options.sequenceGenerated;
+ const constraints: string[] = [];
if (collation) {
- constraints.push(`COLLATE ${collation}`)
+ constraints.push(`COLLATE ${collation}`);
}
if (defaultValue !== undefined) {
- constraints.push(`DEFAULT ${escapeValue(defaultValue)}`)
+ constraints.push(`DEFAULT ${escapeValue(defaultValue)}`);
}
if (unique) {
- constraints.push('UNIQUE')
+ constraints.push('UNIQUE');
}
if (primaryKey) {
- constraints.push('PRIMARY KEY')
+ constraints.push('PRIMARY KEY');
}
if (notNull) {
- constraints.push('NOT NULL')
+ constraints.push('NOT NULL');
}
if (check) {
- constraints.push(`CHECK (${check})`)
+ constraints.push(`CHECK (${check})`);
}
if (references) {
- const name = referencesConstraintName || (referencesConstraintComment ? `${tableName}_fk_${columnName}` : '')
- const constraintName = name ? `CONSTRAINT ${mOptions.literal(name)} ` : ''
- constraints.push(`${constraintName}${parseReferences(options as ReferencesOptions, mOptions.literal)}`)
+ const name =
+ referencesConstraintName ||
+ (referencesConstraintComment ? `${tableName}_fk_${columnName}` : '');
+ const constraintName = name
+ ? `CONSTRAINT ${mOptions.literal(name)} `
+ : '';
+ constraints.push(
+ `${constraintName}${parseReferences(options as ReferencesOptions, mOptions.literal)}`
+ );
if (referencesConstraintComment) {
comments.push(
makeComment(
`CONSTRAINT ${mOptions.literal(name)} ON`,
mOptions.literal(tableName),
- referencesConstraintComment,
- ),
- )
+ referencesConstraintComment
+ )
+ );
}
}
if (deferrable) {
- constraints.push(parseDeferrable(options))
+ constraints.push(parseDeferrable(options));
}
if (sequenceGenerated) {
- const sequenceOptions = parseSequenceOptions(extendingTypeShorthands, sequenceGenerated).join(' ')
+ const sequenceOptions = parseSequenceOptions(
+ extendingTypeShorthands,
+ sequenceGenerated
+ ).join(' ');
constraints.push(
- `GENERATED ${sequenceGenerated.precedence} AS IDENTITY${sequenceOptions ? ` (${sequenceOptions})` : ''}`,
- )
+ `GENERATED ${sequenceGenerated.precedence} AS IDENTITY${sequenceOptions ? ` (${sequenceOptions})` : ''}`
+ );
}
if (expressionGenerated) {
- constraints.push(`GENERATED ALWAYS AS (${expressionGenerated}) STORED`)
+ constraints.push(`GENERATED ALWAYS AS (${expressionGenerated}) STORED`);
}
- const constraintsStr = constraints.length ? ` ${constraints.join(' ')}` : ''
+ const constraintsStr = constraints.length
+ ? ` ${constraints.join(' ')}`
+ : '';
- const sType = typeof type === 'object' ? mOptions.literal(type) : type
+ const sType = typeof type === 'object' ? mOptions.literal(type) : type;
- return `${mOptions.literal(columnName)} ${sType}${constraintsStr}`
+ return `${mOptions.literal(columnName)} ${sType}${constraintsStr}`;
}),
constraints: multiplePrimaryColumns ? { primaryKey: primaryColumns } : {},
comments,
- }
-}
+ };
+};
-const parseConstraints = (table: Name, options: ConstraintOptions, optionName: string | null, literal: Literal) => {
- const { check, unique, primaryKey, foreignKeys, exclude, deferrable, comment }: ConstraintOptions = options
- const tableName = typeof table === 'object' ? table.name : table
- let constraints = []
- const comments = []
+const parseConstraints = (
+ table: Name,
+ options: ConstraintOptions,
+ optionName: string | null,
+ literal: Literal
+) => {
+ const {
+ check,
+ unique,
+ primaryKey,
+ foreignKeys,
+ exclude,
+ deferrable,
+ comment,
+ }: ConstraintOptions = options;
+ const tableName = typeof table === 'object' ? table.name : table;
+ let constraints = [];
+ const comments = [];
if (check) {
if (Array.isArray(check)) {
check.forEach((ch, i) => {
- const name = literal(optionName || `${tableName}_chck_${i + 1}`)
- constraints.push(`CONSTRAINT ${name} CHECK (${ch})`)
- })
+ const name = literal(optionName || `${tableName}_chck_${i + 1}`);
+ constraints.push(`CONSTRAINT ${name} CHECK (${ch})`);
+ });
} else {
- const name = literal(optionName || `${tableName}_chck`)
- constraints.push(`CONSTRAINT ${name} CHECK (${check})`)
+ const name = literal(optionName || `${tableName}_chck`);
+ constraints.push(`CONSTRAINT ${name} CHECK (${check})`);
}
}
if (unique) {
- const uniqueArray: Array = Array.isArray(unique) ? unique : [unique]
- const isArrayOfArrays = uniqueArray.some((uniqueSet) => Array.isArray(uniqueSet))
- ;((isArrayOfArrays ? uniqueArray : [uniqueArray]) as Array).forEach((uniqueSet) => {
- const cols = Array.isArray(uniqueSet) ? uniqueSet : [uniqueSet]
- const name = literal(optionName || `${tableName}_uniq_${cols.join('_')}`)
- constraints.push(`CONSTRAINT ${name} UNIQUE (${cols.map(literal).join(', ')})`)
- })
+ const uniqueArray: Array = Array.isArray(unique)
+ ? unique
+ : [unique];
+ const isArrayOfArrays = uniqueArray.some((uniqueSet) =>
+ Array.isArray(uniqueSet)
+ );
+ (
+ (isArrayOfArrays ? uniqueArray : [uniqueArray]) as Array
+ ).forEach((uniqueSet) => {
+ const cols = Array.isArray(uniqueSet) ? uniqueSet : [uniqueSet];
+ const name = literal(optionName || `${tableName}_uniq_${cols.join('_')}`);
+ constraints.push(
+ `CONSTRAINT ${name} UNIQUE (${cols.map(literal).join(', ')})`
+ );
+ });
}
if (primaryKey) {
- const name = literal(optionName || `${tableName}_pkey`)
- const key = (Array.isArray(primaryKey) ? primaryKey : [primaryKey]).map(literal).join(', ')
- constraints.push(`CONSTRAINT ${name} PRIMARY KEY (${key})`)
+ const name = literal(optionName || `${tableName}_pkey`);
+ const key = (Array.isArray(primaryKey) ? primaryKey : [primaryKey])
+ .map(literal)
+ .join(', ');
+ constraints.push(`CONSTRAINT ${name} PRIMARY KEY (${key})`);
}
if (foreignKeys) {
- ;(Array.isArray(foreignKeys) ? foreignKeys : [foreignKeys]).forEach((fk) => {
- const { columns, referencesConstraintName, referencesConstraintComment } = fk
- const cols = Array.isArray(columns) ? columns : [columns]
- const name = literal(referencesConstraintName || optionName || `${tableName}_fk_${cols.join('_')}`)
- const key = cols.map(literal).join(', ')
- const referencesStr = parseReferences(fk, literal)
- constraints.push(`CONSTRAINT ${name} FOREIGN KEY (${key}) ${referencesStr}`)
+ (Array.isArray(foreignKeys) ? foreignKeys : [foreignKeys]).forEach((fk) => {
+ const { columns, referencesConstraintName, referencesConstraintComment } =
+ fk;
+ const cols = Array.isArray(columns) ? columns : [columns];
+ const name = literal(
+ referencesConstraintName ||
+ optionName ||
+ `${tableName}_fk_${cols.join('_')}`
+ );
+ const key = cols.map(literal).join(', ');
+ const referencesStr = parseReferences(fk, literal);
+ constraints.push(
+ `CONSTRAINT ${name} FOREIGN KEY (${key}) ${referencesStr}`
+ );
if (referencesConstraintComment) {
- comments.push(makeComment(`CONSTRAINT ${name} ON`, literal(tableName), referencesConstraintComment))
+ comments.push(
+ makeComment(
+ `CONSTRAINT ${name} ON`,
+ literal(tableName),
+ referencesConstraintComment
+ )
+ );
}
- })
+ });
}
if (exclude) {
- const name = literal(optionName || `${tableName}_excl`)
- constraints.push(`CONSTRAINT ${name} EXCLUDE ${exclude}`)
+ const name = literal(optionName || `${tableName}_excl`);
+ constraints.push(`CONSTRAINT ${name} EXCLUDE ${exclude}`);
}
if (deferrable) {
- constraints = constraints.map((constraint) => `${constraint} ${parseDeferrable(options)}`)
+ constraints = constraints.map(
+ (constraint) => `${constraint} ${parseDeferrable(options)}`
+ );
}
if (comment) {
- if (!optionName) throw new Error('cannot comment on unspecified constraints')
- comments.push(makeComment(`CONSTRAINT ${literal(optionName)} ON`, literal(tableName), comment))
+ if (!optionName)
+ throw new Error('cannot comment on unspecified constraints');
+ comments.push(
+ makeComment(
+ `CONSTRAINT ${literal(optionName)} ON`,
+ literal(tableName),
+ comment
+ )
+ );
}
return {
constraints,
comments,
- }
-}
+ };
+};
-const parseLike = (like: Name | { table: Name; options?: LikeOptions }, literal: Literal) => {
- const formatOptions = (name: 'INCLUDING' | 'EXCLUDING', options?: Like | Like[]) =>
+const parseLike = (
+ like: Name | { table: Name; options?: LikeOptions },
+ literal: Literal
+) => {
+ const formatOptions = (
+ name: 'INCLUDING' | 'EXCLUDING',
+ options?: Like | Like[]
+ ) =>
(Array.isArray(options) ? options : [options])
.filter((option): option is Like => option !== undefined)
.map((option) => ` ${name} ${option}`)
- .join('')
+ .join('');
- const table = typeof like === 'string' || !('table' in like) ? like : like.table
+ const table =
+ typeof like === 'string' || !('table' in like) ? like : like.table;
const options =
- typeof like === 'string' || !('options' in like) || like.options === undefined
+ typeof like === 'string' ||
+ !('options' in like) ||
+ like.options === undefined
? ''
- : [formatOptions('INCLUDING', like.options.including), formatOptions('EXCLUDING', like.options.excluding)].join(
- '',
- )
- return `LIKE ${literal(table)}${options}`
-}
+ : [
+ formatOptions('INCLUDING', like.options.including),
+ formatOptions('EXCLUDING', like.options.excluding),
+ ].join('');
+ return `LIKE ${literal(table)}${options}`;
+};
// TABLE
export function dropTable(mOptions: MigrationOptions) {
const _drop: DropTable = (tableName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const tableNameStr = mOptions.literal(tableName)
- return `DROP TABLE${ifExistsStr} ${tableNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const tableNameStr = mOptions.literal(tableName);
+ return `DROP TABLE${ifExistsStr} ${tableNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createTable(mOptions: MigrationOptions) {
const _create: CreateTable = (tableName, columns, options = {}) => {
- const { temporary, ifNotExists, inherits, like, constraints: optionsConstraints = {}, comment } = options
+ const {
+ temporary,
+ ifNotExists,
+ inherits,
+ like,
+ constraints: optionsConstraints = {},
+ comment,
+ } = options;
const {
columns: columnLines,
constraints: crossColumnConstraints,
comments: columnComments = [],
- } = parseColumns(tableName, columns, mOptions)
- const dupes = intersection(Object.keys(optionsConstraints), Object.keys(crossColumnConstraints))
+ } = parseColumns(tableName, columns, mOptions);
+ const dupes = intersection(
+ Object.keys(optionsConstraints),
+ Object.keys(crossColumnConstraints)
+ );
if (dupes.length > 0) {
- const dupesStr = dupes.join(', ')
- throw new Error(`There is duplicate constraint definition in table and columns options: ${dupesStr}`)
+ const dupesStr = dupes.join(', ');
+ throw new Error(
+ `There is duplicate constraint definition in table and columns options: ${dupesStr}`
+ );
}
const constraints: ConstraintOptions = {
...optionsConstraints,
...crossColumnConstraints,
- }
- const { constraints: constraintLines, comments: constraintComments } = parseConstraints(
- tableName,
- constraints,
- '',
- mOptions.literal,
- )
- const tableDefinition = [...columnLines, ...constraintLines].concat(like ? [parseLike(like, mOptions.literal)] : [])
+ };
+ const { constraints: constraintLines, comments: constraintComments } =
+ parseConstraints(tableName, constraints, '', mOptions.literal);
+ const tableDefinition = [...columnLines, ...constraintLines].concat(
+ like ? [parseLike(like, mOptions.literal)] : []
+ );
- const temporaryStr = temporary ? ' TEMPORARY' : ''
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const inheritsStr = inherits ? ` INHERITS (${mOptions.literal(inherits)})` : ''
- const tableNameStr = mOptions.literal(tableName)
+ const temporaryStr = temporary ? ' TEMPORARY' : '';
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const inheritsStr = inherits
+ ? ` INHERITS (${mOptions.literal(inherits)})`
+ : '';
+ const tableNameStr = mOptions.literal(tableName);
const createTableQuery = `CREATE${temporaryStr} TABLE${ifNotExistsStr} ${tableNameStr} (
${formatLines(tableDefinition)}
-)${inheritsStr};`
- const comments = [...columnComments, ...constraintComments]
+)${inheritsStr};`;
+ const comments = [...columnComments, ...constraintComments];
if (typeof comment !== 'undefined') {
- comments.push(makeComment('TABLE', mOptions.literal(tableName), comment))
+ comments.push(makeComment('TABLE', mOptions.literal(tableName), comment));
}
- return `${createTableQuery}${comments.length > 0 ? `\n${comments.join('\n')}` : ''}`
- }
- _create.reverse = dropTable(mOptions)
- return _create
+ return `${createTableQuery}${comments.length > 0 ? `\n${comments.join('\n')}` : ''}`;
+ };
+ _create.reverse = dropTable(mOptions);
+ return _create;
}
export function alterTable(mOptions: MigrationOptions) {
const _alter: AlterTable = (tableName, options) => {
- const alterDefinition = []
+ const alterDefinition = [];
if (options.levelSecurity) {
- alterDefinition.push(`${options.levelSecurity} ROW LEVEL SECURITY`)
+ alterDefinition.push(`${options.levelSecurity} ROW LEVEL SECURITY`);
}
return `ALTER TABLE ${mOptions.literal(tableName)}
- ${formatLines(alterDefinition)};`
- }
- return _alter
+ ${formatLines(alterDefinition)};`;
+ };
+ return _alter;
}
// COLUMNS
export function dropColumns(mOptions: MigrationOptions) {
const _drop: DropColumns = (tableName, columns, options = {}) => {
- const { ifExists, cascade } = options
+ const { ifExists, cascade } = options;
if (typeof columns === 'string') {
- columns = [columns] // eslint-disable-line no-param-reassign
+ columns = [columns]; // eslint-disable-line no-param-reassign
} else if (!Array.isArray(columns) && typeof columns === 'object') {
- columns = Object.keys(columns) // eslint-disable-line no-param-reassign
+ columns = Object.keys(columns); // eslint-disable-line no-param-reassign
}
const columnsStr = formatLines(
columns.map(mOptions.literal),
` DROP ${ifExists ? ' IF EXISTS' : ''}`,
- `${cascade ? ' CASCADE' : ''},`,
- )
+ `${cascade ? ' CASCADE' : ''},`
+ );
return `ALTER TABLE ${mOptions.literal(tableName)}
-${columnsStr};`
- }
- return _drop
+${columnsStr};`;
+ };
+ return _drop;
}
export function addColumns(mOptions: MigrationOptions) {
const _add: AddColumns = (tableName, columns, options = {}) => {
- const { ifNotExists } = options
- const { columns: columnLines, comments: columnComments = [] } = parseColumns(tableName, columns, mOptions)
- const columnsStr = formatLines(columnLines, ` ADD ${ifNotExists ? 'IF NOT EXISTS ' : ''}`)
- const tableNameStr = mOptions.literal(tableName)
- const alterTableQuery = `ALTER TABLE ${tableNameStr}\n${columnsStr};`
- const columnCommentsStr = columnComments.length > 0 ? `\n${columnComments.join('\n')}` : ''
- return `${alterTableQuery}${columnCommentsStr}`
- }
- _add.reverse = dropColumns(mOptions)
- return _add
+ const { ifNotExists } = options;
+ const { columns: columnLines, comments: columnComments = [] } =
+ parseColumns(tableName, columns, mOptions);
+ const columnsStr = formatLines(
+ columnLines,
+ ` ADD ${ifNotExists ? 'IF NOT EXISTS ' : ''}`
+ );
+ const tableNameStr = mOptions.literal(tableName);
+ const alterTableQuery = `ALTER TABLE ${tableNameStr}\n${columnsStr};`;
+ const columnCommentsStr =
+ columnComments.length > 0 ? `\n${columnComments.join('\n')}` : '';
+ return `${alterTableQuery}${columnCommentsStr}`;
+ };
+ _add.reverse = dropColumns(mOptions);
+ return _add;
}
export function alterColumn(mOptions: MigrationOptions): AlterColumn {
return (tableName, columnName, options) => {
- const { default: defaultValue, type, collation, using, notNull, allowNull, comment } = options
- const sequenceGenerated = options.sequenceGenerated === undefined ? options.generated : options.sequenceGenerated
- const actions: string[] = []
+ const {
+ default: defaultValue,
+ type,
+ collation,
+ using,
+ notNull,
+ allowNull,
+ comment,
+ } = options;
+ const sequenceGenerated =
+ options.sequenceGenerated === undefined
+ ? options.generated
+ : options.sequenceGenerated;
+ const actions: string[] = [];
if (defaultValue === null) {
- actions.push('DROP DEFAULT')
+ actions.push('DROP DEFAULT');
} else if (defaultValue !== undefined) {
- actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`)
+ actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`);
}
if (type) {
- const typeStr = applyTypeAdapters(type)
- const collationStr = collation ? ` COLLATE ${collation}` : ''
- const usingStr = using ? ` USING ${using}` : ''
- actions.push(`SET DATA TYPE ${typeStr}${collationStr}${usingStr}`)
+ const typeStr = applyTypeAdapters(type);
+ const collationStr = collation ? ` COLLATE ${collation}` : '';
+ const usingStr = using ? ` USING ${using}` : '';
+ actions.push(`SET DATA TYPE ${typeStr}${collationStr}${usingStr}`);
}
if (notNull) {
- actions.push('SET NOT NULL')
+ actions.push('SET NOT NULL');
} else if (notNull === false || allowNull) {
- actions.push('DROP NOT NULL')
+ actions.push('DROP NOT NULL');
}
if (sequenceGenerated !== undefined) {
if (!sequenceGenerated) {
- actions.push('DROP IDENTITY')
+ actions.push('DROP IDENTITY');
} else {
- const sequenceOptions = parseSequenceOptions(mOptions.typeShorthands, sequenceGenerated).join(' ')
+ const sequenceOptions = parseSequenceOptions(
+ mOptions.typeShorthands,
+ sequenceGenerated
+ ).join(' ');
actions.push(
- `ADD GENERATED ${sequenceGenerated.precedence} AS IDENTITY${sequenceOptions ? ` (${sequenceOptions})` : ''}`,
- )
+ `ADD GENERATED ${sequenceGenerated.precedence} AS IDENTITY${sequenceOptions ? ` (${sequenceOptions})` : ''}`
+ );
}
}
- const queries: string[] = []
+ const queries: string[] = [];
if (actions.length > 0) {
- const columnsStr = formatLines(actions, ` ALTER ${mOptions.literal(columnName)} `)
- queries.push(`ALTER TABLE ${mOptions.literal(tableName)}\n${columnsStr};`)
+ const columnsStr = formatLines(
+ actions,
+ ` ALTER ${mOptions.literal(columnName)} `
+ );
+ queries.push(
+ `ALTER TABLE ${mOptions.literal(tableName)}\n${columnsStr};`
+ );
}
if (typeof comment !== 'undefined') {
- queries.push(makeComment('COLUMN', `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`, comment))
+ queries.push(
+ makeComment(
+ 'COLUMN',
+ `${mOptions.literal(tableName)}.${mOptions.literal(columnName)}`,
+ comment
+ )
+ );
}
- return queries.join('\n')
- }
+ return queries.join('\n');
+ };
}
export function renameTable(mOptions: MigrationOptions) {
const _rename: RenameTable = (tableName, newName) => {
- const tableNameStr = mOptions.literal(tableName)
- const newNameStr = mOptions.literal(newName)
- return `ALTER TABLE ${tableNameStr} RENAME TO ${newNameStr};`
- }
- _rename.reverse = (tableName, newName) => _rename(newName, tableName)
- return _rename
+ const tableNameStr = mOptions.literal(tableName);
+ const newNameStr = mOptions.literal(newName);
+ return `ALTER TABLE ${tableNameStr} RENAME TO ${newNameStr};`;
+ };
+ _rename.reverse = (tableName, newName) => _rename(newName, tableName);
+ return _rename;
}
export function renameColumn(mOptions: MigrationOptions) {
const _rename: RenameColumn = (tableName, columnName, newName) => {
- const tableNameStr = mOptions.literal(tableName)
- const columnNameStr = mOptions.literal(columnName)
- const newNameStr = mOptions.literal(newName)
- return `ALTER TABLE ${tableNameStr} RENAME ${columnNameStr} TO ${newNameStr};`
- }
- _rename.reverse = (tableName, columnName, newName) => _rename(tableName, newName, columnName)
- return _rename
+ const tableNameStr = mOptions.literal(tableName);
+ const columnNameStr = mOptions.literal(columnName);
+ const newNameStr = mOptions.literal(newName);
+ return `ALTER TABLE ${tableNameStr} RENAME ${columnNameStr} TO ${newNameStr};`;
+ };
+ _rename.reverse = (tableName, columnName, newName) =>
+ _rename(tableName, newName, columnName);
+ return _rename;
}
export function renameConstraint(mOptions: MigrationOptions) {
const _rename: RenameConstraint = (tableName, constraintName, newName) => {
- const tableNameStr = mOptions.literal(tableName)
- const constraintNameStr = mOptions.literal(constraintName)
- const newNameStr = mOptions.literal(newName)
- return `ALTER TABLE ${tableNameStr} RENAME CONSTRAINT ${constraintNameStr} TO ${newNameStr};`
- }
- _rename.reverse = (tableName, constraintName, newName) => _rename(tableName, newName, constraintName)
- return _rename
+ const tableNameStr = mOptions.literal(tableName);
+ const constraintNameStr = mOptions.literal(constraintName);
+ const newNameStr = mOptions.literal(newName);
+ return `ALTER TABLE ${tableNameStr} RENAME CONSTRAINT ${constraintNameStr} TO ${newNameStr};`;
+ };
+ _rename.reverse = (tableName, constraintName, newName) =>
+ _rename(tableName, newName, constraintName);
+ return _rename;
}
export function dropConstraint(mOptions: MigrationOptions) {
const _drop: DropConstraint = (tableName, constraintName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const tableNameStr = mOptions.literal(tableName)
- const constraintNameStr = mOptions.literal(constraintName)
- return `ALTER TABLE ${tableNameStr} DROP CONSTRAINT${ifExistsStr} ${constraintNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const tableNameStr = mOptions.literal(tableName);
+ const constraintNameStr = mOptions.literal(constraintName);
+ return `ALTER TABLE ${tableNameStr} DROP CONSTRAINT${ifExistsStr} ${constraintNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function addConstraint(mOptions: MigrationOptions) {
const _add: CreateConstraint = (tableName, constraintName, expression) => {
const { constraints, comments } =
typeof expression === 'string'
? {
- constraints: [`${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expression}`],
+ constraints: [
+ `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expression}`,
+ ],
comments: [],
}
: parseConstraints(
tableName,
expression as Exclude,
constraintName,
- mOptions.literal,
- )
- const constraintStr = formatLines(constraints, ' ADD ')
- return [`ALTER TABLE ${mOptions.literal(tableName)}\n${constraintStr};`, ...comments].join('\n')
- }
+ mOptions.literal
+ );
+ const constraintStr = formatLines(constraints, ' ADD ');
+ return [
+ `ALTER TABLE ${mOptions.literal(tableName)}\n${constraintStr};`,
+ ...comments,
+ ].join('\n');
+ };
_add.reverse = (tableName, constraintName, options) => {
if (constraintName === null) {
- throw new Error(`Impossible to automatically infer down migration for addConstraint without naming constraint`)
+ throw new Error(
+ `Impossible to automatically infer down migration for addConstraint without naming constraint`
+ );
}
- return dropConstraint(mOptions)(tableName, constraintName, options)
- }
- return _add
+ return dropConstraint(mOptions)(tableName, constraintName, options);
+ };
+ return _add;
}
diff --git a/src/operations/tablesTypes.ts b/src/operations/tablesTypes.ts
index 821d04d0..032bd48c 100644
--- a/src/operations/tablesTypes.ts
+++ b/src/operations/tablesTypes.ts
@@ -1,123 +1,164 @@
-import { Name, Value, IfNotExistsOption, DropOptions } from './generalTypes'
-import { SequenceOptions } from './sequencesTypes'
+import { DropOptions, IfNotExistsOption, Name, Value } from './generalTypes';
+import { SequenceOptions } from './sequencesTypes';
-export type Action = 'NO ACTION' | 'RESTRICT' | 'CASCADE' | 'SET NULL' | 'SET DEFAULT'
+export type Action =
+ | 'NO ACTION'
+ | 'RESTRICT'
+ | 'CASCADE'
+ | 'SET NULL'
+ | 'SET DEFAULT';
export interface ReferencesOptions {
- referencesConstraintName?: string
- referencesConstraintComment?: string
- references: Name
- onDelete?: Action
- onUpdate?: Action
- match?: 'FULL' | 'SIMPLE'
+ referencesConstraintName?: string;
+ referencesConstraintComment?: string;
+ references: Name;
+ onDelete?: Action;
+ onUpdate?: Action;
+ match?: 'FULL' | 'SIMPLE';
}
-type SequenceGeneratedOptions = { precedence: 'ALWAYS' | 'BY DEFAULT' } & SequenceOptions
+type SequenceGeneratedOptions = {
+ precedence: 'ALWAYS' | 'BY DEFAULT';
+} & SequenceOptions;
export interface ColumnDefinition extends Partial {
- type: string
- collation?: string
- unique?: boolean
- primaryKey?: boolean
- notNull?: boolean
- default?: Value
- check?: string
- deferrable?: boolean
- deferred?: boolean
- comment?: string | null
+ type: string;
+ collation?: string;
+ unique?: boolean;
+ primaryKey?: boolean;
+ notNull?: boolean;
+ default?: Value;
+ check?: string;
+ deferrable?: boolean;
+ deferred?: boolean;
+ comment?: string | null;
/**
* @deprecated use sequenceGenerated
*/
- generated?: SequenceGeneratedOptions
- sequenceGenerated?: SequenceGeneratedOptions
- expressionGenerated?: string
+ generated?: SequenceGeneratedOptions;
+ sequenceGenerated?: SequenceGeneratedOptions;
+ expressionGenerated?: string;
}
export interface ColumnDefinitions {
- [name: string]: ColumnDefinition | string
+ [name: string]: ColumnDefinition | string;
}
-export type Like = 'COMMENTS' | 'CONSTRAINTS' | 'DEFAULTS' | 'IDENTITY' | 'INDEXES' | 'STATISTICS' | 'STORAGE' | 'ALL'
+export type Like =
+ | 'COMMENTS'
+ | 'CONSTRAINTS'
+ | 'DEFAULTS'
+ | 'IDENTITY'
+ | 'INDEXES'
+ | 'STATISTICS'
+ | 'STORAGE'
+ | 'ALL';
export interface LikeOptions {
- including?: Like | Like[]
- excluding?: Like | Like[]
+ including?: Like | Like[];
+ excluding?: Like | Like[];
}
export interface ForeignKeyOptions extends ReferencesOptions {
- columns: Name | Name[]
+ columns: Name | Name[];
}
export interface ConstraintOptions {
- check?: string | string[]
- unique?: Name | Array
- primaryKey?: Name | Name[]
- foreignKeys?: ForeignKeyOptions | ForeignKeyOptions[]
- exclude?: string
- deferrable?: boolean
- deferred?: boolean
- comment?: string
+ check?: string | string[];
+ unique?: Name | Array;
+ primaryKey?: Name | Name[];
+ foreignKeys?: ForeignKeyOptions | ForeignKeyOptions[];
+ exclude?: string;
+ deferrable?: boolean;
+ deferred?: boolean;
+ comment?: string;
}
export interface TableOptions extends IfNotExistsOption {
- temporary?: boolean
- inherits?: Name
- like?: Name | { table: Name; options?: LikeOptions }
- constraints?: ConstraintOptions
- comment?: string | null
+ temporary?: boolean;
+ inherits?: Name;
+ like?: Name | { table: Name; options?: LikeOptions };
+ constraints?: ConstraintOptions;
+ comment?: string | null;
}
export interface AlterTableOptions {
- levelSecurity: 'DISABLE' | 'ENABLE' | 'FORCE' | 'NO FORCE'
+ levelSecurity: 'DISABLE' | 'ENABLE' | 'FORCE' | 'NO FORCE';
}
export interface AlterColumnOptions {
- type?: string
- default?: Value
- notNull?: boolean
- allowNull?: boolean
- collation?: string
- using?: string
- comment?: string | null
+ type?: string;
+ default?: Value;
+ notNull?: boolean;
+ allowNull?: boolean;
+ collation?: string;
+ using?: string;
+ comment?: string | null;
/**
* @deprecated use sequenceGenerated
*/
- generated?: null | false | SequenceGeneratedOptions
- sequenceGenerated?: null | false | SequenceGeneratedOptions
+ generated?: null | false | SequenceGeneratedOptions;
+ sequenceGenerated?: null | false | SequenceGeneratedOptions;
}
type CreateTableFn = (
tableName: Name,
columns: ColumnDefinitions,
- options?: TableOptions & DropOptions,
-) => string | string[]
-export type CreateTable = CreateTableFn & { reverse: CreateTableFn }
-export type DropTable = (tableName: Name, dropOptions?: DropOptions) => string | string[]
-type RenameTableFn = (tableName: Name, newtableName: Name) => string | string[]
-export type RenameTable = RenameTableFn & { reverse: RenameTableFn }
-export type AlterTable = (tableName: Name, alterOptions: AlterTableOptions) => string | string[]
+ options?: TableOptions & DropOptions
+) => string | string[];
+export type CreateTable = CreateTableFn & { reverse: CreateTableFn };
+export type DropTable = (
+ tableName: Name,
+ dropOptions?: DropOptions
+) => string | string[];
+type RenameTableFn = (tableName: Name, newtableName: Name) => string | string[];
+export type RenameTable = RenameTableFn & { reverse: RenameTableFn };
+export type AlterTable = (
+ tableName: Name,
+ alterOptions: AlterTableOptions
+) => string | string[];
type AddColumnsFn = (
tableName: Name,
newColumns: ColumnDefinitions,
- addOptions?: IfNotExistsOption & DropOptions,
-) => string | string[]
-export type AddColumns = AddColumnsFn & { reverse: AddColumnsFn }
+ addOptions?: IfNotExistsOption & DropOptions
+) => string | string[];
+export type AddColumns = AddColumnsFn & { reverse: AddColumnsFn };
export type DropColumns = (
tableName: Name,
columns: string | string[] | { [name: string]: unknown },
- dropOptions?: DropOptions,
-) => string | string[]
-type RenameColumnFn = (tableName: Name, oldColumnName: string, newColumnName: string) => string | string[]
-export type RenameColumn = RenameColumnFn & { reverse: RenameColumnFn }
-export type AlterColumn = (tableName: Name, columnName: string, options: AlterColumnOptions) => string | string[]
+ dropOptions?: DropOptions
+) => string | string[];
+type RenameColumnFn = (
+ tableName: Name,
+ oldColumnName: string,
+ newColumnName: string
+) => string | string[];
+export type RenameColumn = RenameColumnFn & { reverse: RenameColumnFn };
+export type AlterColumn = (
+ tableName: Name,
+ columnName: string,
+ options: AlterColumnOptions
+) => string | string[];
type CreateConstraintFn = (
tableName: Name,
constraintName: string | null,
- expression: (string | ConstraintOptions) & DropOptions,
-) => string | string[]
-export type CreateConstraint = CreateConstraintFn & { reverse: CreateConstraintFn }
-export type DropConstraint = (tableName: Name, constraintName: string, options?: DropOptions) => string | string[]
-type RenameConstraintFn = (tableName: Name, oldConstraintName: string, newConstraintName: string) => string | string[]
-export type RenameConstraint = RenameConstraintFn & { reverse: RenameConstraintFn }
+ expression: (string | ConstraintOptions) & DropOptions
+) => string | string[];
+export type CreateConstraint = CreateConstraintFn & {
+ reverse: CreateConstraintFn;
+};
+export type DropConstraint = (
+ tableName: Name,
+ constraintName: string,
+ options?: DropOptions
+) => string | string[];
+type RenameConstraintFn = (
+ tableName: Name,
+ oldConstraintName: string,
+ newConstraintName: string
+) => string | string[];
+export type RenameConstraint = RenameConstraintFn & {
+ reverse: RenameConstraintFn;
+};
diff --git a/src/operations/triggers.ts b/src/operations/triggers.ts
index 6cb54982..5b81d0aa 100644
--- a/src/operations/triggers.ts
+++ b/src/operations/triggers.ts
@@ -1,107 +1,134 @@
-import { MigrationOptions } from '../types'
-import { escapeValue } from '../utils'
-import { createFunction, dropFunction } from './functions'
-import { Name, DropOptions, Value } from './generalTypes'
-import { CreateTrigger, DropTrigger, RenameTrigger, TriggerOptions } from './triggersTypes'
-import { FunctionOptions } from './functionsTypes'
+import { MigrationOptions } from '../types';
+import { escapeValue } from '../utils';
+import { createFunction, dropFunction } from './functions';
+import { FunctionOptions } from './functionsTypes';
+import { DropOptions, Name, Value } from './generalTypes';
+import {
+ CreateTrigger,
+ DropTrigger,
+ RenameTrigger,
+ TriggerOptions,
+} from './triggersTypes';
-export { CreateTrigger, DropTrigger, RenameTrigger }
+export { CreateTrigger, DropTrigger, RenameTrigger };
export function dropTrigger(mOptions: MigrationOptions) {
const _drop: DropTrigger = (tableName, triggerName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const triggerNameStr = mOptions.literal(triggerName)
- const tableNameStr = mOptions.literal(tableName)
- return `DROP TRIGGER${ifExistsStr} ${triggerNameStr} ON ${tableNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const triggerNameStr = mOptions.literal(triggerName);
+ const tableNameStr = mOptions.literal(tableName);
+ return `DROP TRIGGER${ifExistsStr} ${triggerNameStr} ON ${tableNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createTrigger(mOptions: MigrationOptions) {
const _create: CreateTrigger = (
tableName: Name,
triggerName: string,
- triggerOptions: (TriggerOptions & DropOptions) | (TriggerOptions & FunctionOptions & DropOptions),
- definition?: Value,
+ triggerOptions:
+ | (TriggerOptions & DropOptions)
+ | (TriggerOptions & FunctionOptions & DropOptions),
+ definition?: Value
) => {
- const { constraint, condition, operation, deferrable, deferred, functionParams = [] } = triggerOptions
- let { when, level = 'STATEMENT', function: functionName } = triggerOptions
- const operations = Array.isArray(operation) ? operation.join(' OR ') : operation
+ const {
+ constraint,
+ condition,
+ operation,
+ deferrable,
+ deferred,
+ functionParams = [],
+ } = triggerOptions;
+ let { when, level = 'STATEMENT', function: functionName } = triggerOptions;
+ const operations = Array.isArray(operation)
+ ? operation.join(' OR ')
+ : operation;
if (constraint) {
- when = 'AFTER'
+ when = 'AFTER';
}
if (!when) {
- throw new Error('"when" (BEFORE/AFTER/INSTEAD OF) have to be specified')
+ throw new Error('"when" (BEFORE/AFTER/INSTEAD OF) have to be specified');
}
- const isInsteadOf = /instead\s+of/i.test(when)
+ const isInsteadOf = /instead\s+of/i.test(when);
if (isInsteadOf) {
- level = 'ROW'
+ level = 'ROW';
}
if (definition) {
- functionName = functionName === undefined ? triggerName : functionName
+ functionName = functionName === undefined ? triggerName : functionName;
}
if (!functionName) {
- throw new Error("Can't determine function name")
+ throw new Error("Can't determine function name");
}
if (isInsteadOf && condition) {
- throw new Error("INSTEAD OF trigger can't have condition specified")
+ throw new Error("INSTEAD OF trigger can't have condition specified");
}
if (!operations) {
- throw new Error('"operation" (INSERT/UPDATE[ OF ...]/DELETE/TRUNCATE) have to be specified')
+ throw new Error(
+ '"operation" (INSERT/UPDATE[ OF ...]/DELETE/TRUNCATE) have to be specified'
+ );
}
const defferStr = constraint
? `${deferrable ? `DEFERRABLE INITIALLY ${deferred ? 'DEFERRED' : 'IMMEDIATE'}` : 'NOT DEFERRABLE'}\n `
- : ''
- const conditionClause = condition ? `WHEN (${condition})\n ` : ''
- const constraintStr = constraint ? ' CONSTRAINT' : ''
- const paramsStr = functionParams.map(escapeValue).join(', ')
- const triggerNameStr = mOptions.literal(triggerName)
- const tableNameStr = mOptions.literal(tableName)
- const functionNameStr = mOptions.literal(functionName)
+ : '';
+ const conditionClause = condition ? `WHEN (${condition})\n ` : '';
+ const constraintStr = constraint ? ' CONSTRAINT' : '';
+ const paramsStr = functionParams.map(escapeValue).join(', ');
+ const triggerNameStr = mOptions.literal(triggerName);
+ const tableNameStr = mOptions.literal(tableName);
+ const functionNameStr = mOptions.literal(functionName);
const triggerSQL = `CREATE${constraintStr} TRIGGER ${triggerNameStr}
${when} ${operations} ON ${tableNameStr}
${defferStr}FOR EACH ${level}
- ${conditionClause}EXECUTE PROCEDURE ${functionNameStr}(${paramsStr});`
+ ${conditionClause}EXECUTE PROCEDURE ${functionNameStr}(${paramsStr});`;
const fnSQL = definition
? `${createFunction(mOptions)(
functionName,
[],
{ ...(triggerOptions as FunctionOptions), returns: 'trigger' },
- definition,
+ definition
)}\n`
- : ''
- return `${fnSQL}${triggerSQL}`
- }
+ : '';
+ return `${fnSQL}${triggerSQL}`;
+ };
_create.reverse = (
tableName: Name,
triggerName: string,
triggerOptions: TriggerOptions & DropOptions,
- definition?: Value,
+ definition?: Value
) => {
- const triggerSQL = dropTrigger(mOptions)(tableName, triggerName, triggerOptions)
+ const triggerSQL = dropTrigger(mOptions)(
+ tableName,
+ triggerName,
+ triggerOptions
+ );
const fnSQL = definition
? `\n${dropFunction(mOptions)(triggerOptions.function || triggerName, [], triggerOptions)}`
- : ''
- return `${triggerSQL}${fnSQL}`
- }
+ : '';
+ return `${triggerSQL}${fnSQL}`;
+ };
- return _create
+ return _create;
}
export function renameTrigger(mOptions: MigrationOptions) {
- const _rename: RenameTrigger = (tableName, oldTriggerName, newTriggerName) => {
- const oldTriggerNameStr = mOptions.literal(oldTriggerName)
- const tableNameStr = mOptions.literal(tableName)
- const newTriggerNameStr = mOptions.literal(newTriggerName)
- return `ALTER TRIGGER ${oldTriggerNameStr} ON ${tableNameStr} RENAME TO ${newTriggerNameStr};`
- }
- _rename.reverse = (tableName, oldTriggerName, newTriggerName) => _rename(tableName, newTriggerName, oldTriggerName)
- return _rename
+ const _rename: RenameTrigger = (
+ tableName,
+ oldTriggerName,
+ newTriggerName
+ ) => {
+ const oldTriggerNameStr = mOptions.literal(oldTriggerName);
+ const tableNameStr = mOptions.literal(tableName);
+ const newTriggerNameStr = mOptions.literal(newTriggerName);
+ return `ALTER TRIGGER ${oldTriggerNameStr} ON ${tableNameStr} RENAME TO ${newTriggerNameStr};`;
+ };
+ _rename.reverse = (tableName, oldTriggerName, newTriggerName) =>
+ _rename(tableName, newTriggerName, oldTriggerName);
+ return _rename;
}
diff --git a/src/operations/triggersTypes.ts b/src/operations/triggersTypes.ts
index db668a49..05865d75 100644
--- a/src/operations/triggersTypes.ts
+++ b/src/operations/triggersTypes.ts
@@ -1,31 +1,39 @@
-import { Name, Value, DropOptions } from './generalTypes'
-import { FunctionOptions } from './functionsTypes'
+import { FunctionOptions } from './functionsTypes';
+import { DropOptions, Name, Value } from './generalTypes';
export interface TriggerOptions {
- when?: 'BEFORE' | 'AFTER' | 'INSTEAD OF'
- operation: string | string[]
- constraint?: boolean
- function?: Name
- functionParams?: Value[]
- level?: 'STATEMENT' | 'ROW'
- condition?: string
- deferrable?: boolean
- deferred?: boolean
+ when?: 'BEFORE' | 'AFTER' | 'INSTEAD OF';
+ operation: string | string[];
+ constraint?: boolean;
+ function?: Name;
+ functionParams?: Value[];
+ level?: 'STATEMENT' | 'ROW';
+ condition?: string;
+ deferrable?: boolean;
+ deferred?: boolean;
}
type CreateTriggerFn1 = (
tableName: Name,
triggerName: string,
- triggerOptions: TriggerOptions & DropOptions,
-) => string | string[]
+ triggerOptions: TriggerOptions & DropOptions
+) => string | string[];
type CreateTriggerFn2 = (
tableName: Name,
triggerName: string,
triggerOptions: TriggerOptions & FunctionOptions & DropOptions,
- definition: Value,
-) => string | string[]
-type CreateTriggerFn = CreateTriggerFn1 | CreateTriggerFn2
-export type CreateTrigger = CreateTriggerFn & { reverse: CreateTriggerFn }
-export type DropTrigger = (tableName: Name, triggerName: string, dropOptions?: DropOptions) => string | string[]
-type RenameTriggerFn = (tableName: Name, oldTriggerName: string, newTriggerName: string) => string | string[]
-export type RenameTrigger = RenameTriggerFn & { reverse: RenameTriggerFn }
+ definition: Value
+) => string | string[];
+type CreateTriggerFn = CreateTriggerFn1 | CreateTriggerFn2;
+export type CreateTrigger = CreateTriggerFn & { reverse: CreateTriggerFn };
+export type DropTrigger = (
+ tableName: Name,
+ triggerName: string,
+ dropOptions?: DropOptions
+) => string | string[];
+type RenameTriggerFn = (
+ tableName: Name,
+ oldTriggerName: string,
+ newTriggerName: string
+) => string | string[];
+export type RenameTrigger = RenameTriggerFn & { reverse: RenameTriggerFn };
diff --git a/src/operations/types.ts b/src/operations/types.ts
index b6942c09..1daf5100 100644
--- a/src/operations/types.ts
+++ b/src/operations/types.ts
@@ -1,16 +1,16 @@
-import { MigrationOptions } from '../types'
-import { applyType, escapeValue } from '../utils'
+import { MigrationOptions } from '../types';
+import { applyType, escapeValue } from '../utils';
import {
+ AddTypeAttribute,
+ AddTypeValue,
CreateType,
DropType,
- RenameType,
- AddTypeAttribute,
DropTypeAttribute,
- SetTypeAttribute,
- AddTypeValue,
+ RenameType,
RenameTypeAttribute,
RenameTypeValue,
-} from './typesTypes'
+ SetTypeAttribute,
+} from './typesTypes';
export {
CreateType,
@@ -22,116 +22,130 @@ export {
AddTypeValue,
RenameTypeAttribute,
RenameTypeValue,
-}
+};
export function dropType(mOptions: MigrationOptions) {
const _drop: DropType = (typeName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const typeNameStr = mOptions.literal(typeName)
- return `DROP TYPE${ifExistsStr} ${typeNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const typeNameStr = mOptions.literal(typeName);
+ return `DROP TYPE${ifExistsStr} ${typeNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createType(mOptions: MigrationOptions) {
const _create: CreateType = (typeName, options) => {
if (Array.isArray(options)) {
- const optionsStr = options.map(escapeValue).join(', ')
- const typeNameStr = mOptions.literal(typeName)
- return `CREATE TYPE ${typeNameStr} AS ENUM (${optionsStr});`
+ const optionsStr = options.map(escapeValue).join(', ');
+ const typeNameStr = mOptions.literal(typeName);
+ return `CREATE TYPE ${typeNameStr} AS ENUM (${optionsStr});`;
}
const attributes = Object.entries(options)
.map(([attributeName, attribute]) => {
- const typeStr = applyType(attribute, mOptions.typeShorthands).type
- return `${mOptions.literal(attributeName)} ${typeStr}`
+ const typeStr = applyType(attribute, mOptions.typeShorthands).type;
+ return `${mOptions.literal(attributeName)} ${typeStr}`;
})
- .join(',\n')
- return `CREATE TYPE ${mOptions.literal(typeName)} AS (\n${attributes}\n);`
- }
- _create.reverse = dropType(mOptions)
- return _create
+ .join(',\n');
+ return `CREATE TYPE ${mOptions.literal(typeName)} AS (\n${attributes}\n);`;
+ };
+ _create.reverse = dropType(mOptions);
+ return _create;
}
export function dropTypeAttribute(mOptions: MigrationOptions) {
- const _drop: DropTypeAttribute = (typeName, attributeName, { ifExists } = {}) => {
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const typeNameStr = mOptions.literal(typeName)
- const attributeNameStr = mOptions.literal(attributeName)
- return `ALTER TYPE ${typeNameStr} DROP ATTRIBUTE ${attributeNameStr}${ifExistsStr};`
- }
- return _drop
+ const _drop: DropTypeAttribute = (
+ typeName,
+ attributeName,
+ { ifExists } = {}
+ ) => {
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const typeNameStr = mOptions.literal(typeName);
+ const attributeNameStr = mOptions.literal(attributeName);
+ return `ALTER TYPE ${typeNameStr} DROP ATTRIBUTE ${attributeNameStr}${ifExistsStr};`;
+ };
+ return _drop;
}
export function addTypeAttribute(mOptions: MigrationOptions) {
- const _alterAttributeAdd: AddTypeAttribute = (typeName, attributeName, attributeType) => {
- const typeStr = applyType(attributeType, mOptions.typeShorthands).type
- const typeNameStr = mOptions.literal(typeName)
- const attributeNameStr = mOptions.literal(attributeName)
+ const _alterAttributeAdd: AddTypeAttribute = (
+ typeName,
+ attributeName,
+ attributeType
+ ) => {
+ const typeStr = applyType(attributeType, mOptions.typeShorthands).type;
+ const typeNameStr = mOptions.literal(typeName);
+ const attributeNameStr = mOptions.literal(attributeName);
- return `ALTER TYPE ${typeNameStr} ADD ATTRIBUTE ${attributeNameStr} ${typeStr};`
- }
- _alterAttributeAdd.reverse = dropTypeAttribute(mOptions)
- return _alterAttributeAdd
+ return `ALTER TYPE ${typeNameStr} ADD ATTRIBUTE ${attributeNameStr} ${typeStr};`;
+ };
+ _alterAttributeAdd.reverse = dropTypeAttribute(mOptions);
+ return _alterAttributeAdd;
}
export function setTypeAttribute(mOptions: MigrationOptions): SetTypeAttribute {
return (typeName, attributeName, attributeType) => {
- const typeStr = applyType(attributeType, mOptions.typeShorthands).type
- const typeNameStr = mOptions.literal(typeName)
- const attributeNameStr = mOptions.literal(attributeName)
+ const typeStr = applyType(attributeType, mOptions.typeShorthands).type;
+ const typeNameStr = mOptions.literal(typeName);
+ const attributeNameStr = mOptions.literal(attributeName);
- return `ALTER TYPE ${typeNameStr} ALTER ATTRIBUTE ${attributeNameStr} SET DATA TYPE ${typeStr};`
- }
+ return `ALTER TYPE ${typeNameStr} ALTER ATTRIBUTE ${attributeNameStr} SET DATA TYPE ${typeStr};`;
+ };
}
export function addTypeValue(mOptions: MigrationOptions) {
const _add: AddTypeValue = (typeName, value, options = {}) => {
- const { ifNotExists, before, after } = options
+ const { ifNotExists, before, after } = options;
if (before && after) {
- throw new Error('"before" and "after" can\'t be specified together')
+ throw new Error('"before" and "after" can\'t be specified together');
}
- const beforeStr = before ? ` BEFORE ${escapeValue(before)}` : ''
- const afterStr = after ? ` AFTER ${escapeValue(after)}` : ''
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const valueStr = escapeValue(value)
- const typeNameStr = mOptions.literal(typeName)
+ const beforeStr = before ? ` BEFORE ${escapeValue(before)}` : '';
+ const afterStr = after ? ` AFTER ${escapeValue(after)}` : '';
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const valueStr = escapeValue(value);
+ const typeNameStr = mOptions.literal(typeName);
- return `ALTER TYPE ${typeNameStr} ADD VALUE${ifNotExistsStr} ${valueStr}${beforeStr}${afterStr};`
- }
- return _add
+ return `ALTER TYPE ${typeNameStr} ADD VALUE${ifNotExistsStr} ${valueStr}${beforeStr}${afterStr};`;
+ };
+ return _add;
}
export function renameType(mOptions: MigrationOptions) {
const _rename: RenameType = (typeName, newTypeName) => {
- const typeNameStr = mOptions.literal(typeName)
- const newTypeNameStr = mOptions.literal(newTypeName)
- return `ALTER TYPE ${typeNameStr} RENAME TO ${newTypeNameStr};`
- }
- _rename.reverse = (typeName, newTypeName) => _rename(newTypeName, typeName)
- return _rename
+ const typeNameStr = mOptions.literal(typeName);
+ const newTypeNameStr = mOptions.literal(newTypeName);
+ return `ALTER TYPE ${typeNameStr} RENAME TO ${newTypeNameStr};`;
+ };
+ _rename.reverse = (typeName, newTypeName) => _rename(newTypeName, typeName);
+ return _rename;
}
export function renameTypeAttribute(mOptions: MigrationOptions) {
- const _rename: RenameTypeAttribute = (typeName, attributeName, newAttributeName) => {
- const typeNameStr = mOptions.literal(typeName)
- const attributeNameStr = mOptions.literal(attributeName)
- const newAttributeNameStr = mOptions.literal(newAttributeName)
- return `ALTER TYPE ${typeNameStr} RENAME ATTRIBUTE ${attributeNameStr} TO ${newAttributeNameStr};`
- }
- _rename.reverse = (typeName, attributeName, newAttributeName) => _rename(typeName, newAttributeName, attributeName)
- return _rename
+ const _rename: RenameTypeAttribute = (
+ typeName,
+ attributeName,
+ newAttributeName
+ ) => {
+ const typeNameStr = mOptions.literal(typeName);
+ const attributeNameStr = mOptions.literal(attributeName);
+ const newAttributeNameStr = mOptions.literal(newAttributeName);
+ return `ALTER TYPE ${typeNameStr} RENAME ATTRIBUTE ${attributeNameStr} TO ${newAttributeNameStr};`;
+ };
+ _rename.reverse = (typeName, attributeName, newAttributeName) =>
+ _rename(typeName, newAttributeName, attributeName);
+ return _rename;
}
export function renameTypeValue(mOptions: MigrationOptions) {
const _rename: RenameTypeValue = (typeName, value, newValue) => {
- const valueStr = escapeValue(value)
- const newValueStr = escapeValue(newValue)
- const typeNameStr = mOptions.literal(typeName)
- return `ALTER TYPE ${typeNameStr} RENAME VALUE ${valueStr} TO ${newValueStr};`
- }
- _rename.reverse = (typeName, value, newValue) => _rename(typeName, newValue, value)
- return _rename
+ const valueStr = escapeValue(value);
+ const newValueStr = escapeValue(newValue);
+ const typeNameStr = mOptions.literal(typeName);
+ return `ALTER TYPE ${typeNameStr} RENAME VALUE ${valueStr} TO ${newValueStr};`;
+ };
+ _rename.reverse = (typeName, value, newValue) =>
+ _rename(typeName, newValue, value);
+ return _rename;
}
diff --git a/src/operations/typesTypes.ts b/src/operations/typesTypes.ts
index fbecd4ad..8842c597 100644
--- a/src/operations/typesTypes.ts
+++ b/src/operations/typesTypes.ts
@@ -1,25 +1,64 @@
-import { Name, Value, Type, DropOptions, IfExistsOption, IfNotExistsOption } from './generalTypes'
+import {
+ DropOptions,
+ IfExistsOption,
+ IfNotExistsOption,
+ Name,
+ Type,
+ Value,
+} from './generalTypes';
export interface AddTypeValueOptions extends IfNotExistsOption {
- before?: string
- after?: string
+ before?: string;
+ after?: string;
}
-type CreateTypeFn = (typeName: Name, values: (Value[] | { [name: string]: Type }) & DropOptions) => string | string[]
-export type CreateType = CreateTypeFn & { reverse: CreateTypeFn }
-export type DropType = (typeName: Name, dropOptions?: DropOptions) => string | string[]
-type RenameTypeFn = (typeName: Name, newTypeName: Name) => string | string[]
-export type RenameType = RenameTypeFn & { reverse: RenameTypeFn }
+type CreateTypeFn = (
+ typeName: Name,
+ values: (Value[] | { [name: string]: Type }) & DropOptions
+) => string | string[];
+export type CreateType = CreateTypeFn & { reverse: CreateTypeFn };
+export type DropType = (
+ typeName: Name,
+ dropOptions?: DropOptions
+) => string | string[];
+type RenameTypeFn = (typeName: Name, newTypeName: Name) => string | string[];
+export type RenameType = RenameTypeFn & { reverse: RenameTypeFn };
type AddTypeAttributeFn = (
typeName: Name,
attributeName: string,
- attributeType: Type & IfExistsOption,
-) => string | string[]
-export type AddTypeAttribute = AddTypeAttributeFn & { reverse: AddTypeAttributeFn }
-export type DropTypeAttribute = (typeName: Name, attributeName: string, options: IfExistsOption) => string | string[]
-export type SetTypeAttribute = (typeName: Name, attributeName: string, attributeType: Type) => string | string[]
-export type AddTypeValue = (typeName: Name, value: Value, options?: AddTypeValueOptions) => string | string[]
-type RenameTypeAttributeFn = (typeName: Name, attributeName: string, newAttributeName: string) => string | string[]
-export type RenameTypeAttribute = RenameTypeAttributeFn & { reverse: RenameTypeAttributeFn }
-type RenameTypeValueFn = (typeName: Name, value: string, newValue: string) => string | string[]
-export type RenameTypeValue = RenameTypeValueFn & { reverse: RenameTypeValueFn }
+ attributeType: Type & IfExistsOption
+) => string | string[];
+export type AddTypeAttribute = AddTypeAttributeFn & {
+ reverse: AddTypeAttributeFn;
+};
+export type DropTypeAttribute = (
+ typeName: Name,
+ attributeName: string,
+ options: IfExistsOption
+) => string | string[];
+export type SetTypeAttribute = (
+ typeName: Name,
+ attributeName: string,
+ attributeType: Type
+) => string | string[];
+export type AddTypeValue = (
+ typeName: Name,
+ value: Value,
+ options?: AddTypeValueOptions
+) => string | string[];
+type RenameTypeAttributeFn = (
+ typeName: Name,
+ attributeName: string,
+ newAttributeName: string
+) => string | string[];
+export type RenameTypeAttribute = RenameTypeAttributeFn & {
+ reverse: RenameTypeAttributeFn;
+};
+type RenameTypeValueFn = (
+ typeName: Name,
+ value: string,
+ newValue: string
+) => string | string[];
+export type RenameTypeValue = RenameTypeValueFn & {
+ reverse: RenameTypeValueFn;
+};
diff --git a/src/operations/views.ts b/src/operations/views.ts
index d8f95b09..ba6e201a 100644
--- a/src/operations/views.ts
+++ b/src/operations/views.ts
@@ -1,100 +1,136 @@
-import { MigrationOptions } from '../types'
-import { escapeValue } from '../utils'
-import { CreateView, DropView, AlterView, AlterViewColumn, RenameView, ViewOptions } from './viewsTypes'
-import { Nullable } from './generalTypes'
+import { MigrationOptions } from '../types';
+import { escapeValue } from '../utils';
+import { Nullable } from './generalTypes';
+import {
+ AlterView,
+ AlterViewColumn,
+ CreateView,
+ DropView,
+ RenameView,
+ ViewOptions,
+} from './viewsTypes';
-export { CreateView, DropView, AlterView, AlterViewColumn, RenameView, ViewOptions }
+export {
+ CreateView,
+ DropView,
+ AlterView,
+ AlterViewColumn,
+ RenameView,
+ ViewOptions,
+};
const viewOptionStr =
, K extends keyof T>(options: T) =>
(key: K) => {
- const value = options[key] === true ? '' : ` = ${options[key]}`
- return `${key}${value}`
- }
+ const value = options[key] === true ? '' : ` = ${options[key]}`;
+ return `${key}${value}`;
+ };
export function dropView(mOptions: MigrationOptions) {
const _drop: DropView = (viewName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const viewNameStr = mOptions.literal(viewName)
- return `DROP VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const viewNameStr = mOptions.literal(viewName);
+ return `DROP VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createView(mOptions: MigrationOptions) {
const _create: CreateView = (viewName, viewOptions, definition) => {
- const { temporary, replace, recursive, columns = [], options = {}, checkOption } = viewOptions
- const columnNames = (Array.isArray(columns) ? columns : [columns]).map(mOptions.literal).join(', ')
- const withOptions = Object.keys(options).map(viewOptionStr(options)).join(', ')
+ const {
+ temporary,
+ replace,
+ recursive,
+ columns = [],
+ options = {},
+ checkOption,
+ } = viewOptions;
+ const columnNames = (Array.isArray(columns) ? columns : [columns])
+ .map(mOptions.literal)
+ .join(', ');
+ const withOptions = Object.keys(options)
+ .map(viewOptionStr(options))
+ .join(', ');
- const replaceStr = replace ? ' OR REPLACE' : ''
- const temporaryStr = temporary ? ' TEMPORARY' : ''
- const recursiveStr = recursive ? ' RECURSIVE' : ''
- const columnStr = columnNames ? `(${columnNames})` : ''
- const withOptionsStr = withOptions ? ` WITH (${withOptions})` : ''
- const checkOptionStr = checkOption ? ` WITH ${checkOption} CHECK OPTION` : ''
- const viewNameStr = mOptions.literal(viewName)
+ const replaceStr = replace ? ' OR REPLACE' : '';
+ const temporaryStr = temporary ? ' TEMPORARY' : '';
+ const recursiveStr = recursive ? ' RECURSIVE' : '';
+ const columnStr = columnNames ? `(${columnNames})` : '';
+ const withOptionsStr = withOptions ? ` WITH (${withOptions})` : '';
+ const checkOptionStr = checkOption
+ ? ` WITH ${checkOption} CHECK OPTION`
+ : '';
+ const viewNameStr = mOptions.literal(viewName);
- return `CREATE${replaceStr}${temporaryStr}${recursiveStr} VIEW ${viewNameStr}${columnStr}${withOptionsStr} AS ${definition}${checkOptionStr};`
- }
- _create.reverse = dropView(mOptions)
- return _create
+ return `CREATE${replaceStr}${temporaryStr}${recursiveStr} VIEW ${viewNameStr}${columnStr}${withOptionsStr} AS ${definition}${checkOptionStr};`;
+ };
+ _create.reverse = dropView(mOptions);
+ return _create;
}
export function alterView(mOptions: MigrationOptions) {
const _alter: AlterView = (viewName, viewOptions) => {
- const { checkOption, options = {} } = viewOptions
+ const { checkOption, options = {} } = viewOptions;
if (checkOption !== undefined) {
if (options.check_option === undefined) {
- options.check_option = checkOption
+ options.check_option = checkOption;
} else {
- throw new Error('"options.check_option" and "checkOption" can\'t be specified together')
+ throw new Error(
+ '"options.check_option" and "checkOption" can\'t be specified together'
+ );
}
}
- const clauses = []
+ const clauses = [];
const withOptions = Object.keys(options)
.filter((key) => options[key] !== null)
.map(viewOptionStr(options))
- .join(', ')
+ .join(', ');
if (withOptions) {
- clauses.push(`SET (${withOptions})`)
+ clauses.push(`SET (${withOptions})`);
}
const resetOptions = Object.keys(options)
.filter((key) => options[key] === null)
- .join(', ')
+ .join(', ');
if (resetOptions) {
- clauses.push(`RESET (${resetOptions})`)
+ clauses.push(`RESET (${resetOptions})`);
}
- return clauses.map((clause) => `ALTER VIEW ${mOptions.literal(viewName)} ${clause};`).join('\n')
- }
- return _alter
+ return clauses
+ .map((clause) => `ALTER VIEW ${mOptions.literal(viewName)} ${clause};`)
+ .join('\n');
+ };
+ return _alter;
}
export function alterViewColumn(mOptions: MigrationOptions) {
const _alter: AlterViewColumn = (viewName, columnName, options) => {
- const { default: defaultValue } = options
- const actions = []
+ const { default: defaultValue } = options;
+ const actions = [];
if (defaultValue === null) {
- actions.push('DROP DEFAULT')
+ actions.push('DROP DEFAULT');
} else if (defaultValue !== undefined) {
- actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`)
+ actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`);
}
- const viewNameStr = mOptions.literal(viewName)
- const columnNameStr = mOptions.literal(columnName)
- return actions.map((action) => `ALTER VIEW ${viewNameStr} ALTER COLUMN ${columnNameStr} ${action};`).join('\n')
- }
- return _alter
+ const viewNameStr = mOptions.literal(viewName);
+ const columnNameStr = mOptions.literal(columnName);
+ return actions
+ .map(
+ (action) =>
+ `ALTER VIEW ${viewNameStr} ALTER COLUMN ${columnNameStr} ${action};`
+ )
+ .join('\n');
+ };
+ return _alter;
}
export function renameView(mOptions: MigrationOptions) {
const _rename: RenameView = (viewName, newViewName) => {
- const viewNameStr = mOptions.literal(viewName)
- const newViewNameStr = mOptions.literal(newViewName)
- return `ALTER VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`
- }
- _rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName)
- return _rename
+ const viewNameStr = mOptions.literal(viewName);
+ const newViewNameStr = mOptions.literal(newViewName);
+ return `ALTER VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`;
+ };
+ _rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName);
+ return _rename;
}
diff --git a/src/operations/viewsMaterialized.ts b/src/operations/viewsMaterialized.ts
index bda67f22..e42c8513 100644
--- a/src/operations/viewsMaterialized.ts
+++ b/src/operations/viewsMaterialized.ts
@@ -1,15 +1,15 @@
-import { MigrationOptions } from '../types'
-import { formatLines } from '../utils'
+import { MigrationOptions } from '../types';
+import { formatLines } from '../utils';
+import { Nullable } from './generalTypes';
import {
- StorageParameters,
+ AlterMaterializedView,
CreateMaterializedView,
DropMaterializedView,
- AlterMaterializedView,
+ RefreshMaterializedView,
RenameMaterializedView,
RenameMaterializedViewColumn,
- RefreshMaterializedView,
-} from './viewsMaterializedTypes'
-import { Nullable } from './generalTypes'
+ StorageParameters,
+} from './viewsMaterializedTypes';
export {
CreateMaterializedView,
@@ -18,109 +18,130 @@ export {
RenameMaterializedView,
RenameMaterializedViewColumn,
RefreshMaterializedView,
-}
+};
-const dataClause = (data?: boolean) => (data !== undefined ? ` WITH${data ? '' : ' NO'} DATA` : '')
+const dataClause = (data?: boolean) =>
+ data !== undefined ? ` WITH${data ? '' : ' NO'} DATA` : '';
const storageParameterStr =
- , K extends keyof T>(storageParameters: T) =>
+ , K extends keyof T>(
+ storageParameters: T
+ ) =>
(key: K) => {
- const value = storageParameters[key] === true ? '' : ` = ${storageParameters[key]}`
- return `${key}${value}`
- }
+ const value =
+ storageParameters[key] === true ? '' : ` = ${storageParameters[key]}`;
+ return `${key}${value}`;
+ };
export function dropMaterializedView(mOptions: MigrationOptions) {
const _drop: DropMaterializedView = (viewName, options = {}) => {
- const { ifExists, cascade } = options
- const ifExistsStr = ifExists ? ' IF EXISTS' : ''
- const cascadeStr = cascade ? ' CASCADE' : ''
- const viewNameStr = mOptions.literal(viewName)
- return `DROP MATERIALIZED VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`
- }
- return _drop
+ const { ifExists, cascade } = options;
+ const ifExistsStr = ifExists ? ' IF EXISTS' : '';
+ const cascadeStr = cascade ? ' CASCADE' : '';
+ const viewNameStr = mOptions.literal(viewName);
+ return `DROP MATERIALIZED VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`;
+ };
+ return _drop;
}
export function createMaterializedView(mOptions: MigrationOptions) {
const _create: CreateMaterializedView = (viewName, options, definition) => {
- const { ifNotExists, columns = [], tablespace, storageParameters = {}, data } = options
- const columnNames = (Array.isArray(columns) ? columns : [columns]).map(mOptions.literal).join(', ')
- const withOptions = Object.keys(storageParameters).map(storageParameterStr(storageParameters)).join(', ')
+ const {
+ ifNotExists,
+ columns = [],
+ tablespace,
+ storageParameters = {},
+ data,
+ } = options;
+ const columnNames = (Array.isArray(columns) ? columns : [columns])
+ .map(mOptions.literal)
+ .join(', ');
+ const withOptions = Object.keys(storageParameters)
+ .map(storageParameterStr(storageParameters))
+ .join(', ');
- const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
- const columnsStr = columnNames ? `(${columnNames})` : ''
- const withOptionsStr = withOptions ? ` WITH (${withOptions})` : ''
- const tablespaceStr = tablespace ? `TABLESPACE ${mOptions.literal(tablespace)}` : ''
- const dataStr = dataClause(data)
- const viewNameStr = mOptions.literal(viewName)
+ const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
+ const columnsStr = columnNames ? `(${columnNames})` : '';
+ const withOptionsStr = withOptions ? ` WITH (${withOptions})` : '';
+ const tablespaceStr = tablespace
+ ? `TABLESPACE ${mOptions.literal(tablespace)}`
+ : '';
+ const dataStr = dataClause(data);
+ const viewNameStr = mOptions.literal(viewName);
- return `CREATE MATERIALIZED VIEW${ifNotExistsStr} ${viewNameStr}${columnsStr}${withOptionsStr}${tablespaceStr} AS ${definition}${dataStr};`
- }
- _create.reverse = dropMaterializedView(mOptions)
- return _create
+ return `CREATE MATERIALIZED VIEW${ifNotExistsStr} ${viewNameStr}${columnsStr}${withOptionsStr}${tablespaceStr} AS ${definition}${dataStr};`;
+ };
+ _create.reverse = dropMaterializedView(mOptions);
+ return _create;
}
export function alterMaterializedView(mOptions: MigrationOptions) {
const _alter: AlterMaterializedView = (viewName, options) => {
- const { cluster, extension, storageParameters = {} } = options
- const clauses = []
+ const { cluster, extension, storageParameters = {} } = options;
+ const clauses = [];
if (cluster !== undefined) {
if (cluster) {
- clauses.push(`CLUSTER ON ${mOptions.literal(cluster)}`)
+ clauses.push(`CLUSTER ON ${mOptions.literal(cluster)}`);
} else {
- clauses.push(`SET WITHOUT CLUSTER`)
+ clauses.push(`SET WITHOUT CLUSTER`);
}
}
if (extension) {
- clauses.push(`DEPENDS ON EXTENSION ${mOptions.literal(extension)}`)
+ clauses.push(`DEPENDS ON EXTENSION ${mOptions.literal(extension)}`);
}
const withOptions = Object.keys(storageParameters)
.filter((key) => storageParameters[key] !== null)
.map(storageParameterStr(storageParameters))
- .join(', ')
+ .join(', ');
if (withOptions) {
- clauses.push(`SET (${withOptions})`)
+ clauses.push(`SET (${withOptions})`);
}
const resetOptions = Object.keys(storageParameters)
.filter((key) => storageParameters[key] === null)
- .join(', ')
+ .join(', ');
if (resetOptions) {
- clauses.push(`RESET (${resetOptions})`)
+ clauses.push(`RESET (${resetOptions})`);
}
- const clausesStr = formatLines(clauses)
- const viewNameStr = mOptions.literal(viewName)
- return `ALTER MATERIALIZED VIEW ${viewNameStr}\n${clausesStr};`
- }
- return _alter
+ const clausesStr = formatLines(clauses);
+ const viewNameStr = mOptions.literal(viewName);
+ return `ALTER MATERIALIZED VIEW ${viewNameStr}\n${clausesStr};`;
+ };
+ return _alter;
}
export function renameMaterializedView(mOptions: MigrationOptions) {
const _rename: RenameMaterializedView = (viewName, newViewName) => {
- const viewNameStr = mOptions.literal(viewName)
- const newViewNameStr = mOptions.literal(newViewName)
- return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`
- }
- _rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName)
- return _rename
+ const viewNameStr = mOptions.literal(viewName);
+ const newViewNameStr = mOptions.literal(newViewName);
+ return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`;
+ };
+ _rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName);
+ return _rename;
}
export function renameMaterializedViewColumn(mOptions: MigrationOptions) {
- const _rename: RenameMaterializedViewColumn = (viewName, columnName, newColumnName) => {
- const viewNameStr = mOptions.literal(viewName)
- const columnNameStr = mOptions.literal(columnName)
- const newColumnNameStr = mOptions.literal(newColumnName)
- return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME COLUMN ${columnNameStr} TO ${newColumnNameStr};`
- }
- _rename.reverse = (viewName, columnName, newColumnName) => _rename(viewName, newColumnName, columnName)
- return _rename
+ const _rename: RenameMaterializedViewColumn = (
+ viewName,
+ columnName,
+ newColumnName
+ ) => {
+ const viewNameStr = mOptions.literal(viewName);
+ const columnNameStr = mOptions.literal(columnName);
+ const newColumnNameStr = mOptions.literal(newColumnName);
+ return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME COLUMN ${columnNameStr} TO ${newColumnNameStr};`;
+ };
+ _rename.reverse = (viewName, columnName, newColumnName) =>
+ _rename(viewName, newColumnName, columnName);
+ return _rename;
}
export function refreshMaterializedView(mOptions: MigrationOptions) {
const _refresh: RefreshMaterializedView = (viewName, options = {}) => {
- const { concurrently, data } = options
- const concurrentlyStr = concurrently ? ' CONCURRENTLY' : ''
- const dataStr = dataClause(data)
- const viewNameStr = mOptions.literal(viewName)
- return `REFRESH MATERIALIZED VIEW${concurrentlyStr} ${viewNameStr}${dataStr};`
- }
- _refresh.reverse = _refresh
- return _refresh
+ const { concurrently, data } = options;
+ const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
+ const dataStr = dataClause(data);
+ const viewNameStr = mOptions.literal(viewName);
+ return `REFRESH MATERIALIZED VIEW${concurrentlyStr} ${viewNameStr}${dataStr};`;
+ };
+ _refresh.reverse = _refresh;
+ return _refresh;
}
diff --git a/src/operations/viewsMaterializedTypes.ts b/src/operations/viewsMaterializedTypes.ts
index b5932f8a..15eab9a8 100644
--- a/src/operations/viewsMaterializedTypes.ts
+++ b/src/operations/viewsMaterializedTypes.ts
@@ -1,36 +1,60 @@
-import { Name, DropOptions, IfNotExistsOption, Nullable } from './generalTypes'
+import { DropOptions, IfNotExistsOption, Name, Nullable } from './generalTypes';
-export type StorageParameters = { [key: string]: boolean | number }
+export type StorageParameters = { [key: string]: boolean | number };
export interface CreateMaterializedViewOptions extends IfNotExistsOption {
- columns?: string | string[]
- tablespace?: string
- storageParameters?: StorageParameters
- data?: boolean
+ columns?: string | string[];
+ tablespace?: string;
+ storageParameters?: StorageParameters;
+ data?: boolean;
}
export interface AlterMaterializedViewOptions {
- cluster?: null | false | string
- extension?: string
- storageParameters?: Nullable
+ cluster?: null | false | string;
+ extension?: string;
+ storageParameters?: Nullable;
}
export interface RefreshMaterializedViewOptions {
- concurrently?: boolean
- data?: boolean
+ concurrently?: boolean;
+ data?: boolean;
}
type CreateMaterializedViewFn = (
viewName: Name,
options: CreateMaterializedViewOptions & DropOptions,
- definition: string,
-) => string | string[]
-export type CreateMaterializedView = CreateMaterializedViewFn & { reverse: CreateMaterializedViewFn }
-export type DropMaterializedView = (viewName: Name, options?: DropOptions) => string | string[]
-export type AlterMaterializedView = (viewName: Name, options: AlterMaterializedViewOptions) => string | string[]
-type RenameMaterializedViewFn = (viewName: Name, newViewName: Name) => string | string[]
-export type RenameMaterializedView = RenameMaterializedViewFn & { reverse: RenameMaterializedViewFn }
-type RenameMaterializedViewColumnFn = (viewName: Name, columnName: string, newColumnName: string) => string | string[]
-export type RenameMaterializedViewColumn = RenameMaterializedViewColumnFn & { reverse: RenameMaterializedViewColumnFn }
-type RefreshMaterializedViewFn = (viewName: Name, options?: RefreshMaterializedViewOptions) => string | string[]
-export type RefreshMaterializedView = RefreshMaterializedViewFn & { reverse: RefreshMaterializedViewFn }
+ definition: string
+) => string | string[];
+export type CreateMaterializedView = CreateMaterializedViewFn & {
+ reverse: CreateMaterializedViewFn;
+};
+export type DropMaterializedView = (
+ viewName: Name,
+ options?: DropOptions
+) => string | string[];
+export type AlterMaterializedView = (
+ viewName: Name,
+ options: AlterMaterializedViewOptions
+) => string | string[];
+type RenameMaterializedViewFn = (
+ viewName: Name,
+ newViewName: Name
+) => string | string[];
+export type RenameMaterializedView = RenameMaterializedViewFn & {
+ reverse: RenameMaterializedViewFn;
+};
+type RenameMaterializedViewColumnFn = (
+ viewName: Name,
+ columnName: string,
+ newColumnName: string
+) => string | string[];
+export type RenameMaterializedViewColumn = RenameMaterializedViewColumnFn & {
+ reverse: RenameMaterializedViewColumnFn;
+};
+type RefreshMaterializedViewFn = (
+ viewName: Name,
+ options?: RefreshMaterializedViewOptions
+) => string | string[];
+export type RefreshMaterializedView = RefreshMaterializedViewFn & {
+ reverse: RefreshMaterializedViewFn;
+};
diff --git a/src/operations/viewsTypes.ts b/src/operations/viewsTypes.ts
index 4895491a..ca0e3f92 100644
--- a/src/operations/viewsTypes.ts
+++ b/src/operations/viewsTypes.ts
@@ -1,29 +1,43 @@
-import { Name, Value, DropOptions, Nullable } from './generalTypes'
+import { DropOptions, Name, Nullable, Value } from './generalTypes';
-export type ViewOptions = { [key: string]: boolean | number | string }
+export type ViewOptions = { [key: string]: boolean | number | string };
export interface CreateViewOptions {
- temporary?: boolean
- replace?: boolean
- recursive?: boolean
- columns?: string | string[]
- checkOption?: 'CASCADED' | 'LOCAL'
- options?: ViewOptions
+ temporary?: boolean;
+ replace?: boolean;
+ recursive?: boolean;
+ columns?: string | string[];
+ checkOption?: 'CASCADED' | 'LOCAL';
+ options?: ViewOptions;
}
export interface AlterViewOptions {
- checkOption?: null | 'CASCADED' | 'LOCAL'
- options?: Nullable
+ checkOption?: null | 'CASCADED' | 'LOCAL';
+ options?: Nullable;
}
export interface AlterViewColumnOptions {
- default?: Value
+ default?: Value;
}
-type CreateViewFn = (viewName: Name, options: CreateViewOptions & DropOptions, definition: string) => string | string[]
-export type CreateView = CreateViewFn & { reverse: CreateViewFn }
-export type DropView = (viewName: Name, options?: DropOptions) => string | string[]
-export type AlterView = (viewName: Name, options: AlterViewOptions) => string | string[]
-export type AlterViewColumn = (viewName: Name, columnName: string, options: AlterViewColumnOptions) => string | string[]
-type RenameViewFn = (viewName: Name, newViewName: Name) => string | string[]
-export type RenameView = RenameViewFn & { reverse: RenameViewFn }
+type CreateViewFn = (
+ viewName: Name,
+ options: CreateViewOptions & DropOptions,
+ definition: string
+) => string | string[];
+export type CreateView = CreateViewFn & { reverse: CreateViewFn };
+export type DropView = (
+ viewName: Name,
+ options?: DropOptions
+) => string | string[];
+export type AlterView = (
+ viewName: Name,
+ options: AlterViewOptions
+) => string | string[];
+export type AlterViewColumn = (
+ viewName: Name,
+ columnName: string,
+ options: AlterViewColumnOptions
+) => string | string[];
+type RenameViewFn = (viewName: Name, newViewName: Name) => string | string[];
+export type RenameView = RenameViewFn & { reverse: RenameViewFn };
diff --git a/src/runner.ts b/src/runner.ts
index 307a0c51..5a91edf8 100644
--- a/src/runner.ts
+++ b/src/runner.ts
@@ -1,39 +1,43 @@
-import path from 'path'
-import Db, { DBConnection } from './db'
-import { ColumnDefinitions } from './operations/tablesTypes'
-import { Migration, loadMigrationFiles, RunMigration } from './migration'
+import path from 'path';
+import Db, { DBConnection } from './db';
+import { loadMigrationFiles, Migration, RunMigration } from './migration';
+import { ColumnDefinitions } from './operations/tablesTypes';
+import migrateSqlFile from './sqlMigration';
import {
+ Logger,
MigrationBuilderActions,
MigrationDirection,
+ RunnerOption,
RunnerOptionClient,
RunnerOptionUrl,
- RunnerOption,
- Logger,
-} from './types'
-import { createSchemalize, getMigrationTableSchema, getSchemas } from './utils'
-import migrateSqlFile from './sqlMigration'
+} from './types';
+import { createSchemalize, getMigrationTableSchema, getSchemas } from './utils';
// Random but well-known identifier shared by all instances of node-pg-migrate
-const PG_MIGRATE_LOCK_ID = 7241865325823964
+const PG_MIGRATE_LOCK_ID = 7241865325823964;
-const idColumn = 'id'
-const nameColumn = 'name'
-const runOnColumn = 'run_on'
+const idColumn = 'id';
+const nameColumn = 'name';
+const runOnColumn = 'run_on';
-const loadMigrations = async (db: DBConnection, options: RunnerOption, logger: Logger) => {
+const loadMigrations = async (
+ db: DBConnection,
+ options: RunnerOption,
+ logger: Logger
+) => {
try {
- let shorthands: ColumnDefinitions = {}
- const files = await loadMigrationFiles(options.dir, options.ignorePattern)
+ let shorthands: ColumnDefinitions = {};
+ const files = await loadMigrationFiles(options.dir, options.ignorePattern);
return (
await Promise.all(
files.map(async (file) => {
- const filePath = `${options.dir}/${file}`
+ const filePath = `${options.dir}/${file}`;
const actions: MigrationBuilderActions =
path.extname(filePath) === '.sql'
? await migrateSqlFile(filePath)
: // eslint-disable-next-line global-require,import/no-dynamic-require,security/detect-non-literal-require
- require(path.relative(__dirname, filePath))
- shorthands = { ...shorthands, ...actions.shorthands }
+ require(path.relative(__dirname, filePath));
+ shorthands = { ...shorthands, ...actions.shorthands };
return new Migration(
db,
filePath,
@@ -42,133 +46,169 @@ const loadMigrations = async (db: DBConnection, options: RunnerOption, logger: L
{
...shorthands,
},
- logger,
- )
- }),
+ logger
+ );
+ })
)
).sort((m1, m2) => {
- const compare = m1.timestamp - m2.timestamp
- if (compare !== 0) return compare
- return m1.name.localeCompare(m2.name)
- })
+ const compare = m1.timestamp - m2.timestamp;
+ if (compare !== 0) return compare;
+ return m1.name.localeCompare(m2.name);
+ });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
- throw new Error(`Can't get migration files: ${err.stack}`)
+ throw new Error(`Can't get migration files: ${err.stack}`);
}
-}
+};
const lock = async (db: DBConnection): Promise => {
- const [result] = await db.select(`select pg_try_advisory_lock(${PG_MIGRATE_LOCK_ID}) as "lockObtained"`)
+ const [result] = await db.select(
+ `select pg_try_advisory_lock(${PG_MIGRATE_LOCK_ID}) as "lockObtained"`
+ );
if (!result.lockObtained) {
- throw new Error('Another migration is already running')
+ throw new Error('Another migration is already running');
}
-}
+};
const unlock = async (db: DBConnection): Promise => {
- const [result] = await db.select(`select pg_advisory_unlock(${PG_MIGRATE_LOCK_ID}) as "lockReleased"`)
+ const [result] = await db.select(
+ `select pg_advisory_unlock(${PG_MIGRATE_LOCK_ID}) as "lockReleased"`
+ );
if (!result.lockReleased) {
- throw new Error('Failed to release migration lock')
+ throw new Error('Failed to release migration lock');
}
-}
+};
-const ensureMigrationsTable = async (db: DBConnection, options: RunnerOption): Promise => {
+const ensureMigrationsTable = async (
+ db: DBConnection,
+ options: RunnerOption
+): Promise => {
try {
- const schema = getMigrationTableSchema(options)
- const { migrationsTable } = options
+ const schema = getMigrationTableSchema(options);
+ const { migrationsTable } = options;
const fullTableName = createSchemalize(
Boolean(options.decamelize),
- true,
+ true
)({
schema,
name: migrationsTable,
- })
+ });
const migrationTables = await db.select(
- `SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}'`,
- )
+ `SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}'`
+ );
if (migrationTables && migrationTables.length === 1) {
const primaryKeyConstraints = await db.select(
- `SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}' AND constraint_type = 'PRIMARY KEY'`,
- )
+ `SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}' AND constraint_type = 'PRIMARY KEY'`
+ );
if (!primaryKeyConstraints || primaryKeyConstraints.length !== 1) {
- await db.query(`ALTER TABLE ${fullTableName} ADD PRIMARY KEY (${idColumn})`)
+ await db.query(
+ `ALTER TABLE ${fullTableName} ADD PRIMARY KEY (${idColumn})`
+ );
}
} else {
await db.query(
- `CREATE TABLE ${fullTableName} ( ${idColumn} SERIAL PRIMARY KEY, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`,
- )
+ `CREATE TABLE ${fullTableName} ( ${idColumn} SERIAL PRIMARY KEY, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`
+ );
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
- throw new Error(`Unable to ensure migrations table: ${err.stack}`)
+ throw new Error(`Unable to ensure migrations table: ${err.stack}`);
}
-}
+};
const getRunMigrations = async (db: DBConnection, options: RunnerOption) => {
- const schema = getMigrationTableSchema(options)
- const { migrationsTable } = options
+ const schema = getMigrationTableSchema(options);
+ const { migrationsTable } = options;
const fullTableName = createSchemalize(
Boolean(options.decamelize),
- true,
+ true
)({
schema,
name: migrationsTable,
- })
- return db.column(nameColumn, `SELECT ${nameColumn} FROM ${fullTableName} ORDER BY ${runOnColumn}, ${idColumn}`)
-}
+ });
+ return db.column(
+ nameColumn,
+ `SELECT ${nameColumn} FROM ${fullTableName} ORDER BY ${runOnColumn}, ${idColumn}`
+ );
+};
-const getMigrationsToRun = (options: RunnerOption, runNames: string[], migrations: Migration[]): Migration[] => {
+const getMigrationsToRun = (
+ options: RunnerOption,
+ runNames: string[],
+ migrations: Migration[]
+): Migration[] => {
if (options.direction === 'down') {
const downMigrations: Array = runNames
- .filter((migrationName) => !options.file || options.file === migrationName)
- .map((migrationName) => migrations.find(({ name }) => name === migrationName) || migrationName)
- const { count = 1 } = options
+ .filter(
+ (migrationName) => !options.file || options.file === migrationName
+ )
+ .map(
+ (migrationName) =>
+ migrations.find(({ name }) => name === migrationName) || migrationName
+ );
+ const { count = 1 } = options;
const toRun = (
options.timestamp
- ? downMigrations.filter((migration) => typeof migration === 'object' && migration.timestamp >= count)
+ ? downMigrations.filter(
+ (migration) =>
+ typeof migration === 'object' && migration.timestamp >= count
+ )
: downMigrations.slice(-Math.abs(count))
- ).reverse()
- const deletedMigrations = toRun.filter((migration): migration is string => typeof migration === 'string')
+ ).reverse();
+ const deletedMigrations = toRun.filter(
+ (migration): migration is string => typeof migration === 'string'
+ );
if (deletedMigrations.length) {
- const deletedMigrationsStr = deletedMigrations.join(', ')
- throw new Error(`Definitions of migrations ${deletedMigrationsStr} have been deleted.`)
+ const deletedMigrationsStr = deletedMigrations.join(', ');
+ throw new Error(
+ `Definitions of migrations ${deletedMigrationsStr} have been deleted.`
+ );
}
- return toRun as Migration[]
+ return toRun as Migration[];
}
const upMigrations = migrations.filter(
- ({ name }) => runNames.indexOf(name) < 0 && (!options.file || options.file === name),
- )
- const { count = Infinity } = options
+ ({ name }) =>
+ runNames.indexOf(name) < 0 && (!options.file || options.file === name)
+ );
+ const { count = Infinity } = options;
return options.timestamp
? upMigrations.filter(({ timestamp }) => timestamp <= count)
- : upMigrations.slice(0, Math.abs(count))
-}
+ : upMigrations.slice(0, Math.abs(count));
+};
const checkOrder = (runNames: string[], migrations: Migration[]) => {
- const len = Math.min(runNames.length, migrations.length)
+ const len = Math.min(runNames.length, migrations.length);
for (let i = 0; i < len; i += 1) {
- const runName = runNames[i]
- const migrationName = migrations[i].name
+ const runName = runNames[i];
+ const migrationName = migrations[i].name;
if (runName !== migrationName) {
- throw new Error(`Not run migration ${migrationName} is preceding already run migration ${runName}`)
+ throw new Error(
+ `Not run migration ${migrationName} is preceding already run migration ${runName}`
+ );
}
}
-}
+};
-const runMigrations = (toRun: Migration[], method: 'markAsRun' | 'apply', direction: MigrationDirection) =>
+const runMigrations = (
+ toRun: Migration[],
+ method: 'markAsRun' | 'apply',
+ direction: MigrationDirection
+) =>
toRun.reduce(
- (promise: Promise, migration) => promise.then(() => migration[method](direction)),
- Promise.resolve(),
- )
+ (promise: Promise, migration) =>
+ promise.then(() => migration[method](direction)),
+ Promise.resolve()
+ );
const getLogger = ({ log, logger, verbose }: RunnerOption): Logger => {
- let loggerObject: Logger = console
+ let loggerObject: Logger = console;
if (typeof logger === 'object') {
- loggerObject = logger
+ loggerObject = logger;
} else if (typeof log === 'function') {
- loggerObject = { debug: log, info: log, warn: log, error: log }
+ loggerObject = { debug: log, info: log, warn: log, error: log };
}
return verbose
? loggerObject
@@ -177,81 +217,97 @@ const getLogger = ({ log, logger, verbose }: RunnerOption): Logger => {
info: loggerObject.info.bind(loggerObject),
warn: loggerObject.warn.bind(loggerObject),
error: loggerObject.error.bind(loggerObject),
- }
-}
+ };
+};
export default async (options: RunnerOption): Promise => {
- const logger = getLogger(options)
- const db = Db((options as RunnerOptionClient).dbClient || (options as RunnerOptionUrl).databaseUrl, logger)
+ const logger = getLogger(options);
+ const db = Db(
+ (options as RunnerOptionClient).dbClient ||
+ (options as RunnerOptionUrl).databaseUrl,
+ logger
+ );
try {
- await db.createConnection()
+ await db.createConnection();
if (!options.noLock) {
- await lock(db)
+ await lock(db);
}
if (options.schema) {
- const schemas = getSchemas(options.schema)
+ const schemas = getSchemas(options.schema);
if (options.createSchema) {
- await Promise.all(schemas.map((schema) => db.query(`CREATE SCHEMA IF NOT EXISTS "${schema}"`)))
+ await Promise.all(
+ schemas.map((schema) =>
+ db.query(`CREATE SCHEMA IF NOT EXISTS "${schema}"`)
+ )
+ );
}
- await db.query(`SET search_path TO ${schemas.map((s) => `"${s}"`).join(', ')}`)
+ await db.query(
+ `SET search_path TO ${schemas.map((s) => `"${s}"`).join(', ')}`
+ );
}
if (options.migrationsSchema && options.createMigrationsSchema) {
- await db.query(`CREATE SCHEMA IF NOT EXISTS "${options.migrationsSchema}"`)
+ await db.query(
+ `CREATE SCHEMA IF NOT EXISTS "${options.migrationsSchema}"`
+ );
}
- await ensureMigrationsTable(db, options)
+ await ensureMigrationsTable(db, options);
const [migrations, runNames] = await Promise.all([
loadMigrations(db, options, logger),
getRunMigrations(db, options),
- ])
+ ]);
if (options.checkOrder) {
- checkOrder(runNames, migrations)
+ checkOrder(runNames, migrations);
}
- const toRun: Migration[] = getMigrationsToRun(options, runNames, migrations)
+ const toRun: Migration[] = getMigrationsToRun(
+ options,
+ runNames,
+ migrations
+ );
if (!toRun.length) {
- logger.info('No migrations to run!')
- return []
+ logger.info('No migrations to run!');
+ return [];
}
// TODO: add some fancy colors to logging
- logger.info('> Migrating files:')
+ logger.info('> Migrating files:');
toRun.forEach((m) => {
- logger.info(`> - ${m.name}`)
- })
+ logger.info(`> - ${m.name}`);
+ });
if (options.fake) {
- await runMigrations(toRun, 'markAsRun', options.direction)
+ await runMigrations(toRun, 'markAsRun', options.direction);
} else if (options.singleTransaction) {
- await db.query('BEGIN')
+ await db.query('BEGIN');
try {
- await runMigrations(toRun, 'apply', options.direction)
- await db.query('COMMIT')
+ await runMigrations(toRun, 'apply', options.direction);
+ await db.query('COMMIT');
} catch (err) {
- logger.warn('> Rolling back attempted migration ...')
- await db.query('ROLLBACK')
- throw err
+ logger.warn('> Rolling back attempted migration ...');
+ await db.query('ROLLBACK');
+ throw err;
}
} else {
- await runMigrations(toRun, 'apply', options.direction)
+ await runMigrations(toRun, 'apply', options.direction);
}
return toRun.map((m) => ({
path: m.path,
name: m.name,
timestamp: m.timestamp,
- }))
+ }));
} finally {
if (db.connected()) {
if (!options.noLock) {
- await unlock(db).catch((error) => logger.warn(error.message))
+ await unlock(db).catch((error) => logger.warn(error.message));
}
- db.close()
+ db.close();
}
}
-}
+};
diff --git a/src/sqlMigration.ts b/src/sqlMigration.ts
index 804604fa..83161134 100644
--- a/src/sqlMigration.ts
+++ b/src/sqlMigration.ts
@@ -1,34 +1,40 @@
-import fs from 'fs'
-import { MigrationBuilderActions } from './types'
+import fs from 'fs';
+import { MigrationBuilderActions } from './types';
-const { readFile } = fs.promises
+const { readFile } = fs.promises;
const createMigrationCommentRegex = (direction: 'up' | 'down') =>
- new RegExp(`^\\s*--[\\s-]*${direction}\\s+migration`, 'im') // eslint-disable-line security/detect-non-literal-regexp
+ new RegExp(`^\\s*--[\\s-]*${direction}\\s+migration`, 'im'); // eslint-disable-line security/detect-non-literal-regexp
export const getActions = (content: string): MigrationBuilderActions => {
- const upMigrationCommentRegex = createMigrationCommentRegex('up')
- const downMigrationCommentRegex = createMigrationCommentRegex('down')
+ const upMigrationCommentRegex = createMigrationCommentRegex('up');
+ const downMigrationCommentRegex = createMigrationCommentRegex('down');
- const upMigrationStart = content.search(upMigrationCommentRegex)
- const downMigrationStart = content.search(downMigrationCommentRegex)
+ const upMigrationStart = content.search(upMigrationCommentRegex);
+ const downMigrationStart = content.search(downMigrationCommentRegex);
const upSql =
upMigrationStart >= 0
- ? content.substr(upMigrationStart, downMigrationStart < upMigrationStart ? undefined : downMigrationStart)
- : content
+ ? content.substr(
+ upMigrationStart,
+ downMigrationStart < upMigrationStart ? undefined : downMigrationStart
+ )
+ : content;
const downSql =
downMigrationStart >= 0
- ? content.substr(downMigrationStart, upMigrationStart < downMigrationStart ? undefined : upMigrationStart)
- : undefined
+ ? content.substr(
+ downMigrationStart,
+ upMigrationStart < downMigrationStart ? undefined : upMigrationStart
+ )
+ : undefined;
return {
up: (pgm) => pgm.sql(upSql),
down: downSql === undefined ? false : (pgm) => pgm.sql(downSql),
- }
-}
+ };
+};
export default async (sqlPath: string) => {
- const content = await readFile(sqlPath, 'utf-8')
- return getActions(content)
-}
+ const content = await readFile(sqlPath, 'utf-8');
+ return getActions(content);
+};
diff --git a/src/types.ts b/src/types.ts
index 6a1968b8..3a792c56 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -1,137 +1,181 @@
-import { ClientBase, ClientConfig, QueryArrayResult, QueryResult, QueryArrayConfig, QueryConfig } from 'pg'
-import { Name } from './operations/generalTypes'
+import {
+ ClientBase,
+ ClientConfig,
+ QueryArrayConfig,
+ QueryArrayResult,
+ QueryConfig,
+ QueryResult,
+} from 'pg';
+import * as domains from './operations/domainsTypes';
+import * as extensions from './operations/extensionsTypes';
+import * as functions from './operations/functionsTypes';
+import { Name } from './operations/generalTypes';
+import * as indexes from './operations/indexesTypes';
+import * as operators from './operations/operatorsTypes';
+import * as other from './operations/othersTypes';
+import PgLiteral from './operations/PgLiteral';
+import * as policies from './operations/policiesTypes';
+import * as roles from './operations/rolesTypes';
+import * as schemas from './operations/schemasTypes';
+import * as sequences from './operations/sequencesTypes';
+import * as tables from './operations/tablesTypes';
+import * as triggers from './operations/triggersTypes';
+import * as types from './operations/typesTypes';
+import * as mViews from './operations/viewsMaterializedTypes';
+import * as views from './operations/viewsTypes';
-import * as domains from './operations/domainsTypes'
-import * as extensions from './operations/extensionsTypes'
-import * as functions from './operations/functionsTypes'
-import * as indexes from './operations/indexesTypes'
-import * as operators from './operations/operatorsTypes'
-import * as other from './operations/othersTypes'
-import * as policies from './operations/policiesTypes'
-import * as roles from './operations/rolesTypes'
-import * as schemas from './operations/schemasTypes'
-import * as sequences from './operations/sequencesTypes'
-import * as tables from './operations/tablesTypes'
-import * as triggers from './operations/triggersTypes'
-import * as types from './operations/typesTypes'
-import * as views from './operations/viewsTypes'
-import * as mViews from './operations/viewsMaterializedTypes'
-import PgLiteral from './operations/PgLiteral'
-
-export { ClientConfig, ConnectionConfig } from 'pg'
+export { ClientConfig, ConnectionConfig } from 'pg';
// see ClientBase in @types/pg
export interface DB {
/* eslint-disable @typescript-eslint/no-explicit-any */
- query(queryConfig: QueryArrayConfig, values?: any[]): Promise
- query(queryConfig: QueryConfig): Promise
- query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise
+ query(
+ queryConfig: QueryArrayConfig,
+ values?: any[]
+ ): Promise;
+ query(queryConfig: QueryConfig): Promise;
+ query(
+ queryTextOrConfig: string | QueryConfig,
+ values?: any[]
+ ): Promise;
- select(queryConfig: QueryArrayConfig, values?: any[]): Promise
- select(queryConfig: QueryConfig): Promise
- select(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise
+ select(queryConfig: QueryArrayConfig, values?: any[]): Promise;
+ select(queryConfig: QueryConfig): Promise;
+ select(
+ queryTextOrConfig: string | QueryConfig,
+ values?: any[]
+ ): Promise;
/* eslint-enable @typescript-eslint/no-explicit-any */
}
export interface MigrationBuilder {
- createExtension: (...args: Parameters) => void
- dropExtension: (...args: Parameters) => void
- addExtension: (...args: Parameters) => void
- createTable: (...args: Parameters) => void
- dropTable: (...args: Parameters) => void
- renameTable: (...args: Parameters) => void
- alterTable: (...args: Parameters) => void
- addColumns: (...args: Parameters) => void
- dropColumns: (...args: Parameters) => void
- renameColumn: (...args: Parameters) => void
- alterColumn: (...args: Parameters) => void
- addColumn: (...args: Parameters) => void
- dropColumn: (...args: Parameters) => void
- addConstraint: (...args: Parameters) => void
- dropConstraint: (...args: Parameters) => void
- renameConstraint: (...args: Parameters) => void
- createConstraint: (...args: Parameters) => void
- createType: (...args: Parameters) => void
- dropType: (...args: Parameters) => void
- addType: (...args: Parameters) => void
- renameType: (...args: Parameters) => void
- renameTypeAttribute: (...args: Parameters) => void
- renameTypeValue: (...args: Parameters) => void
- addTypeAttribute: (...args: Parameters) => void
- dropTypeAttribute: (...args: Parameters) => void
- setTypeAttribute: (...args: Parameters) => void
- addTypeValue: (...args: Parameters) => void
- createIndex: (...args: Parameters) => void
- dropIndex: (...args: Parameters) => void
- addIndex: (...args: Parameters) => void
- createRole: (...args: Parameters) => void
- dropRole: (...args: Parameters) => void
- alterRole: (...args: Parameters) => void
- renameRole: (...args: Parameters) => void
- createFunction: (...args: Parameters) => void
- dropFunction: (...args: Parameters