@anthonyalayo/drizzle-kit 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +25 -0
- package/README.md +79 -0
- package/build.dev.ts +51 -0
- package/build.ts +112 -0
- package/imports-checker/analyze.ts +78 -0
- package/imports-checker/checker.ts +296 -0
- package/imports-checker/grammar/grammar.ohm +121 -0
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
- package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
- package/imports-checker/index.ts +48 -0
- package/package.json +142 -0
- package/patches/difflib@0.2.4.patch +31 -0
- package/src/@types/utils.ts +58 -0
- package/src/api.ts +504 -0
- package/src/cli/commands/_es5.ts +2 -0
- package/src/cli/commands/check.ts +52 -0
- package/src/cli/commands/drop.ts +60 -0
- package/src/cli/commands/introspect.ts +772 -0
- package/src/cli/commands/libSqlPushUtils.ts +355 -0
- package/src/cli/commands/migrate.ts +1526 -0
- package/src/cli/commands/mysqlIntrospect.ts +53 -0
- package/src/cli/commands/mysqlPushUtils.ts +352 -0
- package/src/cli/commands/mysqlUp.ts +102 -0
- package/src/cli/commands/pgIntrospect.ts +55 -0
- package/src/cli/commands/pgPushUtils.ts +269 -0
- package/src/cli/commands/pgUp.ts +217 -0
- package/src/cli/commands/push.ts +637 -0
- package/src/cli/commands/singlestoreIntrospect.ts +53 -0
- package/src/cli/commands/singlestorePushUtils.ts +456 -0
- package/src/cli/commands/singlestoreUp.ts +1 -0
- package/src/cli/commands/sqliteIntrospect.ts +96 -0
- package/src/cli/commands/sqlitePushUtils.ts +322 -0
- package/src/cli/commands/sqliteUp.ts +52 -0
- package/src/cli/commands/utils.ts +858 -0
- package/src/cli/connections.ts +914 -0
- package/src/cli/index.ts +48 -0
- package/src/cli/schema.ts +794 -0
- package/src/cli/selector-ui.ts +38 -0
- package/src/cli/utils.ts +112 -0
- package/src/cli/validations/cli.ts +79 -0
- package/src/cli/validations/common.ts +193 -0
- package/src/cli/validations/libsql.ts +27 -0
- package/src/cli/validations/mysql.ts +61 -0
- package/src/cli/validations/outputs.ts +91 -0
- package/src/cli/validations/postgres.ts +83 -0
- package/src/cli/validations/singlestore.ts +61 -0
- package/src/cli/validations/sqlite.ts +104 -0
- package/src/cli/validations/studio.ts +24 -0
- package/src/cli/views.ts +658 -0
- package/src/extensions/getTablesFilterByExtensions.ts +16 -0
- package/src/extensions/vector.ts +10 -0
- package/src/global.ts +61 -0
- package/src/index.ts +326 -0
- package/src/introspect-mysql.ts +1030 -0
- package/src/introspect-pg.ts +1435 -0
- package/src/introspect-singlestore.ts +913 -0
- package/src/introspect-sqlite.ts +533 -0
- package/src/jsonDiffer.js +882 -0
- package/src/jsonStatements.ts +3590 -0
- package/src/loader.mjs +57 -0
- package/src/migrationPreparator.ts +217 -0
- package/src/schemaValidator.ts +23 -0
- package/src/serializer/index.ts +142 -0
- package/src/serializer/mysqlImports.ts +38 -0
- package/src/serializer/mysqlSchema.ts +423 -0
- package/src/serializer/mysqlSerializer.ts +999 -0
- package/src/serializer/pgImports.ts +106 -0
- package/src/serializer/pgSchema.ts +937 -0
- package/src/serializer/pgSerializer.ts +2223 -0
- package/src/serializer/singlestoreImports.ts +38 -0
- package/src/serializer/singlestoreSchema.ts +257 -0
- package/src/serializer/singlestoreSerializer.ts +767 -0
- package/src/serializer/sqliteImports.ts +41 -0
- package/src/serializer/sqliteSchema.ts +352 -0
- package/src/serializer/sqliteSerializer.ts +931 -0
- package/src/serializer/studio.ts +694 -0
- package/src/serializer/utils.ts +45 -0
- package/src/simulator.ts +157 -0
- package/src/snapshotsDiffer.ts +4398 -0
- package/src/sqlgenerator.ts +4212 -0
- package/src/statementCombiner.ts +597 -0
- package/src/utils/certs.ts +44 -0
- package/src/utils/words.ts +1333 -0
- package/src/utils.ts +370 -0
- package/tests/bin.test.ts +58 -0
- package/tests/cli/d1http.config.ts +12 -0
- package/tests/cli/drizzle.config.ts +9 -0
- package/tests/cli/durable-sqlite.config.ts +7 -0
- package/tests/cli/expo.config.ts +7 -0
- package/tests/cli/postgres.config.ts +13 -0
- package/tests/cli/postgres2.config.ts +17 -0
- package/tests/cli/schema.ts +1 -0
- package/tests/cli/turso.config.ts +10 -0
- package/tests/cli-export.test.ts +79 -0
- package/tests/cli-generate.test.ts +259 -0
- package/tests/cli-migrate.test.ts +103 -0
- package/tests/cli-push.test.ts +126 -0
- package/tests/common.ts +15 -0
- package/tests/indexes/common.ts +21 -0
- package/tests/indexes/pg.test.ts +245 -0
- package/tests/introspect/libsql.test.ts +35 -0
- package/tests/introspect/mysql.test.ts +319 -0
- package/tests/introspect/pg.test.ts +894 -0
- package/tests/introspect/singlestore.test.ts +275 -0
- package/tests/introspect/sqlite.test.ts +122 -0
- package/tests/libsql-checks.test.ts +308 -0
- package/tests/libsql-statements.test.ts +989 -0
- package/tests/libsql-views.test.ts +218 -0
- package/tests/migrate/libsq-schema.ts +6 -0
- package/tests/migrate/libsql-migrate.test.ts +58 -0
- package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
- package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
- package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/_journal.json +20 -0
- package/tests/mysql-checks.test.ts +291 -0
- package/tests/mysql-generated.test.ts +1290 -0
- package/tests/mysql-schemas.test.ts +156 -0
- package/tests/mysql-views.test.ts +553 -0
- package/tests/mysql.test.ts +863 -0
- package/tests/pg-array.test.ts +368 -0
- package/tests/pg-checks.test.ts +329 -0
- package/tests/pg-columns.test.ts +484 -0
- package/tests/pg-domains.test.ts +306 -0
- package/tests/pg-enums.test.ts +751 -0
- package/tests/pg-generated.test.ts +529 -0
- package/tests/pg-identity.test.ts +447 -0
- package/tests/pg-schemas.test.ts +105 -0
- package/tests/pg-sequences.test.ts +298 -0
- package/tests/pg-tables.test.ts +1021 -0
- package/tests/pg-views.test.ts +1929 -0
- package/tests/push/common.ts +58 -0
- package/tests/push/libsql.test.ts +1400 -0
- package/tests/push/mysql-push.test.ts +345 -0
- package/tests/push/mysql.test.ts +788 -0
- package/tests/push/pg.test.ts +4341 -0
- package/tests/push/singlestore-push.test.ts +894 -0
- package/tests/push/singlestore.test.ts +444 -0
- package/tests/push/sqlite.test.ts +1613 -0
- package/tests/rls/pg-policy.test.ts +1656 -0
- package/tests/rls/pg-role.test.ts +234 -0
- package/tests/schemaDiffer.ts +2852 -0
- package/tests/singlestore-generated.test.ts +1290 -0
- package/tests/singlestore-schemas.test.ts +155 -0
- package/tests/singlestore.test.ts +978 -0
- package/tests/sqlite-checks.test.ts +308 -0
- package/tests/sqlite-columns.test.ts +1049 -0
- package/tests/sqlite-generated.test.ts +1726 -0
- package/tests/sqlite-tables.test.ts +637 -0
- package/tests/sqlite-views.test.ts +218 -0
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
- package/tests/test/sqlite.test.ts +39 -0
- package/tests/testsinglestore.ts +29 -0
- package/tests/validations.test.ts +869 -0
- package/tests/wrap-param.test.ts +16 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.cli-types.json +9 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JsonCreateIndexStatement,
|
|
3
|
+
JsonRecreateTableStatement,
|
|
4
|
+
JsonStatement,
|
|
5
|
+
prepareCreateIndexesJson,
|
|
6
|
+
} from './jsonStatements';
|
|
7
|
+
import { SingleStoreSchemaSquashed } from './serializer/singlestoreSchema';
|
|
8
|
+
import { SQLiteSchemaSquashed, SQLiteSquasher } from './serializer/sqliteSchema';
|
|
9
|
+
|
|
10
|
+
export const prepareLibSQLRecreateTable = (
|
|
11
|
+
table: SQLiteSchemaSquashed['tables'][keyof SQLiteSchemaSquashed['tables']],
|
|
12
|
+
action?: 'push',
|
|
13
|
+
): (JsonRecreateTableStatement | JsonCreateIndexStatement)[] => {
|
|
14
|
+
const { name, columns, uniqueConstraints, indexes, checkConstraints } = table;
|
|
15
|
+
|
|
16
|
+
const composites: string[][] = Object.values(table.compositePrimaryKeys).map(
|
|
17
|
+
(it) => SQLiteSquasher.unsquashPK(it),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const references: string[] = Object.values(table.foreignKeys);
|
|
21
|
+
const fks = references.map((it) =>
|
|
22
|
+
action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const statements: (JsonRecreateTableStatement | JsonCreateIndexStatement)[] = [
|
|
26
|
+
{
|
|
27
|
+
type: 'recreate_table',
|
|
28
|
+
tableName: name,
|
|
29
|
+
columns: Object.values(columns),
|
|
30
|
+
compositePKs: composites,
|
|
31
|
+
referenceData: fks,
|
|
32
|
+
uniqueConstraints: Object.values(uniqueConstraints),
|
|
33
|
+
checkConstraints: Object.values(checkConstraints),
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
if (Object.keys(indexes).length) {
|
|
38
|
+
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
39
|
+
}
|
|
40
|
+
return statements;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const prepareSQLiteRecreateTable = (
|
|
44
|
+
table: SQLiteSchemaSquashed['tables'][keyof SQLiteSchemaSquashed['tables']],
|
|
45
|
+
action?: 'push',
|
|
46
|
+
): JsonStatement[] => {
|
|
47
|
+
const { name, columns, uniqueConstraints, indexes, checkConstraints } = table;
|
|
48
|
+
|
|
49
|
+
const composites: string[][] = Object.values(table.compositePrimaryKeys).map(
|
|
50
|
+
(it) => SQLiteSquasher.unsquashPK(it),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const references: string[] = Object.values(table.foreignKeys);
|
|
54
|
+
const fks = references.map((it) =>
|
|
55
|
+
action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const statements: JsonStatement[] = [
|
|
59
|
+
{
|
|
60
|
+
type: 'recreate_table',
|
|
61
|
+
tableName: name,
|
|
62
|
+
columns: Object.values(columns),
|
|
63
|
+
compositePKs: composites,
|
|
64
|
+
referenceData: fks,
|
|
65
|
+
uniqueConstraints: Object.values(uniqueConstraints),
|
|
66
|
+
checkConstraints: Object.values(checkConstraints),
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
if (Object.keys(indexes).length) {
|
|
71
|
+
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
72
|
+
}
|
|
73
|
+
return statements;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const libSQLCombineStatements = (
|
|
77
|
+
statements: JsonStatement[],
|
|
78
|
+
json2: SQLiteSchemaSquashed,
|
|
79
|
+
action?: 'push',
|
|
80
|
+
) => {
|
|
81
|
+
// const tablesContext: Record<string, string[]> = {};
|
|
82
|
+
const newStatements: Record<string, JsonStatement[]> = {};
|
|
83
|
+
for (const statement of statements) {
|
|
84
|
+
if (
|
|
85
|
+
statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
86
|
+
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
87
|
+
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
88
|
+
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
89
|
+
|| statement.type === 'create_composite_pk'
|
|
90
|
+
|| statement.type === 'alter_composite_pk'
|
|
91
|
+
|| statement.type === 'delete_composite_pk'
|
|
92
|
+
|| statement.type === 'create_check_constraint'
|
|
93
|
+
|| statement.type === 'delete_check_constraint'
|
|
94
|
+
) {
|
|
95
|
+
const tableName = statement.tableName;
|
|
96
|
+
|
|
97
|
+
const statementsForTable = newStatements[tableName];
|
|
98
|
+
|
|
99
|
+
if (!statementsForTable) {
|
|
100
|
+
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
101
|
+
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
106
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
107
|
+
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
108
|
+
|
|
109
|
+
if (wasRename) {
|
|
110
|
+
newStatements[tableName].push(...preparedStatements);
|
|
111
|
+
} else {
|
|
112
|
+
newStatements[tableName] = preparedStatements;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
statement.type === 'alter_table_alter_column_set_type'
|
|
123
|
+
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
124
|
+
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
125
|
+
|| statement.type === 'alter_table_alter_column_set_default'
|
|
126
|
+
|| statement.type === 'alter_table_alter_column_drop_default'
|
|
127
|
+
) {
|
|
128
|
+
const { tableName, columnName, columnPk } = statement;
|
|
129
|
+
|
|
130
|
+
const columnIsPartOfForeignKey = Object.values(
|
|
131
|
+
json2.tables[tableName].foreignKeys,
|
|
132
|
+
).some((it) => {
|
|
133
|
+
const unsquashFk = action === 'push' ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
unsquashFk.columnsFrom.includes(columnName)
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const statementsForTable = newStatements[tableName];
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
!statementsForTable && (columnIsPartOfForeignKey || columnPk)
|
|
144
|
+
) {
|
|
145
|
+
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (
|
|
150
|
+
statementsForTable && (columnIsPartOfForeignKey || columnPk)
|
|
151
|
+
) {
|
|
152
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
153
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
154
|
+
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
155
|
+
|
|
156
|
+
if (wasRename) {
|
|
157
|
+
newStatements[tableName].push(...preparedStatements);
|
|
158
|
+
} else {
|
|
159
|
+
newStatements[tableName] = preparedStatements;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (
|
|
165
|
+
statementsForTable && !(columnIsPartOfForeignKey || columnPk)
|
|
166
|
+
) {
|
|
167
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
168
|
+
newStatements[tableName].push(statement);
|
|
169
|
+
}
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
newStatements[tableName] = [statement];
|
|
174
|
+
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (statement.type === 'create_reference') {
|
|
179
|
+
const tableName = statement.tableName;
|
|
180
|
+
|
|
181
|
+
const data = action === 'push'
|
|
182
|
+
? SQLiteSquasher.unsquashPushFK(statement.data)
|
|
183
|
+
: SQLiteSquasher.unsquashFK(statement.data);
|
|
184
|
+
|
|
185
|
+
const statementsForTable = newStatements[tableName];
|
|
186
|
+
|
|
187
|
+
if (!statementsForTable) {
|
|
188
|
+
newStatements[tableName] = statement.isMulticolumn
|
|
189
|
+
? prepareLibSQLRecreateTable(json2.tables[tableName], action)
|
|
190
|
+
: [statement];
|
|
191
|
+
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// if add column with reference -> skip create_reference statement
|
|
196
|
+
if (
|
|
197
|
+
!statement.isMulticolumn
|
|
198
|
+
&& statementsForTable.some((st) =>
|
|
199
|
+
st.type === 'sqlite_alter_table_add_column' && st.column.name === data.columnsFrom[0]
|
|
200
|
+
)
|
|
201
|
+
) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (statement.isMulticolumn) {
|
|
206
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
207
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
208
|
+
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
209
|
+
|
|
210
|
+
if (wasRename) {
|
|
211
|
+
newStatements[tableName].push(...preparedStatements);
|
|
212
|
+
} else {
|
|
213
|
+
newStatements[tableName] = preparedStatements;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
223
|
+
newStatements[tableName].push(statement);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (statement.type === 'delete_reference') {
|
|
230
|
+
const tableName = statement.tableName;
|
|
231
|
+
|
|
232
|
+
const statementsForTable = newStatements[tableName];
|
|
233
|
+
|
|
234
|
+
if (!statementsForTable) {
|
|
235
|
+
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
240
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
241
|
+
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
242
|
+
|
|
243
|
+
if (wasRename) {
|
|
244
|
+
newStatements[tableName].push(...preparedStatements);
|
|
245
|
+
} else {
|
|
246
|
+
newStatements[tableName] = preparedStatements;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (statement.type === 'sqlite_alter_table_add_column' && statement.column.primaryKey) {
|
|
256
|
+
const tableName = statement.tableName;
|
|
257
|
+
|
|
258
|
+
const statementsForTable = newStatements[tableName];
|
|
259
|
+
|
|
260
|
+
if (!statementsForTable) {
|
|
261
|
+
newStatements[tableName] = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
266
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
267
|
+
const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName], action);
|
|
268
|
+
|
|
269
|
+
if (wasRename) {
|
|
270
|
+
newStatements[tableName].push(...preparedStatements);
|
|
271
|
+
} else {
|
|
272
|
+
newStatements[tableName] = preparedStatements;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const tableName = statement.type === 'rename_table'
|
|
282
|
+
? statement.tableNameTo
|
|
283
|
+
: (statement as { tableName: string }).tableName;
|
|
284
|
+
const statementsForTable = newStatements[tableName];
|
|
285
|
+
|
|
286
|
+
if (!statementsForTable) {
|
|
287
|
+
newStatements[tableName] = [statement];
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
292
|
+
newStatements[tableName].push(statement);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const combinedStatements = Object.values(newStatements).flat();
|
|
297
|
+
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
298
|
+
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
299
|
+
|
|
300
|
+
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
301
|
+
|
|
302
|
+
return [...renamedTables, ...renamedColumns, ...rest];
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
export const sqliteCombineStatements = (
|
|
306
|
+
statements: JsonStatement[],
|
|
307
|
+
json2: SQLiteSchemaSquashed,
|
|
308
|
+
action?: 'push',
|
|
309
|
+
) => {
|
|
310
|
+
// const tablesContext: Record<string, string[]> = {};
|
|
311
|
+
const newStatements: Record<string, JsonStatement[]> = {};
|
|
312
|
+
for (const statement of statements) {
|
|
313
|
+
if (
|
|
314
|
+
statement.type === 'alter_table_alter_column_set_type'
|
|
315
|
+
|| statement.type === 'alter_table_alter_column_set_default'
|
|
316
|
+
|| statement.type === 'alter_table_alter_column_drop_default'
|
|
317
|
+
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
318
|
+
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
319
|
+
|| statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
320
|
+
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
321
|
+
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
322
|
+
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
323
|
+
|| statement.type === 'delete_reference'
|
|
324
|
+
|| statement.type === 'alter_reference'
|
|
325
|
+
|| statement.type === 'create_composite_pk'
|
|
326
|
+
|| statement.type === 'alter_composite_pk'
|
|
327
|
+
|| statement.type === 'delete_composite_pk'
|
|
328
|
+
|| statement.type === 'create_unique_constraint'
|
|
329
|
+
|| statement.type === 'delete_unique_constraint'
|
|
330
|
+
|| statement.type === 'create_check_constraint'
|
|
331
|
+
|| statement.type === 'delete_check_constraint'
|
|
332
|
+
) {
|
|
333
|
+
const tableName = statement.tableName;
|
|
334
|
+
|
|
335
|
+
const statementsForTable = newStatements[tableName];
|
|
336
|
+
|
|
337
|
+
if (!statementsForTable) {
|
|
338
|
+
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
343
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
344
|
+
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
345
|
+
|
|
346
|
+
if (wasRename) {
|
|
347
|
+
newStatements[tableName].push(...preparedStatements);
|
|
348
|
+
} else {
|
|
349
|
+
newStatements[tableName] = preparedStatements;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (statement.type === 'sqlite_alter_table_add_column' && statement.column.primaryKey) {
|
|
359
|
+
const tableName = statement.tableName;
|
|
360
|
+
|
|
361
|
+
const statementsForTable = newStatements[tableName];
|
|
362
|
+
|
|
363
|
+
if (!statementsForTable) {
|
|
364
|
+
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
369
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
370
|
+
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
371
|
+
|
|
372
|
+
if (wasRename) {
|
|
373
|
+
newStatements[tableName].push(...preparedStatements);
|
|
374
|
+
} else {
|
|
375
|
+
newStatements[tableName] = preparedStatements;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (statement.type === 'create_reference') {
|
|
385
|
+
const tableName = statement.tableName;
|
|
386
|
+
|
|
387
|
+
const data = action === 'push'
|
|
388
|
+
? SQLiteSquasher.unsquashPushFK(statement.data)
|
|
389
|
+
: SQLiteSquasher.unsquashFK(statement.data);
|
|
390
|
+
const statementsForTable = newStatements[tableName];
|
|
391
|
+
|
|
392
|
+
if (!statementsForTable) {
|
|
393
|
+
newStatements[tableName] = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// if add column with reference -> skip create_reference statement
|
|
398
|
+
if (
|
|
399
|
+
data.columnsFrom.length === 1
|
|
400
|
+
&& statementsForTable.some((st) =>
|
|
401
|
+
st.type === 'sqlite_alter_table_add_column' && st.column.name === data.columnsFrom[0]
|
|
402
|
+
)
|
|
403
|
+
) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
408
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
409
|
+
const preparedStatements = prepareSQLiteRecreateTable(json2.tables[tableName], action);
|
|
410
|
+
|
|
411
|
+
if (wasRename) {
|
|
412
|
+
newStatements[tableName].push(...preparedStatements);
|
|
413
|
+
} else {
|
|
414
|
+
newStatements[tableName] = preparedStatements;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const tableName = statement.type === 'rename_table'
|
|
424
|
+
? statement.tableNameTo
|
|
425
|
+
: (statement as { tableName: string }).tableName;
|
|
426
|
+
|
|
427
|
+
const statementsForTable = newStatements[tableName];
|
|
428
|
+
|
|
429
|
+
if (!statementsForTable) {
|
|
430
|
+
newStatements[tableName] = [statement];
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
435
|
+
newStatements[tableName].push(statement);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const combinedStatements = Object.values(newStatements).flat();
|
|
440
|
+
|
|
441
|
+
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
442
|
+
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
443
|
+
|
|
444
|
+
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
445
|
+
|
|
446
|
+
return [...renamedTables, ...renamedColumns, ...rest];
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
export const prepareSingleStoreRecreateTable = (
|
|
450
|
+
table: SingleStoreSchemaSquashed['tables'][keyof SingleStoreSchemaSquashed['tables']],
|
|
451
|
+
): JsonStatement[] => {
|
|
452
|
+
const { name, columns, uniqueConstraints, indexes, compositePrimaryKeys } = table;
|
|
453
|
+
|
|
454
|
+
const composites: string[] = Object.values(compositePrimaryKeys);
|
|
455
|
+
|
|
456
|
+
const statements: JsonStatement[] = [
|
|
457
|
+
{
|
|
458
|
+
type: 'singlestore_recreate_table',
|
|
459
|
+
tableName: name,
|
|
460
|
+
columns: Object.values(columns),
|
|
461
|
+
compositePKs: composites,
|
|
462
|
+
uniqueConstraints: Object.values(uniqueConstraints),
|
|
463
|
+
},
|
|
464
|
+
];
|
|
465
|
+
|
|
466
|
+
if (Object.keys(indexes).length) {
|
|
467
|
+
statements.push(...prepareCreateIndexesJson(name, '', indexes));
|
|
468
|
+
}
|
|
469
|
+
return statements;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
export const singleStoreCombineStatements = (
|
|
473
|
+
statements: JsonStatement[],
|
|
474
|
+
json2: SingleStoreSchemaSquashed,
|
|
475
|
+
) => {
|
|
476
|
+
const newStatements: Record<string, JsonStatement[]> = {};
|
|
477
|
+
|
|
478
|
+
for (const statement of statements) {
|
|
479
|
+
if (
|
|
480
|
+
statement.type === 'alter_table_alter_column_set_type'
|
|
481
|
+
|| statement.type === 'alter_table_alter_column_set_notnull'
|
|
482
|
+
|| statement.type === 'alter_table_alter_column_drop_notnull'
|
|
483
|
+
|| statement.type === 'alter_table_alter_column_drop_autoincrement'
|
|
484
|
+
|| statement.type === 'alter_table_alter_column_set_autoincrement'
|
|
485
|
+
|| statement.type === 'alter_table_alter_column_drop_pk'
|
|
486
|
+
|| statement.type === 'alter_table_alter_column_set_pk'
|
|
487
|
+
|| statement.type === 'create_composite_pk'
|
|
488
|
+
|| statement.type === 'alter_composite_pk'
|
|
489
|
+
|| statement.type === 'delete_composite_pk'
|
|
490
|
+
) {
|
|
491
|
+
const tableName = statement.tableName;
|
|
492
|
+
|
|
493
|
+
const statementsForTable = newStatements[tableName];
|
|
494
|
+
|
|
495
|
+
if (!statementsForTable) {
|
|
496
|
+
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
501
|
+
const wasRename = statementsForTable.some(({ type }) =>
|
|
502
|
+
type === 'rename_table' || type === 'alter_table_rename_column'
|
|
503
|
+
);
|
|
504
|
+
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
505
|
+
|
|
506
|
+
if (wasRename) {
|
|
507
|
+
newStatements[tableName].push(...preparedStatements);
|
|
508
|
+
} else {
|
|
509
|
+
newStatements[tableName] = preparedStatements;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (
|
|
519
|
+
(statement.type === 'alter_table_alter_column_drop_default'
|
|
520
|
+
|| statement.type === 'alter_table_alter_column_set_default') && statement.columnNotNull
|
|
521
|
+
) {
|
|
522
|
+
const tableName = statement.tableName;
|
|
523
|
+
|
|
524
|
+
const statementsForTable = newStatements[tableName];
|
|
525
|
+
|
|
526
|
+
if (!statementsForTable) {
|
|
527
|
+
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
532
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
533
|
+
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
534
|
+
|
|
535
|
+
if (wasRename) {
|
|
536
|
+
newStatements[tableName].push(...preparedStatements);
|
|
537
|
+
} else {
|
|
538
|
+
newStatements[tableName] = preparedStatements;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (statement.type === 'alter_table_add_column' && statement.column.primaryKey) {
|
|
548
|
+
const tableName = statement.tableName;
|
|
549
|
+
|
|
550
|
+
const statementsForTable = newStatements[tableName];
|
|
551
|
+
|
|
552
|
+
if (!statementsForTable) {
|
|
553
|
+
newStatements[tableName] = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if (!statementsForTable.some(({ type }) => type === 'recreate_table')) {
|
|
558
|
+
const wasRename = statementsForTable.some(({ type }) => type === 'rename_table');
|
|
559
|
+
const preparedStatements = prepareSingleStoreRecreateTable(json2.tables[tableName]);
|
|
560
|
+
|
|
561
|
+
if (wasRename) {
|
|
562
|
+
newStatements[tableName].push(...preparedStatements);
|
|
563
|
+
} else {
|
|
564
|
+
newStatements[tableName] = preparedStatements;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const tableName = statement.type === 'rename_table'
|
|
574
|
+
? statement.tableNameTo
|
|
575
|
+
: (statement as { tableName: string }).tableName;
|
|
576
|
+
|
|
577
|
+
const statementsForTable = newStatements[tableName];
|
|
578
|
+
|
|
579
|
+
if (!statementsForTable) {
|
|
580
|
+
newStatements[tableName] = [statement];
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (!statementsForTable.some(({ type }) => type === 'singlestore_recreate_table')) {
|
|
585
|
+
newStatements[tableName].push(statement);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const combinedStatements = Object.values(newStatements).flat();
|
|
590
|
+
|
|
591
|
+
const renamedTables = combinedStatements.filter((it) => it.type === 'rename_table');
|
|
592
|
+
const renamedColumns = combinedStatements.filter((it) => it.type === 'alter_table_rename_column');
|
|
593
|
+
|
|
594
|
+
const rest = combinedStatements.filter((it) => it.type !== 'rename_table' && it.type !== 'alter_table_rename_column');
|
|
595
|
+
|
|
596
|
+
return [...renamedTables, ...renamedColumns, ...rest];
|
|
597
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import envPaths from 'env-paths';
|
|
2
|
+
import { mkdirSync } from 'fs';
|
|
3
|
+
import { access, readFile } from 'fs/promises';
|
|
4
|
+
import { exec, ExecOptions } from 'node:child_process';
|
|
5
|
+
import { join } from 'path';
|
|
6
|
+
|
|
7
|
+
export function runCommand(command: string, options: ExecOptions = {}) {
|
|
8
|
+
return new Promise<{ exitCode: number }>((resolve) => {
|
|
9
|
+
exec(command, options, (error) => {
|
|
10
|
+
return resolve({ exitCode: error?.code ?? 0 });
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const certs = async () => {
|
|
16
|
+
const res = await runCommand('mkcert --help');
|
|
17
|
+
|
|
18
|
+
if (res.exitCode === 0) {
|
|
19
|
+
const p = envPaths('drizzle-studio', {
|
|
20
|
+
suffix: '',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// create ~/.local/share/drizzle-studio
|
|
24
|
+
mkdirSync(p.data, { recursive: true });
|
|
25
|
+
|
|
26
|
+
// ~/.local/share/drizzle-studio
|
|
27
|
+
const keyPath = join(p.data, 'localhost-key.pem');
|
|
28
|
+
const certPath = join(p.data, 'localhost.pem');
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
// check if the files exist
|
|
32
|
+
await Promise.all([access(keyPath), access(certPath)]);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
// if not create them
|
|
35
|
+
await runCommand(`mkcert localhost`, { cwd: p.data });
|
|
36
|
+
}
|
|
37
|
+
const [key, cert] = await Promise.all([
|
|
38
|
+
readFile(keyPath, { encoding: 'utf-8' }),
|
|
39
|
+
readFile(certPath, { encoding: 'utf-8' }),
|
|
40
|
+
]);
|
|
41
|
+
return key && cert ? { key, cert } : null;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
};
|