Skip to content

Commit

Permalink
fix(postgres): handle case sensitive table names correctly
Browse files Browse the repository at this point in the history
Closes #472
  • Loading branch information
B4nan committed Apr 10, 2020
1 parent 886e4a1 commit aa3a087
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/schema/PostgreSqlSchemaHelper.ts
Expand Up @@ -114,7 +114,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
async getEnumDefinitions(connection: AbstractSqlConnection, tableName: string, schemaName?: string): 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`;
where contype = 'c' and conrelid = '"${schemaName}"."${tableName}"'::regclass order by contype`;
const enums = await connection.execute<any[]>(sql);

return enums.reduce((o, item) => {
Expand Down Expand Up @@ -167,7 +167,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
from pg_index idx
left join pg_class AS i on i.oid = idx.indexrelid
left join pg_attribute a on a.attrelid = idx.indrelid and a.attnum = ANY(idx.indkey) and a.attnum > 0
where indrelid = '${schemaName}.${tableName}'::regclass`;
where indrelid = '"${schemaName}"."${tableName}"'::regclass`;
}

}
40 changes: 40 additions & 0 deletions tests/issues/GH472.test.ts
@@ -0,0 +1,40 @@
import { Entity, PrimaryKey, Property, MikroORM, ReflectMetadataProvider, EntityCaseNamingStrategy } from '../../lib';
import { SqliteDriver } from '../../lib/drivers/SqliteDriver';

@Entity()
class A {

@PrimaryKey()
id!: string;

@Property()
prop?: string;

}

describe('GH issue 472', () => {

let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A],
dbName: 'mikro_orm_test_gh472',
type: 'postgresql',
metadataProvider: ReflectMetadataProvider,
namingStrategy: EntityCaseNamingStrategy,
cache: { enabled: false },
});
await orm.getSchemaGenerator().ensureDatabase();
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});

afterAll(async () => await orm.close(true));

test(`case sensitive table names`, async () => {
await expect(orm.getSchemaGenerator().updateSchema()).resolves.toBeUndefined();
await orm.getSchemaGenerator().dropDatabase(orm.config.get('dbName'));
});

});

0 comments on commit aa3a087

Please sign in to comment.