Skip to content

Commit

Permalink
feat(migrations): ensure the database exists when using migrator
Browse files Browse the repository at this point in the history
Closes #1757
  • Loading branch information
B4nan committed Aug 27, 2021
1 parent 2a664d2 commit 02dd67c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/migrations/src/Migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ export class Migrator {

async getExecutedMigrations(): Promise<MigrationRow[]> {
await this.ensureMigrationsDirExists();
await this.schemaGenerator.ensureDatabase();
await this.storage.ensureTable();
return this.storage.getExecutedMigrations();
}

async getPendingMigrations(): Promise<UmzugMigration[]> {
await this.ensureMigrationsDirExists();
await this.schemaGenerator.ensureDatabase();
await this.storage.ensureTable();
return this.umzug.pending();
}
Expand Down Expand Up @@ -262,6 +264,7 @@ export class Migrator {

private async runMigrations(method: 'up' | 'down', options?: string | string[] | MigrateOptions) {
await this.ensureMigrationsDirExists();
await this.schemaGenerator.ensureDatabase();
await this.storage.ensureTable();

if (!this.options.transactional || !this.options.allOrNothing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ export class Migration20191013214813 extends Migration {

exports[`Migrator (postgres) up/down params [all or nothing disabled]: all-or-nothing-disabled 1`] = `
Array [
"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",
"create table \\"mikro_orm_migrations\\" (\\"id\\" serial primary key, \\"name\\" varchar(255), \\"executed_at\\" timestamptz default current_timestamp)",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -608,24 +609,28 @@ Array [
"select 1",
"commit",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"begin",
"commit",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"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",
"begin",
"select 1",
"commit",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"begin",
"commit",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"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",
Expand All @@ -634,8 +639,10 @@ Array [
"select 1",
"commit",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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 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",
Expand All @@ -648,6 +655,7 @@ Array [

exports[`Migrator (postgres) up/down params [all or nothing enabled]: all-or-nothing 1`] = `
Array [
"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",
"create table \\"mikro_orm_migrations\\" (\\"id\\" serial primary key, \\"name\\" varchar(255), \\"executed_at\\" timestamptz default current_timestamp)",
"begin",
Expand All @@ -658,6 +666,7 @@ Array [
"release savepointtrx\\\\d+",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -666,6 +675,7 @@ Array [
"release savepointtrx\\\\d+",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -674,6 +684,7 @@ Array [
"release savepointtrx\\\\d+",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -682,6 +693,7 @@ Array [
"release savepointtrx\\\\d+",
"delete from \\"mikro_orm_migrations\\" where \\"name\\" = $1",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -692,10 +704,12 @@ Array [
"release savepointtrx\\\\d+",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
"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",
"begin",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -711,6 +725,7 @@ Array [
exports[`Migrator (postgres) up/down with explicit transaction: explicit-tx 1`] = `
Array [
"begin",
"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",
"create table \\"mikro_orm_migrations\\" (\\"id\\" serial primary key, \\"name\\" varchar(255), \\"executed_at\\" timestamptz default current_timestamp)",
"select * from \\"mikro_orm_migrations\\" order by \\"id\\" asc",
Expand All @@ -724,20 +739,23 @@ Array [
"select 1",
"release savepointtrx_xx",
"insert into \\"mikro_orm_migrations\\" (\\"name\\") values ($1)",
"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",
"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",
"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",
"commit",
Expand Down
Loading

0 comments on commit 02dd67c

Please sign in to comment.