-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconvert-to-sqlite.ts
executable file
·205 lines (192 loc) · 6.16 KB
/
convert-to-sqlite.ts
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
/* eslint-env node */
import { readFileSync, readdirSync, writeFileSync } from 'fs-extra';
import { resolve, join } from 'path';
import {
parse,
type CreateTableStmt,
type AlterTableStmt,
type Program,
} from 'sql-parser-cst';
// Currently this script only cares about CREATE TABLE statements and ALTER
// TABLE statements that add primary key constraints. All the other schema aspects of the
// pg_dump are generally beyond the capability of SQLite. Perhaps index creation
// can be added but it will get really tricky fast since SQLite's indices are
// much more simplistic than postgres.
const args = process.argv;
const migrationsDir = resolve(join(__dirname, '..', 'migrations'));
const sqliteSchemaDir = resolve(
join(__dirname, '..', '..', 'host', 'config', 'schema'),
);
const INDENT = ' ';
let pgDumpFile = args[2];
if (!pgDumpFile) {
console.error(`please specify the path of the pg_dump file`);
process.exit(-1);
}
let pgDump = readFileSync(pgDumpFile, 'utf8');
let cst = parse(prepareDump(pgDump), {
dialect: 'postgresql',
});
let sql: string[] = [
`
-- This is auto-generated by packages/realm-server/scripts/convert-to-sqlite.ts
-- Please don't directly modify this file
`,
];
for (let statement of cst.statements) {
if (statement.type !== 'create_table_stmt') {
continue;
}
sql.push('CREATE TABLE IF NOT EXISTS');
if (
statement.name.type === 'member_expr' &&
statement.name.property.type === 'identifier'
) {
let tableName = statement.name.property.name;
sql.push(statement.name.property.name, '(\n');
createColumns(cst, tableName, statement, sql);
} else {
throw new Error(`could not determine table name to be created`);
}
sql.push('\n);\n\n');
}
let result = sql.join(' ').trim();
let filename = getSchemaFilename();
let schemaFile = join(sqliteSchemaDir, filename);
writeFileSync(schemaFile, result);
console.log(`created SQLite schema file ${schemaFile}`);
function createColumns(
cst: Program,
tableName: string,
statement: CreateTableStmt,
sql: string[],
) {
if (!statement.columns) {
return;
}
let columns: string[] = [];
for (let [index, item] of statement.columns.expr.items.entries()) {
if (item.type !== 'column_definition') {
continue;
}
let column: string[] = [];
column.push(index === 0 ? INDENT.substring(1) : INDENT, item.name.name);
if (item.dataType?.type === 'named_data_type') {
let dataTypeName = Array.isArray(item.dataType.nameKw)
? item.dataType.nameKw[0]
: item.dataType.nameKw;
switch (dataTypeName.name) {
case 'CHARACTER':
column.push('TEXT');
break;
case 'JSONB':
// TODO change this to 'BLOB' after we do the sqlite BLOB storage
// support in CS-6668 for faster performance
column.push('JSON');
break;
case 'BOOLEAN':
column.push('BOOLEAN');
break;
case 'INTEGER':
column.push('INTEGER');
break;
}
}
for (let constraint of item.constraints) {
switch (constraint.type) {
case 'constraint_not_null':
column.push('NOT NULL');
break;
case 'constraint_primary_key':
column.push('PRIMARY KEY');
break;
default:
throw new Error(
`Don't know how to serialize constraint ${constraint.type} for column '${item.name.name}'`,
);
}
}
columns.push(column.join(' '));
}
let pkConstraint = makePrimaryKeyConstraint(cst, tableName);
sql.push([...columns, ...(pkConstraint ? [pkConstraint] : [])].join(',\n'));
}
function makePrimaryKeyConstraint(
cst: Program,
tableName: string,
): string | undefined {
let alterTableStmts = cst.statements.filter(
(s) =>
s.type === 'alter_table_stmt' &&
s.table.type === 'table_without_inheritance' &&
s.table.table.type === 'member_expr' &&
s.table.table.property.type === 'identifier' &&
s.table.table.property.name === tableName,
) as AlterTableStmt[];
let pkConstraint: string[] = [];
for (let alterTableStmt of alterTableStmts) {
for (let item of alterTableStmt.actions.items) {
if (item.type === 'alter_action_add_constraint') {
switch (item.constraint.type) {
case 'constraint_primary_key': {
if (pkConstraint.length > 0) {
throw new Error(
`encountered multiple primary key constraints for table ${tableName}`,
);
}
if (item.constraint.columns) {
let columns: string[] = [];
if (item.constraint.columns.type === 'paren_expr') {
for (let column of item.constraint.columns.expr.items) {
if (
column.type === 'index_specification' &&
column.expr.type === 'identifier'
) {
columns.push(column.expr.name);
}
}
} else {
throw new Error(
`Don't know how to serialize constraint ${item.constraint.type} for table '${tableName}'`,
);
}
if (columns.length > 0) {
pkConstraint.push(
INDENT,
'PRIMARY KEY (',
columns.join(', '),
')',
);
}
}
break;
}
default:
throw new Error(
`Don't know how to serialize constraint ${item.constraint.type} for table '${tableName}'`,
);
}
}
}
}
if (pkConstraint.length === 0) {
return undefined;
}
return pkConstraint.join(' ');
}
// This strips out all the things that our SQL AST chokes on (it's still in an
// experimental phase for postgresql)
function prepareDump(sql: string): string {
let result = sql
.replace(/\s*SET\s[^;].*;/gm, '')
.replace(/\s*CREATE\sTYPE\s[^;]*;/gm, '');
return result;
}
function getSchemaFilename(): string {
let files = readdirSync(migrationsDir);
let lastFile = files
.filter((f) => f !== '.eslintrc.js')
.sort()
.pop()!;
return `${lastFile.replace(/_.*/, '')}_schema.sql`;
}