Skip to content

Commit

Permalink
fix(schema): fix diffing tables in other than default schema
Browse files Browse the repository at this point in the history
Closes #1142
Closes #1143
  • Loading branch information
B4nan committed Dec 2, 2020
1 parent dde119f commit 429d832
Show file tree
Hide file tree
Showing 7 changed files with 1,035 additions and 18 deletions.
16 changes: 8 additions & 8 deletions packages/knex/src/schema/DatabaseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class DatabaseSchema {

private readonly tables: DatabaseTable[] = [];

addTable(name: string, schema: string | undefined): DatabaseTable {
const table = new DatabaseTable(name, schema);
addTable(name: string, schema: string | undefined | null): DatabaseTable {
const table = new DatabaseTable(name, schema ?? undefined);
this.tables.push(table);

return table;
Expand All @@ -20,7 +20,7 @@ export class DatabaseSchema {
}

getTable(name: string): DatabaseTable | undefined {
return this.tables.find(t => t.name === name);
return this.tables.find(t => t.name === name || `${t.schema}.${t.name}` === name);
}

static async create(connection: AbstractSqlConnection, helper: SchemaHelper, config: Configuration) {
Expand All @@ -33,11 +33,11 @@ export class DatabaseSchema {
}

const table = schema.addTable(t.table_name, t.schema_name);
const cols = await helper.getColumns(connection, t.table_name, t.schema_name);
const indexes = await helper.getIndexes(connection, t.table_name, t.schema_name);
const pks = await helper.getPrimaryKeys(connection, indexes, t.table_name, t.schema_name);
const fks = await helper.getForeignKeys(connection, t.table_name, t.schema_name);
const enums = await helper.getEnumDefinitions(connection, t.table_name, t.schema_name);
const cols = await helper.getColumns(connection, table.name, table.schema);
const indexes = await helper.getIndexes(connection, table.name, table.schema);
const pks = await helper.getPrimaryKeys(connection, indexes, table.name, table.schema);
const fks = await helper.getForeignKeys(connection, table.name, table.schema);
const enums = await helper.getEnumDefinitions(connection, table.name, table.schema);
table.init(cols, indexes, pks, fks, enums);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export class SchemaGenerator {
}

const definedTables = metadata.map(meta => meta.collection);
const remove = schema.getTables().filter(table => !definedTables.includes(table.name));
const remove = schema.getTables().filter(table => !definedTables.includes(table.name) && !definedTables.includes(`${table.schema}.${table.name}`));

for (const table of remove) {
ret += this.dump(this.dropTable(table.name));
ret += this.dump(this.dropTable(table.name, table.schema));
}

return this.wrapSchema(ret, wrap);
Expand Down Expand Up @@ -333,8 +333,9 @@ export class SchemaGenerator {
}
}

private dropTable(name: string): SchemaBuilder {
let builder = this.knex.schema.dropTableIfExists(name);
private dropTable(name: string, schema?: string): SchemaBuilder {
/* istanbul ignore next */
let builder = this.knex.schema.dropTableIfExists(schema ? `${schema}.${name}` : name);

if (this.platform.usesCascadeStatement()) {
builder = this.knex.schema.raw(builder.toQuery() + ' cascade');
Expand Down
12 changes: 6 additions & 6 deletions packages/postgresql/src/PostgreSqlSchemaHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
}

getListTablesSQL(): string {
return 'select table_name, table_schema as schema_name '
+ `from information_schema.tables where table_schema not like 'pg_%' and table_schema = current_schema() `
return `select table_name, nullif(table_schema, 'public') as schema_name `
+ `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`;
}

async getColumns(connection: AbstractSqlConnection, tableName: string, schemaName: string): Promise<any[]> {
async getColumns(connection: AbstractSqlConnection, tableName: string, schemaName = 'public'): Promise<any[]> {
const sql = `select column_name, column_default, is_nullable, udt_name, coalesce(datetime_precision, character_maximum_length) length, data_type
from information_schema.columns where table_schema = '${schemaName}' and table_name = '${tableName}'`;
const columns = await connection.execute<any[]>(sql);
Expand All @@ -97,7 +97,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
}));
}

getForeignKeysSQL(tableName: string, schemaName: string): string {
getForeignKeysSQL(tableName: string, schemaName = 'public'): string {
return `select kcu.table_name as table_name, rel_kcu.table_name as referenced_table_name, kcu.column_name as column_name,
rel_kcu.column_name as referenced_column_name, kcu.constraint_name, rco.update_rule, rco.delete_rule
from information_schema.table_constraints tco
Expand All @@ -115,7 +115,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
order by kcu.table_schema, kcu.table_name, kcu.ordinal_position`;
}

async getEnumDefinitions(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Dictionary> {
async getEnumDefinitions(connection: AbstractSqlConnection, tableName: string, schemaName = 'public'): Promise<Dictionary> {
const sql = `select conrelid::regclass as table_from, conname, pg_get_constraintdef(c.oid) as enum_def
from pg_constraint c join pg_namespace n on n.oid = c.connamespace
where contype = 'c' and conrelid = '"${schemaName}"."${tableName}"'::regclass order by contype`;
Expand Down Expand Up @@ -167,7 +167,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
return 'postgres';
}

private getIndexesSQL(tableName: string, schemaName: string): string {
private getIndexesSQL(tableName: string, schemaName = 'public'): string {
return `select relname as constraint_name, attname as column_name, idx.indisunique as unique, idx.indisprimary as primary
from pg_index idx
left join pg_class AS i on i.oid = idx.indexrelid
Expand Down
Loading

0 comments on commit 429d832

Please sign in to comment.