lakutata
Version:
An IoC-based universal application framework.
156 lines (151 loc) • 5.44 kB
TypeScript
import { DataSource } from './TypeDef.internal.33.js';
import { QueryRunner } from './TypeDef.internal.40.js';
/**
* Migrations should implement this interface and all its methods.
*/
interface MigrationInterface {
/**
* Optional migration name, defaults to class name.
*/
name?: string;
/**
* Optional flag to determine whether to run the migration in a transaction or not.
* Can only be used when `migrationsTransactionMode` is either "each" or "none"
* Defaults to `true` when `migrationsTransactionMode` is "each"
* Defaults to `false` when `migrationsTransactionMode` is "none"
*/
transaction?: boolean;
/**
* Run the migrations.
*/
up(queryRunner: QueryRunner): Promise<any>;
/**
* Reverse the migrations.
*/
down(queryRunner: QueryRunner): Promise<any>;
}
/**
* Represents entity of the migration in the database.
*/
declare class Migration {
/**
* Migration id.
* Indicates order of the executed migrations.
*/
id: number | undefined;
/**
* Timestamp of the migration.
*/
timestamp: number;
/**
* Name of the migration (class name).
*/
name: string;
/**
* Migration instance that needs to be run.
*/
instance?: MigrationInterface;
/**
* Whether to run this migration within a transaction
*/
transaction?: boolean;
constructor(id: number | undefined, timestamp: number, name: string, instance?: MigrationInterface, transaction?: boolean);
}
/**
* Executes migrations: runs pending and reverts previously executed migrations.
*/
declare class MigrationExecutor {
protected connection: DataSource;
protected queryRunner?: QueryRunner | undefined;
/**
* Indicates how migrations should be run in transactions.
* all: all migrations are run in a single transaction
* none: all migrations are run without a transaction
* each: each migration is run in a separate transaction
*/
transaction: "all" | "none" | "each";
/**
* Option to fake-run or fake-revert a migration, adding to the
* executed migrations table, but not actually running it. This feature is
* useful for when migrations are added after the fact or for
* interoperability between applications which are desired to each keep
* a consistent migration history.
*/
fake: boolean;
private readonly migrationsDatabase?;
private readonly migrationsSchema?;
private readonly migrationsTable;
private readonly migrationsTableName;
constructor(connection: DataSource, queryRunner?: QueryRunner | undefined);
/**
* Tries to execute a single migration given.
*/
executeMigration(migration: Migration): Promise<Migration>;
/**
* Returns an array of all migrations.
*/
getAllMigrations(): Promise<Migration[]>;
/**
* Returns an array of all executed migrations.
*/
getExecutedMigrations(): Promise<Migration[]>;
/**
* Returns an array of all pending migrations.
*/
getPendingMigrations(): Promise<Migration[]>;
/**
* Inserts an executed migration.
*/
insertMigration(migration: Migration): Promise<void>;
/**
* Deletes an executed migration.
*/
deleteMigration(migration: Migration): Promise<void>;
/**
* Lists all migrations and whether they have been executed or not
* returns true if there are unapplied migrations
*/
showMigrations(): Promise<boolean>;
/**
* Executes all pending migrations. Pending migrations are migrations that are not yet executed,
* thus not saved in the database.
*/
executePendingMigrations(): Promise<Migration[]>;
/**
* Reverts last migration that were run.
*/
undoLastMigration(): Promise<void>;
/**
* Creates table "migrations" that will store information about executed migrations.
*/
protected createMigrationsTableIfNotExist(queryRunner: QueryRunner): Promise<void>;
/**
* Loads all migrations that were executed and saved into the database (sorts by id).
*/
protected loadExecutedMigrations(queryRunner: QueryRunner): Promise<Migration[]>;
/**
* Gets all migrations that setup for this connection.
*/
protected getMigrations(): Migration[];
protected checkForDuplicateMigrations(migrations: Migration[]): void;
/**
* Finds the latest migration (sorts by timestamp) in the given array of migrations.
*/
protected getLatestTimestampMigration(migrations: Migration[]): Migration | undefined;
/**
* Finds the latest migration in the given array of migrations.
* PRE: Migration array must be sorted by descending id.
*/
protected getLatestExecutedMigration(sortedMigrations: Migration[]): Migration | undefined;
/**
* Inserts new executed migration's data into migrations table.
*/
protected insertExecutedMigration(queryRunner: QueryRunner, migration: Migration): Promise<void>;
/**
* Delete previously executed migration's data from the migrations table.
*/
protected deleteExecutedMigration(queryRunner: QueryRunner, migration: Migration): Promise<void>;
protected withQueryRunner<T extends any>(callback: (queryRunner: QueryRunner) => T | Promise<T>): Promise<T>;
}
export { Migration, MigrationExecutor };
export type { MigrationInterface };