Skip to content

Commit

Permalink
feat(migrations): store migrations without extensions
Browse files Browse the repository at this point in the history
Running migrations in production via node and ts-node is now handled the same.
This should actually not be breaking, as old format with extension is still
supported (e.g. they still can be rolled back), but newly logged migrations
will not contain the extension.

Closes #2239
  • Loading branch information
B4nan committed Nov 14, 2021
1 parent 4dd414a commit 4036716
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 30 deletions.
4 changes: 3 additions & 1 deletion docs/docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ title: Migrations
MikroORM has integrated support for migrations via [umzug](https://github.com/sequelize/umzug).
It allows you to generate migrations with current schema differences.

> Since v5, migrations are stored without extension.
By default, each migration will be all executed inside a transaction, and all of them will
be wrapped in one master transaction, so if one of them fails, everything will be rolled back.

Expand Down Expand Up @@ -72,7 +74,7 @@ await MikroORM.init({
migrations: {
tableName: 'mikro_orm_migrations', // name of database table with log of executed transactions
path: './migrations', // path to the folder with migrations
pattern: /^[\w-]+\d+\.ts$/, // regex pattern for the migration files
pattern: /^[\w-]+\d+\.[jt]s$/, // regex pattern for the migration files
transactional: true, // wrap each migration in a transaction
disableForeignKeys: true, // wrap statements with `set foreign_key_checks = 0` or equivalent
allOrNothing: true, // wrap all migrations in master transaction
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/upgrading-v4-to-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ class CustomNamingStrategy extends AbstractNamingStrategy {
Note that in v5 it is possible to use `expr()` helper to access the alias name
dynamically, e.g. ``expr(alias => `lower('${alias}.name')`)``, which should be
now preferred way instead of hardcoding the aliases.

## Migrations are now stored without extensions

Running migrations in production via node and ts-node is now handled the same.
This should actually not be breaking, as old format with extension is still
supported (e.g. they still can be rolled back), but newly logged migrations
will not contain the extension.
2 changes: 1 addition & 1 deletion packages/core/src/utils/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Configuration<D extends IDatabaseDriver = IDatabaseDriver> {
migrations: {
tableName: 'mikro_orm_migrations',
path: './migrations',
pattern: /^[\w-]+\d+\.ts$/,
pattern: /^[\w-]+\d+\.[jt]s$/,
transactional: true,
disableForeignKeys: true,
allOrNothing: true,
Expand Down
20 changes: 18 additions & 2 deletions packages/migrations/src/MigrationStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { MigrationsOptions, Transaction } from '@mikro-orm/core';
import { Utils } from '@mikro-orm/core';
import type { AbstractSqlDriver, Table } from '@mikro-orm/knex';
import type { MigrationRow } from './typings';

Expand All @@ -14,15 +15,25 @@ export class MigrationStorage {

async executed(): Promise<string[]> {
const migrations = await this.getExecutedMigrations();
return migrations.map(row => row.name);
const ext = this.options.emit === 'js' || !Utils.detectTsNode() ? 'js' : 'ts';

return migrations.map(({ name }) => `${this.getMigrationName(name)}.${ext}`);
}

async logMigration(name: string): Promise<void> {
name = this.getMigrationName(name);
await this.driver.nativeInsert(this.options.tableName!, { name }, { ctx: this.masterTransaction });
}

async unlogMigration(name: string): Promise<void> {
await this.driver.nativeDelete(this.options.tableName!, { name }, { ctx: this.masterTransaction });
const withoutExt = this.getMigrationName(name);
const qb = this.knex.delete().from(this.options.tableName!).where('name', 'in', [name, withoutExt]);

if (this.masterTransaction) {
qb.transacting(this.masterTransaction);
}

await this.connection.execute(qb);
}

async getExecutedMigrations(): Promise<MigrationRow[]> {
Expand Down Expand Up @@ -57,4 +68,9 @@ export class MigrationStorage {
delete this.masterTransaction;
}

protected getMigrationName(name: string) {
// strip extension
return name.replace(/\.\w+$/, '');
}

}
2 changes: 1 addition & 1 deletion tests/features/migrations/Migrator.postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('Migrator (postgres)', () => {
await storage.ensureTable(); // creates the table
await storage.logMigration('test');
await expect(storage.getExecutedMigrations()).resolves.toMatchObject([{ name: 'test' }]);
await expect(storage.executed()).resolves.toEqual(['test']);
await expect(storage.executed()).resolves.toEqual(['test.ts']);

await storage.ensureTable(); // table exists, no-op
await storage.unlogMigration('test');
Expand Down
2 changes: 1 addition & 1 deletion tests/features/migrations/Migrator.sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('Migrator (sqlite)', () => {
await storage.ensureTable(); // creates the table
await storage.logMigration('test');
await expect(storage.getExecutedMigrations()).resolves.toMatchObject([{ name: 'test' }]);
await expect(storage.executed()).resolves.toEqual(['test']);
await expect(storage.executed()).resolves.toEqual(['test.ts']);

await storage.ensureTable(); // table exists, no-op
await storage.unlogMigration('test');
Expand Down
4 changes: 2 additions & 2 deletions tests/features/migrations/Migrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ describe('Migrator', () => {
const storage = migrator.storage;

await storage.ensureTable(); // creates the table
await storage.logMigration('test');
await storage.logMigration('test.ts'); // can have extension
await expect(storage.getExecutedMigrations()).resolves.toMatchObject([{ name: 'test' }]);
await expect(storage.executed()).resolves.toEqual(['test']);
await expect(storage.executed()).resolves.toEqual(['test.ts']);

await storage.ensureTable(); // table exists, no-op
await storage.unlogMigration('test');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"begin",
"commit",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -691,7 +691,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"begin",
"commit",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -711,7 +711,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"begin",
"commit",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
]
`;
Expand All @@ -735,7 +735,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
Expand All @@ -753,7 +753,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
Expand All @@ -779,7 +779,7 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"commit",
]
`;
Expand Down Expand Up @@ -808,15 +808,15 @@ Array [
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"savepointtrx_xx",
"release savepointtrx_xx",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"savepointtrx_xx",
"release savepointtrx_xx",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test'",
"select table_name, nullif(table_schema, 'public') as schema_name, (select pg_catalog.obj_description(c.oid) from pg_catalog.pg_class c where c.oid = (select ('\\"' || table_schema || '\\".\\"' || table_name || '\\"')::regclass::oid) and c.relname = table_name) as table_comment from information_schema.tables where table_schema not like 'pg_%' and table_schema != 'information_schema' and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"begin",
"commit",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
"select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name",
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"begin",
Expand All @@ -254,7 +254,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"begin",
"commit",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
"select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name",
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
Expand All @@ -271,7 +271,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"begin",
"commit",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
]
`;

Expand All @@ -293,7 +293,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
"commit",
"select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name",
"begin",
Expand All @@ -309,7 +309,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
"commit",
"select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name",
"begin",
Expand All @@ -332,7 +332,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc",
"savepointtrx\\\\d+",
"release savepointtrx\\\\d+",
"delete from \`mikro_orm_migrations\` where \`name\` = ?",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?)",
"commit",
]
`;
16 changes: 8 additions & 8 deletions tests/features/migrations/__snapshots__/Migrator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"begin (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
Expand All @@ -876,7 +876,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"begin (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
Expand All @@ -896,7 +896,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"begin (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
]
`;

Expand All @@ -920,7 +920,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"release savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
Expand All @@ -938,7 +938,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"release savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
Expand All @@ -964,7 +964,7 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"release savepointtrx\\\\d+ (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"commit (via write connection '127.0.0.1')",
]
`;
Expand Down Expand Up @@ -993,15 +993,15 @@ Array [
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"savepointtrx_xx (via write connection '127.0.0.1')",
"release savepointtrx_xx (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
"savepointtrx_xx (via write connection '127.0.0.1')",
"release savepointtrx_xx (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` = ? (via write connection '127.0.0.1')",
"delete from \`mikro_orm_migrations\` where \`name\` in (?, ?) (via write connection '127.0.0.1')",
"select 1 from information_schema.schemata where schema_name = 'mikro_orm_test' (via write connection '127.0.0.1')",
"select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema() (via write connection '127.0.0.1')",
"select * from \`mikro_orm_migrations\` order by \`id\` asc (via write connection '127.0.0.1')",
Expand Down

0 comments on commit 4036716

Please sign in to comment.