Skip to content

Commit

Permalink
fix: table names being handled in a case-sensitive manner
Browse files Browse the repository at this point in the history
  • Loading branch information
aldis-ameriks committed Jan 27, 2024
1 parent f487aed commit 63d16e9
Show file tree
Hide file tree
Showing 12 changed files with 580 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cjs/src/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const typeMapping = {
function getTableDefinitions (sql, schema) {
return sql`
SELECT c.table_name AS name,
obj_description(c.table_name::regclass) AS comment,
obj_description(('"' || c.table_name || '"')::regclass) AS comment,
t.table_type = 'VIEW' AS "isView",
jsonb_agg(
DISTINCT jsonb_build_object(
Expand All @@ -31,7 +31,7 @@ function getTableDefinitions (sql, schema) {
'comment', (
SELECT coalesce(pg_catalog.col_description(pc.oid, c.ordinal_position::int), '')
FROM pg_catalog.pg_class pc
WHERE pc.oid = c.table_name::regclass::oid
WHERE pc.oid = (('"' || c.table_name || '"')::regclass)::oid
AND pc.relname = c.table_name
),
'indices', (
Expand Down
4 changes: 2 additions & 2 deletions cjs/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ t.test('reports success to stdout', t => {
})

child.on('close', () => {
t.equal(result.data, '✔ Generated types from 9 tables and 4 enums\n')
t.equal(result.data, '✔ Generated types from 10 tables and 4 enums\n')
})
})

Expand Down Expand Up @@ -167,7 +167,7 @@ t.test('generates types with header', t => {
})
})

t.only('generates types with pascal case enums', t => {
t.test('generates types with pascal case enums', t => {
t.plan(1)

const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--pascal-enums'], {
Expand Down
8 changes: 8 additions & 0 deletions cjs/test/helpers/setup-postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ module.exports.setupTestPostgres = setupTestPostgres; async function setupTestPo
COMMENT ON COLUMN users.name IS 'Very long long long long long long long long long long long long long long long long long long long long long comment';
`

await sql`
CREATE TABLE IF NOT EXISTS "PascalTableName"
(
id serial4 NOT NULL,
"Name" text NOT NULL
);
`

await sql`
CREATE TABLE IF NOT EXISTS "kebab-test" (
id int4 NOT NULL
Expand Down
4 changes: 2 additions & 2 deletions src/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const typeMapping = {
function getTableDefinitions (sql, schema) {
return sql`
SELECT c.table_name AS name,
obj_description(c.table_name::regclass) AS comment,
obj_description(('"' || c.table_name || '"')::regclass) AS comment,
t.table_type = 'VIEW' AS "isView",
jsonb_agg(
DISTINCT jsonb_build_object(
Expand All @@ -31,7 +31,7 @@ function getTableDefinitions (sql, schema) {
'comment', (
SELECT coalesce(pg_catalog.col_description(pc.oid, c.ordinal_position::int), '')
FROM pg_catalog.pg_class pc
WHERE pc.oid = c.table_name::regclass::oid
WHERE pc.oid = (('"' || c.table_name || '"')::regclass)::oid
AND pc.relname = c.table_name
),
'indices', (
Expand Down
67 changes: 66 additions & 1 deletion tap-snapshots/cjs/test/cli.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
'use strict'
exports['cjs/test/cli.js > TAP > generates table names > must match snapshot 1'] = `
export type Tables = 'address' | 'histories' | 'kebab-test' | 'snake_test' | 'types' | 'users'
export type Tables = 'address' | 'histories' | 'kebab-test' | 'PascalTableName' | 'snake_test' | 'types' | 'users'
export enum DeliciousKebab {
'big-mix' = 'big-mix',
Expand Down Expand Up @@ -54,6 +54,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -232,6 +237,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -410,6 +420,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -587,6 +602,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -765,6 +785,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SomeViewEntity {
test: number | null
test_text: string | null
Expand Down Expand Up @@ -831,6 +856,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1021,6 +1051,16 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface PascalTableNameInsertEntity {
id?: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1322,6 +1362,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1500,6 +1545,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1678,6 +1728,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1856,6 +1911,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null;
};
export interface PascalTableNameEntity {
id: number;
Name: string;
};
export interface SnakeTestEntity {
id: number;
};
Expand Down Expand Up @@ -2034,6 +2094,11 @@ export type MaterializedOtherItemEntity = {
test_text: string | null
}
export type PascalTableNameEntity = {
id: number
Name: string
}
export type SnakeTestEntity = {
id: number
}
Expand Down
88 changes: 87 additions & 1 deletion tap-snapshots/cjs/test/pg-typegen.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,31 @@ Object {
"isView": true,
"name": "materialized_other_items",
},
Object {
"columns": Array [
Object {
"comment": "",
"defaultValue": "nextval('\\"PascalTableName_id_seq\\"'::regclass)",
"hasDefault": true,
"indices": Array [],
"isNullable": false,
"name": "id",
"type": "int4",
},
Object {
"comment": "",
"defaultValue": null,
"hasDefault": false,
"indices": Array [],
"isNullable": false,
"name": "Name",
"type": "text",
},
],
"comment": null,
"isView": false,
"name": "PascalTableName",
},
Object {
"columns": Array [
Object {
Expand Down Expand Up @@ -1342,6 +1367,7 @@ Object {
"address": "AddressInsertEntity",
"histories": "HistoryInsertEntity",
"kebab-test": "KebabTestInsertEntity",
"PascalTableName": "PascalTableNameInsertEntity",
"snake_test": "SnakeTestInsertEntity",
"types": "TypeInsertEntity",
"users": "UserInsertEntity",
Expand All @@ -1352,6 +1378,7 @@ Object {
"kebab-test": "KebabTestEntity",
"materialized_items": "MaterializedItemEntity",
"materialized_other_items": "MaterializedOtherItemEntity",
"PascalTableName": "PascalTableNameEntity",
"snake_test": "SnakeTestEntity",
"some_view": "SomeViewEntity",
"types": "TypeEntity",
Expand Down Expand Up @@ -1416,6 +1443,16 @@ Object {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface PascalTableNameInsertEntity {
id?: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1718,6 +1755,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -1895,6 +1937,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -2026,7 +2073,7 @@ export interface UserEntity {
`

exports['cjs/test/pg-typegen.js > TAP > generates types to file > must match snapshot 2'] = `
✔ Generated types from 9 tables and 4 enums
✔ Generated types from 10 tables and 4 enums
`

exports['cjs/test/pg-typegen.js > TAP > generates types with comments > must match snapshot 1'] = `
Expand Down Expand Up @@ -2076,6 +2123,11 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface SnakeTestEntity {
/**
* PRIMARY KEY
Expand Down Expand Up @@ -2278,6 +2330,16 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface PascalTableNameInsertEntity {
id?: number
Name: string
}
export interface SnakeTestEntity {
/**
* PRIMARY KEY
Expand Down Expand Up @@ -2616,6 +2678,16 @@ export interface MaterializedOtherItemEntity {
test_text: string | null
}
export interface PascalTableNameEntity {
id: number
Name: string
}
export interface PascalTableNameInsertEntity {
id?: number
Name: string
}
export interface SnakeTestEntity {
id: number
}
Expand Down Expand Up @@ -2925,6 +2997,13 @@ export type MaterializedOtherItemEntity = {
'materialized_other_items.test_text'?: string
}
export type PascalTableNameEntity = {
id: number
'PascalTableName.id': number
Name: string
'PascalTableName.Name': string
}
export type SnakeTestEntity = {
id: number
'snake_test.id': number
Expand Down Expand Up @@ -3227,6 +3306,13 @@ export type MaterializedOtherItemEntity = {
'materialized_other_items.test_text': string | null
}
export type PascalTableNameEntity = {
id: number
'PascalTableName.id': number
Name: string
'PascalTableName.Name': string
}
export type SnakeTestEntity = {
id: number
'snake_test.id': number
Expand Down
Loading

0 comments on commit 63d16e9

Please sign in to comment.