@contember/database-migrations
Advanced tools
Comparing version 0.10.0-alpha.2 to 0.10.0-alpha.3
@@ -10,3 +10,3 @@ import { MigrationBuilder } from 'node-pg-migrate'; | ||
export declare function createPgClient(cfg: ClientConfig): Promise<Client>; | ||
export declare const createDatabaseIfNotExists: (db: DatabaseCredentials) => Promise<void>; | ||
export declare const createDatabaseIfNotExists: (db: DatabaseCredentials, log: (message: string) => void) => Promise<void>; | ||
//# sourceMappingURL=helpers.d.ts.map |
@@ -44,3 +44,3 @@ "use strict"; | ||
exports.createPgClient = createPgClient; | ||
exports.createDatabaseIfNotExists = async (db) => { | ||
exports.createDatabaseIfNotExists = async (db, log) => { | ||
const Connection = (await Promise.resolve().then(() => __importStar(require('@contember/database')))).Connection; | ||
@@ -50,4 +50,3 @@ const connection = new Connection({ ...db, database: 'postgres' }, {}); | ||
if (result.rowCount === 0) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Database ${db.database} does not exist, attempting to create it...`); | ||
log(`Database ${db.database} does not exist, attempting to create it...`); | ||
await connection.query(`CREATE DATABASE ${wrapIdentifier(db.database)}`); | ||
@@ -54,0 +53,0 @@ } |
@@ -9,5 +9,7 @@ import { DatabaseCredentials } from '@contember/database'; | ||
constructor(db: DatabaseCredentials, schema: string, dir: string, dbClient?: ClientBase | undefined); | ||
migrate<MigrationArgs>(log?: boolean, migrationArgs?: MigrationArgs): Promise<void>; | ||
migrate<MigrationArgs>(log: (msg: string) => void, migrationArgs?: MigrationArgs): Promise<{ | ||
name: string; | ||
}[]>; | ||
private createDatabaseIfNotExists; | ||
} | ||
//# sourceMappingURL=MigrationsRunner.d.ts.map |
@@ -31,4 +31,4 @@ "use strict"; | ||
} | ||
async migrate(log = true, migrationArgs) { | ||
await this.createDatabaseIfNotExists(); | ||
async migrate(log, migrationArgs) { | ||
await this.createDatabaseIfNotExists(log); | ||
const dbParams = this.dbClient | ||
@@ -38,3 +38,3 @@ ? { dbClient: this.dbClient } | ||
const migrate = (await Promise.resolve().then(() => __importStar(require('./runner')))).default; | ||
await migrate({ | ||
return await migrate({ | ||
...dbParams, | ||
@@ -47,10 +47,7 @@ dir: this.dir, | ||
migrationArgs, | ||
log: (msg) => { | ||
// eslint-disable-next-line no-console | ||
log && console.log(msg); | ||
}, | ||
log, | ||
}); | ||
} | ||
async createDatabaseIfNotExists() { | ||
await helpers_1.createDatabaseIfNotExists(this.db); | ||
async createDatabaseIfNotExists(log) { | ||
await helpers_1.createDatabaseIfNotExists(this.db, log); | ||
} | ||
@@ -57,0 +54,0 @@ } |
@@ -146,19 +146,23 @@ "use strict"; | ||
if (!toRun.length) { | ||
logger.info('No migrations to run!'); | ||
return []; | ||
} | ||
logger.info('Migrating files:'); | ||
toRun.forEach(m => { | ||
logger.info(`- ${m.name}`); | ||
}); | ||
logger.info(`Migrating ${toRun.length} file(s):`); | ||
await db.query('BEGIN'); | ||
try { | ||
for (const migration of toRun) { | ||
const migrationsBuilder = helpers_1.createMigrationBuilder(); | ||
await migration.migration(migrationsBuilder, options.migrationArgs); | ||
const steps = migrationsBuilder.getSqlSteps(); | ||
for (const sql of steps) { | ||
await db.query(sql); | ||
try { | ||
logger.info(` Executing migration ${migration.name}...`); | ||
const migrationsBuilder = helpers_1.createMigrationBuilder(); | ||
await migration.migration(migrationsBuilder, options.migrationArgs); | ||
const steps = migrationsBuilder.getSqlSteps(); | ||
for (const sql of steps) { | ||
await db.query(sql); | ||
} | ||
await db.query(`INSERT INTO ${getMigrationsTableName(options)} (${nameColumn}, ${runOnColumn}) VALUES ('${migration.name}', NOW());`); | ||
logger.info(` Done`); | ||
} | ||
await db.query(`INSERT INTO ${getMigrationsTableName(options)} (${nameColumn}, ${runOnColumn}) VALUES ('${migration.name}', NOW());`); | ||
catch (e) { | ||
logger.warn(` FAILED`); | ||
throw e; | ||
} | ||
} | ||
@@ -178,5 +182,5 @@ await db.query('COMMIT'); | ||
await unlock(db).catch(error => logger.warn(error.message)); | ||
db.close(); | ||
await db.close(); | ||
} | ||
}; | ||
//# sourceMappingURL=runner.js.map |
{ | ||
"name": "@contember/database-migrations", | ||
"version": "0.10.0-alpha.2", | ||
"version": "0.10.0-alpha.3", | ||
"scripts": { | ||
@@ -11,3 +11,3 @@ "test": "echo 'No tests'" | ||
"dependencies": { | ||
"@contember/database": "^0.10.0-alpha.2", | ||
"@contember/database": "^0.10.0-alpha.3", | ||
"node-pg-migrate": "^5.9.0" | ||
@@ -14,0 +14,0 @@ }, |
@@ -31,3 +31,3 @@ import { MigrationBuilder } from 'node-pg-migrate' | ||
export const createDatabaseIfNotExists = async (db: DatabaseCredentials) => { | ||
export const createDatabaseIfNotExists = async (db: DatabaseCredentials, log: (message: string) => void) => { | ||
const Connection = (await import('@contember/database')).Connection | ||
@@ -38,4 +38,3 @@ const connection = new Connection({ ...db, database: 'postgres' }, {}) | ||
if (result.rowCount === 0) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Database ${db.database} does not exist, attempting to create it...`) | ||
log(`Database ${db.database} does not exist, attempting to create it...`) | ||
await connection.query(`CREATE DATABASE ${wrapIdentifier(db.database)}`) | ||
@@ -42,0 +41,0 @@ } |
@@ -14,4 +14,7 @@ import { DatabaseCredentials } from '@contember/database' | ||
public async migrate<MigrationArgs>(log: boolean = true, migrationArgs?: MigrationArgs) { | ||
await this.createDatabaseIfNotExists() | ||
public async migrate<MigrationArgs>( | ||
log: (msg: string) => void, | ||
migrationArgs?: MigrationArgs, | ||
): Promise<{ name: string }[]> { | ||
await this.createDatabaseIfNotExists(log) | ||
const dbParams: RunnerOptionClient | RunnerOptionUrl = this.dbClient | ||
@@ -21,3 +24,3 @@ ? { dbClient: this.dbClient } | ||
const migrate = (await import('./runner')).default | ||
await migrate({ | ||
return await migrate({ | ||
...dbParams, | ||
@@ -30,12 +33,9 @@ dir: this.dir, | ||
migrationArgs, | ||
log: (msg: string) => { | ||
// eslint-disable-next-line no-console | ||
log && console.log(msg) | ||
}, | ||
log, | ||
}) | ||
} | ||
private async createDatabaseIfNotExists() { | ||
await createDatabaseIfNotExists(this.db) | ||
private async createDatabaseIfNotExists(log: (msg: string) => void) { | ||
await createDatabaseIfNotExists(this.db, log) | ||
} | ||
} |
@@ -185,10 +185,6 @@ /* | ||
if (!toRun.length) { | ||
logger.info('No migrations to run!') | ||
return [] | ||
} | ||
logger.info('Migrating files:') | ||
toRun.forEach(m => { | ||
logger.info(`- ${m.name}`) | ||
}) | ||
logger.info(`Migrating ${toRun.length} file(s):`) | ||
@@ -198,15 +194,22 @@ await db.query('BEGIN') | ||
for (const migration of toRun) { | ||
const migrationsBuilder = createMigrationBuilder() | ||
await migration.migration(migrationsBuilder, options.migrationArgs) | ||
const steps = migrationsBuilder.getSqlSteps() | ||
try { | ||
logger.info(` Executing migration ${migration.name}...`) | ||
const migrationsBuilder = createMigrationBuilder() | ||
await migration.migration(migrationsBuilder, options.migrationArgs) | ||
const steps = migrationsBuilder.getSqlSteps() | ||
for (const sql of steps) { | ||
await db.query(sql) | ||
for (const sql of steps) { | ||
await db.query(sql) | ||
} | ||
await db.query( | ||
`INSERT INTO ${getMigrationsTableName(options)} (${nameColumn}, ${runOnColumn}) VALUES ('${ | ||
migration.name | ||
}', NOW());`, | ||
) | ||
logger.info(` Done`) | ||
} catch (e) { | ||
logger.warn(` FAILED`) | ||
throw e | ||
} | ||
await db.query( | ||
`INSERT INTO ${getMigrationsTableName(options)} (${nameColumn}, ${runOnColumn}) VALUES ('${ | ||
migration.name | ||
}', NOW());`, | ||
) | ||
} | ||
@@ -225,4 +228,4 @@ await db.query('COMMIT') | ||
await unlock(db).catch(error => logger.warn(error.message)) | ||
db.close() | ||
await db.close() | ||
} | ||
} |
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
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
322927
644