Skip to content

Commit

Permalink
refactor: ensure db exists before running the schema dump commands
Browse files Browse the repository at this point in the history
Related: #3713
  • Loading branch information
B4nan committed Nov 10, 2022
1 parent dc00374 commit 11756ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
11 changes: 10 additions & 1 deletion packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>

private readonly helper = this.platform.getSchemaHelper()!;
private readonly options = this.config.get('schemaGenerator');
protected lastEnsuredDatabase?: string;

/** @deprecated use `dropSchema` and `createSchema` commands respectively */
async generate(): Promise<string> {
Expand All @@ -36,7 +37,13 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
*/
async ensureDatabase(): Promise<boolean> {
const dbName = this.config.get('dbName')!;

if (this.lastEnsuredDatabase === dbName) {
return true;
}

const exists = await this.helper.databaseExists(this.connection, dbName);
this.lastEnsuredDatabase = dbName;

if (!exists) {
this.config.set('dbName', this.helper.getManagementDbName());
Expand Down Expand Up @@ -87,7 +94,6 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
return this.dropDatabase(name);
}

await this.ensureDatabase();
const sql = await this.getDropSchemaSQL(options);
await this.execute(sql);
}
Expand Down Expand Up @@ -118,6 +124,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
}

async getDropSchemaSQL(options: { wrap?: boolean; dropMigrationsTable?: boolean; schema?: string } = {}): Promise<string> {
await this.ensureDatabase();
const wrap = options.wrap ?? this.options.disableForeignKeys;
const metadata = this.getOrderedMetadata(options.schema).reverse();
const schema = await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema);
Expand Down Expand Up @@ -160,6 +167,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
}

async getUpdateSchemaSQL(options: { wrap?: boolean; safe?: boolean; dropTables?: boolean; fromSchema?: DatabaseSchema; schema?: string } = {}): Promise<string> {
await this.ensureDatabase();
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
const comparator = new SchemaComparator(this.platform);
const diffUp = comparator.compare(fromSchema, toSchema);
Expand All @@ -168,6 +176,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
}

async getUpdateSchemaMigrationSQL(options: { wrap?: boolean; safe?: boolean; dropTables?: boolean; fromSchema?: DatabaseSchema; schema?: string } = {}): Promise<{ up: string; down: string }> {
await this.ensureDatabase();
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
const comparator = new SchemaComparator(this.platform);
const diffUp = comparator.compare(fromSchema, toSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,36 +617,30 @@ exports[`Migrator (postgres) up/down params [all or nothing disabled]: all-or-no
"select 1",
"commit",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"begin",
"commit",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"begin",
"select 1",
"commit",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"begin",
"commit",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"begin",
"select 1",
"commit",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"begin",
Expand All @@ -668,15 +662,13 @@ exports[`Migrator (postgres) up/down params [all or nothing enabled]: all-or-not
"release savepointtrx\\d+",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"savepointtrx\\d+",
"release savepointtrx\\d+",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
Expand All @@ -685,15 +677,13 @@ exports[`Migrator (postgres) up/down params [all or nothing enabled]: all-or-not
"release savepointtrx\\d+",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"savepointtrx\\d+",
"release savepointtrx\\d+",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
Expand All @@ -702,12 +692,10 @@ exports[`Migrator (postgres) up/down params [all or nothing enabled]: all-or-not
"release savepointtrx\\d+",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"commit",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"begin",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
Expand All @@ -734,19 +722,16 @@ exports[`Migrator (postgres) up/down with explicit transaction: explicit-tx 1`]
"select 1",
"release savepointtrx_xx",
"insert into "custom"."mikro_orm_migrations" ("name") values ($1)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"savepointtrx_xx",
"release savepointtrx_xx",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"savepointtrx_xx",
"release savepointtrx_xx",
"delete from "custom"."mikro_orm_migrations" where "name" in ($1, $2)",
"select 1 from pg_database where datname = 'mikro_orm_test_migrations'",
"select table_name, table_schema 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" not like 'crdb_%' and "table_schema" not in ('information_schema', 'tiger', 'topology') and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' order by table_name",
"select * from "custom"."mikro_orm_migrations" order by "id" asc",
"commit",
Expand Down

0 comments on commit 11756ba

Please sign in to comment.