@contember/database-migrations
Advanced tools
Comparing version 0.12.0-alpha.2 to 0.12.0-alpha.4
export * from './MigrationsRunner'; | ||
export * from './helpers'; | ||
export { Migration, loadMigrations } from './runner'; | ||
export { MigrationBuilder, Name } from 'node-pg-migrate'; | ||
export { DatabaseCredentials } from '@contember/database'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -13,4 +13,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.loadMigrations = exports.Migration = void 0; | ||
__exportStar(require("./MigrationsRunner"), exports); | ||
__exportStar(require("./helpers"), exports); | ||
var runner_1 = require("./runner"); | ||
Object.defineProperty(exports, "Migration", { enumerable: true, get: function () { return runner_1.Migration; } }); | ||
Object.defineProperty(exports, "loadMigrations", { enumerable: true, get: function () { return runner_1.loadMigrations; } }); | ||
//# sourceMappingURL=index.js.map |
import { DatabaseCredentials } from '@contember/database'; | ||
import { ClientBase } from 'pg'; | ||
import { Migration } from './runner'; | ||
export declare class MigrationsRunner { | ||
private readonly db; | ||
private readonly schema; | ||
private readonly dir; | ||
private readonly migrations; | ||
private readonly dbClient?; | ||
constructor(db: DatabaseCredentials, schema: string, dir: string, dbClient?: ClientBase | undefined); | ||
constructor(db: DatabaseCredentials, schema: string, migrations: () => Promise<Migration[]>, dbClient?: ClientBase | undefined); | ||
migrate<MigrationArgs>(log: (msg: string) => void, migrationArgs?: MigrationArgs): Promise<{ | ||
@@ -10,0 +11,0 @@ name: string; |
@@ -25,6 +25,6 @@ "use strict"; | ||
class MigrationsRunner { | ||
constructor(db, schema, dir, dbClient) { | ||
constructor(db, schema, migrations, dbClient) { | ||
this.db = db; | ||
this.schema = schema; | ||
this.dir = dir; | ||
this.migrations = migrations; | ||
this.dbClient = dbClient; | ||
@@ -38,8 +38,6 @@ } | ||
const migrate = (await Promise.resolve().then(() => __importStar(require('./runner')))).default; | ||
return await migrate({ | ||
return await migrate(this.migrations, { | ||
...dbParams, | ||
dir: this.dir, | ||
schema: this.schema, | ||
migrationsTable: 'migrations', | ||
ignorePattern: '(\\..*)|(.+\\.ts)|tsconfig\\..+|.+\\.map|.+\\.test\\.js', | ||
createSchema: true, | ||
@@ -46,0 +44,0 @@ migrationArgs, |
@@ -7,5 +7,2 @@ import { LogFn, Logger, RunnerOptionClient, RunnerOptionUrl } from 'node-pg-migrate/dist/types'; | ||
schema?: string; | ||
dir: string; | ||
ignorePattern?: string; | ||
file?: string; | ||
createSchema?: boolean; | ||
@@ -20,8 +17,8 @@ createMigrationsSchema?: boolean; | ||
export declare class Migration { | ||
readonly filePath: string; | ||
readonly migration: (builder: MigrationBuilder, args: any) => Promise<void>; | ||
name: string; | ||
constructor(filePath: string, migration: (builder: MigrationBuilder, args: any) => Promise<void>); | ||
readonly name: string; | ||
readonly migration: (builder: MigrationBuilder, args: any) => Promise<void> | void; | ||
constructor(name: string, migration: (builder: MigrationBuilder, args: any) => Promise<void> | void); | ||
} | ||
declare const _default: (options: RunnerOption) => Promise<{ | ||
export declare const loadMigrations: (sqlDir: string, additional: Migration[]) => Promise<Migration[]>; | ||
declare const _default: (getMigrations: () => Promise<Migration[]>, options: RunnerOption) => Promise<{ | ||
name: string; | ||
@@ -28,0 +25,0 @@ }[]>; |
@@ -29,2 +29,21 @@ "use strict"; | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -34,9 +53,9 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Migration = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
exports.loadMigrations = exports.Migration = void 0; | ||
const path_1 = __importStar(require("path")); | ||
const db_1 = __importDefault(require("node-pg-migrate/dist/db")); | ||
const migration_1 = require("node-pg-migrate/dist/migration"); | ||
const utils_1 = require("node-pg-migrate/dist/utils"); | ||
const sqlMigration_1 = __importDefault(require("node-pg-migrate/dist/sqlMigration")); | ||
const helpers_1 = require("./helpers"); | ||
const promises_1 = require("fs/promises"); | ||
// Random but well-known identifier shared by all instances of node-pg-migrate | ||
@@ -48,25 +67,22 @@ const PG_MIGRATE_LOCK_ID = 7241865325823964; | ||
class Migration { | ||
constructor(filePath, migration) { | ||
this.filePath = filePath; | ||
constructor(name, migration) { | ||
this.name = name; | ||
this.migration = migration; | ||
this.name = path_1.default.basename(filePath, path_1.default.extname(filePath)); | ||
} | ||
} | ||
exports.Migration = Migration; | ||
const loadMigrations = async (db, options) => { | ||
try { | ||
const files = await migration_1.loadMigrationFiles(options.dir, options.ignorePattern); | ||
return Promise.all(files.map(async (file) => { | ||
const filePath = `${options.dir}/${file}`; | ||
const actions = path_1.default.extname(filePath) === '.sql' | ||
? (await sqlMigration_1.default(filePath)).up | ||
: // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require(path_1.default.relative(__dirname, filePath)).default; | ||
return new Migration(filePath, actions); | ||
})); | ||
} | ||
catch (err) { | ||
throw new Error(`Can't get migration files: ${err.stack}`); | ||
} | ||
const loadMigrations = async (sqlDir, additional) => { | ||
return (await Promise.all((await promises_1.readdir(sqlDir)) | ||
.filter(it => path_1.default.extname(it) === '.sql') | ||
.map(async (it) => { | ||
const migration = (await sqlMigration_1.default(path_1.join(sqlDir, it))).up; | ||
if (!migration) { | ||
throw new Error(); | ||
} | ||
return new Migration(path_1.default.basename(it, path_1.default.extname(it)), migration); | ||
}))) | ||
.concat(additional) | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
}; | ||
exports.loadMigrations = loadMigrations; | ||
const lock = async (db) => { | ||
@@ -103,3 +119,3 @@ await db.select(`select pg_advisory_lock(${PG_MIGRATE_LOCK_ID})`); | ||
const getMigrationsToRun = (options, runNames, migrations) => { | ||
return migrations.filter(({ name }) => runNames.indexOf(name) < 0 && (!options.file || options.file === name)); | ||
return migrations.filter(({ name }) => runNames.indexOf(name) < 0); | ||
}; | ||
@@ -129,3 +145,3 @@ const checkOrder = (runNames, migrations) => { | ||
}; | ||
exports.default = async (options) => { | ||
exports.default = async (getMigrations, options) => { | ||
const logger = getLogger(options); | ||
@@ -147,3 +163,4 @@ const db = db_1.default(options.dbClient || options.databaseUrl, logger); | ||
await ensureMigrationsTable(db, options); | ||
const [migrations, runNames] = await Promise.all([loadMigrations(db, options), getRunMigrations(db, options)]); | ||
const runNames = await getRunMigrations(db, options); | ||
const migrations = await getMigrations(); | ||
checkOrder(runNames, migrations); | ||
@@ -150,0 +167,0 @@ const toRun = getMigrationsToRun(options, runNames, migrations); |
{ | ||
"name": "@contember/database-migrations", | ||
"version": "0.12.0-alpha.2", | ||
"version": "0.12.0-alpha.4", | ||
"scripts": { | ||
@@ -11,3 +11,3 @@ "test": "echo 'No tests'" | ||
"dependencies": { | ||
"@contember/database": "^0.12.0-alpha.2", | ||
"@contember/database": "^0.12.0-alpha.4", | ||
"node-pg-migrate": "^5.9.0" | ||
@@ -14,0 +14,0 @@ }, |
export * from './MigrationsRunner' | ||
export * from './helpers' | ||
export { Migration, loadMigrations } from './runner' | ||
export { MigrationBuilder, Name } from 'node-pg-migrate' | ||
export { DatabaseCredentials } from '@contember/database' |
@@ -5,2 +5,3 @@ import { DatabaseCredentials } from '@contember/database' | ||
import { createDatabaseIfNotExists } from './helpers' | ||
import { Migration } from './runner' | ||
@@ -11,3 +12,3 @@ export class MigrationsRunner { | ||
private readonly schema: string, | ||
private readonly dir: string, | ||
private readonly migrations: () => Promise<Migration[]>, | ||
private readonly dbClient?: ClientBase, | ||
@@ -25,8 +26,6 @@ ) {} | ||
const migrate = (await import('./runner')).default | ||
return await migrate({ | ||
return await migrate(this.migrations, { | ||
...dbParams, | ||
dir: this.dir, | ||
schema: this.schema, | ||
migrationsTable: 'migrations', | ||
ignorePattern: '(\\..*)|(.+\\.ts)|tsconfig\\..+|.+\\.map|.+\\.test\\.js', | ||
createSchema: true, | ||
@@ -33,0 +32,0 @@ migrationArgs, |
@@ -29,5 +29,4 @@ /* | ||
import path from 'path' | ||
import path, { join } from 'path' | ||
import Db, { DBConnection } from 'node-pg-migrate/dist/db' | ||
import { loadMigrationFiles } from 'node-pg-migrate/dist/migration' | ||
import { LogFn, Logger, RunnerOptionClient, RunnerOptionUrl } from 'node-pg-migrate/dist/types' | ||
@@ -38,2 +37,3 @@ import { createSchemalize, getSchemas } from 'node-pg-migrate/dist/utils' | ||
import { createMigrationBuilder } from './helpers' | ||
import { readdir } from 'fs/promises' | ||
@@ -44,5 +44,2 @@ export interface RunnerOptionConfig { | ||
schema?: string | ||
dir: string | ||
ignorePattern?: string | ||
file?: string | ||
createSchema?: boolean | ||
@@ -66,28 +63,26 @@ createMigrationsSchema?: boolean | ||
export class Migration { | ||
public name: string | ||
constructor( | ||
public readonly filePath: string, | ||
public readonly migration: (builder: MigrationBuilder, args: any) => Promise<void>, | ||
) { | ||
this.name = path.basename(filePath, path.extname(filePath)) | ||
} | ||
public readonly name: string, | ||
public readonly migration: (builder: MigrationBuilder, args: any) => Promise<void> | void, | ||
) {} | ||
} | ||
const loadMigrations = async (db: DBConnection, options: RunnerOption) => { | ||
try { | ||
const files = await loadMigrationFiles(options.dir, options.ignorePattern) | ||
return Promise.all( | ||
files.map(async file => { | ||
const filePath = `${options.dir}/${file}` | ||
const actions = | ||
path.extname(filePath) === '.sql' | ||
? (await migrateSqlFile(filePath)).up | ||
: // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require(path.relative(__dirname, filePath)).default | ||
return new Migration(filePath, actions) | ||
}), | ||
export const loadMigrations = async (sqlDir: string, additional: Migration[]): Promise<Migration[]> => { | ||
return ( | ||
await Promise.all( | ||
( | ||
await readdir(sqlDir) | ||
) | ||
.filter(it => path.extname(it) === '.sql') | ||
.map(async it => { | ||
const migration = (await migrateSqlFile(join(sqlDir, it))).up | ||
if (!migration) { | ||
throw new Error() | ||
} | ||
return new Migration(path.basename(it, path.extname(it)), migration) | ||
}), | ||
) | ||
} catch (err) { | ||
throw new Error(`Can't get migration files: ${err.stack}`) | ||
} | ||
) | ||
.concat(additional) | ||
.sort((a, b) => a.name.localeCompare(b.name)) | ||
} | ||
@@ -135,3 +130,3 @@ | ||
const getMigrationsToRun = (options: RunnerOption, runNames: string[], migrations: Migration[]): Migration[] => { | ||
return migrations.filter(({ name }) => runNames.indexOf(name) < 0 && (!options.file || options.file === name)) | ||
return migrations.filter(({ name }) => runNames.indexOf(name) < 0) | ||
} | ||
@@ -163,3 +158,6 @@ | ||
export default async (options: RunnerOption): Promise<{ name: string }[]> => { | ||
export default async ( | ||
getMigrations: () => Promise<Migration[]>, | ||
options: RunnerOption, | ||
): Promise<{ name: string }[]> => { | ||
const logger = getLogger(options) | ||
@@ -184,4 +182,4 @@ const db = Db((options as RunnerOptionClient).dbClient || (options as RunnerOptionUrl).databaseUrl, logger) | ||
const [migrations, runNames] = await Promise.all([loadMigrations(db, options), getRunMigrations(db, options)]) | ||
const runNames = await getRunMigrations(db, options) | ||
const migrations = await getMigrations() | ||
checkOrder(runNames, migrations) | ||
@@ -188,0 +186,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
662
78557