drizzle-kit 0.20.6-88f61dc → 0.20.7-3bc340d

Sign up to get free protection for your applications and to get access to all the features.
package/utils-studio.js CHANGED
@@ -1098,470 +1098,17 @@ var init_outputs = __esm({
1098
1098
  }
1099
1099
  });
1100
1100
 
1101
- // src/serializer/sqliteSerializer.ts
1102
- function mapSqlToSqliteType(sqlType) {
1103
- if ([
1104
- "int",
1105
- "integer",
1106
- "integer auto_increment",
1107
- "tinyint",
1108
- "smallint",
1109
- "mediumint",
1110
- "bigint",
1111
- "unsigned big int",
1112
- "int2",
1113
- "int8"
1114
- ].includes(sqlType.toLowerCase())) {
1115
- return "integer";
1116
- } else if ([
1117
- "character",
1118
- "varchar",
1119
- "vatying character",
1120
- "nchar",
1121
- "native character",
1122
- "nvarchar",
1123
- "text",
1124
- "clob"
1125
- ].some((it) => it.startsWith(sqlType.toLowerCase()))) {
1126
- return "text";
1127
- } else if (sqlType.toLowerCase() === "blob") {
1128
- return "blob";
1129
- } else if (["real", "double", "double precision", "float"].includes(
1130
- sqlType.toLowerCase()
1131
- )) {
1132
- return "real";
1133
- } else {
1134
- return "numeric";
1135
- }
1136
- }
1137
- var import_drizzle_orm, import_sqlite_core2, dialect, fromDatabase;
1138
- var init_sqliteSerializer = __esm({
1139
- "src/serializer/sqliteSerializer.ts"() {
1140
- import_drizzle_orm = require("drizzle-orm");
1141
- import_sqlite_core2 = require("drizzle-orm/sqlite-core");
1142
- init_serializer();
1143
- init_outputs();
1144
- init_source();
1145
- dialect = new import_sqlite_core2.SQLiteSyncDialect();
1146
- fromDatabase = async (db, tablesFilter = (table) => true, progressCallback) => {
1147
- const result = {};
1148
- const columns = await db.query(
1149
- `SELECT
1150
- m.name as "tableName", p.name as "columnName", p.type as "columnType", p."notnull" as "notNull", p.dflt_value as "defaultValue", p.pk as pk
1151
- FROM sqlite_master AS m JOIN pragma_table_info(m.name) AS p
1152
- WHERE m.type = 'table' and m.tbl_name != 'sqlite_sequence' and m.tbl_name != 'sqlite_stat1' and m.tbl_name != '_litestream_seq' and m.tbl_name != '_litestream_lock' and m.tbl_name != 'libsql_wasm_func_table';
1153
- `
1154
- );
1155
- const tablesWithSeq = [];
1156
- const seq = await db.query(
1157
- `SELECT * FROM sqlite_master WHERE name != 'sqlite_sequence' and name != 'sqlite_stat1' and name != '_litestream_seq' and name != '_litestream_lock' and sql GLOB '*[ *' || CHAR(9) || CHAR(10) || CHAR(13) || ']AUTOINCREMENT[^'']*';`
1158
- );
1159
- for (const s of seq) {
1160
- tablesWithSeq.push(s.name);
1161
- }
1162
- let columnsCount = 0;
1163
- let tablesCount = /* @__PURE__ */ new Set();
1164
- let indexesCount = 0;
1165
- let foreignKeysCount = 0;
1166
- const tableToPk = {};
1167
- for (const column of columns) {
1168
- if (!tablesFilter(column.tableName))
1169
- continue;
1170
- columnsCount += 1;
1171
- if (progressCallback) {
1172
- progressCallback("columns", columnsCount, "fetching");
1173
- }
1174
- const tableName = column.tableName;
1175
- tablesCount.add(tableName);
1176
- if (progressCallback) {
1177
- progressCallback("tables", tablesCount.size, "fetching");
1178
- }
1179
- const columnName = column.columnName;
1180
- const isNotNull = column.notNull === 1;
1181
- const columnType = column.columnType;
1182
- const isPrimary = column.pk !== 0;
1183
- const columnDefault = column.defaultValue;
1184
- const isAutoincrement = isPrimary && tablesWithSeq.includes(tableName);
1185
- if (isPrimary) {
1186
- if (typeof tableToPk[tableName] === "undefined") {
1187
- tableToPk[tableName] = [columnName];
1188
- } else {
1189
- tableToPk[tableName].push(columnName);
1190
- }
1191
- }
1192
- const table = result[tableName];
1193
- const newColumn = {
1194
- default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) ? Number(columnDefault) : ["CURRENT_TIME", "CURRENT_DATE", "CURRENT_TIMESTAMP"].includes(
1195
- columnDefault
1196
- ) ? `(${columnDefault})` : columnDefault === "false" ? false : columnDefault === "true" ? true : columnDefault.startsWith("'") && columnDefault.endsWith("'") ? columnDefault : (
1197
- // ? columnDefault.substring(1, columnDefault.length - 1)
1198
- `(${columnDefault})`
1199
- ),
1200
- autoincrement: isAutoincrement,
1201
- name: columnName,
1202
- type: mapSqlToSqliteType(columnType),
1203
- primaryKey: false,
1204
- notNull: isNotNull
1205
- };
1206
- if (!table) {
1207
- result[tableName] = {
1208
- name: tableName,
1209
- columns: {
1210
- [columnName]: newColumn
1211
- },
1212
- compositePrimaryKeys: {},
1213
- indexes: {},
1214
- foreignKeys: {},
1215
- uniqueConstraints: {}
1216
- };
1217
- } else {
1218
- result[tableName].columns[columnName] = newColumn;
1219
- }
1220
- }
1221
- for (const [key, value] of Object.entries(tableToPk)) {
1222
- if (value.length > 1) {
1223
- value.sort();
1224
- result[key].compositePrimaryKeys = {
1225
- [`${key}_${value.join("_")}_pk`]: {
1226
- columns: value,
1227
- name: `${key}_${value.join("_")}_pk`
1228
- }
1229
- };
1230
- } else if (value.length === 1) {
1231
- result[key].columns[value[0]].primaryKey = true;
1232
- } else {
1233
- }
1234
- }
1235
- if (progressCallback) {
1236
- progressCallback("columns", columnsCount, "done");
1237
- progressCallback("tables", tablesCount.size, "done");
1238
- }
1239
- try {
1240
- const fks = await db.query(
1241
- `SELECT m.name as "tableFrom", f.id as "id", f."table" as "tableTo", f."from", f."to", f."on_update" as "onUpdate", f."on_delete" as "onDelete", f.seq as "seq"
1242
- FROM sqlite_master m, pragma_foreign_key_list(m.name) as f;`
1243
- );
1244
- const fkByTableName = {};
1245
- for (const fkRow of fks) {
1246
- foreignKeysCount += 1;
1247
- if (progressCallback) {
1248
- progressCallback("fks", foreignKeysCount, "fetching");
1249
- }
1250
- const tableName = fkRow.tableFrom;
1251
- const columnName = fkRow.from;
1252
- const refTableName = fkRow.tableTo;
1253
- const refColumnName = fkRow.to;
1254
- const updateRule = fkRow.onUpdate;
1255
- const deleteRule = fkRow.onDelete;
1256
- const sequence = fkRow.seq;
1257
- const id = fkRow.id;
1258
- const tableInResult = result[tableName];
1259
- if (typeof tableInResult === "undefined")
1260
- continue;
1261
- if (typeof fkByTableName[`${tableName}_${id}`] !== "undefined") {
1262
- fkByTableName[`${tableName}_${id}`].columnsFrom.push(columnName);
1263
- fkByTableName[`${tableName}_${id}`].columnsTo.push(refColumnName);
1264
- } else {
1265
- fkByTableName[`${tableName}_${id}`] = {
1266
- name: "",
1267
- tableFrom: tableName,
1268
- tableTo: refTableName,
1269
- columnsFrom: [columnName],
1270
- columnsTo: [refColumnName],
1271
- onDelete: deleteRule == null ? void 0 : deleteRule.toLowerCase(),
1272
- onUpdate: updateRule == null ? void 0 : updateRule.toLowerCase()
1273
- };
1274
- }
1275
- const columnsFrom = fkByTableName[`${tableName}_${id}`].columnsFrom;
1276
- const columnsTo = fkByTableName[`${tableName}_${id}`].columnsTo;
1277
- fkByTableName[`${tableName}_${id}`].name = `${tableName}_${columnsFrom.join(
1278
- "_"
1279
- )}_${refTableName}_${columnsTo.join("_")}_fk`;
1280
- }
1281
- for (const idx of Object.keys(fkByTableName)) {
1282
- const value = fkByTableName[idx];
1283
- result[value.tableFrom].foreignKeys[value.name] = value;
1284
- }
1285
- } catch (e) {
1286
- }
1287
- if (progressCallback) {
1288
- progressCallback("fks", foreignKeysCount, "done");
1289
- }
1290
- const idxs = await db.query(
1291
- `SELECT
1292
- m.tbl_name as tableName,
1293
- il.name as indexName,
1294
- ii.name as columnName,
1295
- il.[unique] as isUnique,
1296
- il.seq as seq
1297
- FROM sqlite_master AS m,
1298
- pragma_index_list(m.name) AS il,
1299
- pragma_index_info(il.name) AS ii
1300
- WHERE
1301
- m.type = 'table' and il.name NOT LIKE 'sqlite_autoindex_%';`
1302
- );
1303
- for (const idxRow of idxs) {
1304
- const tableName = idxRow.tableName;
1305
- const constraintName = idxRow.indexName;
1306
- const columnName = idxRow.columnName;
1307
- const isUnique = idxRow.isUnique === 1;
1308
- const tableInResult = result[tableName];
1309
- if (typeof tableInResult === "undefined")
1310
- continue;
1311
- indexesCount += 1;
1312
- if (progressCallback) {
1313
- progressCallback("indexes", indexesCount, "fetching");
1314
- }
1315
- if (typeof tableInResult.indexes[constraintName] !== "undefined") {
1316
- tableInResult.indexes[constraintName].columns.push(columnName);
1317
- } else {
1318
- tableInResult.indexes[constraintName] = {
1319
- name: constraintName,
1320
- columns: [columnName],
1321
- isUnique
1322
- };
1323
- }
1324
- }
1325
- if (progressCallback) {
1326
- progressCallback("indexes", indexesCount, "done");
1327
- progressCallback("enums", 0, "done");
1328
- }
1329
- return {
1330
- version: "5",
1331
- dialect: "sqlite",
1332
- tables: result,
1333
- enums: {},
1334
- _meta: {
1335
- tables: {},
1336
- columns: {}
1337
- }
1338
- };
1339
- };
1340
- }
1341
- });
1342
-
1343
- // node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js
1344
- var require_balanced_match = __commonJS({
1345
- "node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js"(exports, module2) {
1346
- "use strict";
1347
- module2.exports = balanced;
1348
- function balanced(a, b, str) {
1349
- if (a instanceof RegExp)
1350
- a = maybeMatch(a, str);
1351
- if (b instanceof RegExp)
1352
- b = maybeMatch(b, str);
1353
- var r = range(a, b, str);
1354
- return r && {
1355
- start: r[0],
1356
- end: r[1],
1357
- pre: str.slice(0, r[0]),
1358
- body: str.slice(r[0] + a.length, r[1]),
1359
- post: str.slice(r[1] + b.length)
1360
- };
1361
- }
1362
- function maybeMatch(reg, str) {
1363
- var m = str.match(reg);
1364
- return m ? m[0] : null;
1365
- }
1366
- balanced.range = range;
1367
- function range(a, b, str) {
1368
- var begs, beg, left, right, result;
1369
- var ai = str.indexOf(a);
1370
- var bi = str.indexOf(b, ai + 1);
1371
- var i = ai;
1372
- if (ai >= 0 && bi > 0) {
1373
- if (a === b) {
1374
- return [ai, bi];
1375
- }
1376
- begs = [];
1377
- left = str.length;
1378
- while (i >= 0 && !result) {
1379
- if (i == ai) {
1380
- begs.push(i);
1381
- ai = str.indexOf(a, i + 1);
1382
- } else if (begs.length == 1) {
1383
- result = [begs.pop(), bi];
1384
- } else {
1385
- beg = begs.pop();
1386
- if (beg < left) {
1387
- left = beg;
1388
- right = bi;
1389
- }
1390
- bi = str.indexOf(b, i + 1);
1391
- }
1392
- i = ai < bi && ai >= 0 ? ai : bi;
1393
- }
1394
- if (begs.length) {
1395
- result = [left, right];
1396
- }
1397
- }
1398
- return result;
1399
- }
1400
- }
1401
- });
1402
-
1403
- // node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js
1404
- var require_brace_expansion = __commonJS({
1405
- "node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js"(exports, module2) {
1406
- var balanced = require_balanced_match();
1407
- module2.exports = expandTop;
1408
- var escSlash = "\0SLASH" + Math.random() + "\0";
1409
- var escOpen = "\0OPEN" + Math.random() + "\0";
1410
- var escClose = "\0CLOSE" + Math.random() + "\0";
1411
- var escComma = "\0COMMA" + Math.random() + "\0";
1412
- var escPeriod = "\0PERIOD" + Math.random() + "\0";
1413
- function numeric(str) {
1414
- return parseInt(str, 10) == str ? parseInt(str, 10) : str.charCodeAt(0);
1415
- }
1416
- function escapeBraces(str) {
1417
- return str.split("\\\\").join(escSlash).split("\\{").join(escOpen).split("\\}").join(escClose).split("\\,").join(escComma).split("\\.").join(escPeriod);
1418
- }
1419
- function unescapeBraces(str) {
1420
- return str.split(escSlash).join("\\").split(escOpen).join("{").split(escClose).join("}").split(escComma).join(",").split(escPeriod).join(".");
1421
- }
1422
- function parseCommaParts(str) {
1423
- if (!str)
1424
- return [""];
1425
- var parts = [];
1426
- var m = balanced("{", "}", str);
1427
- if (!m)
1428
- return str.split(",");
1429
- var pre = m.pre;
1430
- var body = m.body;
1431
- var post = m.post;
1432
- var p = pre.split(",");
1433
- p[p.length - 1] += "{" + body + "}";
1434
- var postParts = parseCommaParts(post);
1435
- if (post.length) {
1436
- p[p.length - 1] += postParts.shift();
1437
- p.push.apply(p, postParts);
1438
- }
1439
- parts.push.apply(parts, p);
1440
- return parts;
1441
- }
1442
- function expandTop(str) {
1443
- if (!str)
1444
- return [];
1445
- if (str.substr(0, 2) === "{}") {
1446
- str = "\\{\\}" + str.substr(2);
1447
- }
1448
- return expand2(escapeBraces(str), true).map(unescapeBraces);
1449
- }
1450
- function embrace(str) {
1451
- return "{" + str + "}";
1452
- }
1453
- function isPadded(el) {
1454
- return /^-?0\d/.test(el);
1455
- }
1456
- function lte(i, y) {
1457
- return i <= y;
1458
- }
1459
- function gte(i, y) {
1460
- return i >= y;
1461
- }
1462
- function expand2(str, isTop) {
1463
- var expansions = [];
1464
- var m = balanced("{", "}", str);
1465
- if (!m)
1466
- return [str];
1467
- var pre = m.pre;
1468
- var post = m.post.length ? expand2(m.post, false) : [""];
1469
- if (/\$$/.test(m.pre)) {
1470
- for (var k = 0; k < post.length; k++) {
1471
- var expansion = pre + "{" + m.body + "}" + post[k];
1472
- expansions.push(expansion);
1473
- }
1474
- } else {
1475
- var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
1476
- var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
1477
- var isSequence = isNumericSequence || isAlphaSequence;
1478
- var isOptions = m.body.indexOf(",") >= 0;
1479
- if (!isSequence && !isOptions) {
1480
- if (m.post.match(/,.*\}/)) {
1481
- str = m.pre + "{" + m.body + escClose + m.post;
1482
- return expand2(str);
1483
- }
1484
- return [str];
1485
- }
1486
- var n;
1487
- if (isSequence) {
1488
- n = m.body.split(/\.\./);
1489
- } else {
1490
- n = parseCommaParts(m.body);
1491
- if (n.length === 1) {
1492
- n = expand2(n[0], false).map(embrace);
1493
- if (n.length === 1) {
1494
- return post.map(function(p) {
1495
- return m.pre + n[0] + p;
1496
- });
1497
- }
1498
- }
1499
- }
1500
- var N;
1501
- if (isSequence) {
1502
- var x = numeric(n[0]);
1503
- var y = numeric(n[1]);
1504
- var width = Math.max(n[0].length, n[1].length);
1505
- var incr = n.length == 3 ? Math.abs(numeric(n[2])) : 1;
1506
- var test = lte;
1507
- var reverse = y < x;
1508
- if (reverse) {
1509
- incr *= -1;
1510
- test = gte;
1511
- }
1512
- var pad = n.some(isPadded);
1513
- N = [];
1514
- for (var i = x; test(i, y); i += incr) {
1515
- var c;
1516
- if (isAlphaSequence) {
1517
- c = String.fromCharCode(i);
1518
- if (c === "\\")
1519
- c = "";
1520
- } else {
1521
- c = String(i);
1522
- if (pad) {
1523
- var need = width - c.length;
1524
- if (need > 0) {
1525
- var z = new Array(need + 1).join("0");
1526
- if (i < 0)
1527
- c = "-" + z + c.slice(1);
1528
- else
1529
- c = z + c;
1530
- }
1531
- }
1532
- }
1533
- N.push(c);
1534
- }
1535
- } else {
1536
- N = [];
1537
- for (var j = 0; j < n.length; j++) {
1538
- N.push.apply(N, expand2(n[j], false));
1539
- }
1540
- }
1541
- for (var j = 0; j < N.length; j++) {
1542
- for (var k = 0; k < post.length; k++) {
1543
- var expansion = pre + N[j] + post[k];
1544
- if (!isTop || isSequence || expansion)
1545
- expansions.push(expansion);
1546
- }
1547
- }
1548
- }
1549
- return expansions;
1550
- }
1551
- }
1552
- });
1553
-
1554
1101
  // src/serializer/pgSerializer.ts
