Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function main() {
// Execute the migrations
console.log("Running the migrations...");
return migrate.default({
databaseUrl,
migrationsTable,
migrationsSchema,
schema,
direction,
dir
});
}
async _migrateDb() {
let client = new Client(Object.assign({}, config, { database: 'postgres' }));
try {
await client.connect();
let response = await client.query(`select count(*)=1 as has_database from pg_database where datname=$1`, [
config.database,
]);
if (!response.rows[0].has_database) {
await client.query(`create database ${safeDatabaseName(config.database)}`);
}
} finally {
client.end();
}
await migrate({
direction: 'up',
migrationsTable: 'migrations',
singleTransaction: true,
checkOrder: false,
databaseUrl: {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port,
},
dir: join(__dirname, 'migrations'),
log: (...args) => log.debug(...args),
});
}
async _runMigrations() {
let config = this.config;
if (!config.migrationsDir) {
return;
}
let dir = path.join(this.projectPath, config.migrationsDir);
await migrate({
direction: 'up',
migrationsTable: 'migrations',
singleTransaction: true,
checkOrder: false,
databaseUrl: {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port
},
dir,
log: (...args) => log.debug(...args)
});
}
});
async _migrateDb() {
let client = new Client(Object.assign({}, config, { database: 'postgres' }));
try {
await client.connect();
let response = await client.query(`select count(*)=1 as has_database from pg_database where datname=$1`, [config.database]);
if (!response.rows[0].has_database) {
await client.query(`create database ${this.safeDatabaseName(config.database)}`);
}
} finally {
client.end();
}
await migrate({
direction: 'up',
migrationsTable: 'migrations',
singleTransaction: true,
checkOrder: false,
databaseUrl: {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port
},
dir: join(__dirname, 'migrations'),
log: (...args) => log.debug(...args)
});
}
async _migrateDb() {
let client = new Client(Object.assign({}, config, { database: 'postgres' }));
try {
await client.connect();
let response = await client.query(`select count(*)=1 as has_database from pg_database where datname=$1`, [config.database]);
if (!response.rows[0].has_database) {
await client.query(`create database ${safeDatabaseName(config.database)}`);
}
} finally {
client.end();
}
await migrate({
direction: 'up',
migrationsTable: 'migrations',
singleTransaction: true,
checkOrder: false,
databaseUrl: {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port
},
dir: join(__dirname, 'migrations'),
log: (...args) => log.debug(...args)
});
}
private async migrateDb() {
const config = postgresConfig();
let client = new Client(Object.assign({}, config, { database: 'postgres' }));
try {
await client.connect();
let response = await client.query(`select count(*)=1 as has_database from pg_database where datname=$1`, [
config.database,
]);
if (!response.rows[0].has_database) {
await client.query(`create database ${safeName(config.database)}`);
}
} finally {
client.end();
}
await migrate({
direction: 'up',
migrationsTable: 'migrations',
singleTransaction: true,
checkOrder: false,
databaseUrl: {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port,
},
count: Infinity,
ignorePattern: '.*\\.(?!js)[^.]+',
dir: join(__dirname, 'migrations'),
log: (...args) => log.debug(...args),
});
const migrateDatabase = async (event) => {
try {
const dbConnectionConfig = await getDbConnectionConfig()
const dbClient = new Client(dbConnectionConfig)
await dbClient.connect()
const config = {
dbClient,
direction: event.direction || 'up',
dir: 'migrations',
migrationsTable: 'pgmigrations'
}
const migrationResponse = await pgMigrate(config)
await dbClient.end()
if (migrationResponse.length) {
return {
isBase64Encoded: false,
statusCode: 200,
body: JSON.stringify(migrationResponse)
}
}
return {
isBase64Encoded: false,
statusCode: 200,
body: JSON.stringify({ message: 'No migrations to run!' })
}
try {
dbConnectionConfig = await getDbConnectionConfig(dbConnectionConfig)
dbConnectionConfig.database = 'uat-import-test'
const dbClient = new Client(dbConnectionConfig)
await dbClient.connect()
const config = {
dbClient,
direction: event.direction || 'up',
dir: 'migrations',
migrationsTable: 'pgmigrations'
}
const migrationResponse = await pgMigrate(config)
await dbClient.end()
if (migrationResponse.length) {
return {
isBase64Encoded: false,
statusCode: 200,
body: JSON.stringify(migrationResponse)
}
}
return {
isBase64Encoded: false,
statusCode: 200,
body: JSON.stringify({ message: 'No migrations to run!' })
}