-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cb0491
commit 8d1c946
Showing
5 changed files
with
206 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import logUpdate from 'log-update' | ||
import { BaseCommand } from '@adonisjs/ace' | ||
import { MigratedFileNode, MigratorContract } from '@ioc:Adonis/Lucid/Migrator' | ||
|
||
/** | ||
* Base class to execute migrations and print logs | ||
*/ | ||
export default abstract class MigrationsBase extends BaseCommand { | ||
/** | ||
* Returns beautified log message string | ||
*/ | ||
protected $getLogMessage (file: MigratedFileNode): string { | ||
const message = `${file.migration.name} ${this.colors.gray(`(batch: ${file.batch})`)}` | ||
|
||
if (file.status === 'pending') { | ||
return `${this.colors.yellow('pending')} ${message}` | ||
} | ||
|
||
const lines: string[] = [] | ||
|
||
if (file.status === 'completed') { | ||
lines.push(`${this.colors.green('completed')} ${message}`) | ||
} else { | ||
lines.push(`${this.colors.red('error')} ${message}`) | ||
} | ||
|
||
if (file.queries.length) { | ||
lines.push(' START QUERIES') | ||
lines.push(' ================') | ||
file.queries.forEach((query) => lines.push(` ${query}`)) | ||
lines.push(' ================') | ||
lines.push(' END QUERIES') | ||
} | ||
|
||
return lines.join('\n') | ||
} | ||
|
||
/** | ||
* Runs the migrations using the migrator | ||
*/ | ||
protected async $runMigrations (migrator: MigratorContract) { | ||
/** | ||
* A set of files processed and emitted using event emitter. | ||
*/ | ||
const processedFiles: Set<string> = new Set() | ||
|
||
/** | ||
* Starting to process a new migration file | ||
*/ | ||
migrator.on('migration:start', (file) => { | ||
processedFiles.add(file.migration.name) | ||
logUpdate(this.$getLogMessage(file)) | ||
}) | ||
|
||
/** | ||
* Migration completed | ||
*/ | ||
migrator.on('migration:completed', (file) => { | ||
logUpdate(this.$getLogMessage(file)) | ||
logUpdate.done() | ||
}) | ||
|
||
/** | ||
* Migration error | ||
*/ | ||
migrator.on('migration:error', (file) => { | ||
logUpdate(this.$getLogMessage(file)) | ||
logUpdate.done() | ||
}) | ||
|
||
/** | ||
* Run and close db connection | ||
*/ | ||
await migrator.run() | ||
await migrator.close() | ||
|
||
/** | ||
* Log all pending files. This will happen, when one of the migration | ||
* fails with an error and then the migrator stops emitting events. | ||
*/ | ||
Object.keys(migrator.migratedFiles).forEach((file) => { | ||
if (!processedFiles.has(file)) { | ||
console.log(this.$getLogMessage(migrator.migratedFiles[file])) | ||
} | ||
}) | ||
|
||
/** | ||
* Log final status | ||
*/ | ||
switch (migrator.status) { | ||
case 'skipped': | ||
const message = migrator.direction === 'up' ? 'Already upto date' : 'Already at latest batch' | ||
console.log(this.colors.cyan(message)) | ||
break | ||
case 'error': | ||
this.logger.fatal(migrator.error!) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { flags } from '@adonisjs/ace' | ||
import { inject } from '@adonisjs/fold' | ||
import { DatabaseContract } from '@ioc:Adonis/Lucid/Database' | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application' | ||
|
||
import MigrationsBase from './MigrationsBase' | ||
|
||
/** | ||
* The command is meant to migrate the database by execute migrations | ||
* in `up` direction. | ||
*/ | ||
@inject([null, 'Adonis/Lucid/Database']) | ||
export default class Migrate extends MigrationsBase { | ||
public static commandName = 'migration:rollback' | ||
public static description = 'Rollback migrations to a given batch number' | ||
|
||
@flags.string({ description: 'Define a custom database connection' }) | ||
public connection: string | ||
|
||
@flags.boolean({ description: 'Print SQL queries, instead of running the migrations' }) | ||
public dryRun: boolean | ||
|
||
@flags.number({ | ||
description: 'Define custom batch number for rollback. Use 0 to rollback to initial state', | ||
}) | ||
public batch: number | ||
|
||
/** | ||
* This command loads the application, since we need the runtime | ||
* to find the migration directories for a given connection | ||
*/ | ||
public static settings = { | ||
loadApp: true, | ||
} | ||
|
||
constructor (app: ApplicationContract, private _db: DatabaseContract) { | ||
super(app) | ||
} | ||
|
||
/** | ||
* Handle command | ||
*/ | ||
public async handle () { | ||
const connection = this._db.getRawConnection(this.connection || this._db.primaryConnectionName) | ||
|
||
/** | ||
* Ensure the define connection name does exists in the | ||
* config file | ||
*/ | ||
if (!connection) { | ||
this.logger.error( | ||
`${this.connection} is not a valid connection name. Double check config/database file`, | ||
) | ||
return | ||
} | ||
|
||
/** | ||
* New up migrator | ||
*/ | ||
const { Migrator } = await import('../src/Migrator') | ||
const migrator = new Migrator(this._db, this.application, { | ||
direction: 'down', | ||
batch: this.batch, | ||
connectionName: this.connection, | ||
dryRun: this.dryRun, | ||
}) | ||
|
||
await this.$runMigrations(migrator) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters