This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+39
View File
@@ -0,0 +1,39 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
import { MigratedFileNode, MigratorContract } from '@ioc:Adonis/Lucid/Migrator';
/**
* Base class to execute migrations and print logs
*/
export default abstract class MigrationsBase extends BaseCommand {
/**
* Should print one-liner compact output
*/
protected compactOutput: boolean;
/**
* Not a valid connection
*/
protected printNotAValidConnection(connection: string): void;
/**
* Prompts to take consent for running migrations in production
*/
protected takeProductionConstent(): Promise<boolean>;
/**
* Returns beautified log message string
*/
protected printLogMessage(file: MigratedFileNode, direction: 'down' | 'up'): void;
/**
* Pretty print sql queries of a file
*/
private prettyPrintSql;
/**
* Log final status with verbose output
*/
private logVerboseFinalStatus;
/**
* Log final status with compact output
*/
private logCompactFinalStatus;
/**
* Runs the migrations using the migrator
*/
protected runMigrations(migrator: MigratorContract, connectionName: string): Promise<void>;
}
+214
View File
@@ -0,0 +1,214 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pretty_hrtime_1 = __importDefault(require("pretty-hrtime"));
const standalone_1 = require("@adonisjs/core/build/standalone");
const utils_1 = require("../../src/utils");
const prettyPrint_1 = require("../../src/Helpers/prettyPrint");
/**
* Base class to execute migrations and print logs
*/
class MigrationsBase extends standalone_1.BaseCommand {
constructor() {
super(...arguments);
/**
* Should print one-liner compact output
*/
Object.defineProperty(this, "compactOutput", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
}
/**
* Not a valid connection
*/
printNotAValidConnection(connection) {
this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
}
/**
* Prompts to take consent for running migrations in production
*/
async takeProductionConstent() {
/**
* Do not prompt when CLI is not interactive
*/
if (!this.isInteractive) {
return false;
}
const question = 'You are in production environment. Want to continue running migrations?';
try {
return await this.prompt.confirm(question);
}
catch (error) {
return false;
}
}
/**
* Returns beautified log message string
*/
printLogMessage(file, direction) {
const color = file.status === 'pending' ? 'gray' : file.status === 'completed' ? 'green' : 'red';
const arrow = this.colors[color]('');
const message = file.status === 'pending'
? direction === 'up'
? 'migrating'
: 'reverting'
: file.status === 'completed'
? direction === 'up'
? 'migrated'
: 'reverted'
: 'error';
this.logger.logUpdate(`${arrow} ${this.colors[color](message)} ${file.file.name}`);
}
/**
* Pretty print sql queries of a file
*/
prettyPrintSql(file, connectionName) {
console.log(this.logger.colors.gray(`------------- ${file.file.name} -------------`));
console.log();
file.queries.map((sql) => {
(0, prettyPrint_1.prettyPrint)({
connection: connectionName,
sql: sql,
ddl: true,
method: (0, utils_1.getDDLMethod)(sql),
bindings: [],
});
console.log();
});
console.log(this.logger.colors.gray('------------- END -------------'));
}
/**
* Log final status with verbose output
*/
logVerboseFinalStatus(migrator, duration) {
switch (migrator.status) {
case 'completed':
const completionMessage = migrator.direction === 'up' ? 'Migrated in' : 'Reverted in';
console.log(`\n${completionMessage} ${this.colors.cyan((0, pretty_hrtime_1.default)(duration))}`);
break;
case 'skipped':
const message = migrator.direction === 'up' ? 'Already up to date' : 'Already at latest batch';
console.log(this.colors.cyan(message));
break;
case 'error':
this.logger.fatal(migrator.error);
this.exitCode = 1;
break;
}
}
/**
* Log final status with compact output
*/
logCompactFinalStatus(processedFiles, migrator, duration) {
let output = '';
let message = '';
let isUp = migrator.direction === 'up';
switch (migrator.status) {
case 'completed':
message = ` ${isUp ? 'Executed' : 'Reverted'} ${processedFiles.size} migrations`;
output = this.colors.grey(message + ` (${(0, pretty_hrtime_1.default)(duration)})`);
break;
case 'skipped':
message = ` ${isUp ? 'Already up to date' : 'Already at latest batch'}`;
output = this.colors.grey(message);
break;
case 'error':
const skippedMigrations = Object.values(migrator.migratedFiles).filter((file) => file.status === 'pending').length;
message = ` Executed ${processedFiles.size} migrations, 1 error, ${skippedMigrations} skipped`;
console.log(this.colors.red(message));
console.log('\n' + this.colors.red(migrator.error.message));
this.exitCode = 1;
break;
}
this.logger.log(output);
}
/**
* Runs the migrations using the migrator
*/
async runMigrations(migrator, connectionName) {
/**
* Pretty print SQL in dry run and return early
*/
if (migrator.dryRun) {
await migrator.run();
Object.keys(migrator.migratedFiles).forEach((file) => {
this.prettyPrintSql(migrator.migratedFiles[file], connectionName);
});
return;
}
/**
* A set of files processed and emitted using event emitter.
*/
const processedFiles = new Set();
let start;
let duration;
/**
* Starting to process a new migration file
*/
migrator.on('migration:start', (file) => {
processedFiles.add(file.file.name);
if (!this.compactOutput) {
this.printLogMessage(file, migrator.direction);
}
});
/**
* Migration completed
*/
migrator.on('migration:completed', (file) => {
if (!this.compactOutput) {
this.printLogMessage(file, migrator.direction);
this.logger.logUpdatePersist();
}
});
/**
* Migration error
*/
migrator.on('migration:error', (file) => {
if (!this.compactOutput) {
this.printLogMessage(file, migrator.direction);
this.logger.logUpdatePersist();
}
});
/**
* Migration completed
*/
migrator.on('upgrade:version', ({ from, to }) => {
this.logger.info(`Upgrading migrations version from "${from}" to "${to}"`);
});
migrator.on('start', () => (start = process.hrtime()));
migrator.on('end', () => (duration = process.hrtime(start)));
/**
* Run migrations
*/
await migrator.run();
/**
* 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) && !this.compactOutput) {
this.printLogMessage(migrator.migratedFiles[file], migrator.direction);
}
});
if (this.compactOutput) {
this.logCompactFinalStatus(processedFiles, migrator, duration);
}
else {
this.logVerboseFinalStatus(migrator, duration);
}
}
}
exports.default = MigrationsBase;
+60
View File
@@ -0,0 +1,60 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
/**
* This command reset the database by rolling back to batch 0 and then
* re-run all migrations.
*/
export default class Refresh extends BaseCommand {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
/**
* Custom connection for running migrations.
*/
connection: string;
/**
* Force command execution in production
*/
force: boolean;
/**
* Run seeders
*/
seed: boolean;
/**
* Drop all views in database
*/
dropViews: boolean;
/**
* Drop all types in database
*/
dropTypes: boolean;
/**
* Disable advisory locks
*/
disableLocks: boolean;
/**
* Converting command properties to arguments
*/
private getArgs;
/**
* Converting command properties to db:wipe arguments
*/
private getWipeArgs;
/**
* Wipe the database
*/
private runDbWipe;
/**
* Run migrations
*/
private runMigrations;
/**
* Run seeders
*/
private runDbSeed;
/**
* Handle command
*/
run(): Promise<void>;
}
+201
View File
@@ -0,0 +1,201 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
* This command reset the database by rolling back to batch 0 and then
* re-run all migrations.
*/
class Refresh extends standalone_1.BaseCommand {
constructor() {
super(...arguments);
/**
* Custom connection for running migrations.
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Force command execution in production
*/
Object.defineProperty(this, "force", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Run seeders
*/
Object.defineProperty(this, "seed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Drop all views in database
*/
Object.defineProperty(this, "dropViews", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Drop all types in database
*/
Object.defineProperty(this, "dropTypes", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Disable advisory locks
*/
Object.defineProperty(this, "disableLocks", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Converting command properties to arguments
*/
getArgs() {
const args = [];
if (this.force) {
args.push('--force');
}
if (this.connection) {
args.push(`--connection="${this.connection}"`);
}
if (this.disableLocks) {
args.push('--disable-locks');
}
return args;
}
/**
* Converting command properties to db:wipe arguments
*/
getWipeArgs() {
const args = this.getArgs();
if (this.dropTypes) {
args.push('--drop-types');
}
if (this.dropViews) {
args.push('--drop-views');
}
return args;
}
/**
* Wipe the database
*/
async runDbWipe() {
const dbWipe = await this.kernel.exec('db:wipe', this.getWipeArgs());
this.exitCode = dbWipe.exitCode;
this.error = dbWipe.error;
}
/**
* Run migrations
*/
async runMigrations() {
const migrate = await this.kernel.exec('migration:run', this.getArgs());
this.exitCode = migrate.exitCode;
this.error = migrate.error;
}
/**
* Run seeders
*/
async runDbSeed() {
const args = [];
if (this.connection) {
args.push(`--connection="${this.connection}"`);
}
const dbSeed = await this.kernel.exec('db:seed', args);
this.exitCode = dbSeed.exitCode;
this.error = dbSeed.error;
}
/**
* Handle command
*/
async run() {
await this.runDbWipe();
if (this.exitCode) {
return;
}
await this.runMigrations();
if (this.exitCode) {
return;
}
if (this.seed) {
await this.runDbSeed();
}
}
}
Object.defineProperty(Refresh, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:fresh'
});
Object.defineProperty(Refresh, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Drop all tables and re-migrate the database'
});
Object.defineProperty(Refresh, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Refresh.prototype, "connection", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "force", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Run seeders' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "seed", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Drop all views' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "dropViews", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Drop all custom types (Postgres only)' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "dropTypes", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "disableLocks", void 0);
exports.default = Refresh;
+52
View File
@@ -0,0 +1,52 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
/**
* This command reset the database by rolling back to batch 0 and then
* re-run all migrations.
*/
export default class Refresh extends BaseCommand {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
/**
* Custom connection for running migrations.
*/
connection: string;
/**
* Force command execution in production
*/
force: boolean;
/**
* Perform dry run
*/
dryRun: boolean;
/**
* Run seeders
*/
seed: boolean;
/**
* Disable advisory locks
*/
disableLocks: boolean;
/**
* Converting command properties to arguments
*/
private getArgs;
/**
* Reset all migrations
*/
private resetMigrations;
/**
* Run migrations
*/
private runMigrations;
/**
* Run seeders
*/
private runDbSeed;
/**
* Handle command
*/
run(): Promise<void>;
}
+178
View File
@@ -0,0 +1,178 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
* This command reset the database by rolling back to batch 0 and then
* re-run all migrations.
*/
class Refresh extends standalone_1.BaseCommand {
constructor() {
super(...arguments);
/**
* Custom connection for running migrations.
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Force command execution in production
*/
Object.defineProperty(this, "force", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Perform dry run
*/
Object.defineProperty(this, "dryRun", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Run seeders
*/
Object.defineProperty(this, "seed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Disable advisory locks
*/
Object.defineProperty(this, "disableLocks", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Converting command properties to arguments
*/
getArgs() {
const args = [];
if (this.force) {
args.push('--force');
}
if (this.connection) {
args.push(`--connection="${this.connection}"`);
}
if (this.dryRun) {
args.push('--dry-run');
}
if (this.disableLocks) {
args.push('--disable-locks');
}
return args;
}
/**
* Reset all migrations
*/
async resetMigrations() {
const reset = await this.kernel.exec('migration:reset', this.getArgs());
this.exitCode = reset.exitCode;
this.error = reset.error;
}
/**
* Run migrations
*/
async runMigrations() {
const migrate = await this.kernel.exec('migration:run', this.getArgs());
this.exitCode = migrate.exitCode;
this.error = migrate.error;
}
/**
* Run seeders
*/
async runDbSeed() {
const args = [];
if (this.connection) {
args.push(`--connection="${this.connection}"`);
}
const dbSeed = await this.kernel.exec('db:seed', args);
this.exitCode = dbSeed.exitCode;
this.error = dbSeed.error;
}
/**
* Handle command
*/
async run() {
await this.resetMigrations();
if (this.exitCode) {
return;
}
await this.runMigrations();
if (this.exitCode) {
return;
}
if (this.seed) {
await this.runDbSeed();
}
}
}
Object.defineProperty(Refresh, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:refresh'
});
Object.defineProperty(Refresh, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Rollback and migrate database'
});
Object.defineProperty(Refresh, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Refresh.prototype, "connection", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "force", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "dryRun", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Run seeders' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "seed", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "disableLocks", void 0);
exports.default = Refresh;
+36
View File
@@ -0,0 +1,36 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
/**
* This command resets the database by rolling back to batch 0. Same
* as calling "migration:rollback --batch=0"
*/
export default class Reset extends BaseCommand {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
/**
* Custom connection for running migrations.
*/
connection: string;
/**
* Force command execution in production
*/
force: boolean;
/**
* Perform dry run
*/
dryRun: boolean;
/**
* Disable advisory locks
*/
disableLocks: boolean;
/**
* Converting command properties to arguments
*/
private getArgs;
/**
* Handle command
*/
run(): Promise<void>;
}
+129
View File
@@ -0,0 +1,129 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
* This command resets the database by rolling back to batch 0. Same
* as calling "migration:rollback --batch=0"
*/
class Reset extends standalone_1.BaseCommand {
constructor() {
super(...arguments);
/**
* Custom connection for running migrations.
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Force command execution in production
*/
Object.defineProperty(this, "force", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Perform dry run
*/
Object.defineProperty(this, "dryRun", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Disable advisory locks
*/
Object.defineProperty(this, "disableLocks", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Converting command properties to arguments
*/
getArgs() {
const args = ['--batch=0'];
if (this.force) {
args.push('--force');
}
if (this.connection) {
args.push(`--connection="${this.connection}"`);
}
if (this.dryRun) {
args.push('--dry-run');
}
if (this.disableLocks) {
args.push('--disable-locks');
}
return args;
}
/**
* Handle command
*/
async run() {
const rollback = await this.kernel.exec('migration:rollback', this.getArgs());
this.exitCode = rollback.exitCode;
this.error = rollback.error;
}
}
Object.defineProperty(Reset, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:reset'
});
Object.defineProperty(Reset, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Rollback all migrations'
});
Object.defineProperty(Reset, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Reset.prototype, "connection", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
__metadata("design:type", Boolean)
], Reset.prototype, "force", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
__metadata("design:type", Boolean)
], Reset.prototype, "dryRun", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
__metadata("design:type", Boolean)
], Reset.prototype, "disableLocks", void 0);
exports.default = Reset;
+62
View File
@@ -0,0 +1,62 @@
import MigrationsBase from './Base';
/**
* The command is meant to migrate the database by executing migrations
* in `down` direction.
*/
export default class Migrate extends MigrationsBase {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
private migrator;
/**
* Custom connection for running migrations.
*/
connection: string;
/**
* Force run migrations in production
*/
force: boolean;
/**
* Perform dry run
*/
dryRun: boolean;
/**
* Define custom batch, instead of rolling back to the latest batch
*/
batch: number;
/**
* Display migrations result in one compact single-line output
*/
compactOutput: boolean;
/**
* Disable advisory locks
*/
disableLocks: boolean;
/**
* Instantiating the migrator instance
*/
private instantiateMigrator;
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
private runAsSubCommand;
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
private runAsMain;
/**
* Handle command
*/
run(): Promise<void>;
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
completed(): Promise<void>;
}
+215
View File
@@ -0,0 +1,215 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
const Base_1 = __importDefault(require("./Base"));
/**
* The command is meant to migrate the database by executing migrations
* in `down` direction.
*/
class Migrate extends Base_1.default {
constructor() {
super(...arguments);
Object.defineProperty(this, "migrator", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Custom connection for running migrations.
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Force run migrations in production
*/
Object.defineProperty(this, "force", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Perform dry run
*/
Object.defineProperty(this, "dryRun", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Define custom batch, instead of rolling back to the latest batch
*/
Object.defineProperty(this, "batch", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Display migrations result in one compact single-line output
*/
Object.defineProperty(this, "compactOutput", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
/**
* Disable advisory locks
*/
Object.defineProperty(this, "disableLocks", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Instantiating the migrator instance
*/
instantiateMigrator() {
const db = this.application.container.use('Adonis/Lucid/Database');
const Migrator = this.application.container.resolveBinding('Adonis/Lucid/Migrator');
this.migrator = new Migrator(db, this.application, {
direction: 'down',
connectionName: this.connection,
batch: this.batch,
dryRun: this.dryRun,
disableLocks: this.disableLocks,
});
}
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
async runAsSubCommand() {
const db = this.application.container.use('Adonis/Lucid/Database');
this.connection = this.connection || db.primaryConnectionName;
/**
* Continue with migrations when not in prod or force flag
* is passed
*/
let continueMigrations = !this.application.inProduction || this.force;
if (!continueMigrations) {
continueMigrations = await this.takeProductionConstent();
}
/**
* Do not continue when in prod and the prompt was cancelled
*/
if (!continueMigrations) {
return;
}
/**
* Invalid database connection
*/
if (!db.manager.has(this.connection)) {
this.printNotAValidConnection(this.connection);
this.exitCode = 1;
return;
}
this.instantiateMigrator();
await this.runMigrations(this.migrator, this.connection);
}
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
runAsMain() {
return this.runAsSubCommand();
}
/**
* Handle command
*/
async run() {
if (this.isMain) {
await this.runAsMain();
}
else {
await this.runAsSubCommand();
}
}
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
async completed() {
if (this.migrator && this.isMain) {
await this.migrator.close();
}
}
}
Object.defineProperty(Migrate, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:rollback'
});
Object.defineProperty(Migrate, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Rollback migrations to a specific batch number'
});
Object.defineProperty(Migrate, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Migrate.prototype, "connection", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Explictly force to run migrations in production' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "force", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "dryRun", void 0);
__decorate([
standalone_1.flags.number({
description: 'Define custom batch number for rollback. Use 0 to rollback to initial state',
}),
__metadata("design:type", Number)
], Migrate.prototype, "batch", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'A compact single-line output' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "compactOutput", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "disableLocks", void 0);
exports.default = Migrate;
+58
View File
@@ -0,0 +1,58 @@
import MigrationsBase from './Base';
/**
* The command is meant to migrate the database by executing migrations
* in `up` direction.
*/
export default class Migrate extends MigrationsBase {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
private migrator;
/**
* Custom connection for running migrations.
*/
connection: string;
/**
* Force run migrations in production
*/
force: boolean;
/**
* Perform dry run
*/
dryRun: boolean;
/**
* Display migrations result in one compact single-line output
*/
compactOutput: boolean;
/**
* Disable advisory locks
*/
disableLocks: boolean;
/**
* Instantiating the migrator instance
*/
private instantiateMigrator;
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
private runAsSubCommand;
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
private runAsMain;
/**
* Handle command
*/
run(): Promise<void>;
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
completed(): Promise<void>;
}
+199
View File
@@ -0,0 +1,199 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
const Base_1 = __importDefault(require("./Base"));
/**
* The command is meant to migrate the database by executing migrations
* in `up` direction.
*/
class Migrate extends Base_1.default {
constructor() {
super(...arguments);
Object.defineProperty(this, "migrator", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Custom connection for running migrations.
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Force run migrations in production
*/
Object.defineProperty(this, "force", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Perform dry run
*/
Object.defineProperty(this, "dryRun", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Display migrations result in one compact single-line output
*/
Object.defineProperty(this, "compactOutput", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
/**
* Disable advisory locks
*/
Object.defineProperty(this, "disableLocks", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Instantiating the migrator instance
*/
instantiateMigrator() {
const db = this.application.container.use('Adonis/Lucid/Database');
const Migrator = this.application.container.resolveBinding('Adonis/Lucid/Migrator');
this.migrator = new Migrator(db, this.application, {
direction: 'up',
connectionName: this.connection,
dryRun: this.dryRun,
disableLocks: this.disableLocks,
});
}
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
async runAsSubCommand() {
const db = this.application.container.use('Adonis/Lucid/Database');
this.connection = this.connection || db.primaryConnectionName;
/**
* Continue with migrations when not in prod or force flag
* is passed
*/
let continueMigrations = !this.application.inProduction || this.force;
if (!continueMigrations) {
continueMigrations = await this.takeProductionConstent();
}
/**
* Do not continue when in prod and the prompt was cancelled
*/
if (!continueMigrations) {
return;
}
/**
* Invalid database connection
*/
if (!db.manager.has(this.connection)) {
this.printNotAValidConnection(this.connection);
this.exitCode = 1;
return;
}
this.instantiateMigrator();
await this.runMigrations(this.migrator, this.connection);
}
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
async runAsMain() {
await this.runAsSubCommand();
}
/**
* Handle command
*/
async run() {
if (this.isMain) {
await this.runAsMain();
}
else {
await this.runAsSubCommand();
}
}
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
async completed() {
if (this.migrator && this.isMain) {
await this.migrator.close();
}
}
}
Object.defineProperty(Migrate, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:run'
});
Object.defineProperty(Migrate, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Migrate database by running pending migrations'
});
Object.defineProperty(Migrate, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Migrate.prototype, "connection", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Explicitly force to run migrations in production' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "force", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "dryRun", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'A compact single-line output' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "compactOutput", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
__metadata("design:type", Boolean)
], Migrate.prototype, "disableLocks", void 0);
exports.default = Migrate;
+54
View File
@@ -0,0 +1,54 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
/**
* The command is meant to migrate the database by execute migrations
* in `up` direction.
*/
export default class Status extends BaseCommand {
static commandName: string;
static description: string;
static settings: {
loadApp: boolean;
};
private migrator;
/**
* Define custom connection
*/
connection: string;
/**
* Not a valid connection
*/
protected printNotAValidConnection(connection: string): void;
/**
* Colorizes the status string
*/
private colorizeStatus;
/**
* Instantiating the migrator instance
*/
private instantiateMigrator;
/**
* Render list inside a table
*/
private renderList;
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
private runAsSubCommand;
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
private runAsMain;
/**
* Handle command
*/
run(): Promise<void>;
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
completed(): Promise<void>;
}
+165
View File
@@ -0,0 +1,165 @@
"use strict";
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
* The command is meant to migrate the database by execute migrations
* in `up` direction.
*/
class Status extends standalone_1.BaseCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "migrator", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Define custom connection
*/
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Not a valid connection
*/
printNotAValidConnection(connection) {
this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
}
/**
* Colorizes the status string
*/
colorizeStatus(status) {
switch (status) {
case 'pending':
return this.colors.yellow('pending');
case 'migrated':
return this.colors.green('completed');
case 'corrupt':
return this.colors.red('corrupt');
}
}
/**
* Instantiating the migrator instance
*/
instantiateMigrator() {
const db = this.application.container.use('Adonis/Lucid/Database');
const Migrator = this.application.container.resolveBinding('Adonis/Lucid/Migrator');
this.migrator = new Migrator(db, this.application, {
direction: 'up',
connectionName: this.connection,
});
}
/**
* Render list inside a table
*/
renderList(list) {
const table = this.ui.table();
table.head(['Name', 'Status', 'Batch', 'Message']);
/**
* Push a new row to the table
*/
list.forEach((node) => {
table.row([
node.name,
this.colorizeStatus(node.status),
node.batch ? String(node.batch) : 'NA',
node.status === 'corrupt' ? 'The migration file is missing on filesystem' : '',
]);
});
table.render();
}
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
async runAsSubCommand() {
const db = this.application.container.use('Adonis/Lucid/Database');
this.connection = this.connection || db.primaryConnectionName;
/**
* Not a valid connection
*/
if (!db.manager.has(this.connection)) {
this.printNotAValidConnection(this.connection);
this.exitCode = 1;
return;
}
this.instantiateMigrator();
this.renderList(await this.migrator.getList());
}
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
async runAsMain() {
await this.runAsSubCommand();
}
/**
* Handle command
*/
async run() {
if (this.isMain) {
await this.runAsMain();
}
else {
await this.runAsSubCommand();
}
}
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
async completed() {
if (this.migrator && this.isMain) {
await this.migrator.close();
}
}
}
Object.defineProperty(Status, "commandName", {
enumerable: true,
configurable: true,
writable: true,
value: 'migration:status'
});
Object.defineProperty(Status, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'View migrations status'
});
Object.defineProperty(Status, "settings", {
enumerable: true,
configurable: true,
writable: true,
value: {
loadApp: true,
}
});
__decorate([
standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
__metadata("design:type", String)
], Status.prototype, "connection", void 0);
exports.default = Status;