1555
- var import_pg_core2, import_pg_core3, import_drizzle_orm2, dialect2, trimChar, fromDatabase2, columnToDefault, defaultForColumn;
1102
+ var import_pg_core, import_pg_core2, import_drizzle_orm, dialect, trimChar, fromDatabase, columnToDefault, defaultForColumn, toDrizzle;
1556
1103
  var init_pgSerializer = __esm({
1557
1104
  "src/serializer/pgSerializer.ts"() {
1105
+ import_pg_core = require("drizzle-orm/pg-core");
1558
1106
  import_pg_core2 = require("drizzle-orm/pg-core");
1559
- import_pg_core3 = require("drizzle-orm/pg-core");
1560
- import_drizzle_orm2 = require("drizzle-orm");
1107
+ import_drizzle_orm = require("drizzle-orm");
1561
1108
  init_serializer();
1562
1109
  init_source();
1563
1110
  init_outputs();
1564
- dialect2 = new import_pg_core2.PgDialect();
1111
+ dialect = new import_pg_core.PgDialect();
1565
1112
  trimChar = (str, char) => {
1566
1113
  let start = 0;
1567
1114
  let end = str.length;
@@ -1571,7 +1118,7 @@ var init_pgSerializer = __esm({
1571
1118
  --end;
1572
1119
  return start > 0 || end < str.length ? str.substring(start, end) : str.toString();
1573
1120
  };
1574
- fromDatabase2 = async (db, tablesFilter = (table) => true, schemaFilters, progressCallback) => {
1121
+ fromDatabase = async (db, tablesFilter = (table) => true, schemaFilters, progressCallback) => {
1575
1122
  const result = {};
1576
1123
  const internals = { tables: {} };
1577
1124
  const where = schemaFilters.map((t) => `table_schema = '${t}'`).join(" or ");
@@ -1852,114 +1399,713 @@ var init_pgSerializer = __esm({
1852
1399
  rej(e);
1853
1400
  return;
1854
1401
  }
1855
- res("");
1856
- });
1857
- });
1858
- if (progressCallback) {
1859
- progressCallback("tables", tableCount, "done");
1402
+ res("");
1403
+ });
1404
+ });
1405
+ if (progressCallback) {
1406
+ progressCallback("tables", tableCount, "done");
1407
+ }
1408
+ for await (const _ of all) {
1409
+ }
1410
+ if (progressCallback) {
1411
+ progressCallback("columns", columnsCount, "done");
1412
+ progressCallback("indexes", indexesCount, "done");
1413
+ progressCallback("fks", foreignKeysCount, "done");
1414
+ }
1415
+ const allEnums = await db.query(
1416
+ `select n.nspname as enum_schema,
1417
+ t.typname as enum_name,
1418
+ e.enumlabel as enum_value
1419
+ from pg_type t
1420
+ join pg_enum e on t.oid = e.enumtypid
1421
+ join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;`
1422
+ );
1423
+ const enumsToReturn = {};
1424
+ for (const dbEnum of allEnums) {
1425
+ const enumName = dbEnum.enum_name;
1426
+ const enumValue = dbEnum.enum_value;
1427
+ if (enumsToReturn[enumName] !== void 0 && enumsToReturn[enumName] !== null) {
1428
+ enumsToReturn[enumName].values[enumValue] = enumValue;
1429
+ } else {
1430
+ enumsToReturn[enumName] = {
1431
+ name: enumName,
1432
+ values: { [enumValue]: enumValue }
1433
+ };
1434
+ }
1435
+ }
1436
+ if (progressCallback) {
1437
+ progressCallback("enums", Object.keys(enumsToReturn).length, "done");
1438
+ }
1439
+ const schemasObject = Object.fromEntries([...schemas].map((it) => [it, it]));
1440
+ return {
1441
+ version: "5",
1442
+ dialect: "pg",
1443
+ tables: result,
1444
+ enums: enumsToReturn,
1445
+ schemas: schemasObject,
1446
+ _meta: {
1447
+ schemas: {},
1448
+ tables: {},
1449
+ columns: {}
1450
+ },
1451
+ internal: internals
1452
+ };
1453
+ };
1454
+ columnToDefault = {
1455
+ "numeric(": "::numeric",
1456
+ // text: "::text",
1457
+ // "character varying": "::character varying",
1458
+ // "double precision": "::double precision",
1459
+ // "time with time zone": "::time with time zone",
1460
+ "time without time zone": "::time without time zone",
1461
+ // "timestamp with time zone": "::timestamp with time zone",
1462
+ "timestamp without time zone": "::timestamp without time zone",
1463
+ // date: "::date",
1464
+ // interval: "::interval",
1465
+ // character: "::bpchar",
1466
+ // macaddr8: "::macaddr8",
1467
+ // macaddr: "::macaddr",
1468
+ // inet: "::inet",
1469
+ // cidr: "::cidr",
1470
+ // jsonb: "::jsonb",
1471
+ // json: "::json",
1472
+ "character(": "::bpchar"
1473
+ };
1474
+ defaultForColumn = (column) => {
1475
+ if (column.data_type === "serial" || column.data_type === "smallserial" || column.data_type === "bigserial") {
1476
+ return void 0;
1477
+ }
1478
+ const hasDifferentDefaultCast = Object.keys(columnToDefault).find(
1479
+ (it) => column.data_type.startsWith(it)
1480
+ );
1481
+ if (column.column_default === null) {
1482
+ return void 0;
1483
+ }
1484
+ const columnDefaultAsString = column.column_default.toString();
1485
+ if (columnDefaultAsString.endsWith(
1486
+ hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column.data_type
1487
+ )) {
1488
+ const nonPrefixPart = column.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`).length - 1;
1489
+ const rt = column.column_default.toString().substring(1, nonPrefixPart);
1490
+ if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column.data_type.startsWith("numeric")) {
1491
+ return Number(rt);
1492
+ } else if (column.data_type === "json" || column.data_type === "jsonb") {
1493
+ const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
1494
+ return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`}`;
1495
+ } else if (column.data_type === "boolean") {
1496
+ return column.column_default === "true";
1497
+ } else {
1498
+ return `'${rt}'`;
1499
+ }
1500
+ } else {
1501
+ if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !column.data_type.startsWith("numeric")) {
1502
+ return Number(columnDefaultAsString);
1503
+ } else if (column.data_type === "boolean") {
1504
+ return column.column_default === "true";
1505
+ } else {
1506
+ return `${columnDefaultAsString}`;
1507
+ }
1508
+ }
1509
+ };
1510
+ toDrizzle = (schema, schemaName) => {
1511
+ const tables = {};
1512
+ Object.values(schema.tables).forEach((t) => {
1513
+ const columns = {};
1514
+ Object.values(t.columns).forEach((c) => {
1515
+ const columnName = c.name;
1516
+ const type = c.type;
1517
+ let columnBuilder;
1518
+ if (type === "bigint") {
1519
+ columnBuilder = new import_pg_core.PgBigInt53Builder(columnName);
1520
+ } else if (type === "bigserial") {
1521
+ columnBuilder = new import_pg_core.PgBigSerial53Builder(columnName);
1522
+ } else if (type === "boolean") {
1523
+ columnBuilder = new import_pg_core.PgBooleanBuilder(columnName);
1524
+ } else if (type === "cidr") {
1525
+ columnBuilder = new import_pg_core.PgCidrBuilder(columnName);
1526
+ } else if (type === "date") {
1527
+ columnBuilder = new import_pg_core.PgDateBuilder(columnName);
1528
+ } else if (type === "double precision") {
1529
+ columnBuilder = new import_pg_core.PgDoublePrecisionBuilder(columnName);
1530
+ } else if (type === "inet") {
1531
+ columnBuilder = new import_pg_core.PgInetBuilder(columnName);
1532
+ } else if (type === "integer") {
1533
+ columnBuilder = new import_pg_core.PgIntegerBuilder(columnName);
1534
+ } else if (type === "interval" || type.startsWith("interval ")) {
1535
+ columnBuilder = new import_pg_core.PgIntervalBuilder(columnName, {});
1536
+ } else if (type === "json") {
1537
+ columnBuilder = new import_pg_core.PgJsonBuilder(columnName);
1538
+ } else if (type === "jsonb") {
1539
+ columnBuilder = new import_pg_core.PgJsonbBuilder(columnName);
1540
+ } else if (type === "macaddr") {
1541
+ columnBuilder = new import_pg_core.PgMacaddrBuilder(columnName);
1542
+ } else if (type === "macaddr8") {
1543
+ columnBuilder = new import_pg_core.PgMacaddr8Builder(columnName);
1544
+ } else if (type === "numeric" || type.startsWith("numeric(")) {
1545
+ columnBuilder = new import_pg_core.PgNumericBuilder(columnName);
1546
+ } else if (type === "real") {
1547
+ columnBuilder = new import_pg_core.PgRealBuilder(columnName);
1548
+ } else if (type === "serial") {
1549
+ columnBuilder = new import_pg_core.PgSerialBuilder(columnName);
1550
+ } else if (type === "smallint") {
1551
+ columnBuilder = new import_pg_core.PgSmallIntBuilder(columnName);
1552
+ } else if (type === "smallserial") {
1553
+ columnBuilder = new import_pg_core.PgSmallSerialBuilder(columnName);
1554
+ } else if (type === "text") {
1555
+ columnBuilder = new import_pg_core.PgTextBuilder(columnName, {});
1556
+ } else if (type === "time" || type.startsWith("time(") || type === "time with time zone") {
1557
+ columnBuilder = new import_pg_core.PgTimeBuilder(columnName, false, void 0);
1558
+ } else if (type === "timestamp" || type.startsWith("timestamp(") || type === "timestamp with time zone") {
1559
+ columnBuilder = new import_pg_core.PgTimestampBuilder(columnName, false, void 0);
1560
+ } else if (type === "uuid") {
1561
+ columnBuilder = new import_pg_core.PgUUIDBuilder(columnName);
1562
+ } else if (type === "varchar" || type.startsWith("varchar(")) {
1563
+ columnBuilder = new import_pg_core.PgVarcharBuilder(columnName, {});
1564
+ } else if (type === "char" || type.startsWith("char(")) {
1565
+ columnBuilder = new import_pg_core.PgCharBuilder(columnName, {});
1566
+ } else {
1567
+ columnBuilder = (0, import_pg_core.customType)({
1568
+ dataType() {
1569
+ return type;
1570
+ }
1571
+ })(columnName);
1572
+ }
1573
+ if (c.notNull) {
1574
+ columnBuilder = columnBuilder.notNull();
1575
+ }
1576
+ if (c.default) {
1577
+ columnBuilder = columnBuilder.default(c.default);
1578
+ }
1579
+ if (c.primaryKey) {
1580
+ columnBuilder = columnBuilder.primaryKey();
1581
+ }
1582
+ columns[columnName] = columnBuilder;
1583
+ });
1584
+ if (schemaName === "public") {
1585
+ tables[t.name] = (0, import_pg_core.pgTable)(t.name, columns, (cb) => {
1586
+ const res = {};
1587
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1588
+ const gh = cpk.columns.map((c) => cb[c]);
1589
+ res[cpk.name] = new import_pg_core.PrimaryKeyBuilder(
1590
+ gh,
1591
+ cpk.name
1592
+ );
1593
+ });
1594
+ return res;
1595
+ });
1596
+ } else {
1597
+ tables[t.name] = (0, import_pg_core.pgSchema)(schemaName).table(t.name, columns, (cb) => {
1598
+ const res = {};
1599
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1600
+ const gh = cpk.columns.map((c) => cb[c]);
1601
+ res[cpk.name] = new import_pg_core.PrimaryKeyBuilder(
1602
+ gh,
1603
+ cpk.name
1604
+ );
1605
+ });
1606
+ return res;
1607
+ });
1608
+ }
1609
+ });
1610
+ return tables;
1611
+ };
1612
+ }
1613
+ });
1614
+
1615
+ // src/serializer/sqliteSerializer.ts
1616
+ function mapSqlToSqliteType(sqlType) {
1617
+ if ([
1618
+ "int",
1619
+ "integer",
1620
+ "integer auto_increment",
1621
+ "tinyint",
1622
+ "smallint",
1623
+ "mediumint",
1624
+ "bigint",
1625
+ "unsigned big int",
1626
+ "int2",
1627
+ "int8"
1628
+ ].includes(sqlType.toLowerCase())) {
1629
+ return "integer";
1630
+ } else if ([
1631
+ "character",
1632
+ "varchar",
1633
+ "vatying character",
1634
+ "nchar",
1635
+ "native character",
1636
+ "nvarchar",
1637
+ "text",
1638
+ "clob"
1639
+ ].some((it) => it.startsWith(sqlType.toLowerCase()))) {
1640
+ return "text";
1641
+ } else if (sqlType.toLowerCase() === "blob") {
1642
+ return "blob";
1643
+ } else if (["real", "double", "double precision", "float"].includes(
1644
+ sqlType.toLowerCase()
1645
+ )) {
1646
+ return "real";
1647
+ } else {
1648
+ return "numeric";
1649
+ }
1650
+ }
1651
+ var import_drizzle_orm2, import_sqlite_core, dialect2, fromDatabase2, toDrizzle2;
1652
+ var init_sqliteSerializer = __esm({
1653
+ "src/serializer/sqliteSerializer.ts"() {
1654
+ import_drizzle_orm2 = require("drizzle-orm");
1655
+ import_sqlite_core = require("drizzle-orm/sqlite-core");
1656
+ init_serializer();
1657
+ init_outputs();
1658
+ init_source();
1659
+ dialect2 = new import_sqlite_core.SQLiteSyncDialect();
1660
+ fromDatabase2 = async (db, tablesFilter = (table) => true, progressCallback) => {
1661
+ const result = {};
1662
+ const columns = await db.query(
1663
+ `SELECT
1664
+ m.name as "tableName", p.name as "columnName", p.type as "columnType", p."notnull" as "notNull", p.dflt_value as "defaultValue", p.pk as pk
1665
+ FROM sqlite_master AS m JOIN pragma_table_info(m.name) AS p
1666
+ WHERE m.type = 'table' and m.tbl_name != 'sqlite_sequence' and m.tbl_name != 'sqlite_stat1' and m.tbl_name != '_litestream_seq' and m.tbl_name != '_litestream_lock' and m.tbl_name != 'libsql_wasm_func_table';
1667
+ `
1668
+ );
1669
+ const tablesWithSeq = [];
1670
+ const seq = await db.query(
1671
+ `SELECT * FROM sqlite_master WHERE name != 'sqlite_sequence' and name != 'sqlite_stat1' and name != '_litestream_seq' and name != '_litestream_lock' and sql GLOB '*[ *' || CHAR(9) || CHAR(10) || CHAR(13) || ']AUTOINCREMENT[^'']*';`
1672
+ );
1673
+ for (const s of seq) {
1674
+ tablesWithSeq.push(s.name);
1675
+ }
1676
+ let columnsCount = 0;
1677
+ let tablesCount = /* @__PURE__ */ new Set();
1678
+ let indexesCount = 0;
1679
+ let foreignKeysCount = 0;
1680
+ const tableToPk = {};
1681
+ for (const column of columns) {
1682
+ if (!tablesFilter(column.tableName))
1683
+ continue;
1684
+ columnsCount += 1;
1685
+ if (progressCallback) {
1686
+ progressCallback("columns", columnsCount, "fetching");
1687
+ }
1688
+ const tableName = column.tableName;
1689
+ tablesCount.add(tableName);
1690
+ if (progressCallback) {
1691
+ progressCallback("tables", tablesCount.size, "fetching");
1692
+ }
1693
+ const columnName = column.columnName;
1694
+ const isNotNull = column.notNull === 1;
1695
+ const columnType = column.columnType;
1696
+ const isPrimary = column.pk !== 0;
1697
+ const columnDefault = column.defaultValue;
1698
+ const isAutoincrement = isPrimary && tablesWithSeq.includes(tableName);
1699
+ if (isPrimary) {
1700
+ if (typeof tableToPk[tableName] === "undefined") {
1701
+ tableToPk[tableName] = [columnName];
1702
+ } else {
1703
+ tableToPk[tableName].push(columnName);
1704
+ }
1705
+ }
1706
+ const table = result[tableName];
1707
+ const newColumn = {
1708
+ default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) ? Number(columnDefault) : ["CURRENT_TIME", "CURRENT_DATE", "CURRENT_TIMESTAMP"].includes(
1709
+ columnDefault
1710
+ ) ? `(${columnDefault})` : columnDefault === "false" ? false : columnDefault === "true" ? true : columnDefault.startsWith("'") && columnDefault.endsWith("'") ? columnDefault : (
1711
+ // ? columnDefault.substring(1, columnDefault.length - 1)
1712
+ `(${columnDefault})`
1713
+ ),
1714
+ autoincrement: isAutoincrement,
1715
+ name: columnName,
1716
+ type: mapSqlToSqliteType(columnType),
1717
+ primaryKey: false,
1718
+ notNull: isNotNull
1719
+ };
1720
+ if (!table) {
1721
+ result[tableName] = {
1722
+ name: tableName,
1723
+ columns: {
1724
+ [columnName]: newColumn
1725
+ },
1726
+ compositePrimaryKeys: {},
1727
+ indexes: {},
1728
+ foreignKeys: {},
1729
+ uniqueConstraints: {}
1730
+ };
1731
+ } else {
1732
+ result[tableName].columns[columnName] = newColumn;
1733
+ }
1860
1734
  }
1861
- for await (const _ of all) {
1735
+ for (const [key, value] of Object.entries(tableToPk)) {
1736
+ if (value.length > 1) {
1737
+ value.sort();
1738
+ result[key].compositePrimaryKeys = {
1739
+ [`${key}_${value.join("_")}_pk`]: {
1740
+ columns: value,
1741
+ name: `${key}_${value.join("_")}_pk`
1742
+ }
1743
+ };
1744
+ } else if (value.length === 1) {
1745
+ result[key].columns[value[0]].primaryKey = true;
1746
+ } else {
1747
+ }
1862
1748
  }
1863
1749
  if (progressCallback) {
1864
1750
  progressCallback("columns", columnsCount, "done");
1865
- progressCallback("indexes", indexesCount, "done");
1751
+ progressCallback("tables", tablesCount.size, "done");
1752
+ }
1753
+ try {
1754
+ const fks = await db.query(
1755
+ `SELECT m.name as "tableFrom", f.id as "id", f."table" as "tableTo", f."from", f."to", f."on_update" as "onUpdate", f."on_delete" as "onDelete", f.seq as "seq"
1756
+ FROM sqlite_master m, pragma_foreign_key_list(m.name) as f;`
1757
+ );
1758
+ const fkByTableName = {};
1759
+ for (const fkRow of fks) {
1760
+ foreignKeysCount += 1;
1761
+ if (progressCallback) {
1762
+ progressCallback("fks", foreignKeysCount, "fetching");
1763
+ }
1764
+ const tableName = fkRow.tableFrom;
1765
+ const columnName = fkRow.from;
1766
+ const refTableName = fkRow.tableTo;
1767
+ const refColumnName = fkRow.to;
1768
+ const updateRule = fkRow.onUpdate;
1769
+ const deleteRule = fkRow.onDelete;
1770
+ const sequence = fkRow.seq;
1771
+ const id = fkRow.id;
1772
+ const tableInResult = result[tableName];
1773
+ if (typeof tableInResult === "undefined")
1774
+ continue;
1775
+ if (typeof fkByTableName[`${tableName}_${id}`] !== "undefined") {
1776
+ fkByTableName[`${tableName}_${id}`].columnsFrom.push(columnName);
1777
+ fkByTableName[`${tableName}_${id}`].columnsTo.push(refColumnName);
1778
+ } else {
1779
+ fkByTableName[`${tableName}_${id}`] = {
1780
+ name: "",
1781
+ tableFrom: tableName,
1782
+ tableTo: refTableName,
1783
+ columnsFrom: [columnName],
1784
+ columnsTo: [refColumnName],
1785
+ onDelete: deleteRule == null ? void 0 : deleteRule.toLowerCase(),
1786
+ onUpdate: updateRule == null ? void 0 : updateRule.toLowerCase()
1787
+ };
1788
+ }
1789
+ const columnsFrom = fkByTableName[`${tableName}_${id}`].columnsFrom;
1790
+ const columnsTo = fkByTableName[`${tableName}_${id}`].columnsTo;
1791
+ fkByTableName[`${tableName}_${id}`].name = `${tableName}_${columnsFrom.join(
1792
+ "_"
1793
+ )}_${refTableName}_${columnsTo.join("_")}_fk`;
1794
+ }
1795
+ for (const idx of Object.keys(fkByTableName)) {
1796
+ const value = fkByTableName[idx];
1797
+ result[value.tableFrom].foreignKeys[value.name] = value;
1798
+ }
1799
+ } catch (e) {
1800
+ }
1801
+ if (progressCallback) {
1866
1802
  progressCallback("fks", foreignKeysCount, "done");
1867
1803
  }
1868
- const allEnums = await db.query(
1869
- `select n.nspname as enum_schema,
1870
- t.typname as enum_name,
1871
- e.enumlabel as enum_value
1872
- from pg_type t
1873
- join pg_enum e on t.oid = e.enumtypid
1874
- join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;`
1804
+ const idxs = await db.query(
1805
+ `SELECT
1806
+ m.tbl_name as tableName,
1807
+ il.name as indexName,
1808
+ ii.name as columnName,
1809
+ il.[unique] as isUnique,
1810
+ il.seq as seq
1811
+ FROM sqlite_master AS m,
1812
+ pragma_index_list(m.name) AS il,
1813
+ pragma_index_info(il.name) AS ii
1814
+ WHERE
1815
+ m.type = 'table' and il.name NOT LIKE 'sqlite_autoindex_%';`
1875
1816
  );
1876
- const enumsToReturn = {};
1877
- for (const dbEnum of allEnums) {
1878
- const enumName = dbEnum.enum_name;
1879
- const enumValue = dbEnum.enum_value;
1880
- if (enumsToReturn[enumName] !== void 0 && enumsToReturn[enumName] !== null) {
1881
- enumsToReturn[enumName].values[enumValue] = enumValue;
1817
+ for (const idxRow of idxs) {
1818
+ const tableName = idxRow.tableName;
1819
+ const constraintName = idxRow.indexName;
1820
+ const columnName = idxRow.columnName;
1821
+ const isUnique = idxRow.isUnique === 1;
1822
+ const tableInResult = result[tableName];
1823
+ if (typeof tableInResult === "undefined")
1824
+ continue;
1825
+ indexesCount += 1;
1826
+ if (progressCallback) {
1827
+ progressCallback("indexes", indexesCount, "fetching");
1828
+ }
1829
+ if (typeof tableInResult.indexes[constraintName] !== "undefined") {
1830
+ tableInResult.indexes[constraintName].columns.push(columnName);
1882
1831
  } else {
1883
- enumsToReturn[enumName] = {
1884
- name: enumName,
1885
- values: { [enumValue]: enumValue }
1832
+ tableInResult.indexes[constraintName] = {
1833
+ name: constraintName,
1834
+ columns: [columnName],
1835
+ isUnique
1886
1836
  };
1887
1837
  }
1888
1838
  }
1889
- if (progressCallback) {
1890
- progressCallback("enums", Object.keys(enumsToReturn).length, "done");
1891
- }
1892
- const schemasObject = Object.fromEntries([...schemas].map((it) => [it, it]));
1893
- return {
1894
- version: "5",
1895
- dialect: "pg",
1896
- tables: result,
1897
- enums: enumsToReturn,
1898
- schemas: schemasObject,
1899
- _meta: {
1900
- schemas: {},
1901
- tables: {},
1902
- columns: {}
1903
- },
1904
- internal: internals
1905
- };
1906
- };
1907
- columnToDefault = {
1908
- "numeric(": "::numeric",
1909
- // text: "::text",
1910
- // "character varying": "::character varying",
1911
- // "double precision": "::double precision",
1912
- // "time with time zone": "::time with time zone",
1913
- "time without time zone": "::time without time zone",
1914
- // "timestamp with time zone": "::timestamp with time zone",
1915
- "timestamp without time zone": "::timestamp without time zone",
1916
- // date: "::date",
1917
- // interval: "::interval",
1918
- // character: "::bpchar",
1919
- // macaddr8: "::macaddr8",
1920
- // macaddr: "::macaddr",
1921
- // inet: "::inet",
1922
- // cidr: "::cidr",
1923
- // jsonb: "::jsonb",
1924
- // json: "::json",
1925
- "character(": "::bpchar"
1926
- };
1927
- defaultForColumn = (column) => {
1928
- if (column.data_type === "serial" || column.data_type === "smallserial" || column.data_type === "bigserial") {
1929
- return void 0;
1839
+ if (progressCallback) {
1840
+ progressCallback("indexes", indexesCount, "done");
1841
+ progressCallback("enums", 0, "done");
1842
+ }
1843
+ return {
1844
+ version: "5",
1845
+ dialect: "sqlite",
1846
+ tables: result,
1847
+ enums: {},
1848
+ _meta: {
1849
+ tables: {},
1850
+ columns: {}
1851
+ }
1852
+ };
1853
+ };
1854
+ toDrizzle2 = (schema) => {
1855
+ const tables = {};
1856
+ Object.values(schema.tables).forEach((t) => {
1857
+ const columns = {};
1858
+ Object.values(t.columns).forEach((c) => {
1859
+ const columnName = c.name;
1860
+ const type = c.type;
1861
+ let columnBuilder;
1862
+ if (type === "integer") {
1863
+ columnBuilder = new import_sqlite_core.SQLiteIntegerBuilder(columnName);
1864
+ } else if (type === "text") {
1865
+ columnBuilder = new import_sqlite_core.SQLiteTextBuilder(columnName, {});
1866
+ } else if (type === "blob") {
1867
+ columnBuilder = new import_sqlite_core.SQLiteBlobBufferBuilder(columnName);
1868
+ } else if (type === "real") {
1869
+ columnBuilder = new import_sqlite_core.SQLiteRealBuilder(columnName);
1870
+ } else {
1871
+ columnBuilder = new import_sqlite_core.SQLiteNumericBuilder(columnName);
1872
+ }
1873
+ if (c.notNull) {
1874
+ columnBuilder = columnBuilder.notNull();
1875
+ }
1876
+ if (c.default) {
1877
+ columnBuilder = columnBuilder.default(c.default);
1878
+ }
1879
+ if (c.primaryKey) {
1880
+ columnBuilder = columnBuilder.primaryKey();
1881
+ }
1882
+ columns[columnName] = columnBuilder;
1883
+ });
1884
+ tables[t.name] = (0, import_sqlite_core.sqliteTable)(t.name, columns, (cb) => {
1885
+ const res = {};
1886
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1887
+ const gh = cpk.columns.map((c) => cb[c]);
1888
+ res[cpk.name] = new import_sqlite_core.PrimaryKeyBuilder(
1889
+ gh,
1890
+ cpk.name
1891
+ );
1892
+ });
1893
+ return res;
1894
+ });
1895
+ });
1896
+ return tables;
1897
+ };
1898
+ }
1899
+ });
1900
+
1901
+ // node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js
1902
+ var require_balanced_match = __commonJS({
1903
+ "node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js"(exports, module2) {
1904
+ "use strict";
1905
+ module2.exports = balanced;
1906
+ function balanced(a, b, str) {
1907
+ if (a instanceof RegExp)
1908
+ a = maybeMatch(a, str);
1909
+ if (b instanceof RegExp)
1910
+ b = maybeMatch(b, str);
1911
+ var r = range(a, b, str);
1912
+ return r && {
1913
+ start: r[0],
1914
+ end: r[1],
1915
+ pre: str.slice(0, r[0]),
1916
+ body: str.slice(r[0] + a.length, r[1]),
1917
+ post: str.slice(r[1] + b.length)
1918
+ };
1919
+ }
1920
+ function maybeMatch(reg, str) {
1921
+ var m = str.match(reg);
1922
+ return m ? m[0] : null;
1923
+ }
1924
+ balanced.range = range;
1925
+ function range(a, b, str) {
1926
+ var begs, beg, left, right, result;
1927
+ var ai = str.indexOf(a);
1928
+ var bi = str.indexOf(b, ai + 1);
1929
+ var i = ai;
1930
+ if (ai >= 0 && bi > 0) {
1931
+ if (a === b) {
1932
+ return [ai, bi];
1933
+ }
1934
+ begs = [];
1935
+ left = str.length;
1936
+ while (i >= 0 && !result) {
1937
+ if (i == ai) {
1938
+ begs.push(i);
1939
+ ai = str.indexOf(a, i + 1);
1940
+ } else if (begs.length == 1) {
1941
+ result = [begs.pop(), bi];
1942
+ } else {
1943
+ beg = begs.pop();
1944
+ if (beg < left) {
1945
+ left = beg;
1946
+ right = bi;
1947
+ }
1948
+ bi = str.indexOf(b, i + 1);
1949
+ }
1950
+ i = ai < bi && ai >= 0 ? ai : bi;
1951
+ }
1952
+ if (begs.length) {
1953
+ result = [left, right];
1954
+ }
1955
+ }
1956
+ return result;
1957
+ }
1958
+ }
1959
+ });
1960
+
1961
+ // node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js
1962
+ var require_brace_expansion = __commonJS({
1963
+ "node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js"(exports, module2) {
1964
+ var balanced = require_balanced_match();
1965
+ module2.exports = expandTop;
1966
+ var escSlash = "\0SLASH" + Math.random() + "\0";
1967
+ var escOpen = "\0OPEN" + Math.random() + "\0";
1968
+ var escClose = "\0CLOSE" + Math.random() + "\0";
1969
+ var escComma = "\0COMMA" + Math.random() + "\0";
1970
+ var escPeriod = "\0PERIOD" + Math.random() + "\0";
1971
+ function numeric(str) {
1972
+ return parseInt(str, 10) == str ? parseInt(str, 10) : str.charCodeAt(0);
1973
+ }
1974
+ function escapeBraces(str) {
1975
+ return str.split("\\\\").join(escSlash).split("\\{").join(escOpen).split("\\}").join(escClose).split("\\,").join(escComma).split("\\.").join(escPeriod);
1976
+ }
1977
+ function unescapeBraces(str) {
1978
+ return str.split(escSlash).join("\\").split(escOpen).join("{").split(escClose).join("}").split(escComma).join(",").split(escPeriod).join(".");
1979
+ }
1980
+ function parseCommaParts(str) {
1981
+ if (!str)
1982
+ return [""];
1983
+ var parts = [];
1984
+ var m = balanced("{", "}", str);
1985
+ if (!m)
1986
+ return str.split(",");
1987
+ var pre = m.pre;
1988
+ var body = m.body;
1989
+ var post = m.post;
1990
+ var p = pre.split(",");
1991
+ p[p.length - 1] += "{" + body + "}";
1992
+ var postParts = parseCommaParts(post);
1993
+ if (post.length) {
1994
+ p[p.length - 1] += postParts.shift();
1995
+ p.push.apply(p, postParts);
1930
1996
  }
1931
- const hasDifferentDefaultCast = Object.keys(columnToDefault).find(
1932
- (it) => column.data_type.startsWith(it)
1933
- );
1934
- if (column.column_default === null) {
1935
- return void 0;
1997
+ parts.push.apply(parts, p);
1998
+ return parts;
1999
+ }
2000
+ function expandTop(str) {
2001
+ if (!str)
2002
+ return [];
2003
+ if (str.substr(0, 2) === "{}") {
2004
+ str = "\\{\\}" + str.substr(2);
1936
2005
  }
1937
- const columnDefaultAsString = column.column_default.toString();
1938
- if (columnDefaultAsString.endsWith(
1939
- hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column.data_type
1940
- )) {
1941
- const nonPrefixPart = column.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`).length - 1;
1942
- const rt = column.column_default.toString().substring(1, nonPrefixPart);
1943
- if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column.data_type.startsWith("numeric")) {
1944
- return Number(rt);
1945
- } else if (column.data_type === "json" || column.data_type === "jsonb") {
1946
- const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
1947
- return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`}`;
1948
- } else if (column.data_type === "boolean") {
1949
- return column.column_default === "true";
1950
- } else {
1951
- return `'${rt}'`;
2006
+ return expand2(escapeBraces(str), true).map(unescapeBraces);
2007
+ }
2008
+ function embrace(str) {
2009
+ return "{" + str + "}";
2010
+ }
2011
+ function isPadded(el) {
2012
+ return /^-?0\d/.test(el);
2013
+ }
2014
+ function lte(i, y) {
2015
+ return i <= y;
2016
+ }
2017
+ function gte(i, y) {
2018
+ return i >= y;
2019
+ }
2020
+ function expand2(str, isTop) {
2021
+ var expansions = [];
2022
+ var m = balanced("{", "}", str);
2023
+ if (!m)
2024
+ return [str];
2025
+ var pre = m.pre;
2026
+ var post = m.post.length ? expand2(m.post, false) : [""];
2027
+ if (/\$$/.test(m.pre)) {
2028
+ for (var k = 0; k < post.length; k++) {
2029
+ var expansion = pre + "{" + m.body + "}" + post[k];
2030
+ expansions.push(expansion);
1952
2031
  }
1953
2032
  } else {
1954
- if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !column.data_type.startsWith("numeric")) {
1955
- return Number(columnDefaultAsString);
1956
- } else if (column.data_type === "boolean") {
1957
- return column.column_default === "true";
2033
+ var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
2034
+ var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
2035
+ var isSequence = isNumericSequence || isAlphaSequence;
2036
+ var isOptions = m.body.indexOf(",") >= 0;
2037
+ if (!isSequence && !isOptions) {
2038
+ if (m.post.match(/,.*\}/)) {
2039
+ str = m.pre + "{" + m.body + escClose + m.post;
2040
+ return expand2(str);
2041
+ }
2042
+ return [str];
2043
+ }
2044
+ var n;
2045
+ if (isSequence) {
2046
+ n = m.body.split(/\.\./);
1958
2047
  } else {
1959
- return `${columnDefaultAsString}`;
2048
+ n = parseCommaParts(m.body);
2049
+ if (n.length === 1) {
2050
+ n = expand2(n[0], false).map(embrace);
2051
+ if (n.length === 1) {
2052
+ return post.map(function(p) {
2053
+ return m.pre + n[0] + p;
2054
+ });
2055
+ }
2056
+ }
2057
+ }
2058
+ var N;
2059
+ if (isSequence) {
2060
+ var x = numeric(n[0]);
2061
+ var y = numeric(n[1]);
2062
+ var width = Math.max(n[0].length, n[1].length);
2063
+ var incr = n.length == 3 ? Math.abs(numeric(n[2])) : 1;
2064
+ var test = lte;
2065
+ var reverse = y < x;
2066
+ if (reverse) {
2067
+ incr *= -1;
2068
+ test = gte;
2069
+ }
2070
+ var pad = n.some(isPadded);
2071
+ N = [];
2072
+ for (var i = x; test(i, y); i += incr) {
2073
+ var c;
2074
+ if (isAlphaSequence) {
2075
+ c = String.fromCharCode(i);
2076
+ if (c === "\\")
2077
+ c = "";
2078
+ } else {
2079
+ c = String(i);
2080
+ if (pad) {
2081
+ var need = width - c.length;
2082
+ if (need > 0) {
2083
+ var z = new Array(need + 1).join("0");
2084
+ if (i < 0)
2085
+ c = "-" + z + c.slice(1);
2086
+ else
2087
+ c = z + c;
2088
+ }
2089
+ }
2090
+ }
2091
+ N.push(c);
2092
+ }
2093
+ } else {
2094
+ N = [];
2095
+ for (var j = 0; j < n.length; j++) {
2096
+ N.push.apply(N, expand2(n[j], false));
2097
+ }
2098
+ }
2099
+ for (var j = 0; j < N.length; j++) {
2100
+ for (var k = 0; k < post.length; k++) {
2101
+ var expansion = pre + N[j] + post[k];
2102
+ if (!isTop || isSequence || expansion)
2103
+ expansions.push(expansion);
2104
+ }
1960
2105
  }
1961
2106
  }
