Skip to content

Commit e996e3e

Browse files
committed
refactor: update ID fields for the types
1 parent e2e82ea commit e996e3e

File tree

14 files changed

+95
-100
lines changed

14 files changed

+95
-100
lines changed

src/api/tables.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ primary_keys AS ( ${primary_keys} ),
1818
relationships AS ( ${relationships} )
1919
SELECT
2020
*,
21-
${coalesceRowsToArray(
22-
'columns',
23-
'SELECT * FROM columns WHERE columns.table_id = tables.table_id'
24-
)},
25-
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.table_id = tables.table_id')},
21+
${coalesceRowsToArray('columns', 'SELECT * FROM columns WHERE columns.table_id = tables.id')},
22+
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.table_id = tables.id')},
2623
${coalesceRowsToArray(
2724
'primary_keys',
28-
'SELECT * FROM primary_keys WHERE primary_keys.table_id = tables.table_id'
25+
'SELECT * FROM primary_keys WHERE primary_keys.table_id = tables.id'
2926
)},
3027
${coalesceRowsToArray(
3128
'relationships',
@@ -34,8 +31,8 @@ SELECT
3431
FROM
3532
relationships
3633
WHERE
37-
relationships.source_table_id = tables.table_id
38-
OR relationships.target_table_id = tables.table_id`
34+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
35+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
3936
)}
4037
FROM
4138
tables`

src/lib/interfaces.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export namespace Roles {
1818
}
1919

2020
export interface Grant {
21-
table_id: string
21+
table_id: number
2222
grantor: string
2323
grantee: string
2424
catalog: string
@@ -32,6 +32,7 @@ export namespace Roles {
3232

3333
export namespace Schemas {
3434
export interface Schema {
35+
id: number
3536
catalog_name: string
3637
name: string
3738
owner: string
@@ -44,7 +45,7 @@ export namespace Schemas {
4445

4546
export namespace Tables {
4647
export interface Table {
47-
table_id: string
48+
id: string
4849
catalog: string
4950
schema: string
5051
name: string
@@ -81,19 +82,19 @@ export namespace Tables {
8182
}
8283

8384
export interface Column {
85+
table_id: number
8486
schema: string
8587
table: string
88+
ordinal_position: number
8689
name: string
8790
default_value: string | null
88-
is_identity: boolean
89-
is_nullable: boolean
90-
is_updatable: boolean
9191
data_type: string
9292
format: string
93-
identity_generation: string | null
94-
table_id: string
9593
description: string | null
96-
enums: string[]
94+
is_identity: boolean
95+
identity_generation: string | null
96+
is_nullable: boolean
97+
is_updatable: boolean
9798
}
9899

99100
export interface PrimaryKey {
@@ -104,11 +105,9 @@ export namespace Tables {
104105
}
105106

106107
export interface Relationship {
107-
source_table_id: string
108108
source_schema: string
109109
source_table_name: string
110110
source_column_name: string
111-
target_table_id: string
112111
target_table_schema: string
113112
target_table_name: string
114113
target_column_name: string
@@ -118,7 +117,7 @@ export namespace Tables {
118117

119118
export namespace Types {
120119
export interface Type {
121-
type_id: string
120+
id: number
122121
name: string
123122
schema: string
124123
format: string

src/lib/sql/columns.sql

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1+
-- We can uniquely point to a specific column using table_id + ordinal_position.
12
SELECT
3+
c.oid AS table_id,
24
table_schema AS schema,
35
table_name AS table,
6+
ordinal_position,
47
column_name AS name,
58
column_default AS default_value,
6-
is_identity :: boolean,
7-
is_nullable :: boolean,
8-
is_updatable :: boolean,
99
data_type,
1010
udt_name AS format,
11-
identity_generation,
12-
(table_schema || '.' || table_name) AS table_id,
1311
col_description(
14-
(table_schema || '."' || table_name || '"') :: regclass,
12+
c.oid,
1513
ordinal_position
1614
) AS description,
17-
array_to_json(
18-
array(
19-
SELECT
20-
enumlabel
21-
FROM
22-
pg_catalog.pg_enum enums
23-
WHERE
24-
udt_name = pg_catalog.Format_type(enums.enumtypid :: regclass, NULL)
25-
ORDER BY
26-
enums.enumsortorder
27-
)
28-
) AS enums
15+
is_identity::boolean,
16+
identity_generation,
17+
is_nullable::boolean,
18+
is_updatable::boolean
2919
FROM
3020
information_schema.columns
21+
JOIN pg_class c ON c.relnamespace = table_schema::text::regnamespace
22+
AND c.relname = table_name::text

src/lib/sql/constraints.sql

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SELECT
2+
c.oid AS id,
23
COALESCE(table_schema, referenced_schema) AS table_schema,
34
COALESCE(table_name, referenced_table) AS table_name,
45
COALESCE(column_name, referenced_column) AS column_name,
@@ -10,9 +11,13 @@ SELECT
1011
referenced_table,
1112
referenced_column
1213
FROM
13-
information_schema.table_constraints NATURAL FULL
14-
JOIN information_schema.key_column_usage NATURAL FULL
15-
JOIN information_schema.check_constraints
14+
information_schema.table_constraints
15+
-- This doesn't guarantee uniqueness, but works for now
16+
JOIN pg_constraint c ON c.connamespace = table_schema::text::regnamespace
17+
AND c.conrelid = table_name::text::regclass
18+
AND c.conname = constraint_name
19+
NATURAL FULL JOIN information_schema.key_column_usage
20+
NATURAL FULL JOIN information_schema.check_constraints
1621
INNER JOIN (
1722
SELECT
1823
table_schema AS referenced_schema,
@@ -22,8 +27,6 @@ FROM
2227
FROM
2328
information_schema.constraint_column_usage
2429
) AS referenced_columns USING (constraint_name)
25-
WHERE
26-
constraint_schema = ?
2730
ORDER BY
2831
table_schema,
2932
table_name,

src/lib/sql/extensions_enabled.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SELECT
2+
e.oid AS id,
23
e.extname AS name,
34
e.extversion AS version,
45
n.nspname AS schema,

src/lib/sql/grants.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SELECT
2-
(table_schema || '.' || table_name) AS table_id,
2+
c.oid AS table_id,
33
grantor,
44
grantee,
55
table_catalog AS catalog,
@@ -10,3 +10,5 @@ SELECT
1010
with_hierarchy :: boolean
1111
FROM
1212
information_schema.role_table_grants
13+
INNER JOIN pg_class c ON table_schema::text::regnamespace = c.relnamespace
14+
AND table_name::text = c.relname

src/lib/sql/primary_keys.sql

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
SELECT
2-
pg_namespace.nspname AS schema,
3-
pg_class.oid :: regclass AS table_name,
4-
pg_attribute.attname AS name,
5-
(
6-
pg_namespace.nspname || '.' || (pg_class.oid :: regclass)
7-
) AS table_id
2+
n.nspname AS schema,
3+
c.oid :: regclass AS table_name,
4+
a.attname AS name,
5+
c.oid AS table_id
86
FROM
9-
pg_index,
10-
pg_class,
11-
pg_attribute,
12-
pg_namespace
7+
pg_index i,
8+
pg_class c,
9+
pg_attribute a,
10+
pg_namespace n
1311
WHERE
14-
indrelid = pg_class.oid
15-
AND pg_class.relnamespace = pg_namespace.oid
16-
AND pg_attribute.attrelid = pg_class.oid
17-
AND pg_attribute.attnum = ANY (pg_index.indkey)
18-
AND indisprimary
12+
i.indrelid = c.oid
13+
AND c.relnamespace = n.oid
14+
AND a.attrelid = c.oid
15+
AND a.attnum = ANY (i.indkey)
16+
AND i.indisprimary

src/lib/sql/relationships.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
SELECT
2-
(tc.table_schema || '.' || (tc.table_name)) AS source_table_id,
32
tc.table_schema AS source_schema,
43
tc.table_name AS source_table_name,
54
kcu.column_name AS source_column_name,
6-
(ccu.table_schema || '.' || (ccu.table_name)) AS target_table_id,
75
ccu.table_schema AS target_table_schema,
86
ccu.table_name AS target_table_name,
97
ccu.column_name AS target_column_name,

src/lib/sql/schemas.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SELECT
2+
nsp.oid AS id,
23
catalog_name,
34
schema_name AS name,
45
schema_owner AS owner,
@@ -8,3 +9,4 @@ SELECT
89
sql_path
910
FROM
1011
information_schema.schemata
12+
JOIN pg_namespace nsp ON schema_name::text::regnamespace = nsp.oid

src/lib/sql/sequences.sql

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1+
-- Adapted from the source for information_schema.sequences
12
SELECT
2-
table_schema,
3-
table_name,
4-
column_name,
5-
sequence_name,
6-
start_value,
7-
minimum_value,
8-
increment
9-
FROM
10-
information_schema.columns
11-
INNER JOIN information_schema.sequences ON (
12-
table_schema = sequence_schema
13-
AND pg_get_serial_sequence(
14-
table_schema || '."' || table_name || '"',
15-
column_name
16-
) = sequence_schema || '.' || sequence_name
17-
)
18-
WHERE
19-
sequence_schema = ?
20-
ORDER BY
21-
table_schema,
22-
table_name,
23-
ordinal_position
3+
c.oid AS id,
4+
nc.nspname::information_schema.sql_identifier AS sequence_schema,
5+
c.relname::information_schema.sql_identifier AS sequence_name,
6+
s.seqstart::information_schema.character_data AS start_value,
7+
s.seqmin::information_schema.character_data AS minimum_value,
8+
s.seqmax::information_schema.character_data AS maximum_value,
9+
s.seqincrement::information_schema.character_data AS increment
10+
FROM pg_namespace nc,
11+
pg_class c,
12+
pg_sequence s
13+
WHERE c.relnamespace = nc.oid AND c.relkind = 'S'::"char" AND NOT (EXISTS (
14+
SELECT 1
15+
FROM pg_depend
16+
WHERE pg_depend.classid = 'pg_class'::regclass::oid
17+
AND pg_depend.objid = c.oid
18+
AND pg_depend.deptype = 'i'::"char"))
19+
AND NOT pg_is_other_temp_schema(nc.oid)
20+
AND c.oid = s.seqrelid
21+
AND (pg_has_role(c.relowner, 'USAGE'::text)
22+
OR has_sequence_privilege(c.oid, 'SELECT, UPDATE, USAGE'::text)
23+
)

0 commit comments

Comments
 (0)