Skip to content

Commit

Permalink
Merge pull request #112 from connectedcars/cd/add-dry-run-print-state…
Browse files Browse the repository at this point in the history
…ments-options

Add options dryRun and printStatements to assess what is actually going to run
  • Loading branch information
chribsen authored Jul 20, 2023
2 parents 5de3b8d + 8890df6 commit e61137e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/mysql/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export interface MigrateOptions {
cachePaths?: string[]
ignoreCache?: boolean
subdirectories?: string[]
printStatements?: boolean
dryRun?: boolean
}

export class Migrate {
Expand All @@ -85,7 +87,8 @@ export class Migrate {
private timings: string[] = []
private migrationsPaths: string[]
private subdirectories?: string[]

private printStatements: boolean
private dryRun: boolean
public constructor(options: MigrateOptions) {
this.mysqlClient = options.mysqlClient
this.migrationsPaths = options.migrationsPaths || ['./migrations']
Expand All @@ -96,6 +99,9 @@ export class Migrate {
// Ignore so we don't et unhandled promise rejection
})
this.subdirectories = options.subdirectories

this.printStatements = options.printStatements ?? false
this.dryRun = options.dryRun ?? false
}

public async init(): Promise<void> {
Expand Down Expand Up @@ -280,6 +286,14 @@ export class Migrate {
if (appliedMigrations.find(m => m.timestamp === migration.timestamp && m.name === migrationStatementName)) {
continue
}
if (this.printStatements) {
console.log(`${migration.path} to be applied on ${database}:\n${migrationStatement}\n`)
}

if (this.dryRun) {
continue
}

await this.mysqlClient.query(connection, migrationStatement)
await this.mysqlClient.query(connection, 'INSERT INTO `Migrations` (`timestamp`, `name`) VALUES (?, ?)', [
migration.timestamp,
Expand Down

0 comments on commit e61137e

Please sign in to comment.