1962
- };
2107
+ return expansions;
2108
+ }
1963
2109
  }
1964
2110
  });
1965
2111
 
@@ -2000,162 +2146,14 @@ var utils_studio_exports = {};
2000
2146
  __export(utils_studio_exports, {
2001
2147
  DrizzleORMPgClient: () => DrizzleORMPgClient,
2002
2148
  TursoSqlite: () => TursoSqlite,
2003
- drizzleSchemaPg: () => pgSchemaToDrizzle,
2004
- drizzleSchemaSQLite: () => sqliteSchemaToDrizzle,
2149
+ drizzleSchemaPg: () => toDrizzle,
2150
+ drizzleSchemaSQLite: () => toDrizzle2,
2005
2151
  pgPushIntrospect: () => pgPushIntrospect,
2006
2152
  sqlitePushIntrospect: () => sqlitePushIntrospect
2007
2153
  });
2008
2154
  module.exports = __toCommonJS(utils_studio_exports);
2009
-
2010
- // src/serializer/schemaToDrizzle.ts
2011
- var import_pg_core = require("drizzle-orm/pg-core");
2012
- var import_sqlite_core = require("drizzle-orm/sqlite-core");
2013
- var pgSchemaToDrizzle = (schema, schemaName) => {
2014
- const tables = {};
2015
- Object.values(schema.tables).forEach((t) => {
2016
- const columns = {};
2017
- Object.values(t.columns).forEach((c) => {
2018
- const columnName = c.name;
2019
- const type = c.type;
2020
- let columnBuilder;
2021
- if (type === "bigint") {
2022
- columnBuilder = new import_pg_core.PgBigInt53Builder(columnName);
2023
- } else if (type === "bigserial") {
2024
- columnBuilder = new import_pg_core.PgBigSerial53Builder(columnName);
2025
- } else if (type === "boolean") {
2026
- columnBuilder = new import_pg_core.PgBooleanBuilder(columnName);
2027
- } else if (type === "cidr") {
2028
- columnBuilder = new import_pg_core.PgCidrBuilder(columnName);
2029
- } else if (type === "date") {
2030
- columnBuilder = new import_pg_core.PgDateBuilder(columnName);
2031
- } else if (type === "double precision") {
2032
- columnBuilder = new import_pg_core.PgDoublePrecisionBuilder(columnName);
2033
- } else if (type === "inet") {
2034
- columnBuilder = new import_pg_core.PgInetBuilder(columnName);
2035
- } else if (type === "integer") {
2036
- columnBuilder = new import_pg_core.PgIntegerBuilder(columnName);
2037
- } else if (type === "interval" || type.startsWith("interval ")) {
2038
- columnBuilder = new import_pg_core.PgIntervalBuilder(columnName, {});
2039
- } else if (type === "json") {
2040
- columnBuilder = new import_pg_core.PgJsonBuilder(columnName);
2041
- } else if (type === "jsonb") {
2042
- columnBuilder = new import_pg_core.PgJsonbBuilder(columnName);
2043
- } else if (type === "macaddr") {
2044
- columnBuilder = new import_pg_core.PgMacaddrBuilder(columnName);
2045
- } else if (type === "macaddr8") {
2046
- columnBuilder = new import_pg_core.PgMacaddr8Builder(columnName);
2047
- } else if (type === "numeric" || type.startsWith("numeric(")) {
2048
- columnBuilder = new import_pg_core.PgNumericBuilder(columnName);
2049
- } else if (type === "real") {
2050
- columnBuilder = new import_pg_core.PgRealBuilder(columnName);
2051
- } else if (type === "serial") {
2052
- columnBuilder = new import_pg_core.PgSerialBuilder(columnName);
2053
- } else if (type === "smallint") {
2054
- columnBuilder = new import_pg_core.PgSmallIntBuilder(columnName);
2055
- } else if (type === "smallserial") {
2056
- columnBuilder = new import_pg_core.PgSmallSerialBuilder(columnName);
2057
- } else if (type === "text") {
2058
- columnBuilder = new import_pg_core.PgTextBuilder(columnName, {});
2059
- } else if (type === "time" || type.startsWith("time(") || type === "time with time zone") {
2060
- columnBuilder = new import_pg_core.PgTimeBuilder(columnName, false, void 0);
2061
- } else if (type === "timestamp" || type.startsWith("timestamp(") || type === "timestamp with time zone") {
2062
- columnBuilder = new import_pg_core.PgTimestampBuilder(columnName, false, void 0);
2063
- } else if (type === "uuid") {
2064
- columnBuilder = new import_pg_core.PgUUIDBuilder(columnName);
2065
- } else if (type === "varchar" || type.startsWith("varchar(")) {
2066
- columnBuilder = new import_pg_core.PgVarcharBuilder(columnName, {});
2067
- } else if (type === "char" || type.startsWith("char(")) {
2068
- columnBuilder = new import_pg_core.PgCharBuilder(columnName, {});
2069
- } else {
2070
- columnBuilder = (0, import_pg_core.customType)({
2071
- dataType() {
2072
- return type;
2073
- }
2074
- })(columnName);
2075
- }
2076
- if (c.notNull) {
2077
- columnBuilder = columnBuilder.notNull();
2078
- }
2079
- if (c.default) {
2080
- columnBuilder = columnBuilder.default(c.default);
2081
- }
2082
- if (c.primaryKey) {
2083
- columnBuilder = columnBuilder.primaryKey();
2084
- }
2085
- columns[columnName] = columnBuilder;
2086
- });
2087
- if (schemaName === "public") {
2088
- tables[t.name] = (0, import_pg_core.pgTable)(t.name, columns, (cb) => {
2089
- const res = {};
2090
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2091
- const gh = cpk.columns.map((c) => cb[c]);
2092
- res[cpk.name] = new import_pg_core.PrimaryKeyBuilder(
2093
- gh,
2094
- cpk.name
2095
- );
2096
- });
2097
- return res;
2098
- });
2099
- } else {
2100
- tables[t.name] = (0, import_pg_core.pgSchema)(schemaName).table(t.name, columns, (cb) => {
2101
- const res = {};
2102
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2103
- const gh = cpk.columns.map((c) => cb[c]);
2104
- res[cpk.name] = new import_pg_core.PrimaryKeyBuilder(
2105
- gh,
2106
- cpk.name
2107
- );
2108
- });
2109
- return res;
2110
- });
2111
- }
2112
- });
2113
- return tables;
2114
- };
2115
- var sqliteSchemaToDrizzle = (schema) => {
2116
- const tables = {};
2117
- Object.values(schema.tables).forEach((t) => {
2118
- const columns = {};
2119
- Object.values(t.columns).forEach((c) => {
2120
- const columnName = c.name;
2121
- const type = c.type;
2122
- let columnBuilder;
2123
- if (type === "integer") {
2124
- columnBuilder = new import_sqlite_core.SQLiteIntegerBuilder(columnName);
2125
- } else if (type === "text") {
2126
- columnBuilder = new import_sqlite_core.SQLiteTextBuilder(columnName, {});
2127
- } else if (type === "blob") {
2128
- columnBuilder = new import_sqlite_core.SQLiteBlobBufferBuilder(columnName);
2129
- } else if (type === "real") {
2130
- columnBuilder = new import_sqlite_core.SQLiteRealBuilder(columnName);
2131
- } else {
2132
- columnBuilder = new import_sqlite_core.SQLiteNumericBuilder(columnName);
2133
- }
2134
- if (c.notNull) {
2135
- columnBuilder = columnBuilder.notNull();
2136
- }
2137
- if (c.default) {
2138
- columnBuilder = columnBuilder.default(c.default);
2139
- }
2140
- if (c.primaryKey) {
2141
- columnBuilder = columnBuilder.primaryKey();
2142
- }
2143
- columns[columnName] = columnBuilder;
2144
- });
2145
- tables[t.name] = (0, import_sqlite_core.sqliteTable)(t.name, columns, (cb) => {
2146
- const res = {};
2147
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2148
- const gh = cpk.columns.map((c) => cb[c]);
2149
- res[cpk.name] = new import_sqlite_core.PrimaryKeyBuilder(
2150
- gh,
2151
- cpk.name
2152
- );
2153
- });
2154
- return res;
2155
- });
2156
- });
2157
- return tables;
2158
- };
2155
+ init_pgSerializer();
2156
+ init_sqliteSerializer();
2159
2157
 
2160
2158
  // src/cli/commands/sqliteIntrospect.ts
2161
2159
  init_views();
@@ -3333,7 +3331,7 @@ var sqlitePushIntrospect = async (client, filters) => {
3333
3331
  }
3334
3332
  return false;
3335
3333
  };
3336
- const res = await fromDatabase(client, filter2);
3334
+ const res = await fromDatabase2(client, filter2);
3337
3335
  const schema = { id: originUUID, prevId: "", ...res };
3338
3336
  return { schema };
3339
3337
  };
@@ -3364,7 +3362,7 @@ var pgPushIntrospect = async (connection, filters, schemaFilters) => {
3364
3362
  }
3365
3363
  return false;
3366
3364
  };
3367
- const res = await fromDatabase2(client, filter2, schemaFilters);
3365
+ const res = await fromDatabase(client, filter2, schemaFilters);
3368
3366
  const schema = { id: originUUID, prevId: "", ...res };
3369
3367
  const { internal, ...schemaWithoutInternals } = schema;
3370
3368
  return { schema: schemaWithoutInternals };