Skip to content

Commit

Permalink
fix(postgres): ignore internal timescale schemas automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Dec 10, 2022
1 parent a572050 commit 85d9083
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
8 changes: 7 additions & 1 deletion packages/postgresql/src/PostgreSqlSchemaHelper.ts
Expand Up @@ -47,7 +47,13 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
...this.platform.getConfig().get('schemaGenerator').ignoreSchema ?? [],
].map(s => this.platform.quoteValue(s)).join(', ');

return `"${column}" not like 'pg_%' and "${column}" not like 'crdb_%' and "${column}" not in (${ignored})`;
const ignoredPrefixes = [
'pg_',
'crdb_',
'_timescaledb_',
].map(p => `"${column}" not like '${p}%'`).join(' and ');

return `${ignoredPrefixes} and "${column}" not in (${ignored})`;
}

async loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions tests/features/migrations/Migrator.postgres.test.ts
Expand Up @@ -417,8 +417,8 @@ test('ensureTable when the schema does not exist', async () => {

const mock = mockLogger(orm);
await storage.ensureTable!(); // ensures the schema first
expect(mock.mock.calls[0][0]).toMatch(`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`);
expect(mock.mock.calls[1][0]).toMatch(`select schema_name from information_schema.schemata where "schema_name" not like 'pg_%' and "schema_name" not like 'crdb_%' and "schema_name" not in ('information_schema', 'tiger', 'topology') order by schema_name`);
expect(mock.mock.calls[0][0]).toMatch(`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 like '_timescaledb_%' 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`);
expect(mock.mock.calls[1][0]).toMatch(`select schema_name from information_schema.schemata where "schema_name" not like 'pg_%' and "schema_name" not like 'crdb_%' and "schema_name" not like '_timescaledb_%' and "schema_name" not in ('information_schema', 'tiger', 'topology') order by schema_name`);
expect(mock.mock.calls[2][0]).toMatch(`create schema "custom2"`);
expect(mock.mock.calls[3][0]).toMatch(`create table "custom2"."mikro_orm_migrations" ("id" serial primary key, "name" varchar(255), "executed_at" timestamptz default current_timestamp)`);
await orm.close();
Expand Down

0 comments on commit 85d9083

Please sign in to comment.