drizzle-kit 0.20.0-50d6b73 → 0.20.0-572b8ee
Sign up to get free protection for your applications and to get access to all the features.
- package/cli/commands/pgIntrospect.d.ts +1 -0
- package/drivers/index.d.ts +3 -3
- package/index.cjs +46 -22
- package/package.json +1 -1
- package/serializer/pgSerializer.d.ts +4 -4
- package/serializer/studioUtils.d.ts +2 -1
- package/utils-studio.d.ts +1 -0
- package/utils-studio.js +53 -11
- package/utils.d.ts +2 -22
- package/utils.js +1415 -672
package/utils.js
CHANGED
@@ -1075,7 +1075,7 @@ var require_hanji = __commonJS({
|
|
1075
1075
|
});
|
1076
1076
|
|
1077
1077
|
// src/cli/views.ts
|
1078
|
-
var import_hanji, info, schema, isRenamePromptItem, ResolveColumnSelect, ResolveTableSelect, ResolveSchemasSelect;
|
1078
|
+
var import_hanji, info, schema, isRenamePromptItem, ResolveColumnSelect, ResolveTableSelect, ResolveSchemasSelect, Spinner, IntrospectProgress;
|
1079
1079
|
var init_views = __esm({
|
1080
1080
|
"src/cli/views.ts"() {
|
1081
1081
|
init_source();
|
@@ -1247,6 +1247,91 @@ Is ${source_default.bold.blue(
|
|
1247
1247
|
return this.state.items[this.state.selectedIdx];
|
1248
1248
|
}
|
1249
1249
|
};
|
1250
|
+
Spinner = class {
|
1251
|
+
constructor(frames) {
|
1252
|
+
this.frames = frames;
|
1253
|
+
this.offset = 0;
|
1254
|
+
this.tick = () => {
|
1255
|
+
this.iterator();
|
1256
|
+
};
|
1257
|
+
this.value = () => {
|
1258
|
+
return this.frames[this.offset];
|
1259
|
+
};
|
1260
|
+
this.iterator = () => {
|
1261
|
+
this.offset += 1;
|
1262
|
+
this.offset %= frames.length - 1;
|
1263
|
+
};
|
1264
|
+
}
|
1265
|
+
};
|
1266
|
+
IntrospectProgress = class extends import_hanji.TaskView {
|
1267
|
+
constructor() {
|
1268
|
+
super();
|
1269
|
+
this.spinner = new Spinner("\u28F7\u28EF\u28DF\u287F\u28BF\u28FB\u28FD\u28FE".split(""));
|
1270
|
+
this.state = {
|
1271
|
+
tables: {
|
1272
|
+
count: 0,
|
1273
|
+
name: "tables",
|
1274
|
+
status: "fetching"
|
1275
|
+
},
|
1276
|
+
columns: {
|
1277
|
+
count: 0,
|
1278
|
+
name: "columns",
|
1279
|
+
status: "fetching"
|
1280
|
+
},
|
1281
|
+
enums: {
|
1282
|
+
count: 0,
|
1283
|
+
name: "enums",
|
1284
|
+
status: "fetching"
|
1285
|
+
},
|
1286
|
+
indexes: {
|
1287
|
+
count: 0,
|
1288
|
+
name: "indexes",
|
1289
|
+
status: "fetching"
|
1290
|
+
},
|
1291
|
+
fks: {
|
1292
|
+
count: 0,
|
1293
|
+
name: "foreign keys",
|
1294
|
+
status: "fetching"
|
1295
|
+
}
|
1296
|
+
};
|
1297
|
+
this.formatCount = (count) => {
|
1298
|
+
const width = Math.max.apply(
|
1299
|
+
null,
|
1300
|
+
Object.values(this.state).map((it) => it.count.toFixed(0).length)
|
1301
|
+
);
|
1302
|
+
return count.toFixed(0).padEnd(width, " ");
|
1303
|
+
};
|
1304
|
+
this.statusText = (spinner, stage) => {
|
1305
|
+
const { name, count } = stage;
|
1306
|
+
const isDone = stage.status === "done";
|
1307
|
+
const prefix = isDone ? `[${source_default.green("\u2713")}]` : `[${spinner}]`;
|
1308
|
+
const formattedCount = this.formatCount(count);
|
1309
|
+
const suffix = isDone ? `${formattedCount} ${name} fetched` : `${formattedCount} ${name} fetching`;
|
1310
|
+
return `${prefix} ${suffix}
|
1311
|
+
`;
|
1312
|
+
};
|
1313
|
+
this.timeout = setInterval(() => {
|
1314
|
+
this.spinner.tick();
|
1315
|
+
this.requestLayout();
|
1316
|
+
}, 128);
|
1317
|
+
this.on("detach", () => clearInterval(this.timeout));
|
1318
|
+
}
|
1319
|
+
update(stage, count, status) {
|
1320
|
+
this.state[stage].count = count;
|
1321
|
+
this.state[stage].status = status;
|
1322
|
+
this.requestLayout();
|
1323
|
+
}
|
1324
|
+
render() {
|
1325
|
+
let info2 = "";
|
1326
|
+
const spin = this.spinner.value();
|
1327
|
+
info2 += this.statusText(spin, this.state.tables);
|
1328
|
+
info2 += this.statusText(spin, this.state.columns);
|
1329
|
+
info2 += this.statusText(spin, this.state.enums);
|
1330
|
+
info2 += this.statusText(spin, this.state.indexes);
|
1331
|
+
info2 += this.statusText(spin, this.state.fks);
|
1332
|
+
return info2;
|
1333
|
+
}
|
1334
|
+
};
|
1250
1335
|
}
|
1251
1336
|
});
|
1252
1337
|
|
@@ -5349,8 +5434,8 @@ var init_jsonDiffer = __esm({
|
|
5349
5434
|
alteredUniqueConstraints
|
5350
5435
|
};
|
5351
5436
|
};
|
5352
|
-
alternationsInColumn = (
|
5353
|
-
const altered = [
|
5437
|
+
alternationsInColumn = (column5) => {
|
5438
|
+
const altered = [column5];
|
5354
5439
|
const result = altered.map((it) => {
|
5355
5440
|
if (typeof it.name !== "string" && "__old" in it.name) {
|
5356
5441
|
return { ...it, name: { type: "changed", old: it.name.__old, new: it.name.__new } };
|
@@ -8173,16 +8258,16 @@ var require_node2 = __commonJS({
|
|
8173
8258
|
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
|
8174
8259
|
if (source) {
|
8175
8260
|
var line = frame.getLineNumber();
|
8176
|
-
var
|
8261
|
+
var column5 = frame.getColumnNumber() - 1;
|
8177
8262
|
var noHeader = /^v(10\.1[6-9]|10\.[2-9][0-9]|10\.[0-9]{3,}|1[2-9]\d*|[2-9]\d|\d{3,}|11\.11)/;
|
8178
8263
|
var headerLength = noHeader.test(process.version) ? 0 : 62;
|
8179
|
-
if (line === 1 &&
|
8180
|
-
|
8264
|
+
if (line === 1 && column5 > headerLength && !isInBrowser() && !frame.isEval()) {
|
8265
|
+
column5 -= headerLength;
|
8181
8266
|
}
|
8182
8267
|
var position = mapSourcePosition({
|
8183
8268
|
source,
|
8184
8269
|
line,
|
8185
|
-
column:
|
8270
|
+
column: column5
|
8186
8271
|
});
|
8187
8272
|
state.curPosition = position;
|
8188
8273
|
frame = cloneCallSite(frame);
|
@@ -8240,7 +8325,7 @@ var require_node2 = __commonJS({
|
|
8240
8325
|
if (match2) {
|
8241
8326
|
var source = match2[1];
|
8242
8327
|
var line = +match2[2];
|
8243
|
-
var
|
8328
|
+
var column5 = +match2[3];
|
8244
8329
|
var contents = fileContentsCache[source];
|
8245
8330
|
if (!contents && fs32 && fs32.existsSync(source)) {
|
8246
8331
|
try {
|
@@ -8252,7 +8337,7 @@ var require_node2 = __commonJS({
|
|
8252
8337
|
if (contents) {
|
8253
8338
|
var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1];
|
8254
8339
|
if (code) {
|
8255
|
-
return source + ":" + line + "\n" + code + "\n" + new Array(
|
8340
|
+
return source + ":" + line + "\n" + code + "\n" + new Array(column5).join(" ") + "^";
|
8256
8341
|
}
|
8257
8342
|
}
|
8258
8343
|
}
|
@@ -9202,7 +9287,7 @@ var require_node2 = __commonJS({
|
|
9202
9287
|
var stack = void 0;
|
9203
9288
|
var pos = void 0;
|
9204
9289
|
var line = void 0;
|
9205
|
-
var
|
9290
|
+
var column5 = void 0;
|
9206
9291
|
var token = void 0;
|
9207
9292
|
var key = void 0;
|
9208
9293
|
var root = void 0;
|
@@ -9212,7 +9297,7 @@ var require_node2 = __commonJS({
|
|
9212
9297
|
stack = [];
|
9213
9298
|
pos = 0;
|
9214
9299
|
line = 1;
|
9215
|
-
|
9300
|
+
column5 = 0;
|
9216
9301
|
token = void 0;
|
9217
9302
|
key = void 0;
|
9218
9303
|
root = void 0;
|
@@ -9266,11 +9351,11 @@ var require_node2 = __commonJS({
|
|
9266
9351
|
var c2 = peek();
|
9267
9352
|
if (c2 === "\n") {
|
9268
9353
|
line++;
|
9269
|
-
|
9354
|
+
column5 = 0;
|
9270
9355
|
} else if (c2) {
|
9271
|
-
|
9356
|
+
column5 += c2.length;
|
9272
9357
|
} else {
|
9273
|
-
|
9358
|
+
column5++;
|
9274
9359
|
}
|
9275
9360
|
if (c2) {
|
9276
9361
|
pos += c2.length;
|
@@ -9704,7 +9789,7 @@ var require_node2 = __commonJS({
|
|
9704
9789
|
throw invalidChar(read());
|
9705
9790
|
} };
|
9706
9791
|
function newToken(type, value) {
|
9707
|
-
return { type, value, line, column:
|
9792
|
+
return { type, value, line, column: column5 };
|
9708
9793
|
}
|
9709
9794
|
function literal(s) {
|
9710
9795
|
var _iteratorNormalCompletion = true;
|
@@ -9941,16 +10026,16 @@ var require_node2 = __commonJS({
|
|
9941
10026
|
}
|
9942
10027
|
function invalidChar(c2) {
|
9943
10028
|
if (c2 === void 0) {
|
9944
|
-
return syntaxError("JSON5: invalid end of input at " + line + ":" +
|
10029
|
+
return syntaxError("JSON5: invalid end of input at " + line + ":" + column5);
|
9945
10030
|
}
|
9946
|
-
return syntaxError("JSON5: invalid character '" + formatChar(c2) + "' at " + line + ":" +
|
10031
|
+
return syntaxError("JSON5: invalid character '" + formatChar(c2) + "' at " + line + ":" + column5);
|
9947
10032
|
}
|
9948
10033
|
function invalidEOF() {
|
9949
|
-
return syntaxError("JSON5: invalid end of input at " + line + ":" +
|
10034
|
+
return syntaxError("JSON5: invalid end of input at " + line + ":" + column5);
|
9950
10035
|
}
|
9951
10036
|
function invalidIdentifier() {
|
9952
|
-
|
9953
|
-
return syntaxError("JSON5: invalid identifier character at " + line + ":" +
|
10037
|
+
column5 -= 5;
|
10038
|
+
return syntaxError("JSON5: invalid identifier character at " + line + ":" + column5);
|
9954
10039
|
}
|
9955
10040
|
function separatorChar(c2) {
|
9956
10041
|
console.warn("JSON5: '" + c2 + "' is not valid ECMAScript; consider escaping");
|
@@ -9969,7 +10054,7 @@ var require_node2 = __commonJS({
|
|
9969
10054
|
function syntaxError(message) {
|
9970
10055
|
var err = new SyntaxError(message);
|
9971
10056
|
err.lineNumber = line;
|
9972
|
-
err.columnNumber =
|
10057
|
+
err.columnNumber = column5;
|
9973
10058
|
return err;
|
9974
10059
|
}
|
9975
10060
|
module22.exports = exports2["default"];
|
@@ -11197,28 +11282,28 @@ var init_mysqlSerializer = __esm({
|
|
11197
11282
|
const foreignKeysObject = {};
|
11198
11283
|
const primaryKeysObject = {};
|
11199
11284
|
const uniqueConstraintObject = {};
|
11200
|
-
columns.forEach((
|
11201
|
-
const notNull =
|
11202
|
-
const sqlTypeLowered =
|
11203
|
-
const autoIncrement = typeof
|
11285
|
+
columns.forEach((column5) => {
|
11286
|
+
const notNull = column5.notNull;
|
11287
|
+
const sqlTypeLowered = column5.getSQLType().toLowerCase();
|
11288
|
+
const autoIncrement = typeof column5.autoIncrement === "undefined" ? false : column5.autoIncrement;
|
11204
11289
|
const columnToSet = {
|
11205
|
-
name:
|
11206
|
-
type:
|
11290
|
+
name: column5.name,
|
11291
|
+
type: column5.getSQLType(),
|
11207
11292
|
primaryKey: false,
|
11208
11293
|
// If field is autoincrement it's notNull by default
|
11209
11294
|
// notNull: autoIncrement ? true : notNull,
|
11210
11295
|
notNull,
|
11211
11296
|
autoincrement: autoIncrement,
|
11212
|
-
onUpdate:
|
11297
|
+
onUpdate: column5.hasOnUpdateNow
|
11213
11298
|
};
|
11214
|
-
if (
|
11215
|
-
primaryKeysObject[`${tableName}_${
|
11216
|
-
name: `${tableName}_${
|
11217
|
-
columns: [
|
11299
|
+
if (column5.primary) {
|
11300
|
+
primaryKeysObject[`${tableName}_${column5.name}`] = {
|
11301
|
+
name: `${tableName}_${column5.name}`,
|
11302
|
+
columns: [column5.name]
|
11218
11303
|
};
|
11219
11304
|
}
|
11220
|
-
if (
|
11221
|
-
const existingUnique = uniqueConstraintObject[
|
11305
|
+
if (column5.isUnique) {
|
11306
|
+
const existingUnique = uniqueConstraintObject[column5.uniqueName];
|
11222
11307
|
if (typeof existingUnique !== "undefined") {
|
11223
11308
|
console.log(
|
11224
11309
|
`
|
@@ -11226,9 +11311,9 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
11226
11311
|
tableName
|
11227
11312
|
)} table.
|
11228
11313
|
The unique constraint ${source_default.underline.blue(
|
11229
|
-
|
11314
|
+
column5.uniqueName
|
11230
11315
|
)} on the ${source_default.underline.blue(
|
11231
|
-
|
11316
|
+
column5.name
|
11232
11317
|
)} column is confilcting with a unique constraint name already defined for ${source_default.underline.blue(
|
11233
11318
|
existingUnique.columns.join(",")
|
11234
11319
|
)} columns
|
@@ -11236,36 +11321,36 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
11236
11321
|
);
|
11237
11322
|
process.exit(1);
|
11238
11323
|
}
|
11239
|
-
uniqueConstraintObject[
|
11240
|
-
name:
|
11324
|
+
uniqueConstraintObject[column5.uniqueName] = {
|
11325
|
+
name: column5.uniqueName,
|
11241
11326
|
columns: [columnToSet.name]
|
11242
11327
|
};
|
11243
11328
|
}
|
11244
|
-
if (
|
11245
|
-
if ((0, import_drizzle_orm2.is)(
|
11246
|
-
columnToSet.default = sqlToStr(
|
11329
|
+
if (column5.default !== void 0) {
|
11330
|
+
if ((0, import_drizzle_orm2.is)(column5.default, import_drizzle_orm3.SQL)) {
|
11331
|
+
columnToSet.default = sqlToStr(column5.default);
|
11247
11332
|
} else {
|
11248
|
-
if (typeof
|
11249
|
-
columnToSet.default = `'${
|
11333
|
+
if (typeof column5.default === "string") {
|
11334
|
+
columnToSet.default = `'${column5.default}'`;
|
11250
11335
|
} else {
|
11251
11336
|
if (sqlTypeLowered === "json") {
|
11252
|
-
columnToSet.default = `'${JSON.stringify(
|
11253
|
-
} else if (
|
11337
|
+
columnToSet.default = `'${JSON.stringify(column5.default)}'`;
|
11338
|
+
} else if (column5.default instanceof Date) {
|
11254
11339
|
if (sqlTypeLowered === "date") {
|
11255
|
-
columnToSet.default = `'${
|
11340
|
+
columnToSet.default = `'${column5.default.toISOString().split("T")[0]}'`;
|
11256
11341
|
} else if (sqlTypeLowered.startsWith("datetime") || sqlTypeLowered.startsWith("timestamp")) {
|
11257
|
-
columnToSet.default = `'${
|
11342
|
+
columnToSet.default = `'${column5.default.toISOString().replace("T", " ").slice(0, 23)}'`;
|
11258
11343
|
}
|
11259
11344
|
} else {
|
11260
|
-
columnToSet.default =
|
11345
|
+
columnToSet.default = column5.default;
|
11261
11346
|
}
|
11262
11347
|
}
|
11263
|
-
if (["blob", "text", "json"].includes(
|
11348
|
+
if (["blob", "text", "json"].includes(column5.getSQLType())) {
|
11264
11349
|
columnToSet.default = `(${columnToSet.default})`;
|
11265
11350
|
}
|
11266
11351
|
}
|
11267
11352
|
}
|
11268
|
-
columnsObject[
|
11353
|
+
columnsObject[column5.name] = columnToSet;
|
11269
11354
|
});
|
11270
11355
|
primaryKeys.map((pk) => {
|
11271
11356
|
const columnNames = pk.columns.map((c) => c.name);
|
@@ -11273,8 +11358,8 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
11273
11358
|
name: pk.getName(),
|
11274
11359
|
columns: columnNames
|
11275
11360
|
};
|
11276
|
-
for (const
|
11277
|
-
columnsObject[
|
11361
|
+
for (const column5 of pk.columns) {
|
11362
|
+
columnsObject[column5.name].notNull = true;
|
11278
11363
|
}
|
11279
11364
|
});
|
11280
11365
|
uniqueConstraints == null ? void 0 : uniqueConstraints.map((unq) => {
|
@@ -11427,33 +11512,33 @@ We have encountered a collision between the index name on columns ${source_defau
|
|
11427
11512
|
[inputSchema]
|
11428
11513
|
);
|
11429
11514
|
const idxRows = idxs[0];
|
11430
|
-
for (const
|
11431
|
-
if (!tablesFilter(
|
11515
|
+
for (const column5 of response) {
|
11516
|
+
if (!tablesFilter(column5["TABLE_NAME"]))
|
11432
11517
|
continue;
|
11433
11518
|
columnsCount += 1;
|
11434
11519
|
if (progressCallback) {
|
11435
11520
|
progressCallback("columns", columnsCount, "fetching");
|
11436
11521
|
}
|
11437
|
-
const schema4 =
|
11438
|
-
const tableName =
|
11522
|
+
const schema4 = column5["TABLE_SCHEMA"];
|
11523
|
+
const tableName = column5["TABLE_NAME"];
|
11439
11524
|
tablesCount.add(`${schema4}.${tableName}`);
|
11440
11525
|
if (progressCallback) {
|
11441
11526
|
progressCallback("columns", tablesCount.size, "fetching");
|
11442
11527
|
}
|
11443
|
-
const columnName =
|
11444
|
-
const isNullable =
|
11445
|
-
const dataType =
|
11446
|
-
const columnType =
|
11447
|
-
const isPrimary =
|
11448
|
-
const columnDefault =
|
11449
|
-
const collation =
|
11450
|
-
let columnExtra =
|
11528
|
+
const columnName = column5["COLUMN_NAME"];
|
11529
|
+
const isNullable = column5["IS_NULLABLE"] === "YES";
|
11530
|
+
const dataType = column5["DATA_TYPE"];
|
11531
|
+
const columnType = column5["COLUMN_TYPE"];
|
11532
|
+
const isPrimary = column5["COLUMN_KEY"] === "PRI";
|
11533
|
+
const columnDefault = column5["COLUMN_DEFAULT"];
|
11534
|
+
const collation = column5["CHARACTER_SET_NAME"];
|
11535
|
+
let columnExtra = column5["EXTRA"];
|
11451
11536
|
let isAutoincrement = false;
|
11452
11537
|
let isDefaultAnExpression = false;
|
11453
|
-
if (typeof
|
11454
|
-
columnExtra =
|
11455
|
-
isAutoincrement =
|
11456
|
-
isDefaultAnExpression =
|
11538
|
+
if (typeof column5["EXTRA"] !== "undefined") {
|
11539
|
+
columnExtra = column5["EXTRA"];
|
11540
|
+
isAutoincrement = column5["EXTRA"] === "auto_increment";
|
11541
|
+
isDefaultAnExpression = column5["EXTRA"].includes("DEFAULT_GENERATED");
|
11457
11542
|
}
|
11458
11543
|
if (schema4 !== inputSchema) {
|
11459
11544
|
schemas.push(schema4);
|
@@ -11763,18 +11848,18 @@ var init_pgSerializer = __esm({
|
|
11763
11848
|
const foreignKeysObject = {};
|
11764
11849
|
const primaryKeysObject = {};
|
11765
11850
|
const uniqueConstraintObject = {};
|
11766
|
-
columns.forEach((
|
11767
|
-
const notNull =
|
11768
|
-
const primaryKey =
|
11769
|
-
const sqlTypeLowered =
|
11851
|
+
columns.forEach((column5) => {
|
11852
|
+
const notNull = column5.notNull;
|
11853
|
+
const primaryKey = column5.primary;
|
11854
|
+
const sqlTypeLowered = column5.getSQLType().toLowerCase();
|
11770
11855
|
const columnToSet = {
|
11771
|
-
name:
|
11772
|
-
type:
|
11856
|
+
name: column5.name,
|
11857
|
+
type: column5.getSQLType(),
|
11773
11858
|
primaryKey,
|
11774
11859
|
notNull
|
11775
11860
|
};
|
11776
|
-
if (
|
11777
|
-
const existingUnique = uniqueConstraintObject[
|
11861
|
+
if (column5.isUnique) {
|
11862
|
+
const existingUnique = uniqueConstraintObject[column5.uniqueName];
|
11778
11863
|
if (typeof existingUnique !== "undefined") {
|
11779
11864
|
console.log(
|
11780
11865
|
`
|
@@ -11782,9 +11867,9 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
11782
11867
|
tableName
|
11783
11868
|
)} table.
|
11784
11869
|
The unique constraint ${source_default.underline.blue(
|
11785
|
-
|
11870
|
+
column5.uniqueName
|
11786
11871
|
)} on the ${source_default.underline.blue(
|
11787
|
-
|
11872
|
+
column5.name
|
11788
11873
|
)} column is confilcting with a unique constraint name already defined for ${source_default.underline.blue(
|
11789
11874
|
existingUnique.columns.join(",")
|
11790
11875
|
)} columns
|
@@ -11792,38 +11877,38 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
11792
11877
|
);
|
11793
11878
|
process.exit(1);
|
11794
11879
|
}
|
11795
|
-
uniqueConstraintObject[
|
11796
|
-
name:
|
11797
|
-
nullsNotDistinct:
|
11880
|
+
uniqueConstraintObject[column5.uniqueName] = {
|
11881
|
+
name: column5.uniqueName,
|
11882
|
+
nullsNotDistinct: column5.uniqueType === "not distinct",
|
11798
11883
|
columns: [columnToSet.name]
|
11799
11884
|
};
|
11800
11885
|
}
|
11801
|
-
if (
|
11802
|
-
if ((0, import_drizzle_orm5.is)(
|
11803
|
-
columnToSet.default = sqlToStr(
|
11886
|
+
if (column5.default !== void 0) {
|
11887
|
+
if ((0, import_drizzle_orm5.is)(column5.default, import_drizzle_orm5.SQL)) {
|
11888
|
+
columnToSet.default = sqlToStr(column5.default);
|
11804
11889
|
} else {
|
11805
|
-
if (typeof
|
11806
|
-
columnToSet.default = `'${
|
11890
|
+
if (typeof column5.default === "string") {
|
11891
|
+
columnToSet.default = `'${column5.default}'`;
|
11807
11892
|
} else {
|
11808
11893
|
if (sqlTypeLowered === "jsonb" || sqlTypeLowered === "json") {
|
11809
11894
|
columnToSet.default = `'${JSON.stringify(
|
11810
|
-
|
11895
|
+
column5.default
|
11811
11896
|
)}'::${sqlTypeLowered}`;
|
11812
|
-
} else if (
|
11897
|
+
} else if (column5.default instanceof Date) {
|
11813
11898
|
if (sqlTypeLowered === "date") {
|
11814
|
-
columnToSet.default = `'${
|
11899
|
+
columnToSet.default = `'${column5.default.toISOString().split("T")[0]}'`;
|
11815
11900
|
} else if (sqlTypeLowered === "timestamp") {
|
11816
|
-
columnToSet.default = `'${
|
11901
|
+
columnToSet.default = `'${column5.default.toISOString().replace("T", " ").slice(0, 23)}'`;
|
11817
11902
|
} else {
|
11818
|
-
columnToSet.default = `'${
|
11903
|
+
columnToSet.default = `'${column5.default.toISOString()}'`;
|
11819
11904
|
}
|
11820
11905
|
} else {
|
11821
|
-
columnToSet.default =
|
11906
|
+
columnToSet.default = column5.default;
|
11822
11907
|
}
|
11823
11908
|
}
|
11824
11909
|
}
|
11825
11910
|
}
|
11826
|
-
columnsObject[
|
11911
|
+
columnsObject[column5.name] = columnToSet;
|
11827
11912
|
});
|
11828
11913
|
primaryKeys.map((pk) => {
|
11829
11914
|
const columnNames = pk.columns.map((c) => c.name);
|
@@ -12303,43 +12388,43 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
12303
12388
|
// json: "::json",
|
12304
12389
|
"character(": "::bpchar"
|
12305
12390
|
};
|
12306
|
-
defaultForColumn = (
|
12307
|
-
if (
|
12391
|
+
defaultForColumn = (column5) => {
|
12392
|
+
if (column5.data_type === "serial" || column5.data_type === "smallserial" || column5.data_type === "bigserial") {
|
12308
12393
|
return void 0;
|
12309
12394
|
}
|
12310
12395
|
const hasDifferentDefaultCast = Object.keys(columnToDefault).find(
|
12311
|
-
(it) =>
|
12396
|
+
(it) => column5.data_type.startsWith(it)
|
12312
12397
|
);
|
12313
|
-
if (
|
12398
|
+
if (column5.column_default === null) {
|
12314
12399
|
return void 0;
|
12315
12400
|
}
|
12316
|
-
const columnDefaultAsString =
|
12401
|
+
const columnDefaultAsString = column5.column_default.toString();
|
12317
12402
|
if (columnDefaultAsString.endsWith(
|
12318
|
-
hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] :
|
12403
|
+
hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column5.data_type
|
12319
12404
|
)) {
|
12320
|
-
const nonPrefixPart =
|
12321
|
-
const rt =
|
12322
|
-
if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !
|
12405
|
+
const nonPrefixPart = column5.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column5.data_type}`).length - 1;
|
12406
|
+
const rt = column5.column_default.toString().substring(1, nonPrefixPart);
|
12407
|
+
if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column5.data_type.startsWith("numeric")) {
|
12323
12408
|
return Number(rt);
|
12324
|
-
} else if (
|
12409
|
+
} else if (column5.data_type === "json" || column5.data_type === "jsonb") {
|
12325
12410
|
const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
|
12326
|
-
return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${
|
12327
|
-
} else if (
|
12328
|
-
return
|
12411
|
+
return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column5.data_type}`}`;
|
12412
|
+
} else if (column5.data_type === "boolean") {
|
12413
|
+
return column5.column_default === "true";
|
12329
12414
|
} else {
|
12330
12415
|
return `'${rt}'`;
|
12331
12416
|
}
|
12332
12417
|
} else {
|
12333
|
-
if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !
|
12418
|
+
if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !column5.data_type.startsWith("numeric")) {
|
12334
12419
|
return Number(columnDefaultAsString);
|
12335
|
-
} else if (
|
12336
|
-
return
|
12420
|
+
} else if (column5.data_type === "boolean") {
|
12421
|
+
return column5.column_default === "true";
|
12337
12422
|
} else {
|
12338
12423
|
return `${columnDefaultAsString}`;
|
12339
12424
|
}
|
12340
12425
|
}
|
12341
12426
|
};
|
12342
|
-
toDrizzle = (schema4) => {
|
12427
|
+
toDrizzle = (schema4, schemaName) => {
|
12343
12428
|
const tables = {};
|
12344
12429
|
Object.values(schema4.tables).forEach((t) => {
|
12345
12430
|
const columns = {};
|
@@ -12413,17 +12498,31 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
12413
12498
|
}
|
12414
12499
|
columns[columnName] = columnBuilder;
|
12415
12500
|
});
|
12416
|
-
|
12417
|
-
|
12418
|
-
|
12419
|
-
|
12420
|
-
|
12421
|
-
|
12422
|
-
|
12423
|
-
|
12501
|
+
if (schemaName === "public") {
|
12502
|
+
tables[t.name] = (0, import_pg_core2.pgTable)(t.name, columns, (cb) => {
|
12503
|
+
const res = {};
|
12504
|
+
Object.values(t.compositePrimaryKeys).forEach((cpk) => {
|
12505
|
+
const gh = cpk.columns.map((c) => cb[c]);
|
12506
|
+
res[cpk.name] = new import_pg_core2.PrimaryKeyBuilder(
|
12507
|
+
gh,
|
12508
|
+
cpk.name
|
12509
|
+
);
|
12510
|
+
});
|
12511
|
+
return res;
|
12424
12512
|
});
|
12425
|
-
|
12426
|
-
|
12513
|
+
} else {
|
12514
|
+
tables[t.name] = (0, import_pg_core2.pgSchema)(schemaName).table(t.name, columns, (cb) => {
|
12515
|
+
const res = {};
|
12516
|
+
Object.values(t.compositePrimaryKeys).forEach((cpk) => {
|
12517
|
+
const gh = cpk.columns.map((c) => cb[c]);
|
12518
|
+
res[cpk.name] = new import_pg_core2.PrimaryKeyBuilder(
|
12519
|
+
gh,
|
12520
|
+
cpk.name
|
12521
|
+
);
|
12522
|
+
});
|
12523
|
+
return res;
|
12524
|
+
});
|
12525
|
+
}
|
12427
12526
|
});
|
12428
12527
|
return tables;
|
12429
12528
|
};
|
@@ -12528,26 +12627,26 @@ var init_sqliteSerializer = __esm({
|
|
12528
12627
|
primaryKeys,
|
12529
12628
|
uniqueConstraints
|
12530
12629
|
} = (0, import_sqlite_core2.getTableConfig)(table4);
|
12531
|
-
columns.forEach((
|
12532
|
-
const notNull =
|
12533
|
-
const primaryKey =
|
12630
|
+
columns.forEach((column5) => {
|
12631
|
+
const notNull = column5.notNull;
|
12632
|
+
const primaryKey = column5.primary;
|
12534
12633
|
const columnToSet = {
|
12535
|
-
name:
|
12536
|
-
type:
|
12634
|
+
name: column5.name,
|
12635
|
+
type: column5.getSQLType(),
|
12537
12636
|
primaryKey,
|
12538
12637
|
notNull,
|
12539
|
-
autoincrement: (0, import_drizzle_orm7.is)(
|
12638
|
+
autoincrement: (0, import_drizzle_orm7.is)(column5, import_sqlite_core2.SQLiteBaseInteger) ? column5.autoIncrement : false
|
12540
12639
|
};
|
12541
|
-
if (
|
12542
|
-
if ((0, import_drizzle_orm7.is)(
|
12543
|
-
columnToSet.default = sqlToStr(
|
12640
|
+
if (column5.default !== void 0) {
|
12641
|
+
if ((0, import_drizzle_orm7.is)(column5.default, import_drizzle_orm7.SQL)) {
|
12642
|
+
columnToSet.default = sqlToStr(column5.default);
|
12544
12643
|
} else {
|
12545
|
-
columnToSet.default = typeof
|
12644
|
+
columnToSet.default = typeof column5.default === "string" ? `'${column5.default}'` : column5.default;
|
12546
12645
|
}
|
12547
12646
|
}
|
12548
|
-
columnsObject[
|
12549
|
-
if (
|
12550
|
-
const existingUnique = indexesObject[
|
12647
|
+
columnsObject[column5.name] = columnToSet;
|
12648
|
+
if (column5.isUnique) {
|
12649
|
+
const existingUnique = indexesObject[column5.uniqueName];
|
12551
12650
|
if (typeof existingUnique !== "undefined") {
|
12552
12651
|
console.log(
|
12553
12652
|
`
|
@@ -12555,9 +12654,9 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
12555
12654
|
tableName
|
12556
12655
|
)} table.
|
12557
12656
|
The unique constraint ${source_default.underline.blue(
|
12558
|
-
|
12657
|
+
column5.uniqueName
|
12559
12658
|
)} on the ${source_default.underline.blue(
|
12560
|
-
|
12659
|
+
column5.name
|
12561
12660
|
)} column is confilcting with a unique constraint name already defined for ${source_default.underline.blue(
|
12562
12661
|
existingUnique.columns.join(",")
|
12563
12662
|
)} columns
|
@@ -12565,8 +12664,8 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
|
|
12565
12664
|
);
|
12566
12665
|
process.exit(1);
|
12567
12666
|
}
|
12568
|
-
indexesObject[
|
12569
|
-
name:
|
12667
|
+
indexesObject[column5.uniqueName] = {
|
12668
|
+
name: column5.uniqueName,
|
12570
12669
|
columns: [columnToSet.name],
|
12571
12670
|
isUnique: true
|
12572
12671
|
};
|
@@ -12698,23 +12797,23 @@ The unique constraint ${source_default.underline.blue(
|
|
12698
12797
|
let indexesCount = 0;
|
12699
12798
|
let foreignKeysCount = 0;
|
12700
12799
|
const tableToPk = {};
|
12701
|
-
for (const
|
12702
|
-
if (!tablesFilter(
|
12800
|
+
for (const column5 of columns) {
|
12801
|
+
if (!tablesFilter(column5.tableName))
|
12703
12802
|
continue;
|
12704
12803
|
columnsCount += 1;
|
12705
12804
|
if (progressCallback) {
|
12706
12805
|
progressCallback("columns", columnsCount, "fetching");
|
12707
12806
|
}
|
12708
|
-
const tableName =
|
12807
|
+
const tableName = column5.tableName;
|
12709
12808
|
tablesCount.add(tableName);
|
12710
12809
|
if (progressCallback) {
|
12711
12810
|
progressCallback("tables", tablesCount.size, "fetching");
|
12712
12811
|
}
|
12713
|
-
const columnName =
|
12714
|
-
const isNotNull =
|
12715
|
-
const columnType =
|
12716
|
-
const isPrimary =
|
12717
|
-
const columnDefault =
|
12812
|
+
const columnName = column5.columnName;
|
12813
|
+
const isNotNull = column5.notNull === 1;
|
12814
|
+
const columnType = column5.columnType;
|
12815
|
+
const isPrimary = column5.pk !== 0;
|
12816
|
+
const columnDefault = column5.defaultValue;
|
12718
12817
|
const isAutoincrement = isPrimary && tablesWithSeq.includes(tableName);
|
12719
12818
|
if (isPrimary) {
|
12720
12819
|
if (typeof tableToPk[tableName] === "undefined") {
|
@@ -14977,13 +15076,13 @@ var init_sqlgenerator = __esm({
|
|
14977
15076
|
statement += `CREATE TABLE IF NOT EXISTS ${name} (
|
14978
15077
|
`;
|
14979
15078
|
for (let i = 0; i < columns.length; i++) {
|
14980
|
-
const
|
14981
|
-
const primaryKeyStatement =
|
14982
|
-
const notNullStatement =
|
14983
|
-
const defaultStatement =
|
14984
|
-
const uniqueConstraint4 =
|
14985
|
-
const type = isPgNativeType(
|
14986
|
-
statement += ` "${
|
15079
|
+
const column5 = columns[i];
|
15080
|
+
const primaryKeyStatement = column5.primaryKey ? " PRIMARY KEY" : "";
|
15081
|
+
const notNullStatement = column5.notNull ? " NOT NULL" : "";
|
15082
|
+
const defaultStatement = column5.default !== void 0 ? ` DEFAULT ${column5.default}` : "";
|
15083
|
+
const uniqueConstraint4 = column5.isUnique ? ` CONSTRAINT "${column5.uniqueName}" UNIQUE${column5.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}` : "";
|
15084
|
+
const type = isPgNativeType(column5.type) ? column5.type : `"${column5.type}"`;
|
15085
|
+
statement += ` "${column5.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${uniqueConstraint4}`;
|
14987
15086
|
statement += i === columns.length - 1 ? "" : ",\n";
|
14988
15087
|
}
|
14989
15088
|
if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
|
@@ -15016,13 +15115,13 @@ var init_sqlgenerator = __esm({
|
|
15016
15115
|
statement += `CREATE TABLE ${tName} (
|
15017
15116
|
`;
|
15018
15117
|
for (let i = 0; i < columns.length; i++) {
|
15019
|
-
const
|
15020
|
-
const primaryKeyStatement =
|
15021
|
-
const notNullStatement =
|
15022
|
-
const defaultStatement =
|
15023
|
-
const onUpdateStatement =
|
15024
|
-
const autoincrementStatement =
|
15025
|
-
statement += ` \`${
|
15118
|
+
const column5 = columns[i];
|
15119
|
+
const primaryKeyStatement = column5.primaryKey ? " PRIMARY KEY" : "";
|
15120
|
+
const notNullStatement = column5.notNull ? " NOT NULL" : "";
|
15121
|
+
const defaultStatement = column5.default !== void 0 ? ` DEFAULT ${column5.default}` : "";
|
15122
|
+
const onUpdateStatement = column5.onUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
|
15123
|
+
const autoincrementStatement = column5.autoincrement ? " AUTO_INCREMENT" : "";
|
15124
|
+
statement += ` \`${column5.name}\` ${column5.type}${autoincrementStatement}${primaryKeyStatement}${notNullStatement}${defaultStatement}${onUpdateStatement}`;
|
15026
15125
|
statement += i === columns.length - 1 ? "" : ",\n";
|
15027
15126
|
}
|
15028
15127
|
if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
|
@@ -15060,13 +15159,13 @@ var init_sqlgenerator = __esm({
|
|
15060
15159
|
statement += `CREATE TABLE \`${tableName}\` (
|
15061
15160
|
`;
|
15062
15161
|
for (let i = 0; i < columns.length; i++) {
|
15063
|
-
const
|
15064
|
-
const primaryKeyStatement =
|
15065
|
-
const notNullStatement =
|
15066
|
-
const defaultStatement =
|
15067
|
-
const autoincrementStatement =
|
15162
|
+
const column5 = columns[i];
|
15163
|
+
const primaryKeyStatement = column5.primaryKey ? " PRIMARY KEY" : "";
|
15164
|
+
const notNullStatement = column5.notNull ? " NOT NULL" : "";
|
15165
|
+
const defaultStatement = column5.default !== void 0 ? ` DEFAULT ${column5.default}` : "";
|
15166
|
+
const autoincrementStatement = column5.autoincrement ? " AUTOINCREMENT" : "";
|
15068
15167
|
statement += " ";
|
15069
|
-
statement += `\`${
|
15168
|
+
statement += `\`${column5.name}\` ${column5.type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${notNullStatement}`;
|
15070
15169
|
statement += i === columns.length - 1 ? "" : ",\n";
|
15071
15170
|
}
|
15072
15171
|
compositePKs.forEach((it) => {
|
@@ -15324,11 +15423,11 @@ var init_sqlgenerator = __esm({
|
|
15324
15423
|
return statement.type === "alter_table_add_column" && dialect6 === "pg";
|
15325
15424
|
}
|
15326
15425
|
convert(statement) {
|
15327
|
-
const { tableName, column:
|
15328
|
-
const { name, type, notNull } =
|
15426
|
+
const { tableName, column: column5, schema: schema4 } = statement;
|
15427
|
+
const { name, type, notNull } = column5;
|
15329
15428
|
const tableNameWithSchema = schema4 ? `"${schema4}"."${tableName}"` : `"${tableName}"`;
|
15330
|
-
const defaultStatement = `${
|
15331
|
-
const fixedType = isPgNativeType(
|
15429
|
+
const defaultStatement = `${column5.default !== void 0 ? ` DEFAULT ${column5.default}` : ""}`;
|
15430
|
+
const fixedType = isPgNativeType(column5.type) ? column5.type : `"${column5.type}"`;
|
15332
15431
|
const notNullStatement = `${notNull ? " NOT NULL" : ""}`;
|
15333
15432
|
return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN "${name}" ${fixedType}${defaultStatement}${notNullStatement};`;
|
15334
15433
|
}
|
@@ -15338,9 +15437,9 @@ var init_sqlgenerator = __esm({
|
|
15338
15437
|
return statement.type === "alter_table_add_column" && dialect6 === "mysql";
|
15339
15438
|
}
|
15340
15439
|
convert(statement) {
|
15341
|
-
const { tableName, column:
|
15342
|
-
const { name, type, notNull, primaryKey, autoincrement, onUpdate } =
|
15343
|
-
const defaultStatement = `${
|
15440
|
+
const { tableName, column: column5 } = statement;
|
15441
|
+
const { name, type, notNull, primaryKey, autoincrement, onUpdate } = column5;
|
15442
|
+
const defaultStatement = `${column5.default !== void 0 ? ` DEFAULT ${column5.default}` : ""}`;
|
15344
15443
|
const notNullStatement = `${notNull ? " NOT NULL" : ""}`;
|
15345
15444
|
const primaryKeyStatement = `${primaryKey ? " PRIMARY KEY" : ""}`;
|
15346
15445
|
const autoincrementStatement = `${autoincrement ? " AUTO_INCREMENT" : ""}`;
|
@@ -15353,9 +15452,9 @@ var init_sqlgenerator = __esm({
|
|
15353
15452
|
return statement.type === "sqlite_alter_table_add_column" && dialect6 === "sqlite";
|
15354
15453
|
}
|
15355
15454
|
convert(statement) {
|
15356
|
-
const { tableName, column:
|
15357
|
-
const { name, type, notNull, primaryKey } =
|
15358
|
-
const defaultStatement = `${
|
15455
|
+
const { tableName, column: column5, referenceData } = statement;
|
15456
|
+
const { name, type, notNull, primaryKey } = column5;
|
15457
|
+
const defaultStatement = `${column5.default !== void 0 ? ` DEFAULT ${column5.default}` : ""}`;
|
15359
15458
|
const notNullStatement = `${notNull ? " NOT NULL" : ""}`;
|
15360
15459
|
const primaryKeyStatement = `${primaryKey ? " PRIMARY KEY" : ""}`;
|
15361
15460
|
const referenceAsObject = referenceData ? SQLiteSquasher.unsquashFK(referenceData) : void 0;
|
@@ -16398,15 +16497,15 @@ var init_jsonStatements = __esm({
|
|
16398
16497
|
let statements = [];
|
16399
16498
|
let dropPkStatements = [];
|
16400
16499
|
let setPkStatements = [];
|
16401
|
-
for (const
|
16402
|
-
const columnName = typeof
|
16500
|
+
for (const column5 of columns) {
|
16501
|
+
const columnName = typeof column5.name !== "string" ? column5.name.new : column5.name;
|
16403
16502
|
const columnType = json2.tables[tableName].columns[columnName].type;
|
16404
16503
|
const columnDefault = json2.tables[tableName].columns[columnName].default;
|
16405
16504
|
const columnOnUpdate = json2.tables[tableName].columns[columnName].onUpdate;
|
16406
16505
|
const columnNotNull = json2.tables[tableName].columns[columnName].notNull;
|
16407
16506
|
const columnAutoIncrement = json2.tables[tableName].columns[columnName].autoincrement;
|
16408
16507
|
const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
|
16409
|
-
if (((_a3 =
|
16508
|
+
if (((_a3 = column5.autoincrement) == null ? void 0 : _a3.type) === "added") {
|
16410
16509
|
statements.push({
|
16411
16510
|
type: "alter_table_alter_column_set_autoincrement",
|
16412
16511
|
tableName,
|
@@ -16420,8 +16519,8 @@ var init_jsonStatements = __esm({
|
|
16420
16519
|
columnPk
|
16421
16520
|
});
|
16422
16521
|
}
|
16423
|
-
if (((_b =
|
16424
|
-
const type =
|
16522
|
+
if (((_b = column5.autoincrement) == null ? void 0 : _b.type) === "changed") {
|
16523
|
+
const type = column5.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
|
16425
16524
|
statements.push({
|
16426
16525
|
type,
|
16427
16526
|
tableName,
|
@@ -16435,7 +16534,7 @@ var init_jsonStatements = __esm({
|
|
16435
16534
|
columnPk
|
16436
16535
|
});
|
16437
16536
|
}
|
16438
|
-
if (((_c =
|
16537
|
+
if (((_c = column5.autoincrement) == null ? void 0 : _c.type) === "deleted") {
|
16439
16538
|
statements.push({
|
16440
16539
|
type: "alter_table_alter_column_drop_autoincrement",
|
16441
16540
|
tableName,
|
@@ -16450,8 +16549,8 @@ var init_jsonStatements = __esm({
|
|
16450
16549
|
});
|
16451
16550
|
}
|
16452
16551
|
}
|
16453
|
-
for (const
|
16454
|
-
const columnName = typeof
|
16552
|
+
for (const column5 of columns) {
|
16553
|
+
const columnName = typeof column5.name !== "string" ? column5.name.new : column5.name;
|
16455
16554
|
const columnType = json2.tables[tableName].columns[columnName].type;
|
16456
16555
|
const columnDefault = json2.tables[tableName].columns[columnName].default;
|
16457
16556
|
const columnOnUpdate = json2.tables[tableName].columns[columnName].onUpdate;
|
@@ -16459,22 +16558,22 @@ var init_jsonStatements = __esm({
|
|
16459
16558
|
const columnAutoIncrement = json2.tables[tableName].columns[columnName].autoincrement;
|
16460
16559
|
const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
|
16461
16560
|
const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
|
16462
|
-
if (typeof
|
16561
|
+
if (typeof column5.name !== "string") {
|
16463
16562
|
statements.push({
|
16464
16563
|
type: "alter_table_rename_column",
|
16465
16564
|
tableName,
|
16466
|
-
oldColumnName:
|
16467
|
-
newColumnName:
|
16565
|
+
oldColumnName: column5.name.old,
|
16566
|
+
newColumnName: column5.name.new,
|
16468
16567
|
schema: schema4
|
16469
16568
|
});
|
16470
16569
|
}
|
16471
|
-
if (((_d =
|
16570
|
+
if (((_d = column5.type) == null ? void 0 : _d.type) === "changed") {
|
16472
16571
|
statements.push({
|
16473
16572
|
type: "alter_table_alter_column_set_type",
|
16474
16573
|
tableName,
|
16475
16574
|
columnName,
|
16476
|
-
newDataType:
|
16477
|
-
oldDataType:
|
16575
|
+
newDataType: column5.type.new,
|
16576
|
+
oldDataType: column5.type.old,
|
16478
16577
|
schema: schema4,
|
16479
16578
|
columnDefault,
|
16480
16579
|
columnOnUpdate,
|
@@ -16483,7 +16582,7 @@ var init_jsonStatements = __esm({
|
|
16483
16582
|
columnPk
|
16484
16583
|
});
|
16485
16584
|
}
|
16486
|
-
if (((_e =
|
16585
|
+
if (((_e = column5.primaryKey) == null ? void 0 : _e.type) === "deleted" || ((_f = column5.primaryKey) == null ? void 0 : _f.type) === "changed" && !column5.primaryKey.new && typeof compositePk === "undefined") {
|
16487
16586
|
dropPkStatements.push({
|
16488
16587
|
////
|
16489
16588
|
type: "alter_table_alter_column_drop_pk",
|
@@ -16492,12 +16591,12 @@ var init_jsonStatements = __esm({
|
|
16492
16591
|
schema: schema4
|
16493
16592
|
});
|
16494
16593
|
}
|
16495
|
-
if (((_g =
|
16594
|
+
if (((_g = column5.default) == null ? void 0 : _g.type) === "added") {
|
16496
16595
|
statements.push({
|
16497
16596
|
type: "alter_table_alter_column_set_default",
|
16498
16597
|
tableName,
|
16499
16598
|
columnName,
|
16500
|
-
newDefaultValue:
|
16599
|
+
newDefaultValue: column5.default.value,
|
16501
16600
|
schema: schema4,
|
16502
16601
|
columnOnUpdate,
|
16503
16602
|
columnNotNull,
|
@@ -16506,13 +16605,13 @@ var init_jsonStatements = __esm({
|
|
16506
16605
|
columnPk
|
16507
16606
|
});
|
16508
16607
|
}
|
16509
|
-
if (((_h =
|
16608
|
+
if (((_h = column5.default) == null ? void 0 : _h.type) === "changed") {
|
16510
16609
|
statements.push({
|
16511
16610
|
type: "alter_table_alter_column_set_default",
|
16512
16611
|
tableName,
|
16513
16612
|
columnName,
|
16514
|
-
newDefaultValue:
|
16515
|
-
oldDefaultValue:
|
16613
|
+
newDefaultValue: column5.default.new,
|
16614
|
+
oldDefaultValue: column5.default.old,
|
16516
16615
|
schema: schema4,
|
16517
16616
|
columnOnUpdate,
|
16518
16617
|
columnNotNull,
|
@@ -16521,7 +16620,7 @@ var init_jsonStatements = __esm({
|
|
16521
16620
|
columnPk
|
16522
16621
|
});
|
16523
16622
|
}
|
16524
|
-
if (((_i =
|
16623
|
+
if (((_i = column5.default) == null ? void 0 : _i.type) === "deleted") {
|
16525
16624
|
statements.push({
|
16526
16625
|
type: "alter_table_alter_column_drop_default",
|
16527
16626
|
tableName,
|
@@ -16535,7 +16634,7 @@ var init_jsonStatements = __esm({
|
|
16535
16634
|
columnPk
|
16536
16635
|
});
|
16537
16636
|
}
|
16538
|
-
if (((_j =
|
16637
|
+
if (((_j = column5.notNull) == null ? void 0 : _j.type) === "added") {
|
16539
16638
|
statements.push({
|
16540
16639
|
type: "alter_table_alter_column_set_notnull",
|
16541
16640
|
tableName,
|
@@ -16549,8 +16648,8 @@ var init_jsonStatements = __esm({
|
|
16549
16648
|
columnPk
|
16550
16649
|
});
|
16551
16650
|
}
|
16552
|
-
if (((_k =
|
16553
|
-
const type =
|
16651
|
+
if (((_k = column5.notNull) == null ? void 0 : _k.type) === "changed") {
|
16652
|
+
const type = column5.notNull.new ? "alter_table_alter_column_set_notnull" : "alter_table_alter_column_drop_notnull";
|
16554
16653
|
statements.push({
|
16555
16654
|
type,
|
16556
16655
|
tableName,
|
@@ -16564,7 +16663,7 @@ var init_jsonStatements = __esm({
|
|
16564
16663
|
columnPk
|
16565
16664
|
});
|
16566
16665
|
}
|
16567
|
-
if (((_l =
|
16666
|
+
if (((_l = column5.notNull) == null ? void 0 : _l.type) === "deleted") {
|
16568
16667
|
statements.push({
|
16569
16668
|
type: "alter_table_alter_column_drop_notnull",
|
16570
16669
|
tableName,
|
@@ -16578,7 +16677,7 @@ var init_jsonStatements = __esm({
|
|
16578
16677
|
columnPk
|
16579
16678
|
});
|
16580
16679
|
}
|
16581
|
-
if (((_m =
|
16680
|
+
if (((_m = column5.primaryKey) == null ? void 0 : _m.type) === "added" || ((_n = column5.primaryKey) == null ? void 0 : _n.type) === "changed" && column5.primaryKey.new) {
|
16582
16681
|
const wasAutoincrement = statements.filter(
|
16583
16682
|
(it) => it.type === "alter_table_alter_column_set_autoincrement"
|
16584
16683
|
);
|
@@ -16591,7 +16690,7 @@ var init_jsonStatements = __esm({
|
|
16591
16690
|
});
|
16592
16691
|
}
|
16593
16692
|
}
|
16594
|
-
if (((_o =
|
16693
|
+
if (((_o = column5.onUpdate) == null ? void 0 : _o.type) === "added") {
|
16595
16694
|
statements.push({
|
16596
16695
|
type: "alter_table_alter_column_set_on_update",
|
16597
16696
|
tableName,
|
@@ -16605,7 +16704,7 @@ var init_jsonStatements = __esm({
|
|
16605
16704
|
columnPk
|
16606
16705
|
});
|
16607
16706
|
}
|
16608
|
-
if (((_p =
|
16707
|
+
if (((_p = column5.onUpdate) == null ? void 0 : _p.type) === "deleted") {
|
16609
16708
|
statements.push({
|
16610
16709
|
type: "alter_table_alter_column_drop_on_update",
|
16611
16710
|
tableName,
|
@@ -17673,10 +17772,587 @@ var init_utils2 = __esm({
|
|
17673
17772
|
});
|
17674
17773
|
|
17675
17774
|
// src/introspect.ts
|
17775
|
+
var pgImportsList, objToStatement2, timeConfig, possibleIntervals, intervalStrToObj, intervalConfig, importsPatch, relations, withCasing, schemaToTypeScript, isCyclic, isSelf, column4, dimensionsInArray, createTableColumns, createTableIndexes, createTablePKs, createTableUniques, createTableFKs;
|
17676
17776
|
var init_introspect = __esm({
|
17677
17777
|
"src/introspect.ts"() {
|
17678
17778
|
init_utils2();
|
17679
17779
|
init_pgSerializer();
|
17780
|
+
pgImportsList = /* @__PURE__ */ new Set([
|
17781
|
+
"pgTable",
|
17782
|
+
"pgEnum",
|
17783
|
+
"smallint",
|
17784
|
+
"integer",
|
17785
|
+
"bigint",
|
17786
|
+
"boolean",
|
17787
|
+
"text",
|
17788
|
+
"varchar",
|
17789
|
+
"char",
|
17790
|
+
"serial",
|
17791
|
+
"smallserial",
|
17792
|
+
"bigserial",
|
17793
|
+
"decimal",
|
17794
|
+
"numeric",
|
17795
|
+
"real",
|
17796
|
+
"json",
|
17797
|
+
"jsonb",
|
17798
|
+
"time",
|
17799
|
+
"timestamp",
|
17800
|
+
"date",
|
17801
|
+
"interval",
|
17802
|
+
"cidr",
|
17803
|
+
"inet",
|
17804
|
+
"macaddr",
|
17805
|
+
"macaddr8",
|
17806
|
+
"bigint",
|
17807
|
+
"doublePrecision",
|
17808
|
+
"uuid"
|
17809
|
+
]);
|
17810
|
+
objToStatement2 = (json) => {
|
17811
|
+
json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
|
17812
|
+
const keys = Object.keys(json);
|
17813
|
+
if (keys.length === 0)
|
17814
|
+
return;
|
17815
|
+
let statement = "{ ";
|
17816
|
+
statement += keys.map((it) => `${it}: "${json[it]}"`).join(", ");
|
17817
|
+
statement += " }";
|
17818
|
+
return statement;
|
17819
|
+
};
|
17820
|
+
timeConfig = (json) => {
|
17821
|
+
json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
|
17822
|
+
const keys = Object.keys(json);
|
17823
|
+
if (keys.length === 0)
|
17824
|
+
return;
|
17825
|
+
let statement = "{ ";
|
17826
|
+
statement += keys.map((it) => `${it}: ${json[it]}`).join(", ");
|
17827
|
+
statement += " }";
|
17828
|
+
return statement;
|
17829
|
+
};
|
17830
|
+
possibleIntervals = [
|
17831
|
+
"year",
|
17832
|
+
"month",
|
17833
|
+
"day",
|
17834
|
+
"hour",
|
17835
|
+
"minute",
|
17836
|
+
"second",
|
17837
|
+
"year to month",
|
17838
|
+
"day to hour",
|
17839
|
+
"day to minute",
|
17840
|
+
"day to second",
|
17841
|
+
"hour to minute",
|
17842
|
+
"hour to second",
|
17843
|
+
"minute to second"
|
17844
|
+
];
|
17845
|
+
intervalStrToObj = (str) => {
|
17846
|
+
if (str.startsWith("interval(")) {
|
17847
|
+
return {
|
17848
|
+
precision: Number(str.substring("interval(".length, str.length - 1))
|
17849
|
+
};
|
17850
|
+
}
|
17851
|
+
const splitted = str.split(" ");
|
17852
|
+
if (splitted.length === 1) {
|
17853
|
+
return {};
|
17854
|
+
}
|
17855
|
+
const rest = splitted.slice(1, splitted.length).join(" ");
|
17856
|
+
if (possibleIntervals.includes(rest)) {
|
17857
|
+
return { fields: `"${rest}"` };
|
17858
|
+
}
|
17859
|
+
for (const s of possibleIntervals) {
|
17860
|
+
if (rest.startsWith(`${s}(`)) {
|
17861
|
+
return {
|
17862
|
+
fields: `"${s}"`,
|
17863
|
+
precision: Number(rest.substring(s.length + 1, rest.length - 1))
|
17864
|
+
};
|
17865
|
+
}
|
17866
|
+
}
|
17867
|
+
return {};
|
17868
|
+
};
|
17869
|
+
intervalConfig = (str) => {
|
17870
|
+
let json = intervalStrToObj(str);
|
17871
|
+
const keys = Object.keys(json);
|
17872
|
+
if (keys.length === 0)
|
17873
|
+
return;
|
17874
|
+
let statement = "{ ";
|
17875
|
+
statement += keys.map((it) => `${it}: ${json[it]}`).join(", ");
|
17876
|
+
statement += " }";
|
17877
|
+
return statement;
|
17878
|
+
};
|
17879
|
+
importsPatch = {
|
17880
|
+
"double precision": "doublePrecision",
|
17881
|
+
"timestamp without time zone": "timestamp",
|
17882
|
+
"timestamp with time zone": "timestamp",
|
17883
|
+
"time without time zone": "time",
|
17884
|
+
"time with time zone": "time"
|
17885
|
+
};
|
17886
|
+
relations = /* @__PURE__ */ new Set();
|
17887
|
+
withCasing = (value, casing) => {
|
17888
|
+
if (typeof casing === "undefined") {
|
17889
|
+
return value;
|
17890
|
+
}
|
17891
|
+
if (casing.casing === "camel") {
|
17892
|
+
return value.camelCase();
|
17893
|
+
}
|
17894
|
+
return value;
|
17895
|
+
};
|
17896
|
+
schemaToTypeScript = (schema4, casing) => {
|
17897
|
+
Object.values(schema4.tables).forEach((table4) => {
|
17898
|
+
Object.values(table4.foreignKeys).forEach((fk4) => {
|
17899
|
+
const relation = `${fk4.tableFrom}-${fk4.tableTo}`;
|
17900
|
+
relations.add(relation);
|
17901
|
+
});
|
17902
|
+
});
|
17903
|
+
const schemas = Object.fromEntries(
|
17904
|
+
Object.entries(schema4.schemas).map((it) => {
|
17905
|
+
return [it[0], withCasing(it[1], casing)];
|
17906
|
+
})
|
17907
|
+
);
|
17908
|
+
const enumTypes = new Set(Object.values(schema4.enums).map((it) => it.name));
|
17909
|
+
const imports = Object.values(schema4.tables).reduce(
|
17910
|
+
(res, it) => {
|
17911
|
+
const idxImports = Object.values(it.indexes).map(
|
17912
|
+
(idx) => idx.isUnique ? "uniqueIndex" : "index"
|
17913
|
+
);
|
17914
|
+
const fkImpots = Object.values(it.foreignKeys).map((it2) => "foreignKey");
|
17915
|
+
if (Object.values(it.foreignKeys).some((it2) => isCyclic(it2) && !isSelf(it2))) {
|
17916
|
+
res.pg.push("type AnyPgColumn");
|
17917
|
+
}
|
17918
|
+
const pkImports = Object.values(it.compositePrimaryKeys).map(
|
17919
|
+
(it2) => "primaryKey"
|
17920
|
+
);
|
17921
|
+
const uniqueImports = Object.values(it.uniqueConstraints).map(
|
17922
|
+
(it2) => "unique"
|
17923
|
+
);
|
17924
|
+
if (it.schema && it.schema !== "public" && it.schema !== "") {
|
17925
|
+
res.pg.push("pgSchema");
|
17926
|
+
}
|
17927
|
+
res.pg.push(...idxImports);
|
17928
|
+
res.pg.push(...fkImpots);
|
17929
|
+
res.pg.push(...pkImports);
|
17930
|
+
res.pg.push(...uniqueImports);
|
17931
|
+
if (enumTypes.size > 0) {
|
17932
|
+
res.pg.push("pgEnum");
|
17933
|
+
}
|
17934
|
+
const columnImports = Object.values(it.columns).map((col) => {
|
17935
|
+
let patched = importsPatch[col.type] ?? col.type;
|
17936
|
+
patched = patched.startsWith("varchar(") ? "varchar" : patched;
|
17937
|
+
patched = patched.startsWith("char(") ? "char" : patched;
|
17938
|
+
patched = patched.startsWith("numeric(") ? "numeric" : patched;
|
17939
|
+
patched = patched.startsWith("time(") ? "time" : patched;
|
17940
|
+
patched = patched.startsWith("timestamp(") ? "timestamp" : patched;
|
17941
|
+
return patched;
|
17942
|
+
}).filter((type) => {
|
17943
|
+
return pgImportsList.has(type);
|
17944
|
+
});
|
17945
|
+
res.pg.push(...columnImports);
|
17946
|
+
return res;
|
17947
|
+
},
|
17948
|
+
{ pg: [] }
|
17949
|
+
);
|
17950
|
+
const enumStatements = Object.values(schema4.enums).map((it) => {
|
17951
|
+
const values = Object.values(it.values).map((it2) => `'${it2}'`).join(", ");
|
17952
|
+
return `export const ${withCasing(it.name, casing)} = pgEnum("${it.name}", [${values}])
|
17953
|
+
`;
|
17954
|
+
}).join("").concat("\n");
|
17955
|
+
const schemaStatements = Object.entries(schemas).map((it) => {
|
17956
|
+
return `export const ${it[1]} = pgSchema("${it[0]}");
|
17957
|
+
`;
|
17958
|
+
}).join();
|
17959
|
+
const tableStatements = Object.values(schema4.tables).map((table4) => {
|
17960
|
+
const tableSchema = schemas[table4.schema];
|
17961
|
+
const func = tableSchema ? `${tableSchema}.table` : "pgTable";
|
17962
|
+
let statement = `export const ${withCasing(
|
17963
|
+
table4.name,
|
17964
|
+
casing
|
17965
|
+
)} = ${func}("${table4.name}", {
|
17966
|
+
`;
|
17967
|
+
statement += createTableColumns(
|
17968
|
+
table4.name,
|
17969
|
+
Object.values(table4.columns),
|
17970
|
+
Object.values(table4.foreignKeys),
|
17971
|
+
enumTypes,
|
17972
|
+
casing,
|
17973
|
+
schema4.internal
|
17974
|
+
);
|
17975
|
+
statement += "}";
|
17976
|
+
const filteredFKs = Object.values(table4.foreignKeys).filter((it) => {
|
17977
|
+
return it.columnsFrom.length > 1 || isSelf(it);
|
17978
|
+
});
|
17979
|
+
if (Object.keys(table4.indexes).length > 0 || filteredFKs.length > 0 || Object.keys(table4.compositePrimaryKeys).length > 0 || Object.keys(table4.uniqueConstraints).length > 0) {
|
17980
|
+
statement += ",\n";
|
17981
|
+
statement += "(table) => {\n";
|
17982
|
+
statement += " return {\n";
|
17983
|
+
statement += createTableIndexes(
|
17984
|
+
table4.name,
|
17985
|
+
Object.values(table4.indexes),
|
17986
|
+
casing
|
17987
|
+
);
|
17988
|
+
statement += createTableFKs(Object.values(filteredFKs), casing);
|
17989
|
+
statement += createTablePKs(
|
17990
|
+
Object.values(table4.compositePrimaryKeys),
|
17991
|
+
casing
|
17992
|
+
);
|
17993
|
+
statement += createTableUniques(
|
17994
|
+
Object.values(table4.uniqueConstraints),
|
17995
|
+
casing
|
17996
|
+
);
|
17997
|
+
statement += " }\n";
|
17998
|
+
statement += "}";
|
17999
|
+
}
|
18000
|
+
statement += ");";
|
18001
|
+
return statement;
|
18002
|
+
});
|
18003
|
+
const uniquePgImports = ["pgTable", ...new Set(imports.pg)];
|
18004
|
+
let result = `import { ${uniquePgImports.join(
|
18005
|
+
", "
|
18006
|
+
)} } from "drizzle-orm/pg-core"
|
18007
|
+
|
18008
|
+
`;
|
18009
|
+
result += `import { sql } from "drizzle-orm"
|
18010
|
+
`;
|
18011
|
+
result += enumStatements;
|
18012
|
+
result += schemaStatements;
|
18013
|
+
result += "\n";
|
18014
|
+
result += tableStatements.join("\n\n");
|
18015
|
+
return result;
|
18016
|
+
};
|
18017
|
+
isCyclic = (fk4) => {
|
18018
|
+
const key = `${fk4.tableFrom}-${fk4.tableTo}`;
|
18019
|
+
const reverse = `${fk4.tableTo}-${fk4.tableFrom}`;
|
18020
|
+
return relations.has(key) && relations.has(reverse);
|
18021
|
+
};
|
18022
|
+
isSelf = (fk4) => {
|
18023
|
+
return fk4.tableFrom === fk4.tableTo;
|
18024
|
+
};
|
18025
|
+
column4 = (tableName, type, name, enumTypes, defaultValue, casing, internals) => {
|
18026
|
+
var _a3;
|
18027
|
+
const lowered = type.toLowerCase();
|
18028
|
+
if (lowered.startsWith("serial")) {
|
18029
|
+
return `${withCasing(name, casing)}: serial("${name}")`;
|
18030
|
+
}
|
18031
|
+
if (lowered.startsWith("smallserial")) {
|
18032
|
+
return `${withCasing(name, casing)}: smallserial("${name}")`;
|
18033
|
+
}
|
18034
|
+
if (lowered.startsWith("bigserial")) {
|
18035
|
+
return `${withCasing(
|
18036
|
+
name,
|
18037
|
+
casing
|
18038
|
+
)}: bigserial("${name}", { mode: "bigint" })`;
|
18039
|
+
}
|
18040
|
+
if (lowered.startsWith("integer")) {
|
18041
|
+
let out = `${withCasing(name, casing)}: integer("${name}")`;
|
18042
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18043
|
+
return out;
|
18044
|
+
}
|
18045
|
+
if (lowered.startsWith("smallint")) {
|
18046
|
+
let out = `${withCasing(name, casing)}: smallint("${name}")`;
|
18047
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18048
|
+
return out;
|
18049
|
+
}
|
18050
|
+
if (lowered.startsWith("bigint")) {
|
18051
|
+
let out = `// You can use { mode: "bigint" } if numbers are exceeding js number limitations
|
18052
|
+
`;
|
18053
|
+
out += `${withCasing(name, casing)}: bigint("${name}", { mode: "number" })`;
|
18054
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18055
|
+
return out;
|
18056
|
+
}
|
18057
|
+
if (lowered.startsWith("boolean")) {
|
18058
|
+
let out = `${withCasing(name, casing)}: boolean("${name}")`;
|
18059
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18060
|
+
return out;
|
18061
|
+
}
|
18062
|
+
if (lowered.startsWith("double precision")) {
|
18063
|
+
let out = `${withCasing(name, casing)}: doublePrecision("${name}")`;
|
18064
|
+
out += defaultValue ? `.default(${defaultValue})` : "";
|
18065
|
+
return out;
|
18066
|
+
}
|
18067
|
+
if (lowered.startsWith("real")) {
|
18068
|
+
let out = `${withCasing(name, casing)}: real("${name}")`;
|
18069
|
+
out += defaultValue ? `.default(${defaultValue})` : "";
|
18070
|
+
return out;
|
18071
|
+
}
|
18072
|
+
if (lowered.startsWith("uuid")) {
|
18073
|
+
let out = `${withCasing(name, casing)}: uuid("${name}")`;
|
18074
|
+
out += defaultValue === "gen_random_uuid()" ? ".defaultRandom()" : defaultValue ? `.default(sql\`${defaultValue}\`)` : "";
|
18075
|
+
return out;
|
18076
|
+
}
|
18077
|
+
if (lowered.startsWith("numeric")) {
|
18078
|
+
let params;
|
18079
|
+
if (lowered.length > 7) {
|
18080
|
+
const [precision, scale] = lowered.slice(8, lowered.length - 1).split(",");
|
18081
|
+
params = { precision, scale };
|
18082
|
+
}
|
18083
|
+
let out = params ? `${withCasing(name, casing)}: numeric("${name}", ${timeConfig(params)})` : `${withCasing(name, casing)}: numeric("${name}")`;
|
18084
|
+
defaultValue = defaultValue ? defaultValue.startsWith(`'`) && defaultValue.endsWith(`'`) ? defaultValue.substring(1, defaultValue.length - 1) : defaultValue : void 0;
|
18085
|
+
out += defaultValue ? `.default('${defaultValue}')` : "";
|
18086
|
+
return out;
|
18087
|
+
}
|
18088
|
+
if (lowered.startsWith("timestamp")) {
|
18089
|
+
const withTimezone = lowered.includes("with time zone");
|
18090
|
+
let precision = lowered.startsWith("timestamp(") ? Number(
|
18091
|
+
lowered.split(" ")[0].substring("timestamp(".length, lowered.split(" ")[0].length - 1)
|
18092
|
+
) : null;
|
18093
|
+
precision = precision ? precision : null;
|
18094
|
+
const params = timeConfig({
|
18095
|
+
precision,
|
18096
|
+
withTimezone,
|
18097
|
+
mode: "'string'"
|
18098
|
+
});
|
18099
|
+
let out = params ? `${withCasing(name, casing)}: timestamp("${name}", ${params})` : `${withCasing(name, casing)}: timestamp("${name}")`;
|
18100
|
+
defaultValue = defaultValue === "now()" || defaultValue === "CURRENT_TIMESTAMP" ? ".defaultNow()" : defaultValue ? `.default(${defaultValue})` : "";
|
18101
|
+
out += defaultValue;
|
18102
|
+
return out;
|
18103
|
+
}
|
18104
|
+
if (lowered.startsWith("time")) {
|
18105
|
+
const withTimezone = lowered.includes("with time zone");
|
18106
|
+
let precision = lowered.startsWith("time(") ? Number(
|
18107
|
+
lowered.split(" ")[0].substring("time(".length, lowered.split(" ")[0].length - 1)
|
18108
|
+
) : null;
|
18109
|
+
precision = precision ? precision : null;
|
18110
|
+
const params = timeConfig({ precision, withTimezone });
|
18111
|
+
let out = params ? `${withCasing(name, casing)}: time("${name}", ${params})` : `${withCasing(name, casing)}: time("${name}")`;
|
18112
|
+
defaultValue = defaultValue === "now()" ? ".defaultNow()" : defaultValue ? `.default(${defaultValue})` : "";
|
18113
|
+
out += defaultValue;
|
18114
|
+
return out;
|
18115
|
+
}
|
18116
|
+
if (lowered.startsWith("interval")) {
|
18117
|
+
const params = intervalConfig(lowered);
|
18118
|
+
let out = params ? `${withCasing(name, casing)}: interval("${name}", ${params})` : `${withCasing(name, casing)}: interval("${name}")`;
|
18119
|
+
out += defaultValue ? `.default(${defaultValue})` : "";
|
18120
|
+
return out;
|
18121
|
+
}
|
18122
|
+
if (lowered === "date") {
|
18123
|
+
let out = `${withCasing(name, casing)}: date("${name}")`;
|
18124
|
+
defaultValue = defaultValue === "now()" ? ".defaultNow()" : defaultValue ? `.default(${defaultValue})` : "";
|
18125
|
+
out += defaultValue;
|
18126
|
+
return out;
|
18127
|
+
}
|
18128
|
+
if (lowered.startsWith("text")) {
|
18129
|
+
let out = `${withCasing(name, casing)}: text("${name}")`;
|
18130
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18131
|
+
return out;
|
18132
|
+
}
|
18133
|
+
if (lowered === "json") {
|
18134
|
+
let out = `${withCasing(name, casing)}: json("${name}")`;
|
18135
|
+
defaultValue = (defaultValue == null ? void 0 : defaultValue.endsWith("::json")) ? defaultValue.substring(1, defaultValue.length - 7) : defaultValue;
|
18136
|
+
const def = defaultValue ? defaultValue : null;
|
18137
|
+
out += typeof defaultValue !== "undefined" ? `.default(${def})` : "";
|
18138
|
+
return out;
|
18139
|
+
}
|
18140
|
+
if (lowered === "jsonb") {
|
18141
|
+
let out = `${withCasing(name, casing)}: jsonb("${name}")`;
|
18142
|
+
defaultValue = (defaultValue == null ? void 0 : defaultValue.endsWith("::jsonb")) ? defaultValue.substring(1, defaultValue.length - 8) : defaultValue;
|
18143
|
+
const def = typeof defaultValue !== "undefined" ? defaultValue : null;
|
18144
|
+
out += defaultValue ? `.default(${def})` : "";
|
18145
|
+
return out;
|
18146
|
+
}
|
18147
|
+
if (lowered.startsWith("inet")) {
|
18148
|
+
let out = `${withCasing(name, casing)}: inet("${name}")`;
|
18149
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18150
|
+
return out;
|
18151
|
+
}
|
18152
|
+
if (lowered.startsWith("cidr")) {
|
18153
|
+
let out = `${withCasing(name, casing)}: cidr("${name}")`;
|
18154
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18155
|
+
return out;
|
18156
|
+
}
|
18157
|
+
if (lowered.startsWith("macaddr")) {
|
18158
|
+
let out = `${withCasing(name, casing)}: macaddr("${name}")`;
|
18159
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18160
|
+
return out;
|
18161
|
+
}
|
18162
|
+
if (lowered.startsWith("macaddr8")) {
|
18163
|
+
let out = `${withCasing(name, casing)}: macaddr8("${name}")`;
|
18164
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18165
|
+
return out;
|
18166
|
+
}
|
18167
|
+
if (lowered.startsWith("varchar")) {
|
18168
|
+
const split = lowered.split(" ");
|
18169
|
+
let out;
|
18170
|
+
if (lowered.length !== 7) {
|
18171
|
+
out = `${withCasing(
|
18172
|
+
name,
|
18173
|
+
casing
|
18174
|
+
)}: varchar("${name}", { length: ${lowered.substring(
|
18175
|
+
8,
|
18176
|
+
lowered.length - 1
|
18177
|
+
)} })`;
|
18178
|
+
} else {
|
18179
|
+
out = `${withCasing(name, casing)}: varchar("${name}")`;
|
18180
|
+
}
|
18181
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18182
|
+
return out;
|
18183
|
+
}
|
18184
|
+
if (lowered.startsWith("char")) {
|
18185
|
+
let out;
|
18186
|
+
if (lowered.length !== 4) {
|
18187
|
+
out = `${withCasing(
|
18188
|
+
name,
|
18189
|
+
casing
|
18190
|
+
)}: char("${name}", { length: ${lowered.substring(
|
18191
|
+
5,
|
18192
|
+
lowered.length - 1
|
18193
|
+
)} })`;
|
18194
|
+
} else {
|
18195
|
+
out = `${withCasing(name, casing)}: char("${name}")`;
|
18196
|
+
}
|
18197
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18198
|
+
return out;
|
18199
|
+
}
|
18200
|
+
const columnInternals = (_a3 = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _a3.columns[name];
|
18201
|
+
if (typeof columnInternals !== "undefined") {
|
18202
|
+
if (columnInternals.isArray && columnInternals.rawType && enumTypes.has(columnInternals.rawType)) {
|
18203
|
+
let out = `${withCasing(columnInternals.rawType, casing)}: ${withCasing(
|
18204
|
+
columnInternals.rawType,
|
18205
|
+
casing
|
18206
|
+
)}("${name}")`;
|
18207
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18208
|
+
return out;
|
18209
|
+
}
|
18210
|
+
}
|
18211
|
+
if (enumTypes.has(type)) {
|
18212
|
+
let out = `${withCasing(name, casing)}: ${withCasing(
|
18213
|
+
type,
|
18214
|
+
casing
|
18215
|
+
)}("${name}")`;
|
18216
|
+
out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
|
18217
|
+
return out;
|
18218
|
+
}
|
18219
|
+
let unknown = `// TODO: failed to parse database type '${type}'
|
18220
|
+
`;
|
18221
|
+
unknown += ` ${withCasing(name, casing)}: unknown("${name}")`;
|
18222
|
+
return unknown;
|
18223
|
+
};
|
18224
|
+
dimensionsInArray = (size) => {
|
18225
|
+
let res = "";
|
18226
|
+
if (typeof size === "undefined")
|
18227
|
+
return res;
|
18228
|
+
for (let i = 0; i < size; i++) {
|
18229
|
+
res += ".array()";
|
18230
|
+
}
|
18231
|
+
return res;
|
18232
|
+
};
|
18233
|
+
createTableColumns = (tableName, columns, fks, enumTypes, casing, internals) => {
|
18234
|
+
let statement = "";
|
18235
|
+
const oneColumnsFKs = Object.values(fks).filter((it) => {
|
18236
|
+
return !isSelf(it);
|
18237
|
+
}).filter((it) => it.columnsFrom.length === 1);
|
18238
|
+
const fkByColumnName = oneColumnsFKs.reduce((res, it) => {
|
18239
|
+
const arr = res[it.columnsFrom[0]] || [];
|
18240
|
+
arr.push(it);
|
18241
|
+
res[it.columnsFrom[0]] = arr;
|
18242
|
+
return res;
|
18243
|
+
}, {});
|
18244
|
+
columns.forEach((it) => {
|
18245
|
+
var _a3, _b, _c, _d;
|
18246
|
+
const columnStatement = column4(
|
18247
|
+
tableName,
|
18248
|
+
it.type,
|
18249
|
+
it.name,
|
18250
|
+
enumTypes,
|
18251
|
+
it.default,
|
18252
|
+
casing,
|
18253
|
+
internals
|
18254
|
+
);
|
18255
|
+
statement += " ";
|
18256
|
+
statement += columnStatement;
|
18257
|
+
if ((_b = (_a3 = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _a3.columns[it.name]) == null ? void 0 : _b.isArray) {
|
18258
|
+
statement += dimensionsInArray(
|
18259
|
+
(_d = (_c = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _c.columns[it.name]) == null ? void 0 : _d.dimensions
|
18260
|
+
);
|
18261
|
+
}
|
18262
|
+
statement += it.primaryKey ? ".primaryKey()" : "";
|
18263
|
+
statement += it.notNull ? ".notNull()" : "";
|
18264
|
+
const fks2 = fkByColumnName[it.name];
|
18265
|
+
if (fks2) {
|
18266
|
+
const fksStatement = fks2.map((it2) => {
|
18267
|
+
const onDelete = it2.onDelete && it2.onDelete !== "no action" ? it2.onDelete : null;
|
18268
|
+
const onUpdate = it2.onUpdate && it2.onUpdate !== "no action" ? it2.onUpdate : null;
|
18269
|
+
const params = { onDelete, onUpdate };
|
18270
|
+
const typeSuffix = isCyclic(it2) ? ": AnyPgColumn" : "";
|
18271
|
+
const paramsStr = objToStatement2(params);
|
18272
|
+
if (paramsStr) {
|
18273
|
+
return `.references(()${typeSuffix} => ${withCasing(
|
18274
|
+
it2.tableTo,
|
18275
|
+
casing
|
18276
|
+
)}.${withCasing(it2.columnsTo[0], casing)}, ${paramsStr} )`;
|
18277
|
+
}
|
18278
|
+
return `.references(()${typeSuffix} => ${withCasing(
|
18279
|
+
it2.tableTo,
|
18280
|
+
casing
|
18281
|
+
)}.${withCasing(it2.columnsTo[0], casing)})`;
|
18282
|
+
}).join("");
|
18283
|
+
statement += fksStatement;
|
18284
|
+
}
|
18285
|
+
statement += ",\n";
|
18286
|
+
});
|
18287
|
+
return statement;
|
18288
|
+
};
|
18289
|
+
createTableIndexes = (tableName, idxs, casing) => {
|
18290
|
+
let statement = "";
|
18291
|
+
idxs.forEach((it) => {
|
18292
|
+
let idxKey = it.name.startsWith(tableName) && it.name !== tableName ? it.name.slice(tableName.length + 1) : it.name;
|
18293
|
+
idxKey = idxKey.endsWith("_index") ? idxKey.slice(0, -"_index".length) + "_idx" : idxKey;
|
18294
|
+
idxKey = withCasing(idxKey, casing);
|
18295
|
+
const indexGeneratedName = indexName2(tableName, it.columns);
|
18296
|
+
const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
|
18297
|
+
statement += ` ${idxKey}: `;
|
18298
|
+
statement += it.isUnique ? "uniqueIndex(" : "index(";
|
18299
|
+
statement += `${escapedIndexName})`;
|
18300
|
+
statement += `.on(${it.columns.map((it2) => `table.${withCasing(it2, casing)}`).join(", ")}),`;
|
18301
|
+
statement += `
|
18302
|
+
`;
|
18303
|
+
});
|
18304
|
+
return statement;
|
18305
|
+
};
|
18306
|
+
createTablePKs = (pks, casing) => {
|
18307
|
+
let statement = "";
|
18308
|
+
pks.forEach((it) => {
|
18309
|
+
let idxKey = withCasing(it.name, casing);
|
18310
|
+
statement += ` ${idxKey}: `;
|
18311
|
+
statement += "primaryKey({ columns: [";
|
18312
|
+
statement += `${it.columns.map((c) => {
|
18313
|
+
return `table.${withCasing(c, casing)}`;
|
18314
|
+
}).join(", ")}]${it.name ? `, name: "${it.name}"` : ""}}`;
|
18315
|
+
statement += ")";
|
18316
|
+
statement += `
|
18317
|
+
`;
|
18318
|
+
});
|
18319
|
+
return statement;
|
18320
|
+
};
|
18321
|
+
createTableUniques = (unqs, casing) => {
|
18322
|
+
let statement = "";
|
18323
|
+
unqs.forEach((it) => {
|
18324
|
+
const idxKey = withCasing(it.name, casing);
|
18325
|
+
statement += ` ${idxKey}: `;
|
18326
|
+
statement += "unique(";
|
18327
|
+
statement += `"${it.name}")`;
|
18328
|
+
statement += `.on(${it.columns.map((it2) => `table.${withCasing(it2, casing)}`).join(", ")})`;
|
18329
|
+
statement += it.nullsNotDistinct ? `.nullsNotDistinct()` : "";
|
18330
|
+
statement += `,
|
18331
|
+
`;
|
18332
|
+
});
|
18333
|
+
return statement;
|
18334
|
+
};
|
18335
|
+
createTableFKs = (fks, casing) => {
|
18336
|
+
let statement = "";
|
18337
|
+
fks.forEach((it) => {
|
18338
|
+
const isSelf2 = it.tableTo === it.tableFrom;
|
18339
|
+
const tableTo = isSelf2 ? "table" : `${withCasing(it.tableTo, casing)}`;
|
18340
|
+
statement += ` ${withCasing(it.name, casing)}: foreignKey({
|
18341
|
+
`;
|
18342
|
+
statement += ` columns: [${it.columnsFrom.map((i) => `table.${withCasing(i, casing)}`).join(", ")}],
|
18343
|
+
`;
|
18344
|
+
statement += ` foreignColumns: [${it.columnsTo.map((i) => `${tableTo}.${withCasing(i, casing)}`).join(", ")}],
|
18345
|
+
`;
|
18346
|
+
statement += ` name: "${it.name}"
|
18347
|
+
`;
|
18348
|
+
statement += ` })`;
|
18349
|
+
statement += it.onUpdate && it.onUpdate !== "no action" ? `.onUpdate("${it.onUpdate}")` : "";
|
18350
|
+
statement += it.onDelete && it.onDelete !== "no action" ? `.onDelete("${it.onDelete}")` : "";
|
18351
|
+
statement += `,
|
18352
|
+
`;
|
18353
|
+
});
|
18354
|
+
return statement;
|
18355
|
+
};
|
17680
18356
|
}
|
17681
18357
|
});
|
17682
18358
|
|
@@ -18960,425 +19636,6 @@ var init_mjs = __esm({
|
|
18960
19636
|
}
|
18961
19637
|
});
|
18962
19638
|
|
18963
|
-
// src/cli/commands/pgIntrospect.ts
|
18964
|
-
var import_hanji4, pgPushIntrospect;
|
18965
|
-
var init_pgIntrospect = __esm({
|
18966
|
-
"src/cli/commands/pgIntrospect.ts"() {
|
18967
|
-
import_hanji4 = __toESM(require_hanji());
|
18968
|
-
init_views();
|
18969
|
-
init_pgSerializer();
|
18970
|
-
init_introspect();
|
18971
|
-
init_global();
|
18972
|
-
init_mjs();
|
18973
|
-
pgPushIntrospect = async (connection, filters, schemaFilters) => {
|
18974
|
-
const { client } = connection;
|
18975
|
-
const matchers = filters.map((it) => {
|
18976
|
-
return new Minimatch(it);
|
18977
|
-
});
|
18978
|
-
const filter2 = (tableName) => {
|
18979
|
-
if (matchers.length === 0)
|
18980
|
-
return true;
|
18981
|
-
for (let i = 0; i < matchers.length; i++) {
|
18982
|
-
const matcher = matchers[i];
|
18983
|
-
if (matcher.match(tableName))
|
18984
|
-
return true;
|
18985
|
-
}
|
18986
|
-
return false;
|
18987
|
-
};
|
18988
|
-
const res = await fromDatabase2(client, filter2, schemaFilters);
|
18989
|
-
const schema4 = { id: originUUID, prevId: "", ...res };
|
18990
|
-
const { internal, ...schemaWithoutInternals } = schema4;
|
18991
|
-
return { schema: schemaWithoutInternals };
|
18992
|
-
};
|
18993
|
-
}
|
18994
|
-
});
|
18995
|
-
|
18996
|
-
// src/cli/selector-ui.ts
|
18997
|
-
var import_hanji5, Select;
|
18998
|
-
var init_selector_ui = __esm({
|
18999
|
-
"src/cli/selector-ui.ts"() {
|
19000
|
-
init_source();
|
19001
|
-
import_hanji5 = __toESM(require_hanji());
|
19002
|
-
Select = class extends import_hanji5.Prompt {
|
19003
|
-
constructor(items) {
|
19004
|
-
super();
|
19005
|
-
this.on("attach", (terminal) => terminal.toggleCursor("hide"));
|
19006
|
-
this.on("detach", (terminal) => terminal.toggleCursor("show"));
|
19007
|
-
this.data = new import_hanji5.SelectState(
|
19008
|
-
items.map((it) => ({ label: it, value: `${it}-value` }))
|
19009
|
-
);
|
19010
|
-
this.data.bind(this);
|
19011
|
-
}
|
19012
|
-
render(status) {
|
19013
|
-
if (status === "submitted" || status === "aborted")
|
19014
|
-
return "";
|
19015
|
-
let text = ``;
|
19016
|
-
this.data.items.forEach((it, idx) => {
|
19017
|
-
text += idx === this.data.selectedIdx ? `${source_default.green("\u276F " + it.label)}` : ` ${it.label}`;
|
19018
|
-
text += idx != this.data.items.length - 1 ? "\n" : "";
|
19019
|
-
});
|
19020
|
-
return text;
|
19021
|
-
}
|
19022
|
-
result() {
|
19023
|
-
return {
|
19024
|
-
index: this.data.selectedIdx,
|
19025
|
-
value: this.data.items[this.data.selectedIdx].value
|
19026
|
-
};
|
19027
|
-
}
|
19028
|
-
};
|
19029
|
-
}
|
19030
|
-
});
|
19031
|
-
|
19032
|
-
// src/cli/commands/pgPushUtils.ts
|
19033
|
-
function tableNameWithSchemaFrom(statement) {
|
19034
|
-
return statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
|
19035
|
-
}
|
19036
|
-
var import_hanji6, pgSuggestions;
|
19037
|
-
var init_pgPushUtils = __esm({
|
19038
|
-
"src/cli/commands/pgPushUtils.ts"() {
|
19039
|
-
init_source();
|
19040
|
-
import_hanji6 = __toESM(require_hanji());
|
19041
|
-
init_pgSchema();
|
19042
|
-
init_sqlgenerator();
|
19043
|
-
init_selector_ui();
|
19044
|
-
pgSuggestions = async ({
|
19045
|
-
connection,
|
19046
|
-
statements
|
19047
|
-
}) => {
|
19048
|
-
let shouldAskForApprove = false;
|
19049
|
-
const statementsToExecute = [];
|
19050
|
-
const infoToPrint = [];
|
19051
|
-
const tablesToRemove = [];
|
19052
|
-
const columnsToRemove = [];
|
19053
|
-
const schemasToRemove = [];
|
19054
|
-
const tablesToTruncate = [];
|
19055
|
-
for (const statement of statements) {
|
19056
|
-
if (statement.type === "drop_table") {
|
19057
|
-
const res = await connection.query(
|
19058
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19059
|
-
);
|
19060
|
-
const count = Number(res[0].count);
|
19061
|
-
if (count > 0) {
|
19062
|
-
infoToPrint.push(
|
19063
|
-
`\xB7 You're about to delete ${source_default.underline(
|
19064
|
-
statement.tableName
|
19065
|
-
)} table with ${count} items`
|
19066
|
-
);
|
19067
|
-
tablesToRemove.push(statement.tableName);
|
19068
|
-
shouldAskForApprove = true;
|
19069
|
-
}
|
19070
|
-
} else if (statement.type === "alter_table_drop_column") {
|
19071
|
-
const res = await connection.query(
|
19072
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19073
|
-
);
|
19074
|
-
const count = Number(res[0].count);
|
19075
|
-
if (count > 0) {
|
19076
|
-
infoToPrint.push(
|
19077
|
-
`\xB7 You're about to delete ${source_default.underline(
|
19078
|
-
statement.columnName
|
19079
|
-
)} column in ${statement.tableName} table with ${count} items`
|
19080
|
-
);
|
19081
|
-
columnsToRemove.push(`${statement.tableName}_${statement.columnName}`);
|
19082
|
-
shouldAskForApprove = true;
|
19083
|
-
}
|
19084
|
-
} else if (statement.type === "drop_schema") {
|
19085
|
-
const res = await connection.query(
|
19086
|
-
`select count(*) as count from information_schema.tables where table_schema = '${statement.name}';`
|
19087
|
-
);
|
19088
|
-
const count = Number(res[0].count);
|
19089
|
-
if (count > 0) {
|
19090
|
-
infoToPrint.push(
|
19091
|
-
`\xB7 You're about to delete ${source_default.underline(
|
19092
|
-
statement.name
|
19093
|
-
)} schema with ${count} tables`
|
19094
|
-
);
|
19095
|
-
schemasToRemove.push(statement.name);
|
19096
|
-
shouldAskForApprove = true;
|
19097
|
-
}
|
19098
|
-
} else if (statement.type === "alter_table_alter_column_set_type") {
|
19099
|
-
const res = await connection.query(
|
19100
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19101
|
-
);
|
19102
|
-
const count = Number(res[0].count);
|
19103
|
-
if (count > 0) {
|
19104
|
-
infoToPrint.push(
|
19105
|
-
`\xB7 You're about to change ${source_default.underline(
|
19106
|
-
statement.columnName
|
19107
|
-
)} column type from ${source_default.underline(
|
19108
|
-
statement.oldDataType
|
19109
|
-
)} to ${source_default.underline(statement.newDataType)} with ${count} items`
|
19110
|
-
);
|
19111
|
-
statementsToExecute.push(
|
19112
|
-
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
19113
|
-
);
|
19114
|
-
tablesToTruncate.push(statement.tableName);
|
19115
|
-
shouldAskForApprove = true;
|
19116
|
-
}
|
19117
|
-
} else if (statement.type === "alter_table_alter_column_drop_default") {
|
19118
|
-
if (statement.columnNotNull) {
|
19119
|
-
const res = await connection.query(
|
19120
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19121
|
-
);
|
19122
|
-
const count = Number(res[0].count);
|
19123
|
-
if (count > 0) {
|
19124
|
-
infoToPrint.push(
|
19125
|
-
`\xB7 You're about to remove default value from ${source_default.underline(
|
19126
|
-
statement.columnName
|
19127
|
-
)} not-null column with ${count} items`
|
19128
|
-
);
|
19129
|
-
tablesToTruncate.push(statement.tableName);
|
19130
|
-
statementsToExecute.push(
|
19131
|
-
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
19132
|
-
);
|
19133
|
-
shouldAskForApprove = true;
|
19134
|
-
}
|
19135
|
-
}
|
19136
|
-
} else if (statement.type === "alter_table_alter_column_set_notnull") {
|
19137
|
-
if (typeof statement.columnDefault === "undefined") {
|
19138
|
-
const res = await connection.query(
|
19139
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19140
|
-
);
|
19141
|
-
const count = Number(res[0].count);
|
19142
|
-
if (count > 0) {
|
19143
|
-
infoToPrint.push(
|
19144
|
-
`\xB7 You're about to set not-null constraint to ${source_default.underline(
|
19145
|
-
statement.columnName
|
19146
|
-
)} column without default, which contains ${count} items`
|
19147
|
-
);
|
19148
|
-
tablesToTruncate.push(statement.tableName);
|
19149
|
-
statementsToExecute.push(
|
19150
|
-
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
19151
|
-
);
|
19152
|
-
shouldAskForApprove = true;
|
19153
|
-
}
|
19154
|
-
}
|
19155
|
-
} else if (statement.type === "alter_table_alter_column_drop_pk") {
|
19156
|
-
const res = await connection.query(
|
19157
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19158
|
-
);
|
19159
|
-
const count = Number(res[0].count);
|
19160
|
-
if (count > 0) {
|
19161
|
-
infoToPrint.push(
|
19162
|
-
`\xB7 You're about to change ${source_default.underline(
|
19163
|
-
statement.tableName
|
19164
|
-
)} primary key. This statements may fail and you table may left without primary key`
|
19165
|
-
);
|
19166
|
-
tablesToTruncate.push(statement.tableName);
|
19167
|
-
shouldAskForApprove = true;
|
19168
|
-
}
|
19169
|
-
const tableNameWithSchema = tableNameWithSchemaFrom(statement);
|
19170
|
-
const pkNameResponse = await connection.query(
|
19171
|
-
`SELECT constraint_name FROM information_schema.table_constraints
|
19172
|
-
WHERE table_schema = '${typeof statement.schema === "undefined" || statement.schema === "" ? "public" : statement.schema}'
|
19173
|
-
AND table_name = '${statement.tableName}'
|
19174
|
-
AND constraint_type = 'PRIMARY KEY';`
|
19175
|
-
);
|
19176
|
-
statementsToExecute.push(
|
19177
|
-
`ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${pkNameResponse[0].constraint_name}"`
|
19178
|
-
);
|
19179
|
-
continue;
|
19180
|
-
} else if (statement.type === "alter_table_add_column") {
|
19181
|
-
if (statement.column.notNull && typeof statement.column.default === "undefined") {
|
19182
|
-
const res = await connection.query(
|
19183
|
-
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
19184
|
-
);
|
19185
|
-
const count = Number(res[0].count);
|
19186
|
-
if (count > 0) {
|
19187
|
-
infoToPrint.push(
|
19188
|
-
`\xB7 You're about to add not-null ${source_default.underline(
|
19189
|
-
statement.column.name
|
19190
|
-
)} column without default value, which contains ${count} items`
|
19191
|
-
);
|
19192
|
-
tablesToTruncate.push(statement.tableName);
|
19193
|
-
statementsToExecute.push(
|
19194
|
-
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
19195
|
-
);
|
19196
|
-
shouldAskForApprove = true;
|
19197
|
-
}
|
19198
|
-
}
|
19199
|
-
} else if (statement.type === "create_unique_constraint") {
|
19200
|
-
const res = await connection.query(
|
19201
|
-
`select count(*) as count from ${tableNameWithSchemaFrom({
|
19202
|
-
schema: statement.schema,
|
19203
|
-
tableName: statement.tableName
|
19204
|
-
})}`
|
19205
|
-
);
|
19206
|
-
const count = Number(res[0].count);
|
19207
|
-
if (count > 0) {
|
19208
|
-
const unsquashedUnique = PgSquasher.unsquashUnique(statement.data);
|
19209
|
-
console.log(
|
19210
|
-
`\xB7 You're about to add ${source_default.underline(
|
19211
|
-
unsquashedUnique.name
|
19212
|
-
)} 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 ${source_default.underline(
|
19213
|
-
statement.tableName
|
19214
|
-
)} table?
|
19215
|
-
`
|
19216
|
-
);
|
19217
|
-
const { status, data } = await (0, import_hanji6.render)(
|
19218
|
-
new Select([
|
19219
|
-
"No, add the constraint without truncating the table",
|
19220
|
-
`Yes, truncate the table`
|
19221
|
-
])
|
19222
|
-
);
|
19223
|
-
if ((data == null ? void 0 : data.index) === 1) {
|
19224
|
-
tablesToTruncate.push(statement.tableName);
|
19225
|
-
statementsToExecute.push(
|
19226
|
-
`truncate table ${tableNameWithSchemaFrom({
|
19227
|
-
schema: statement.schema,
|
19228
|
-
tableName: statement.tableName
|
19229
|
-
})} cascade;`
|
19230
|
-
);
|
19231
|
-
shouldAskForApprove = true;
|
19232
|
-
}
|
19233
|
-
}
|
19234
|
-
}
|
19235
|
-
const stmnt = fromJson([statement], "pg")[0];
|
19236
|
-
if (typeof stmnt !== "undefined") {
|
19237
|
-
if (statement.type === "drop_table") {
|
19238
|
-
statementsToExecute.push(`DROP TABLE ${tableNameWithSchemaFrom(statement)} CASCADE;`);
|
19239
|
-
} else {
|
19240
|
-
statementsToExecute.push(stmnt);
|
19241
|
-
}
|
19242
|
-
}
|
19243
|
-
}
|
19244
|
-
return {
|
19245
|
-
statementsToExecute,
|
19246
|
-
shouldAskForApprove,
|
19247
|
-
infoToPrint,
|
19248
|
-
columnsToRemove: [...new Set(columnsToRemove)],
|
19249
|
-
schemasToRemove: [...new Set(schemasToRemove)],
|
19250
|
-
tablesToTruncate: [...new Set(tablesToTruncate)],
|
19251
|
-
tablesToRemove: [...new Set(tablesToRemove)]
|
19252
|
-
};
|
19253
|
-
};
|
19254
|
-
}
|
19255
|
-
});
|
19256
|
-
|
19257
|
-
// src/sqlite-introspect.ts
|
19258
|
-
var init_sqlite_introspect = __esm({
|
19259
|
-
"src/sqlite-introspect.ts"() {
|
19260
|
-
init_utils2();
|
19261
|
-
}
|
19262
|
-
});
|
19263
|
-
|
19264
|
-
// src/drivers/index.ts
|
19265
|
-
var drivers_exports = {};
|
19266
|
-
__export(drivers_exports, {
|
19267
|
-
BetterSqlite: () => BetterSqlite,
|
19268
|
-
DrizzleDbClient: () => DrizzleDbClient,
|
19269
|
-
DrizzleORMPgClient: () => DrizzleORMPgClient,
|
19270
|
-
PgPostgres: () => PgPostgres,
|
19271
|
-
TursoSqlite: () => TursoSqlite
|
19272
|
-
});
|
19273
|
-
var import_drizzle_orm8, DrizzleDbClient, DrizzleORMPgClient, BetterSqlite, TursoSqlite, PgPostgres;
|
19274
|
-
var init_drivers = __esm({
|
19275
|
-
"src/drivers/index.ts"() {
|
19276
|
-
import_drizzle_orm8 = require("drizzle-orm");
|
19277
|
-
DrizzleDbClient = class {
|
19278
|
-
constructor(db) {
|
19279
|
-
this.db = db;
|
19280
|
-
}
|
19281
|
-
};
|
19282
|
-
DrizzleORMPgClient = class extends DrizzleDbClient {
|
19283
|
-
async query(query, values) {
|
19284
|
-
const res = await this.db.execute(import_drizzle_orm8.sql.raw(query));
|
19285
|
-
return res.rows;
|
19286
|
-
}
|
19287
|
-
async run(query) {
|
19288
|
-
const res = await this.db.execute(import_drizzle_orm8.sql.raw(query));
|
19289
|
-
return res.rows;
|
19290
|
-
}
|
19291
|
-
};
|
19292
|
-
BetterSqlite = class extends DrizzleDbClient {
|
19293
|
-
async run(query) {
|
19294
|
-
this.db.prepare(query).run();
|
19295
|
-
}
|
19296
|
-
async query(query) {
|
19297
|
-
return this.db.prepare(query).all();
|
19298
|
-
}
|
19299
|
-
};
|
19300
|
-
TursoSqlite = class extends DrizzleDbClient {
|
19301
|
-
async run(query) {
|
19302
|
-
await this.db.execute(query);
|
19303
|
-
}
|
19304
|
-
async query(query) {
|
19305
|
-
const res = await this.db.execute(query);
|
19306
|
-
return res.rows;
|
19307
|
-
}
|
19308
|
-
};
|
19309
|
-
PgPostgres = class extends DrizzleDbClient {
|
19310
|
-
async query(query, values) {
|
19311
|
-
const res = await this.db.query(query, values);
|
19312
|
-
return res.rows;
|
19313
|
-
}
|
19314
|
-
async run(query) {
|
19315
|
-
await this.db.query(query);
|
19316
|
-
}
|
19317
|
-
};
|
19318
|
-
}
|
19319
|
-
});
|
19320
|
-
|
19321
|
-
// src/cli/commands/sqliteIntrospect.ts
|
19322
|
-
var import_hanji7, connectToSQLite, sqlitePushIntrospect;
|
19323
|
-
var init_sqliteIntrospect = __esm({
|
19324
|
-
"src/cli/commands/sqliteIntrospect.ts"() {
|
19325
|
-
init_views();
|
19326
|
-
init_global();
|
19327
|
-
init_sqliteSerializer();
|
19328
|
-
init_sqlite_introspect();
|
19329
|
-
init_mjs();
|
19330
|
-
import_hanji7 = __toESM(require_hanji());
|
19331
|
-
connectToSQLite = async (config) => {
|
19332
|
-
const { BetterSqlite: BetterSqlite2, TursoSqlite: TursoSqlite2 } = await Promise.resolve().then(() => (init_drivers(), drivers_exports));
|
19333
|
-
if (config.driver === "better-sqlite") {
|
19334
|
-
const { default: Database } = await import("better-sqlite3");
|
19335
|
-
return {
|
19336
|
-
client: new BetterSqlite2(new Database(config.dbCredentials.url))
|
19337
|
-
};
|
19338
|
-
}
|
19339
|
-
if (config.driver === "libsql") {
|
19340
|
-
const { createClient } = await import("@libsql/client");
|
19341
|
-
return {
|
19342
|
-
client: new TursoSqlite2(
|
19343
|
-
createClient({
|
19344
|
-
url: config.dbCredentials.url
|
19345
|
-
})
|
19346
|
-
)
|
19347
|
-
};
|
19348
|
-
} else {
|
19349
|
-
const { createClient } = await import("@libsql/client");
|
19350
|
-
return {
|
19351
|
-
client: new TursoSqlite2(
|
19352
|
-
createClient({
|
19353
|
-
url: config.dbCredentials.url,
|
19354
|
-
authToken: config.dbCredentials.authToken
|
19355
|
-
})
|
19356
|
-
)
|
19357
|
-
};
|
19358
|
-
}
|
19359
|
-
return {};
|
19360
|
-
};
|
19361
|
-
sqlitePushIntrospect = async (client, filters) => {
|
19362
|
-
const matchers = filters.map((it) => {
|
19363
|
-
return new Minimatch(it);
|
19364
|
-
});
|
19365
|
-
const filter2 = (tableName) => {
|
19366
|
-
if (matchers.length === 0)
|
19367
|
-
return true;
|
19368
|
-
for (let i = 0; i < matchers.length; i++) {
|
19369
|
-
const matcher = matchers[i];
|
19370
|
-
if (matcher.match(tableName))
|
19371
|
-
return true;
|
19372
|
-
}
|
19373
|
-
return false;
|
19374
|
-
};
|
19375
|
-
const res = await fromDatabase3(client, filter2);
|
19376
|
-
const schema4 = { id: originUUID, prevId: "", ...res };
|
19377
|
-
return { schema: schema4 };
|
19378
|
-
};
|
19379
|
-
}
|
19380
|
-
});
|
19381
|
-
|
19382
19639
|
// node_modules/.pnpm/postgres-array@2.0.0/node_modules/postgres-array/index.js
|
19383
19640
|
var require_postgres_array = __commonJS({
|
19384
19641
|
"node_modules/.pnpm/postgres-array@2.0.0/node_modules/postgres-array/index.js"(exports) {
|
@@ -23921,6 +24178,494 @@ var require_lib2 = __commonJS({
|
|
23921
24178
|
}
|
23922
24179
|
});
|
23923
24180
|
|
24181
|
+
// src/drivers/index.ts
|
24182
|
+
var drivers_exports = {};
|
24183
|
+
__export(drivers_exports, {
|
24184
|
+
BetterSqlite: () => BetterSqlite,
|
24185
|
+
DrizzleDbClient: () => DrizzleDbClient,
|
24186
|
+
DrizzleORMPgClient: () => DrizzleORMPgClient,
|
24187
|
+
PgPostgres: () => PgPostgres,
|
24188
|
+
TursoSqlite: () => TursoSqlite
|
24189
|
+
});
|
24190
|
+
var import_drizzle_orm8, DrizzleDbClient, DrizzleORMPgClient, BetterSqlite, TursoSqlite, PgPostgres;
|
24191
|
+
var init_drivers = __esm({
|
24192
|
+
"src/drivers/index.ts"() {
|
24193
|
+
import_drizzle_orm8 = require("drizzle-orm");
|
24194
|
+
DrizzleDbClient = class {
|
24195
|
+
constructor(db) {
|
24196
|
+
this.db = db;
|
24197
|
+
}
|
24198
|
+
};
|
24199
|
+
DrizzleORMPgClient = class extends DrizzleDbClient {
|
24200
|
+
async query(query, values) {
|
24201
|
+
const res = await this.db.execute(import_drizzle_orm8.sql.raw(query));
|
24202
|
+
return res.rows;
|
24203
|
+
}
|
24204
|
+
async run(query) {
|
24205
|
+
const res = await this.db.execute(import_drizzle_orm8.sql.raw(query));
|
24206
|
+
return res.rows;
|
24207
|
+
}
|
24208
|
+
};
|
24209
|
+
BetterSqlite = class extends DrizzleDbClient {
|
24210
|
+
async run(query) {
|
24211
|
+
this.db.prepare(query).run();
|
24212
|
+
}
|
24213
|
+
async query(query) {
|
24214
|
+
return this.db.prepare(query).all();
|
24215
|
+
}
|
24216
|
+
};
|
24217
|
+
TursoSqlite = class extends DrizzleDbClient {
|
24218
|
+
async run(query) {
|
24219
|
+
await this.db.execute(query);
|
24220
|
+
}
|
24221
|
+
async query(query) {
|
24222
|
+
const res = await this.db.execute(query);
|
24223
|
+
return res.rows;
|
24224
|
+
}
|
24225
|
+
};
|
24226
|
+
PgPostgres = class extends DrizzleDbClient {
|
24227
|
+
async query(query, values) {
|
24228
|
+
const res = await this.db.query(query, values);
|
24229
|
+
return res.rows;
|
24230
|
+
}
|
24231
|
+
async run(query) {
|
24232
|
+
await this.db.query(query);
|
24233
|
+
}
|
24234
|
+
};
|
24235
|
+
}
|
24236
|
+
});
|
24237
|
+
|
24238
|
+
// src/cli/commands/pgConnect.ts
|
24239
|
+
var pgConnect_exports = {};
|
24240
|
+
__export(pgConnect_exports, {
|
24241
|
+
connectToPg: () => connectToPg
|
24242
|
+
});
|
24243
|
+
var import_pg, connectToPg;
|
24244
|
+
var init_pgConnect = __esm({
|
24245
|
+
"src/cli/commands/pgConnect.ts"() {
|
24246
|
+
import_pg = __toESM(require_lib2());
|
24247
|
+
init_drivers();
|
24248
|
+
connectToPg = async (config) => {
|
24249
|
+
if (config.driver === "pg") {
|
24250
|
+
const client = new import_pg.Client(config.dbCredentials);
|
24251
|
+
await client.connect();
|
24252
|
+
const connection = new PgPostgres(client);
|
24253
|
+
return { client: connection };
|
24254
|
+
} else {
|
24255
|
+
throw Error(`Only "pg" is available as a driver option for introspection`);
|
24256
|
+
}
|
24257
|
+
};
|
24258
|
+
}
|
24259
|
+
});
|
24260
|
+
|
24261
|
+
// src/cli/commands/pgIntrospect.ts
|
24262
|
+
var pgIntrospect_exports = {};
|
24263
|
+
__export(pgIntrospect_exports, {
|
24264
|
+
pgIntrospect: () => pgIntrospect,
|
24265
|
+
pgPushIntrospect: () => pgPushIntrospect,
|
24266
|
+
pgSchemas: () => pgSchemas
|
24267
|
+
});
|
24268
|
+
var import_hanji4, pgSchemas, pgPushIntrospect, pgIntrospect;
|
24269
|
+
var init_pgIntrospect = __esm({
|
24270
|
+
"src/cli/commands/pgIntrospect.ts"() {
|
24271
|
+
import_hanji4 = __toESM(require_hanji());
|
24272
|
+
init_views();
|
24273
|
+
init_pgSerializer();
|
24274
|
+
init_introspect();
|
24275
|
+
init_global();
|
24276
|
+
init_mjs();
|
24277
|
+
pgSchemas = async (client) => {
|
24278
|
+
const res = await client.query(`select s.nspname as table_schema,
|
24279
|
+
s.oid as schema_id,
|
24280
|
+
u.usename as owner
|
24281
|
+
from pg_catalog.pg_namespace s
|
24282
|
+
join pg_catalog.pg_user u on u.usesysid = s.nspowner
|
24283
|
+
where nspname not in ('information_schema', 'pg_catalog')
|
24284
|
+
and nspname not like 'pg_toast%'
|
24285
|
+
and nspname not like 'pg_temp_%'
|
24286
|
+
order by table_schema;`);
|
24287
|
+
return res.map((it) => it["table_schema"]);
|
24288
|
+
};
|
24289
|
+
pgPushIntrospect = async (connection, filters, schemaFilters) => {
|
24290
|
+
const { client } = connection;
|
24291
|
+
const matchers = filters.map((it) => {
|
24292
|
+
return new Minimatch(it);
|
24293
|
+
});
|
24294
|
+
const filter2 = (tableName) => {
|
24295
|
+
if (matchers.length === 0)
|
24296
|
+
return true;
|
24297
|
+
for (let i = 0; i < matchers.length; i++) {
|
24298
|
+
const matcher = matchers[i];
|
24299
|
+
if (matcher.match(tableName))
|
24300
|
+
return true;
|
24301
|
+
}
|
24302
|
+
return false;
|
24303
|
+
};
|
24304
|
+
const res = await fromDatabase2(client, filter2, schemaFilters);
|
24305
|
+
const schema4 = { id: originUUID, prevId: "", ...res };
|
24306
|
+
const { internal, ...schemaWithoutInternals } = schema4;
|
24307
|
+
return { schema: schemaWithoutInternals };
|
24308
|
+
};
|
24309
|
+
pgIntrospect = async (config, filters, schemaFilters) => {
|
24310
|
+
const { connectToPg: connectToPg2 } = await Promise.resolve().then(() => (init_pgConnect(), pgConnect_exports));
|
24311
|
+
const pool = await connectToPg2(config);
|
24312
|
+
const matchers = filters.map((it) => {
|
24313
|
+
return new Minimatch(it);
|
24314
|
+
});
|
24315
|
+
const filter2 = (tableName) => {
|
24316
|
+
if (matchers.length === 0)
|
24317
|
+
return true;
|
24318
|
+
for (let i = 0; i < matchers.length; i++) {
|
24319
|
+
const matcher = matchers[i];
|
24320
|
+
if (matcher.match(tableName))
|
24321
|
+
return true;
|
24322
|
+
}
|
24323
|
+
return false;
|
24324
|
+
};
|
24325
|
+
const progress = new IntrospectProgress();
|
24326
|
+
const res = await (0, import_hanji4.renderWithTask)(
|
24327
|
+
progress,
|
24328
|
+
fromDatabase2(pool.client, filter2, schemaFilters, (stage, count, status) => {
|
24329
|
+
progress.update(stage, count, status);
|
24330
|
+
})
|
24331
|
+
);
|
24332
|
+
const schema4 = { id: originUUID, prevId: "", ...res };
|
24333
|
+
const ts = schemaToTypeScript(schema4, { casing: config.introspect.casing });
|
24334
|
+
const { internal, ...schemaWithoutInternals } = schema4;
|
24335
|
+
return { schema: schemaWithoutInternals, ts };
|
24336
|
+
};
|
24337
|
+
}
|
24338
|
+
});
|
24339
|
+
|
24340
|
+
// src/cli/selector-ui.ts
|
24341
|
+
var import_hanji5, Select;
|
24342
|
+
var init_selector_ui = __esm({
|
24343
|
+
"src/cli/selector-ui.ts"() {
|
24344
|
+
init_source();
|
24345
|
+
import_hanji5 = __toESM(require_hanji());
|
24346
|
+
Select = class extends import_hanji5.Prompt {
|
24347
|
+
constructor(items) {
|
24348
|
+
super();
|
24349
|
+
this.on("attach", (terminal) => terminal.toggleCursor("hide"));
|
24350
|
+
this.on("detach", (terminal) => terminal.toggleCursor("show"));
|
24351
|
+
this.data = new import_hanji5.SelectState(
|
24352
|
+
items.map((it) => ({ label: it, value: `${it}-value` }))
|
24353
|
+
);
|
24354
|
+
this.data.bind(this);
|
24355
|
+
}
|
24356
|
+
render(status) {
|
24357
|
+
if (status === "submitted" || status === "aborted")
|
24358
|
+
return "";
|
24359
|
+
let text = ``;
|
24360
|
+
this.data.items.forEach((it, idx) => {
|
24361
|
+
text += idx === this.data.selectedIdx ? `${source_default.green("\u276F " + it.label)}` : ` ${it.label}`;
|
24362
|
+
text += idx != this.data.items.length - 1 ? "\n" : "";
|
24363
|
+
});
|
24364
|
+
return text;
|
24365
|
+
}
|
24366
|
+
result() {
|
24367
|
+
return {
|
24368
|
+
index: this.data.selectedIdx,
|
24369
|
+
value: this.data.items[this.data.selectedIdx].value
|
24370
|
+
};
|
24371
|
+
}
|
24372
|
+
};
|
24373
|
+
}
|
24374
|
+
});
|
24375
|
+
|
24376
|
+
// src/cli/commands/pgPushUtils.ts
|
24377
|
+
function tableNameWithSchemaFrom(statement) {
|
24378
|
+
return statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
|
24379
|
+
}
|
24380
|
+
var import_hanji6, pgSuggestions;
|
24381
|
+
var init_pgPushUtils = __esm({
|
24382
|
+
"src/cli/commands/pgPushUtils.ts"() {
|
24383
|
+
init_source();
|
24384
|
+
import_hanji6 = __toESM(require_hanji());
|
24385
|
+
init_pgSchema();
|
24386
|
+
init_sqlgenerator();
|
24387
|
+
init_selector_ui();
|
24388
|
+
pgSuggestions = async ({
|
24389
|
+
connection,
|
24390
|
+
statements
|
24391
|
+
}) => {
|
24392
|
+
let shouldAskForApprove = false;
|
24393
|
+
const statementsToExecute = [];
|
24394
|
+
const infoToPrint = [];
|
24395
|
+
const tablesToRemove = [];
|
24396
|
+
const columnsToRemove = [];
|
24397
|
+
const schemasToRemove = [];
|
24398
|
+
const tablesToTruncate = [];
|
24399
|
+
for (const statement of statements) {
|
24400
|
+
if (statement.type === "drop_table") {
|
24401
|
+
const res = await connection.query(
|
24402
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24403
|
+
);
|
24404
|
+
const count = Number(res[0].count);
|
24405
|
+
if (count > 0) {
|
24406
|
+
infoToPrint.push(
|
24407
|
+
`\xB7 You're about to delete ${source_default.underline(
|
24408
|
+
statement.tableName
|
24409
|
+
)} table with ${count} items`
|
24410
|
+
);
|
24411
|
+
tablesToRemove.push(statement.tableName);
|
24412
|
+
shouldAskForApprove = true;
|
24413
|
+
}
|
24414
|
+
} else if (statement.type === "alter_table_drop_column") {
|
24415
|
+
const res = await connection.query(
|
24416
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24417
|
+
);
|
24418
|
+
const count = Number(res[0].count);
|
24419
|
+
if (count > 0) {
|
24420
|
+
infoToPrint.push(
|
24421
|
+
`\xB7 You're about to delete ${source_default.underline(
|
24422
|
+
statement.columnName
|
24423
|
+
)} column in ${statement.tableName} table with ${count} items`
|
24424
|
+
);
|
24425
|
+
columnsToRemove.push(`${statement.tableName}_${statement.columnName}`);
|
24426
|
+
shouldAskForApprove = true;
|
24427
|
+
}
|
24428
|
+
} else if (statement.type === "drop_schema") {
|
24429
|
+
const res = await connection.query(
|
24430
|
+
`select count(*) as count from information_schema.tables where table_schema = '${statement.name}';`
|
24431
|
+
);
|
24432
|
+
const count = Number(res[0].count);
|
24433
|
+
if (count > 0) {
|
24434
|
+
infoToPrint.push(
|
24435
|
+
`\xB7 You're about to delete ${source_default.underline(
|
24436
|
+
statement.name
|
24437
|
+
)} schema with ${count} tables`
|
24438
|
+
);
|
24439
|
+
schemasToRemove.push(statement.name);
|
24440
|
+
shouldAskForApprove = true;
|
24441
|
+
}
|
24442
|
+
} else if (statement.type === "alter_table_alter_column_set_type") {
|
24443
|
+
const res = await connection.query(
|
24444
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24445
|
+
);
|
24446
|
+
const count = Number(res[0].count);
|
24447
|
+
if (count > 0) {
|
24448
|
+
infoToPrint.push(
|
24449
|
+
`\xB7 You're about to change ${source_default.underline(
|
24450
|
+
statement.columnName
|
24451
|
+
)} column type from ${source_default.underline(
|
24452
|
+
statement.oldDataType
|
24453
|
+
)} to ${source_default.underline(statement.newDataType)} with ${count} items`
|
24454
|
+
);
|
24455
|
+
statementsToExecute.push(
|
24456
|
+
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
24457
|
+
);
|
24458
|
+
tablesToTruncate.push(statement.tableName);
|
24459
|
+
shouldAskForApprove = true;
|
24460
|
+
}
|
24461
|
+
} else if (statement.type === "alter_table_alter_column_drop_default") {
|
24462
|
+
if (statement.columnNotNull) {
|
24463
|
+
const res = await connection.query(
|
24464
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24465
|
+
);
|
24466
|
+
const count = Number(res[0].count);
|
24467
|
+
if (count > 0) {
|
24468
|
+
infoToPrint.push(
|
24469
|
+
`\xB7 You're about to remove default value from ${source_default.underline(
|
24470
|
+
statement.columnName
|
24471
|
+
)} not-null column with ${count} items`
|
24472
|
+
);
|
24473
|
+
tablesToTruncate.push(statement.tableName);
|
24474
|
+
statementsToExecute.push(
|
24475
|
+
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
24476
|
+
);
|
24477
|
+
shouldAskForApprove = true;
|
24478
|
+
}
|
24479
|
+
}
|
24480
|
+
} else if (statement.type === "alter_table_alter_column_set_notnull") {
|
24481
|
+
if (typeof statement.columnDefault === "undefined") {
|
24482
|
+
const res = await connection.query(
|
24483
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24484
|
+
);
|
24485
|
+
const count = Number(res[0].count);
|
24486
|
+
if (count > 0) {
|
24487
|
+
infoToPrint.push(
|
24488
|
+
`\xB7 You're about to set not-null constraint to ${source_default.underline(
|
24489
|
+
statement.columnName
|
24490
|
+
)} column without default, which contains ${count} items`
|
24491
|
+
);
|
24492
|
+
tablesToTruncate.push(statement.tableName);
|
24493
|
+
statementsToExecute.push(
|
24494
|
+
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
24495
|
+
);
|
24496
|
+
shouldAskForApprove = true;
|
24497
|
+
}
|
24498
|
+
}
|
24499
|
+
} else if (statement.type === "alter_table_alter_column_drop_pk") {
|
24500
|
+
const res = await connection.query(
|
24501
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24502
|
+
);
|
24503
|
+
const count = Number(res[0].count);
|
24504
|
+
if (count > 0) {
|
24505
|
+
infoToPrint.push(
|
24506
|
+
`\xB7 You're about to change ${source_default.underline(
|
24507
|
+
statement.tableName
|
24508
|
+
)} primary key. This statements may fail and you table may left without primary key`
|
24509
|
+
);
|
24510
|
+
tablesToTruncate.push(statement.tableName);
|
24511
|
+
shouldAskForApprove = true;
|
24512
|
+
}
|
24513
|
+
const tableNameWithSchema = tableNameWithSchemaFrom(statement);
|
24514
|
+
const pkNameResponse = await connection.query(
|
24515
|
+
`SELECT constraint_name FROM information_schema.table_constraints
|
24516
|
+
WHERE table_schema = '${typeof statement.schema === "undefined" || statement.schema === "" ? "public" : statement.schema}'
|
24517
|
+
AND table_name = '${statement.tableName}'
|
24518
|
+
AND constraint_type = 'PRIMARY KEY';`
|
24519
|
+
);
|
24520
|
+
statementsToExecute.push(
|
24521
|
+
`ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${pkNameResponse[0].constraint_name}"`
|
24522
|
+
);
|
24523
|
+
continue;
|
24524
|
+
} else if (statement.type === "alter_table_add_column") {
|
24525
|
+
if (statement.column.notNull && typeof statement.column.default === "undefined") {
|
24526
|
+
const res = await connection.query(
|
24527
|
+
`select count(*) as count from ${tableNameWithSchemaFrom(statement)}`
|
24528
|
+
);
|
24529
|
+
const count = Number(res[0].count);
|
24530
|
+
if (count > 0) {
|
24531
|
+
infoToPrint.push(
|
24532
|
+
`\xB7 You're about to add not-null ${source_default.underline(
|
24533
|
+
statement.column.name
|
24534
|
+
)} column without default value, which contains ${count} items`
|
24535
|
+
);
|
24536
|
+
tablesToTruncate.push(statement.tableName);
|
24537
|
+
statementsToExecute.push(
|
24538
|
+
`truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
|
24539
|
+
);
|
24540
|
+
shouldAskForApprove = true;
|
24541
|
+
}
|
24542
|
+
}
|
24543
|
+
} else if (statement.type === "create_unique_constraint") {
|
24544
|
+
const res = await connection.query(
|
24545
|
+
`select count(*) as count from ${tableNameWithSchemaFrom({
|
24546
|
+
schema: statement.schema,
|
24547
|
+
tableName: statement.tableName
|
24548
|
+
})}`
|
24549
|
+
);
|
24550
|
+
const count = Number(res[0].count);
|
24551
|
+
if (count > 0) {
|
24552
|
+
const unsquashedUnique = PgSquasher.unsquashUnique(statement.data);
|
24553
|
+
console.log(
|
24554
|
+
`\xB7 You're about to add ${source_default.underline(
|
24555
|
+
unsquashedUnique.name
|
24556
|
+
)} 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 ${source_default.underline(
|
24557
|
+
statement.tableName
|
24558
|
+
)} table?
|
24559
|
+
`
|
24560
|
+
);
|
24561
|
+
const { status, data } = await (0, import_hanji6.render)(
|
24562
|
+
new Select([
|
24563
|
+
"No, add the constraint without truncating the table",
|
24564
|
+
`Yes, truncate the table`
|
24565
|
+
])
|
24566
|
+
);
|
24567
|
+
if ((data == null ? void 0 : data.index) === 1) {
|
24568
|
+
tablesToTruncate.push(statement.tableName);
|
24569
|
+
statementsToExecute.push(
|
24570
|
+
`truncate table ${tableNameWithSchemaFrom({
|
24571
|
+
schema: statement.schema,
|
24572
|
+
tableName: statement.tableName
|
24573
|
+
})} cascade;`
|
24574
|
+
);
|
24575
|
+
shouldAskForApprove = true;
|
24576
|
+
}
|
24577
|
+
}
|
24578
|
+
}
|
24579
|
+
const stmnt = fromJson([statement], "pg")[0];
|
24580
|
+
if (typeof stmnt !== "undefined") {
|
24581
|
+
if (statement.type === "drop_table") {
|
24582
|
+
statementsToExecute.push(`DROP TABLE ${tableNameWithSchemaFrom(statement)} CASCADE;`);
|
24583
|
+
} else {
|
24584
|
+
statementsToExecute.push(stmnt);
|
24585
|
+
}
|
24586
|
+
}
|
24587
|
+
}
|
24588
|
+
return {
|
24589
|
+
statementsToExecute,
|
24590
|
+
shouldAskForApprove,
|
24591
|
+
infoToPrint,
|
24592
|
+
columnsToRemove: [...new Set(columnsToRemove)],
|
24593
|
+
schemasToRemove: [...new Set(schemasToRemove)],
|
24594
|
+
tablesToTruncate: [...new Set(tablesToTruncate)],
|
24595
|
+
tablesToRemove: [...new Set(tablesToRemove)]
|
24596
|
+
};
|
24597
|
+
};
|
24598
|
+
}
|
24599
|
+
});
|
24600
|
+
|
24601
|
+
// src/sqlite-introspect.ts
|
24602
|
+
var init_sqlite_introspect = __esm({
|
24603
|
+
"src/sqlite-introspect.ts"() {
|
24604
|
+
init_utils2();
|
24605
|
+
}
|
24606
|
+
});
|
24607
|
+
|
24608
|
+
// src/cli/commands/sqliteIntrospect.ts
|
24609
|
+
var import_hanji7, connectToSQLite, sqlitePushIntrospect;
|
24610
|
+
var init_sqliteIntrospect = __esm({
|
24611
|
+
"src/cli/commands/sqliteIntrospect.ts"() {
|
24612
|
+
init_views();
|
24613
|
+
init_global();
|
24614
|
+
init_sqliteSerializer();
|
24615
|
+
init_sqlite_introspect();
|
24616
|
+
init_mjs();
|
24617
|
+
import_hanji7 = __toESM(require_hanji());
|
24618
|
+
connectToSQLite = async (config) => {
|
24619
|
+
const { BetterSqlite: BetterSqlite2, TursoSqlite: TursoSqlite2 } = await Promise.resolve().then(() => (init_drivers(), drivers_exports));
|
24620
|
+
if (config.driver === "better-sqlite") {
|
24621
|
+
const { default: Database } = await import("better-sqlite3");
|
24622
|
+
return {
|
24623
|
+
client: new BetterSqlite2(new Database(config.dbCredentials.url))
|
24624
|
+
};
|
24625
|
+
}
|
24626
|
+
if (config.driver === "libsql") {
|
24627
|
+
const { createClient } = await import("@libsql/client");
|
24628
|
+
return {
|
24629
|
+
client: new TursoSqlite2(
|
24630
|
+
createClient({
|
24631
|
+
url: config.dbCredentials.url
|
24632
|
+
})
|
24633
|
+
)
|
24634
|
+
};
|
24635
|
+
} else {
|
24636
|
+
const { createClient } = await import("@libsql/client");
|
24637
|
+
return {
|
24638
|
+
client: new TursoSqlite2(
|
24639
|
+
createClient({
|
24640
|
+
url: config.dbCredentials.url,
|
24641
|
+
authToken: config.dbCredentials.authToken
|
24642
|
+
})
|
24643
|
+
)
|
24644
|
+
};
|
24645
|
+
}
|
24646
|
+
return {};
|
24647
|
+
};
|
24648
|
+
sqlitePushIntrospect = async (client, filters) => {
|
24649
|
+
const matchers = filters.map((it) => {
|
24650
|
+
return new Minimatch(it);
|
24651
|
+
});
|
24652
|
+
const filter2 = (tableName) => {
|
24653
|
+
if (matchers.length === 0)
|
24654
|
+
return true;
|
24655
|
+
for (let i = 0; i < matchers.length; i++) {
|
24656
|
+
const matcher = matchers[i];
|
24657
|
+
if (matcher.match(tableName))
|
24658
|
+
return true;
|
24659
|
+
}
|
24660
|
+
return false;
|
24661
|
+
};
|
24662
|
+
const res = await fromDatabase3(client, filter2);
|
24663
|
+
const schema4 = { id: originUUID, prevId: "", ...res };
|
24664
|
+
return { schema: schema4 };
|
24665
|
+
};
|
24666
|
+
}
|
24667
|
+
});
|
24668
|
+
|
23924
24669
|
// node_modules/.pnpm/sqlstring@2.3.3/node_modules/sqlstring/lib/SqlString.js
|
23925
24670
|
var require_SqlString = __commonJS({
|
23926
24671
|
"node_modules/.pnpm/sqlstring@2.3.3/node_modules/sqlstring/lib/SqlString.js"(exports) {
|
@@ -33547,30 +34292,30 @@ var require_column_definition = __commonJS({
|
|
33547
34292
|
decimals: this.decimals
|
33548
34293
|
};
|
33549
34294
|
}
|
33550
|
-
static toPacket(
|
34295
|
+
static toPacket(column5, sequenceId) {
|
33551
34296
|
let length = 17;
|
33552
34297
|
fields.forEach((field) => {
|
33553
34298
|
length += Packet.lengthCodedStringLength(
|
33554
|
-
|
33555
|
-
CharsetToEncoding[
|
34299
|
+
column5[field],
|
34300
|
+
CharsetToEncoding[column5.characterSet]
|
33556
34301
|
);
|
33557
34302
|
});
|
33558
34303
|
const buffer = Buffer.allocUnsafe(length);
|
33559
34304
|
const packet = new Packet(sequenceId, buffer, 0, length);
|
33560
34305
|
function writeField(name) {
|
33561
34306
|
packet.writeLengthCodedString(
|
33562
|
-
|
33563
|
-
CharsetToEncoding[
|
34307
|
+
column5[name],
|
34308
|
+
CharsetToEncoding[column5.characterSet]
|
33564
34309
|
);
|
33565
34310
|
}
|
33566
34311
|
packet.offset = 4;
|
33567
34312
|
fields.forEach(writeField);
|
33568
34313
|
packet.writeInt8(12);
|
33569
|
-
packet.writeInt16(
|
33570
|
-
packet.writeInt32(
|
33571
|
-
packet.writeInt8(
|
33572
|
-
packet.writeInt16(
|
33573
|
-
packet.writeInt8(
|
34314
|
+
packet.writeInt16(column5.characterSet);
|
34315
|
+
packet.writeInt32(column5.columnLength);
|
34316
|
+
packet.writeInt8(column5.columnType);
|
34317
|
+
packet.writeInt16(column5.flags);
|
34318
|
+
packet.writeInt8(column5.decimals);
|
33574
34319
|
packet.writeInt16(0);
|
33575
34320
|
return packet;
|
33576
34321
|
}
|
@@ -39246,25 +39991,25 @@ var require_connection2 = __commonJS({
|
|
39246
39991
|
// ===================================
|
39247
39992
|
writeColumns(columns) {
|
39248
39993
|
this.writePacket(Packets.ResultSetHeader.toPacket(columns.length));
|
39249
|
-
columns.forEach((
|
39994
|
+
columns.forEach((column5) => {
|
39250
39995
|
this.writePacket(
|
39251
|
-
Packets.ColumnDefinition.toPacket(
|
39996
|
+
Packets.ColumnDefinition.toPacket(column5, this.serverConfig.encoding)
|
39252
39997
|
);
|
39253
39998
|
});
|
39254
39999
|
this.writeEof();
|
39255
40000
|
}
|
39256
40001
|
// row is array of columns, not hash
|
39257
|
-
writeTextRow(
|
40002
|
+
writeTextRow(column5) {
|
39258
40003
|
this.writePacket(
|
39259
|
-
Packets.TextRow.toPacket(
|
40004
|
+
Packets.TextRow.toPacket(column5, this.serverConfig.encoding)
|
39260
40005
|
);
|
39261
40006
|
}
|
39262
40007
|
writeTextResult(rows, columns) {
|
39263
40008
|
this.writeColumns(columns);
|
39264
40009
|
rows.forEach((row) => {
|
39265
40010
|
const arrayRow = new Array(columns.length);
|
39266
|
-
columns.forEach((
|
39267
|
-
arrayRow.push(row[
|
40011
|
+
columns.forEach((column5) => {
|
40012
|
+
arrayRow.push(row[column5.name]);
|
39268
40013
|
});
|
39269
40014
|
this.writeTextRow(arrayRow);
|
39270
40015
|
});
|
@@ -55960,14 +56705,11 @@ var init_studioUtils = __esm({
|
|
55960
56705
|
import_sqlite_core5 = require("drizzle-orm/sqlite-core");
|
55961
56706
|
init_utils();
|
55962
56707
|
init_serializer();
|
55963
|
-
drizzleDb = async (drizzleConfig, models, logger) => {
|
56708
|
+
drizzleDb = async (drizzleConfig, models, logger, pgClient) => {
|
55964
56709
|
if (drizzleConfig.driver === "pg") {
|
55965
56710
|
const { drizzle: drizzle2 } = await import("drizzle-orm/node-postgres");
|
55966
|
-
const { Pool, types } = await Promise.resolve().then(() => __toESM(require_lib2()));
|
55967
|
-
const client = new Pool({ ...drizzleConfig.dbCredentials, max: 1 });
|
55968
|
-
types.setTypeParser(types.builtins.INTERVAL, (val) => val);
|
55969
56711
|
return {
|
55970
|
-
db: drizzle2(
|
56712
|
+
db: drizzle2(pgClient, { logger }),
|
55971
56713
|
type: "pg",
|
55972
56714
|
schema: models.pgSchema
|
55973
56715
|
};
|
@@ -56026,25 +56768,6 @@ var init_studioUtils = __esm({
|
|
56026
56768
|
}
|
56027
56769
|
});
|
56028
56770
|
|
56029
|
-
// src/cli/commands/pgConnect.ts
|
56030
|
-
var import_pg, connectToPg;
|
56031
|
-
var init_pgConnect = __esm({
|
56032
|
-
"src/cli/commands/pgConnect.ts"() {
|
56033
|
-
import_pg = __toESM(require_lib2());
|
56034
|
-
init_drivers();
|
56035
|
-
connectToPg = async (config) => {
|
56036
|
-
if (config.driver === "pg") {
|
56037
|
-
const client = new import_pg.Client(config.dbCredentials);
|
56038
|
-
await client.connect();
|
56039
|
-
const connection = new PgPostgres(client);
|
56040
|
-
return { client: connection };
|
56041
|
-
} else {
|
56042
|
-
throw Error(`Only "pg" is available as a driver option for introspection`);
|
56043
|
-
}
|
56044
|
-
};
|
56045
|
-
}
|
56046
|
-
});
|
56047
|
-
|
56048
56771
|
// src/utils.ts
|
56049
56772
|
var utils_exports = {};
|
56050
56773
|
__export(utils_exports, {
|
@@ -56264,8 +56987,8 @@ var init_utils4 = __esm({
|
|
56264
56987
|
const out = it.schema ? `"${it.schema}"."${it.name}"` : `"${it.name}"`;
|
56265
56988
|
return out;
|
56266
56989
|
};
|
56267
|
-
columnRenameKey = (table4, schema4,
|
56268
|
-
const out = schema4 ? `"${schema4}"."${table4}"."${
|
56990
|
+
columnRenameKey = (table4, schema4, column5) => {
|
56991
|
+
const out = schema4 ? `"${schema4}"."${table4}"."${column5}"` : `"${table4}"."${column5}"`;
|
56269
56992
|
return out;
|
56270
56993
|
};
|
56271
56994
|
kloudMeta = () => {
|
@@ -56412,20 +57135,40 @@ var init_utils4 = __esm({
|
|
56412
57135
|
[]
|
56413
57136
|
);
|
56414
57137
|
const models = toDrizzle2(schema4);
|
56415
|
-
|
57138
|
+
const drizzleDbInstance = await drizzleDb(
|
57139
|
+
connection,
|
57140
|
+
{ sqliteSchema: models },
|
57141
|
+
false
|
57142
|
+
);
|
57143
|
+
return { public: drizzleDbInstance };
|
56416
57144
|
} else if (connection.driver === "pg") {
|
56417
57145
|
const preparedConnection = await connectToPg({
|
56418
57146
|
driver: connection.driver,
|
56419
57147
|
// @ts-ignore I don't know how to fix it yet. But will find a way
|
56420
57148
|
dbCredentials: connection.dbCredentials
|
56421
57149
|
});
|
56422
|
-
const {
|
56423
|
-
|
56424
|
-
|
56425
|
-
|
56426
|
-
);
|
56427
|
-
|
56428
|
-
|
57150
|
+
const { pgSchemas: pgSchemas2, pgPushIntrospect: pgPushIntrospect2 } = await Promise.resolve().then(() => (init_pgIntrospect(), pgIntrospect_exports));
|
57151
|
+
const schemas = await pgSchemas2(preparedConnection.client);
|
57152
|
+
const response = {};
|
57153
|
+
const { Pool, types } = await Promise.resolve().then(() => __toESM(require_lib2()));
|
57154
|
+
const client = new Pool({ ...connection.dbCredentials, max: 1 });
|
57155
|
+
types.setTypeParser(types.builtins.INTERVAL, (val) => val);
|
57156
|
+
for (const schemaName of schemas) {
|
57157
|
+
const { schema: schema4 } = await pgPushIntrospect2(
|
57158
|
+
{ client: preparedConnection.client },
|
57159
|
+
[],
|
57160
|
+
[schemaName]
|
57161
|
+
);
|
57162
|
+
const models = toDrizzle(schema4, schemaName);
|
57163
|
+
response[schemaName] = await drizzleDb(
|
57164
|
+
// @ts-ignore I don't know how to fix it yet. But will find a way
|
57165
|
+
connection,
|
57166
|
+
{ pgSchema: models },
|
57167
|
+
false,
|
57168
|
+
client
|
57169
|
+
);
|
57170
|
+
}
|
57171
|
+
return response;
|
56429
57172
|
}
|
56430
57173
|
throw Error("Only sqlite and pg is supported for now");
|
56431
57174
|
};
|