UNPKG

pg-flyway

Version:

Migration tool for PostgreSQL database, NodeJS version of Java migration tool - flyway (not wrapper for https://flywaydb.org/documentation/commandline)

101 lines (100 loc) 6.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrateHandler = migrateHandler; exports.migrate = migrate; const commander_1 = require("commander"); const cosmiconfig_1 = require("cosmiconfig"); const path_1 = require("path"); const default_1 = require("../constants/default"); const env_keys_1 = require("../constants/env-keys"); const migrate_service_1 = require("../services/migrate.service"); const replace_env_1 = require("../utils/replace-env"); async function migrateHandler(options) { let configObjects = []; try { const dir = (0, path_1.dirname)(options.config); const searchFrom = dir[0] === '.' ? (0, path_1.join)(process.cwd(), dir) : dir[0] === path_1.sep ? dir : process.cwd(); const moduleName = (0, path_1.basename)(options.config); const config = await (0, cosmiconfig_1.cosmiconfig)(moduleName, { loaders: cosmiconfig_1.defaultLoaders, }).search(searchFrom); if (config && !config?.isEmpty) { configObjects = Array.isArray(config.config) ? config.config : [config.config]; } } catch (err) { console.error(err); } if (configObjects.length === 0) { const migrateService = new migrate_service_1.MigrateService({ dryRun: (0, replace_env_1.replaceEnv)(options.dryRun) === 'true', historyTable: (0, replace_env_1.replaceEnv)(options.historyTable), historySchema: (0, replace_env_1.replaceEnv)(options.historySchema), databaseUrl: (0, replace_env_1.replaceEnv)(options.databaseUrl || ''), locations: (0, replace_env_1.replaceEnv)(options.locations) .split(',') .map((s) => s.trim()), sqlMigrationSuffixes: (0, replace_env_1.replaceEnv)(options.sqlMigrationSuffixes) .split(',') .map((s) => s.trim()), sqlMigrationSeparator: (0, replace_env_1.replaceEnv)(options.sqlMigrationSeparator), sqlMigrationStatementSeparator: (0, replace_env_1.replaceEnv)(options.sqlMigrationStatementSeparator), }); await migrateService.migrate(); migrateService.destroy(); } else { for (let index = 0; index < configObjects.length; index++) { const configObject = configObjects[index]; const migrateService = new migrate_service_1.MigrateService({ dryRun: (0, replace_env_1.replaceEnv)(configObject.dryRun || options.dryRun) === 'true', historyTable: (0, replace_env_1.replaceEnv)(configObject.historyTable || options.historyTable), historySchema: (0, replace_env_1.replaceEnv)(configObject.historySchema || options.historySchema), databaseUrl: (0, replace_env_1.replaceEnv)(configObject.databaseUrl || options.databaseUrl || ''), locations: (0, replace_env_1.replaceEnv)(configObject.locations || options.locations) .split(',') .map((s) => s.trim()), sqlMigrationSuffixes: (0, replace_env_1.replaceEnv)(configObject.sqlMigrationSuffixes || options.sqlMigrationSuffixes) .split(',') .map((s) => s.trim()), sqlMigrationSeparator: (0, replace_env_1.replaceEnv)(configObject.sqlMigrationSeparator || options.sqlMigrationSeparator), sqlMigrationStatementSeparator: (0, replace_env_1.replaceEnv)(configObject.sqlMigrationStatementSeparator || options.sqlMigrationStatementSeparator), }); await migrateService.migrate(); migrateService.destroy(); } } } function migrate(program) { program .command('migrate') .description('Migrates the schema to the latest version') .addOption(new commander_1.Option('-d,--dry-run <boolean>', 'Show content of migrations without apply them in database') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.dryRun) .env(env_keys_1.PG_FLYWAY_DRY_RUN)) .addOption(new commander_1.Option('-c,--config <string>', 'Configuration file for bulk migrations (example content: [{"databaseUrl":"postgres://${POSTGRES_USER}:POSTGRES_PASSWORD@localhost:POSTGRES_PORT/POSTGRES_DATABASE?schema=public"}], rules: https://github.com/cosmiconfig/cosmiconfig)') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.config) .env(env_keys_1.PG_FLYWAY_CONFIG)) .addOption(new commander_1.Option('-u,--database-url <string>', 'Database url for connect (example: postgres://POSTGRES_USER:POSTGRES_PASSWORD@localhost:POSTGRES_PORT/POSTGRES_DATABASE?schema=public)') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.databaseUrl) .env(env_keys_1.PG_FLYWAY_DATABASE_URL)) .addOption(new commander_1.Option('-l,--locations <strings>', 'Locations with migration files') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.locations) .env(env_keys_1.PG_FLYWAY_LOCATIONS)) .addOption(new commander_1.Option('-h,--history-table <string>', 'History table with states of migration') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.historyTable) .env(env_keys_1.PG_FLYWAY_HISTORY_TABLE)) .addOption(new commander_1.Option('--history-schema <string>', 'History table schema with states of migration') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.historySchema) .env(env_keys_1.PG_FLYWAY_HISTORY_SCHEMA)) .addOption(new commander_1.Option('-s,--sql-migration-suffixes <strings>', 'Extension of migration files') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.sqlMigrationSuffixes) .env(env_keys_1.PG_FLYWAY_SQL_MIGRATION_SUFFIXES)) .addOption(new commander_1.Option('--sql-migration-separator <strings>', 'Version separator (example: V1__Name.sql, sqlMigrationSeparator= "__")') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.sqlMigrationSeparator) .env(env_keys_1.PG_FLYWAY_SQL_MIGRATION_SEPARATOR)) .addOption(new commander_1.Option('--sql-migration-statement-separator <strings>', 'Separator of nested queries within a sql query') .default(default_1.PG_FLYWAY_DEFAULT_MIGRATE_CONFIG.sqlMigrationStatementSeparator) .env(env_keys_1.PG_FLYWAY_SQL_MIGRATION_STATEMENT_SEPARATOR)) .action((options) => migrateHandler(options)); }