@anthonyalayo/drizzle-kit 1.0.2 → 1.0.4
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/api.d.mts +2891 -0
- package/api.d.ts +2891 -0
- package/api.js +47534 -0
- package/api.mjs +47521 -0
- package/bin.cjs +80043 -0
- package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
- package/index-Z-1TKnbX.d.ts +308 -0
- package/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +32 -0
- package/index.mjs +7 -0
- package/package.json +1 -1
- package/utils.js +6056 -0
- package/utils.mjs +6032 -0
- package/.gitignore +0 -25
- package/build.dev.ts +0 -51
- package/build.ts +0 -112
- package/imports-checker/analyze.ts +0 -78
- package/imports-checker/checker.ts +0 -296
- package/imports-checker/grammar/grammar.ohm +0 -121
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
- package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
- package/imports-checker/index.ts +0 -48
- package/patches/difflib@0.2.4.patch +0 -31
- package/src/@types/utils.ts +0 -58
- package/src/api.ts +0 -504
- package/src/cli/commands/_es5.ts +0 -2
- package/src/cli/commands/check.ts +0 -52
- package/src/cli/commands/drop.ts +0 -60
- package/src/cli/commands/introspect.ts +0 -772
- package/src/cli/commands/libSqlPushUtils.ts +0 -355
- package/src/cli/commands/migrate.ts +0 -1526
- package/src/cli/commands/mysqlIntrospect.ts +0 -53
- package/src/cli/commands/mysqlPushUtils.ts +0 -352
- package/src/cli/commands/mysqlUp.ts +0 -102
- package/src/cli/commands/pgIntrospect.ts +0 -55
- package/src/cli/commands/pgPushUtils.ts +0 -269
- package/src/cli/commands/pgUp.ts +0 -217
- package/src/cli/commands/push.ts +0 -637
- package/src/cli/commands/singlestoreIntrospect.ts +0 -53
- package/src/cli/commands/singlestorePushUtils.ts +0 -456
- package/src/cli/commands/singlestoreUp.ts +0 -1
- package/src/cli/commands/sqliteIntrospect.ts +0 -96
- package/src/cli/commands/sqlitePushUtils.ts +0 -322
- package/src/cli/commands/sqliteUp.ts +0 -52
- package/src/cli/commands/utils.ts +0 -858
- package/src/cli/connections.ts +0 -914
- package/src/cli/index.ts +0 -48
- package/src/cli/schema.ts +0 -794
- package/src/cli/selector-ui.ts +0 -38
- package/src/cli/utils.ts +0 -112
- package/src/cli/validations/cli.ts +0 -79
- package/src/cli/validations/common.ts +0 -193
- package/src/cli/validations/libsql.ts +0 -27
- package/src/cli/validations/mysql.ts +0 -61
- package/src/cli/validations/outputs.ts +0 -91
- package/src/cli/validations/postgres.ts +0 -83
- package/src/cli/validations/singlestore.ts +0 -61
- package/src/cli/validations/sqlite.ts +0 -104
- package/src/cli/validations/studio.ts +0 -24
- package/src/cli/views.ts +0 -658
- package/src/extensions/getTablesFilterByExtensions.ts +0 -16
- package/src/extensions/vector.ts +0 -10
- package/src/global.ts +0 -61
- package/src/introspect-mysql.ts +0 -1030
- package/src/introspect-pg.ts +0 -1435
- package/src/introspect-singlestore.ts +0 -913
- package/src/introspect-sqlite.ts +0 -533
- package/src/jsonDiffer.js +0 -882
- package/src/jsonStatements.ts +0 -3590
- package/src/loader.mjs +0 -57
- package/src/migrationPreparator.ts +0 -217
- package/src/schemaValidator.ts +0 -23
- package/src/serializer/index.ts +0 -142
- package/src/serializer/mysqlImports.ts +0 -38
- package/src/serializer/mysqlSchema.ts +0 -423
- package/src/serializer/mysqlSerializer.ts +0 -999
- package/src/serializer/pgImports.ts +0 -106
- package/src/serializer/pgSchema.ts +0 -937
- package/src/serializer/pgSerializer.ts +0 -2223
- package/src/serializer/singlestoreImports.ts +0 -38
- package/src/serializer/singlestoreSchema.ts +0 -257
- package/src/serializer/singlestoreSerializer.ts +0 -767
- package/src/serializer/sqliteImports.ts +0 -41
- package/src/serializer/sqliteSchema.ts +0 -352
- package/src/serializer/sqliteSerializer.ts +0 -931
- package/src/serializer/studio.ts +0 -694
- package/src/serializer/utils.ts +0 -45
- package/src/simulator.ts +0 -157
- package/src/snapshotsDiffer.ts +0 -4398
- package/src/sqlgenerator.ts +0 -4212
- package/src/statementCombiner.ts +0 -597
- package/src/utils/certs.ts +0 -44
- package/src/utils/words.ts +0 -1333
- package/src/utils.ts +0 -370
- package/tests/bin.test.ts +0 -58
- package/tests/cli/d1http.config.ts +0 -12
- package/tests/cli/drizzle.config.ts +0 -9
- package/tests/cli/durable-sqlite.config.ts +0 -7
- package/tests/cli/expo.config.ts +0 -7
- package/tests/cli/postgres.config.ts +0 -13
- package/tests/cli/postgres2.config.ts +0 -17
- package/tests/cli/schema.ts +0 -1
- package/tests/cli/turso.config.ts +0 -10
- package/tests/cli-export.test.ts +0 -79
- package/tests/cli-generate.test.ts +0 -259
- package/tests/cli-migrate.test.ts +0 -103
- package/tests/cli-push.test.ts +0 -126
- package/tests/common.ts +0 -15
- package/tests/indexes/common.ts +0 -21
- package/tests/indexes/pg.test.ts +0 -245
- package/tests/introspect/libsql.test.ts +0 -35
- package/tests/introspect/mysql.test.ts +0 -319
- package/tests/introspect/pg.test.ts +0 -894
- package/tests/introspect/singlestore.test.ts +0 -275
- package/tests/introspect/sqlite.test.ts +0 -122
- package/tests/libsql-checks.test.ts +0 -308
- package/tests/libsql-statements.test.ts +0 -989
- package/tests/libsql-views.test.ts +0 -218
- package/tests/migrate/libsq-schema.ts +0 -6
- package/tests/migrate/libsql-migrate.test.ts +0 -58
- package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
- package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
- package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/_journal.json +0 -20
- package/tests/mysql-checks.test.ts +0 -291
- package/tests/mysql-generated.test.ts +0 -1290
- package/tests/mysql-schemas.test.ts +0 -156
- package/tests/mysql-views.test.ts +0 -553
- package/tests/mysql.test.ts +0 -863
- package/tests/pg-array.test.ts +0 -368
- package/tests/pg-checks.test.ts +0 -329
- package/tests/pg-columns.test.ts +0 -484
- package/tests/pg-domains.test.ts +0 -306
- package/tests/pg-enums.test.ts +0 -751
- package/tests/pg-generated.test.ts +0 -529
- package/tests/pg-identity.test.ts +0 -447
- package/tests/pg-schemas.test.ts +0 -105
- package/tests/pg-sequences.test.ts +0 -298
- package/tests/pg-tables.test.ts +0 -1021
- package/tests/pg-views.test.ts +0 -1929
- package/tests/push/common.ts +0 -58
- package/tests/push/libsql.test.ts +0 -1400
- package/tests/push/mysql-push.test.ts +0 -345
- package/tests/push/mysql.test.ts +0 -788
- package/tests/push/pg.test.ts +0 -4341
- package/tests/push/singlestore-push.test.ts +0 -894
- package/tests/push/singlestore.test.ts +0 -444
- package/tests/push/sqlite.test.ts +0 -1613
- package/tests/rls/pg-policy.test.ts +0 -1656
- package/tests/rls/pg-role.test.ts +0 -234
- package/tests/schemaDiffer.ts +0 -2852
- package/tests/singlestore-generated.test.ts +0 -1290
- package/tests/singlestore-schemas.test.ts +0 -155
- package/tests/singlestore.test.ts +0 -978
- package/tests/sqlite-checks.test.ts +0 -308
- package/tests/sqlite-columns.test.ts +0 -1049
- package/tests/sqlite-generated.test.ts +0 -1726
- package/tests/sqlite-tables.test.ts +0 -637
- package/tests/sqlite-views.test.ts +0 -218
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
- package/tests/test/sqlite.test.ts +0 -39
- package/tests/testsinglestore.ts +0 -29
- package/tests/validations.test.ts +0 -869
- package/tests/wrap-param.test.ts +0 -16
- package/tsconfig.build.json +0 -4
- package/tsconfig.cli-types.json +0 -9
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -34
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import { render } from 'hanji';
|
|
3
|
-
import { fromJson } from 'src/sqlgenerator';
|
|
4
|
-
import { TypeOf } from 'zod';
|
|
5
|
-
import { JsonAlterColumnTypeStatement, JsonStatement } from '../../jsonStatements';
|
|
6
|
-
import { Column, SingleStoreSchemaSquashed, SingleStoreSquasher } from '../../serializer/singlestoreSchema';
|
|
7
|
-
import { singlestoreSchema } from '../../serializer/singlestoreSchema';
|
|
8
|
-
import { type DB, findAddedAndRemoved } from '../../utils';
|
|
9
|
-
import { Select } from '../selector-ui';
|
|
10
|
-
import { withStyle } from '../validations/outputs';
|
|
11
|
-
|
|
12
|
-
export const filterStatements = (
|
|
13
|
-
statements: JsonStatement[],
|
|
14
|
-
currentSchema: TypeOf<typeof singlestoreSchema>,
|
|
15
|
-
prevSchema: TypeOf<typeof singlestoreSchema>,
|
|
16
|
-
) => {
|
|
17
|
-
return statements.filter((statement) => {
|
|
18
|
-
if (statement.type === 'alter_table_alter_column_set_type') {
|
|
19
|
-
// Don't need to handle it on migrations step and introspection
|
|
20
|
-
// but for both it should be skipped
|
|
21
|
-
if (
|
|
22
|
-
statement.oldDataType.startsWith('tinyint')
|
|
23
|
-
&& statement.newDataType.startsWith('boolean')
|
|
24
|
-
) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
statement.oldDataType.startsWith('bigint unsigned')
|
|
30
|
-
&& statement.newDataType.startsWith('serial')
|
|
31
|
-
) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
statement.oldDataType.startsWith('serial')
|
|
37
|
-
&& statement.newDataType.startsWith('bigint unsigned')
|
|
38
|
-
) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
} else if (statement.type === 'alter_table_alter_column_set_default') {
|
|
42
|
-
if (
|
|
43
|
-
statement.newDefaultValue === false
|
|
44
|
-
&& statement.oldDefaultValue === 0
|
|
45
|
-
&& statement.newDataType === 'boolean'
|
|
46
|
-
) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
if (
|
|
50
|
-
statement.newDefaultValue === true
|
|
51
|
-
&& statement.oldDefaultValue === 1
|
|
52
|
-
&& statement.newDataType === 'boolean'
|
|
53
|
-
) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
} else if (statement.type === 'delete_unique_constraint') {
|
|
57
|
-
const unsquashed = SingleStoreSquasher.unsquashUnique(statement.data);
|
|
58
|
-
// only if constraint was removed from a serial column, than treat it as removed
|
|
59
|
-
// const serialStatement = statements.find(
|
|
60
|
-
// (it) => it.type === "alter_table_alter_column_set_type"
|
|
61
|
-
// ) as JsonAlterColumnTypeStatement;
|
|
62
|
-
// if (
|
|
63
|
-
// serialStatement?.oldDataType.startsWith("bigint unsigned") &&
|
|
64
|
-
// serialStatement?.newDataType.startsWith("serial") &&
|
|
65
|
-
// serialStatement.columnName ===
|
|
66
|
-
// SingleStoreSquasher.unsquashUnique(statement.data).columns[0]
|
|
67
|
-
// ) {
|
|
68
|
-
// return false;
|
|
69
|
-
// }
|
|
70
|
-
// Check if uniqueindex was only on this column, that is serial
|
|
71
|
-
|
|
72
|
-
// if now serial and was not serial and was unique index
|
|
73
|
-
if (
|
|
74
|
-
unsquashed.columns.length === 1
|
|
75
|
-
&& currentSchema.tables[statement.tableName].columns[unsquashed.columns[0]]
|
|
76
|
-
.type === 'serial'
|
|
77
|
-
&& prevSchema.tables[statement.tableName].columns[unsquashed.columns[0]]
|
|
78
|
-
.type === 'serial'
|
|
79
|
-
&& currentSchema.tables[statement.tableName].columns[unsquashed.columns[0]]
|
|
80
|
-
.name === unsquashed.columns[0]
|
|
81
|
-
) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
} else if (statement.type === 'alter_table_alter_column_drop_notnull') {
|
|
85
|
-
// only if constraint was removed from a serial column, than treat it as removed
|
|
86
|
-
const serialStatement = statements.find(
|
|
87
|
-
(it) => it.type === 'alter_table_alter_column_set_type',
|
|
88
|
-
) as JsonAlterColumnTypeStatement;
|
|
89
|
-
if (
|
|
90
|
-
serialStatement?.oldDataType.startsWith('bigint unsigned')
|
|
91
|
-
&& serialStatement?.newDataType.startsWith('serial')
|
|
92
|
-
&& serialStatement.columnName === statement.columnName
|
|
93
|
-
&& serialStatement.tableName === statement.tableName
|
|
94
|
-
) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
if (statement.newDataType === 'serial' && !statement.columnNotNull) {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
if (statement.columnAutoIncrement) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return true;
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
export function findColumnTypeAlternations(
|
|
110
|
-
columns1: Record<string, Column>,
|
|
111
|
-
columns2: Record<string, Column>,
|
|
112
|
-
): string[] {
|
|
113
|
-
const changes: string[] = [];
|
|
114
|
-
|
|
115
|
-
for (const key in columns1) {
|
|
116
|
-
if (columns1.hasOwnProperty(key) && columns2.hasOwnProperty(key)) {
|
|
117
|
-
const col1 = columns1[key];
|
|
118
|
-
const col2 = columns2[key];
|
|
119
|
-
if (col1.type !== col2.type) {
|
|
120
|
-
changes.push(col2.name);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return changes;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export const logSuggestionsAndReturn = async (
|
|
129
|
-
db: DB,
|
|
130
|
-
statements: JsonStatement[],
|
|
131
|
-
json2: TypeOf<typeof singlestoreSchema>,
|
|
132
|
-
json1: TypeOf<typeof singlestoreSchema>,
|
|
133
|
-
) => {
|
|
134
|
-
let shouldAskForApprove = false;
|
|
135
|
-
const statementsToExecute: string[] = [];
|
|
136
|
-
const infoToPrint: string[] = [];
|
|
137
|
-
|
|
138
|
-
const tablesToRemove: string[] = [];
|
|
139
|
-
const columnsToRemove: string[] = [];
|
|
140
|
-
const schemasToRemove: string[] = [];
|
|
141
|
-
const tablesToTruncate: string[] = [];
|
|
142
|
-
|
|
143
|
-
for (const statement of statements) {
|
|
144
|
-
if (statement.type === 'drop_table') {
|
|
145
|
-
const res = await db.query(
|
|
146
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
147
|
-
);
|
|
148
|
-
const count = Number(res[0].count);
|
|
149
|
-
if (count > 0) {
|
|
150
|
-
infoToPrint.push(
|
|
151
|
-
`· You're about to delete ${
|
|
152
|
-
chalk.underline(
|
|
153
|
-
statement.tableName,
|
|
154
|
-
)
|
|
155
|
-
} table with ${count} items`,
|
|
156
|
-
);
|
|
157
|
-
tablesToRemove.push(statement.tableName);
|
|
158
|
-
shouldAskForApprove = true;
|
|
159
|
-
}
|
|
160
|
-
} else if (statement.type === 'alter_table_drop_column') {
|
|
161
|
-
const res = await db.query(
|
|
162
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
163
|
-
);
|
|
164
|
-
const count = Number(res[0].count);
|
|
165
|
-
if (count > 0) {
|
|
166
|
-
infoToPrint.push(
|
|
167
|
-
`· You're about to delete ${
|
|
168
|
-
chalk.underline(
|
|
169
|
-
statement.columnName,
|
|
170
|
-
)
|
|
171
|
-
} column in ${statement.tableName} table with ${count} items`,
|
|
172
|
-
);
|
|
173
|
-
columnsToRemove.push(`${statement.tableName}_${statement.columnName}`);
|
|
174
|
-
shouldAskForApprove = true;
|
|
175
|
-
}
|
|
176
|
-
} else if (statement.type === 'drop_schema') {
|
|
177
|
-
const res = await db.query(
|
|
178
|
-
`select count(*) as count from information_schema.tables where table_schema = \`${statement.name}\`;`,
|
|
179
|
-
);
|
|
180
|
-
const count = Number(res[0].count);
|
|
181
|
-
if (count > 0) {
|
|
182
|
-
infoToPrint.push(
|
|
183
|
-
`· You're about to delete ${
|
|
184
|
-
chalk.underline(
|
|
185
|
-
statement.name,
|
|
186
|
-
)
|
|
187
|
-
} schema with ${count} tables`,
|
|
188
|
-
);
|
|
189
|
-
schemasToRemove.push(statement.name);
|
|
190
|
-
shouldAskForApprove = true;
|
|
191
|
-
}
|
|
192
|
-
} else if (statement.type === 'alter_table_alter_column_set_type') {
|
|
193
|
-
const res = await db.query(
|
|
194
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
195
|
-
);
|
|
196
|
-
const count = Number(res[0].count);
|
|
197
|
-
if (count > 0) {
|
|
198
|
-
infoToPrint.push(
|
|
199
|
-
`· You're about to change ${
|
|
200
|
-
chalk.underline(
|
|
201
|
-
statement.columnName,
|
|
202
|
-
)
|
|
203
|
-
} column type from ${
|
|
204
|
-
chalk.underline(
|
|
205
|
-
statement.oldDataType,
|
|
206
|
-
)
|
|
207
|
-
} to ${chalk.underline(statement.newDataType)} with ${count} items`,
|
|
208
|
-
);
|
|
209
|
-
statementsToExecute.push(`truncate table ${statement.tableName};`);
|
|
210
|
-
tablesToTruncate.push(statement.tableName);
|
|
211
|
-
shouldAskForApprove = true;
|
|
212
|
-
}
|
|
213
|
-
} else if (statement.type === 'alter_table_alter_column_drop_default') {
|
|
214
|
-
if (statement.columnNotNull) {
|
|
215
|
-
const res = await db.query(
|
|
216
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
const count = Number(res[0].count);
|
|
220
|
-
if (count > 0) {
|
|
221
|
-
infoToPrint.push(
|
|
222
|
-
`· You're about to remove default value from ${
|
|
223
|
-
chalk.underline(
|
|
224
|
-
statement.columnName,
|
|
225
|
-
)
|
|
226
|
-
} not-null column with ${count} items`,
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
tablesToTruncate.push(statement.tableName);
|
|
230
|
-
statementsToExecute.push(`truncate table ${statement.tableName};`);
|
|
231
|
-
|
|
232
|
-
shouldAskForApprove = true;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
// shouldAskForApprove = true;
|
|
236
|
-
} else if (statement.type === 'alter_table_alter_column_set_notnull') {
|
|
237
|
-
if (typeof statement.columnDefault === 'undefined') {
|
|
238
|
-
const res = await db.query(
|
|
239
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
const count = Number(res[0].count);
|
|
243
|
-
if (count > 0) {
|
|
244
|
-
infoToPrint.push(
|
|
245
|
-
`· You're about to set not-null constraint to ${
|
|
246
|
-
chalk.underline(
|
|
247
|
-
statement.columnName,
|
|
248
|
-
)
|
|
249
|
-
} column without default, which contains ${count} items`,
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
tablesToTruncate.push(statement.tableName);
|
|
253
|
-
statementsToExecute.push(`truncate table ${statement.tableName};`);
|
|
254
|
-
|
|
255
|
-
shouldAskForApprove = true;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
} else if (statement.type === 'alter_table_alter_column_drop_pk') {
|
|
259
|
-
const res = await db.query(
|
|
260
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
// if drop pk and json2 has autoincrement in table -> exit process with error
|
|
264
|
-
if (
|
|
265
|
-
Object.values(json2.tables[statement.tableName].columns).filter(
|
|
266
|
-
(column) => column.autoincrement,
|
|
267
|
-
).length > 0
|
|
268
|
-
) {
|
|
269
|
-
console.log(
|
|
270
|
-
`${
|
|
271
|
-
withStyle.errorWarning(
|
|
272
|
-
`You have removed the primary key from a ${statement.tableName} table without removing the auto-increment property from this table. As the database error states: 'there can be only one auto column, and it must be defined as a key. Make sure to remove autoincrement from ${statement.tableName} table`,
|
|
273
|
-
)
|
|
274
|
-
}`,
|
|
275
|
-
);
|
|
276
|
-
process.exit(1);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
const count = Number(res[0].count);
|
|
280
|
-
if (count > 0) {
|
|
281
|
-
infoToPrint.push(
|
|
282
|
-
`· You're about to change ${
|
|
283
|
-
chalk.underline(
|
|
284
|
-
statement.tableName,
|
|
285
|
-
)
|
|
286
|
-
} primary key. This statements may fail and you table may left without primary key`,
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
tablesToTruncate.push(statement.tableName);
|
|
290
|
-
shouldAskForApprove = true;
|
|
291
|
-
}
|
|
292
|
-
} else if (statement.type === 'delete_composite_pk') {
|
|
293
|
-
// if drop pk and json2 has autoincrement in table -> exit process with error
|
|
294
|
-
if (
|
|
295
|
-
Object.values(json2.tables[statement.tableName].columns).filter(
|
|
296
|
-
(column) => column.autoincrement,
|
|
297
|
-
).length > 0
|
|
298
|
-
) {
|
|
299
|
-
console.log(
|
|
300
|
-
`${
|
|
301
|
-
withStyle.errorWarning(
|
|
302
|
-
`You have removed the primary key from a ${statement.tableName} table without removing the auto-increment property from this table. As the database error states: 'there can be only one auto column, and it must be defined as a key. Make sure to remove autoincrement from ${statement.tableName} table`,
|
|
303
|
-
)
|
|
304
|
-
}`,
|
|
305
|
-
);
|
|
306
|
-
process.exit(1);
|
|
307
|
-
}
|
|
308
|
-
} else if (statement.type === 'alter_table_add_column') {
|
|
309
|
-
if (
|
|
310
|
-
statement.column.notNull
|
|
311
|
-
&& typeof statement.column.default === 'undefined'
|
|
312
|
-
) {
|
|
313
|
-
const res = await db.query(
|
|
314
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
315
|
-
);
|
|
316
|
-
const count = Number(res[0].count);
|
|
317
|
-
if (count > 0) {
|
|
318
|
-
infoToPrint.push(
|
|
319
|
-
`· You're about to add not-null ${
|
|
320
|
-
chalk.underline(
|
|
321
|
-
statement.column.name,
|
|
322
|
-
)
|
|
323
|
-
} column without default value, which contains ${count} items`,
|
|
324
|
-
);
|
|
325
|
-
|
|
326
|
-
tablesToTruncate.push(statement.tableName);
|
|
327
|
-
statementsToExecute.push(`truncate table ${statement.tableName};`);
|
|
328
|
-
|
|
329
|
-
shouldAskForApprove = true;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
} else if (statement.type === 'create_unique_constraint') {
|
|
333
|
-
const res = await db.query(
|
|
334
|
-
`select count(*) as count from \`${statement.tableName}\``,
|
|
335
|
-
);
|
|
336
|
-
const count = Number(res[0].count);
|
|
337
|
-
if (count > 0) {
|
|
338
|
-
const unsquashedUnique = SingleStoreSquasher.unsquashUnique(statement.data);
|
|
339
|
-
console.log(
|
|
340
|
-
`· You're about to add ${
|
|
341
|
-
chalk.underline(
|
|
342
|
-
unsquashedUnique.name,
|
|
343
|
-
)
|
|
344
|
-
} unique constraint to the table, which contains ${count} items. If this statement fails, you will receive an error from the database. Do you want to truncate ${
|
|
345
|
-
chalk.underline(
|
|
346
|
-
statement.tableName,
|
|
347
|
-
)
|
|
348
|
-
} table?\n`,
|
|
349
|
-
);
|
|
350
|
-
const { status, data } = await render(
|
|
351
|
-
new Select([
|
|
352
|
-
'No, add the constraint without truncating the table',
|
|
353
|
-
`Yes, truncate the table`,
|
|
354
|
-
]),
|
|
355
|
-
);
|
|
356
|
-
if (data?.index === 1) {
|
|
357
|
-
tablesToTruncate.push(statement.tableName);
|
|
358
|
-
statementsToExecute.push(`truncate table ${statement.tableName};`);
|
|
359
|
-
shouldAskForApprove = true;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
} else if (statement.type === 'singlestore_recreate_table') {
|
|
363
|
-
const tableName = statement.tableName;
|
|
364
|
-
|
|
365
|
-
const prevColumns = json1.tables[tableName].columns;
|
|
366
|
-
const currentColumns = json2.tables[tableName].columns;
|
|
367
|
-
const { removedColumns, addedColumns } = findAddedAndRemoved(
|
|
368
|
-
Object.keys(prevColumns),
|
|
369
|
-
Object.keys(currentColumns),
|
|
370
|
-
);
|
|
371
|
-
|
|
372
|
-
if (removedColumns.length) {
|
|
373
|
-
for (const removedColumn of removedColumns) {
|
|
374
|
-
const res = await db.query<{ count: string }>(
|
|
375
|
-
`select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``,
|
|
376
|
-
);
|
|
377
|
-
|
|
378
|
-
const count = Number(res[0].count);
|
|
379
|
-
if (count > 0) {
|
|
380
|
-
infoToPrint.push(
|
|
381
|
-
`· You're about to delete ${
|
|
382
|
-
chalk.underline(
|
|
383
|
-
removedColumn,
|
|
384
|
-
)
|
|
385
|
-
} column in ${tableName} table with ${count} items`,
|
|
386
|
-
);
|
|
387
|
-
columnsToRemove.push(removedColumn);
|
|
388
|
-
shouldAskForApprove = true;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (addedColumns.length) {
|
|
394
|
-
for (const addedColumn of addedColumns) {
|
|
395
|
-
const [res] = await db.query<{ count: string }>(
|
|
396
|
-
`select count(*) as count from \`${tableName}\``,
|
|
397
|
-
);
|
|
398
|
-
|
|
399
|
-
const columnConf = json2.tables[tableName].columns[addedColumn];
|
|
400
|
-
|
|
401
|
-
const count = Number(res.count);
|
|
402
|
-
if (count > 0 && columnConf.notNull && !columnConf.default) {
|
|
403
|
-
infoToPrint.push(
|
|
404
|
-
`· You're about to add not-null ${
|
|
405
|
-
chalk.underline(
|
|
406
|
-
addedColumn,
|
|
407
|
-
)
|
|
408
|
-
} column without default value to table, which contains ${count} items`,
|
|
409
|
-
);
|
|
410
|
-
shouldAskForApprove = true;
|
|
411
|
-
tablesToTruncate.push(tableName);
|
|
412
|
-
|
|
413
|
-
statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const columnWithChangedType = findColumnTypeAlternations(prevColumns, currentColumns);
|
|
419
|
-
for (const column of columnWithChangedType) {
|
|
420
|
-
const [res] = await db.query<{ count: string }>(
|
|
421
|
-
`select count(*) as count from \`${tableName}\` WHERE \`${tableName}\`.\`${column}\` IS NOT NULL;`,
|
|
422
|
-
);
|
|
423
|
-
|
|
424
|
-
const count = Number(res.count);
|
|
425
|
-
if (count > 0) {
|
|
426
|
-
infoToPrint.push(
|
|
427
|
-
`· You're about recreate ${chalk.underline(tableName)} table with data type changing for ${
|
|
428
|
-
chalk.underline(
|
|
429
|
-
column,
|
|
430
|
-
)
|
|
431
|
-
} column, which contains ${count} items`,
|
|
432
|
-
);
|
|
433
|
-
shouldAskForApprove = true;
|
|
434
|
-
tablesToTruncate.push(tableName);
|
|
435
|
-
|
|
436
|
-
statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
const stmnt = fromJson([statement], 'singlestore', 'push');
|
|
442
|
-
if (typeof stmnt !== 'undefined') {
|
|
443
|
-
statementsToExecute.push(...stmnt);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
return {
|
|
448
|
-
statementsToExecute,
|
|
449
|
-
shouldAskForApprove,
|
|
450
|
-
infoToPrint,
|
|
451
|
-
columnsToRemove: [...new Set(columnsToRemove)],
|
|
452
|
-
schemasToRemove: [...new Set(schemasToRemove)],
|
|
453
|
-
tablesToTruncate: [...new Set(tablesToTruncate)],
|
|
454
|
-
tablesToRemove: [...new Set(tablesToRemove)],
|
|
455
|
-
};
|
|
456
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const upSinglestoreHandler = (out: string) => {};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { renderWithTask } from 'hanji';
|
|
2
|
-
import { Minimatch } from 'minimatch';
|
|
3
|
-
import { originUUID } from '../../global';
|
|
4
|
-
import { schemaToTypeScript } from '../../introspect-sqlite';
|
|
5
|
-
import type { SQLiteSchema } from '../../serializer/sqliteSchema';
|
|
6
|
-
import { fromDatabase } from '../../serializer/sqliteSerializer';
|
|
7
|
-
import type { SQLiteDB } from '../../utils';
|
|
8
|
-
import { Casing } from '../validations/common';
|
|
9
|
-
import type { SqliteCredentials } from '../validations/sqlite';
|
|
10
|
-
import { IntrospectProgress, ProgressView } from '../views';
|
|
11
|
-
|
|
12
|
-
export const sqliteIntrospect = async (
|
|
13
|
-
credentials: SqliteCredentials,
|
|
14
|
-
filters: string[],
|
|
15
|
-
casing: Casing,
|
|
16
|
-
) => {
|
|
17
|
-
const { connectToSQLite } = await import('../connections');
|
|
18
|
-
const db = await connectToSQLite(credentials);
|
|
19
|
-
|
|
20
|
-
const matchers = filters.map((it) => {
|
|
21
|
-
return new Minimatch(it);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const filter = (tableName: string) => {
|
|
25
|
-
if (matchers.length === 0) return true;
|
|
26
|
-
|
|
27
|
-
let flags: boolean[] = [];
|
|
28
|
-
|
|
29
|
-
for (let matcher of matchers) {
|
|
30
|
-
if (matcher.negate) {
|
|
31
|
-
if (!matcher.match(tableName)) {
|
|
32
|
-
flags.push(false);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (matcher.match(tableName)) {
|
|
37
|
-
flags.push(true);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (flags.length > 0) {
|
|
42
|
-
return flags.every(Boolean);
|
|
43
|
-
}
|
|
44
|
-
return false;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const progress = new IntrospectProgress();
|
|
48
|
-
const res = await renderWithTask(
|
|
49
|
-
progress,
|
|
50
|
-
fromDatabase(db, filter, (stage, count, status) => {
|
|
51
|
-
progress.update(stage, count, status);
|
|
52
|
-
}),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const schema = { id: originUUID, prevId: '', ...res } as SQLiteSchema;
|
|
56
|
-
const ts = schemaToTypeScript(schema, casing);
|
|
57
|
-
return { schema, ts };
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const sqlitePushIntrospect = async (db: SQLiteDB, filters: string[]) => {
|
|
61
|
-
const matchers = filters.map((it) => {
|
|
62
|
-
return new Minimatch(it);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const filter = (tableName: string) => {
|
|
66
|
-
if (matchers.length === 0) return true;
|
|
67
|
-
|
|
68
|
-
let flags: boolean[] = [];
|
|
69
|
-
|
|
70
|
-
for (let matcher of matchers) {
|
|
71
|
-
if (matcher.negate) {
|
|
72
|
-
if (!matcher.match(tableName)) {
|
|
73
|
-
flags.push(false);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (matcher.match(tableName)) {
|
|
78
|
-
flags.push(true);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (flags.length > 0) {
|
|
83
|
-
return flags.every(Boolean);
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const progress = new ProgressView(
|
|
89
|
-
'Pulling schema from database...',
|
|
90
|
-
'Pulling schema from database...',
|
|
91
|
-
);
|
|
92
|
-
const res = await renderWithTask(progress, fromDatabase(db, filter));
|
|
93
|
-
|
|
94
|
-
const schema = { id: originUUID, prevId: '', ...res } as SQLiteSchema;
|
|
95
|
-
return { schema };
|
|
96
|
-
};
|