-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
schema-inspector.js
243 lines (215 loc) 路 6.14 KB
/
schema-inspector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
const SQL_QUERIES = {
TABLE_LIST: /* sql */ `
SELECT *
FROM information_schema.tables
WHERE
table_schema = ?
AND table_type = 'BASE TABLE'
AND table_name != 'geometry_columns'
AND table_name != 'spatial_ref_sys';
`,
LIST_COLUMNS: /* sql */ `
SELECT data_type, column_name, character_maximum_length, column_default, is_nullable
FROM information_schema.columns
WHERE table_schema = ? AND table_name = ?;
`,
INDEX_LIST: /* sql */ `
SELECT
ix.indexrelid,
i.relname as index_name,
a.attname as column_name,
ix.indisunique as is_unique,
ix.indisprimary as is_primary
FROM
pg_class t,
pg_namespace s,
pg_class i,
pg_index ix,
pg_attribute a
WHERE
t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND a.attnum = ANY(ix.indkey)
AND t.relkind = 'r'
AND t.relnamespace = s.oid
AND s.nspname = ?
AND t.relname = ?;
`,
FOREIGN_KEY_LIST: /* sql */ `
SELECT
tco."constraint_name" as constraint_name,
kcu."column_name" as column_name,
rel_kcu."table_name" as foreign_table,
rel_kcu."column_name" as fk_column_name,
rco.update_rule as on_update,
rco.delete_rule as on_delete
FROM information_schema.table_constraints tco
JOIN information_schema.key_column_usage kcu
ON tco.constraint_schema = kcu.constraint_schema
AND tco.constraint_name = kcu.constraint_name
JOIN information_schema.referential_constraints rco
ON tco.constraint_schema = rco.constraint_schema
AND tco.constraint_name = rco.constraint_name
JOIN information_schema.key_column_usage rel_kcu
ON rco.unique_constraint_schema = rel_kcu.constraint_schema
AND rco.unique_constraint_name = rel_kcu.constraint_name
AND kcu.ordinal_position = rel_kcu.ordinal_position
WHERE
tco.constraint_type = 'FOREIGN KEY'
AND tco.constraint_schema = ?
AND tco.table_name = ?
ORDER BY kcu.table_schema, kcu.table_name, kcu.ordinal_position, kcu.constraint_name;
`,
};
const toStrapiType = (column) => {
const rootType = column.data_type.toLowerCase().match(/[^(), ]+/)[0];
switch (rootType) {
case 'integer': {
// find a way to figure out the increments
return { type: 'integer' };
}
case 'text': {
return { type: 'text', args: ['longtext'] };
}
case 'boolean': {
return { type: 'boolean' };
}
case 'character': {
return { type: 'string', args: [column.character_maximum_length] };
}
case 'timestamp': {
return { type: 'datetime', args: [{ useTz: false, precision: 6 }] };
}
case 'date': {
return { type: 'date' };
}
case 'time': {
return { type: 'time', args: [{ precision: 3 }] };
}
case 'numeric': {
return { type: 'decimal', args: [10, 2] };
}
case 'real':
case 'double': {
return { type: 'double' };
}
case 'bigint': {
return { type: 'bigInteger' };
}
case 'jsonb': {
return { type: 'jsonb' };
}
default: {
return { type: 'specificType', args: [column.data_type] };
}
}
};
const getIndexType = (index) => {
if (index.is_primary) {
return 'primary';
}
if (index.is_unique) {
return 'unique';
}
return null;
};
class CockroachdbSchemaInspector {
constructor(db) {
this.db = db;
}
async getSchema() {
const schema = { tables: [] };
const tables = await this.getTables();
schema.tables = await Promise.all(
tables.map(async (tableName) => {
const columns = await this.getColumns(tableName);
const indexes = await this.getIndexes(tableName);
const foreignKeys = await this.getForeignKeys(tableName);
return {
name: tableName,
columns,
indexes,
foreignKeys,
};
})
);
return schema;
}
getDatabaseSchema() {
return this.db.connection.getSchemaName() || 'public';
}
async getTables() {
const { rows } = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST, [
this.getDatabaseSchema(),
]);
return rows.map((row) => row.table_name);
}
async getColumns(tableName) {
const { rows } = await this.db.connection.raw(SQL_QUERIES.LIST_COLUMNS, [
this.getDatabaseSchema(),
tableName,
]);
return rows.map((row) => {
const { type, args = [], ...rest } = toStrapiType(row);
const defaultTo =
row.column_default && row.column_default.includes('nextval(') ? null : row.column_default;
return {
type,
args,
defaultTo,
name: row.column_name,
notNullable: row.is_nullable === 'NO',
unsigned: false,
...rest,
};
});
}
async getIndexes(tableName) {
const { rows } = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [
this.getDatabaseSchema(),
tableName,
]);
const ret = {};
for (const index of rows) {
if (index.column_name === 'id') {
continue;
}
if (!ret[index.indexrelid]) {
ret[index.indexrelid] = {
columns: [index.column_name],
name: index.index_name,
type: getIndexType(index),
};
} else {
ret[index.indexrelid].columns.push(index.column_name);
}
}
return Object.values(ret);
}
async getForeignKeys(tableName) {
const { rows } = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [
this.getDatabaseSchema(),
tableName,
]);
const ret = {};
for (const fk of rows) {
if (!ret[fk.constraint_name]) {
ret[fk.constraint_name] = {
name: fk.constraint_name,
columns: [fk.column_name],
referencedColumns: [fk.fk_column_name],
referencedTable: fk.foreign_table,
onUpdate: fk.on_update.toUpperCase(),
onDelete: fk.on_delete.toUpperCase(),
};
} else {
ret[fk.constraint_name].columns.push(fk.column_name);
ret[fk.constraint_name].referencedColumns.push(fk.fk_column_name);
}
}
return Object.values(ret);
}
}
module.exports = CockroachdbSchemaInspector;