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.mjs CHANGED
@@ -1099,13 +1099,565 @@ var init_outputs = __esm({
1099
1099
  }
1100
1100
  });
1101
1101
 
1102
+ // src/serializer/pgSerializer.ts
1103
+ import {
1104
+ customType,
1105
+ PgBigInt53Builder,
1106
+ PgBigSerial53Builder,
1107
+ PgBooleanBuilder,
1108
+ PgCharBuilder,
1109
+ PgCidrBuilder,
1110
+ PgDateBuilder,
1111
+ PgDialect,
1112
+ PgDoublePrecisionBuilder,
1113
+ PgInetBuilder,
1114
+ PgIntegerBuilder,
1115
+ PgIntervalBuilder,
1116
+ PgJsonbBuilder,
1117
+ PgJsonBuilder,
1118
+ PgMacaddr8Builder,
1119
+ PgMacaddrBuilder,
1120
+ PgNumericBuilder,
1121
+ PgRealBuilder,
1122
+ pgSchema,
1123
+ PgSerialBuilder,
1124
+ PgSmallIntBuilder,
1125
+ PgSmallSerialBuilder,
1126
+ pgTable,
1127
+ PgTextBuilder,
1128
+ PgTimeBuilder,
1129
+ PgTimestampBuilder,
1130
+ PgUUIDBuilder,
1131
+ PgVarcharBuilder,
1132
+ PrimaryKeyBuilder,
1133
+ uniqueKeyName
1134
+ } from "drizzle-orm/pg-core";
1135
+ import { getTableConfig } from "drizzle-orm/pg-core";
1136
+ import { is, SQL, getTableName } from "drizzle-orm";
1137
+ var dialect, trimChar, fromDatabase, columnToDefault, defaultForColumn, toDrizzle;
1138
+ var init_pgSerializer = __esm({
1139
+ "src/serializer/pgSerializer.ts"() {
1140
+ init_serializer();
1141
+ init_source();
1142
+ init_outputs();
1143
+ dialect = new PgDialect();
1144
+ trimChar = (str, char) => {
1145
+ let start = 0;
1146
+ let end = str.length;
1147
+ while (start < end && str[start] === char)
1148
+ ++start;
1149
+ while (end > start && str[end - 1] === char)
1150
+ --end;
1151
+ return start > 0 || end < str.length ? str.substring(start, end) : str.toString();
1152
+ };
1153
+ fromDatabase = async (db, tablesFilter = (table) => true, schemaFilters, progressCallback) => {
1154
+ const result = {};
1155
+ const internals = { tables: {} };
1156
+ const where = schemaFilters.map((t) => `table_schema = '${t}'`).join(" or ");
1157
+ const allTables = await db.query(
1158
+ `SELECT table_schema, table_name FROM information_schema.tables WHERE ${where};`
1159
+ );
1160
+ const schemas = new Set(allTables.map((it) => it.table_schema));
1161
+ schemas.delete("public");
1162
+ const allSchemas = await db.query(`select s.nspname as table_schema
1163
+ from pg_catalog.pg_namespace s
1164
+ join pg_catalog.pg_user u on u.usesysid = s.nspowner
1165
+ where nspname not in ('information_schema', 'pg_catalog', 'public')
1166
+ and nspname not like 'pg_toast%'
1167
+ and nspname not like 'pg_temp_%'
1168
+ order by table_schema;`);
1169
+ allSchemas.forEach((item) => {
1170
+ if (schemaFilters.includes(item.table_schema)) {
1171
+ schemas.add(item.table_schema);
1172
+ }
1173
+ });
1174
+ let columnsCount = 0;
1175
+ let indexesCount = 0;
1176
+ let foreignKeysCount = 0;
1177
+ let tableCount = 0;
1178
+ const all = allTables.map((row) => {
1179
+ return new Promise(async (res, rej) => {
1180
+ const tableName = row.table_name;
1181
+ if (!tablesFilter(tableName))
1182
+ return res("");
1183
+ tableCount += 1;
1184
+ const tableSchema = row.table_schema;
1185
+ try {
1186
+ const columnToReturn = {};
1187
+ const indexToReturn = {};
1188
+ const foreignKeysToReturn = {};
1189
+ const primaryKeys = {};
1190
+ const uniqueConstrains = {};
1191
+ const tableResponse = await db.query(
1192
+ `SELECT a.attrelid::regclass::text, a.attname, is_nullable, a.attndims as array_dimensions
1193
+ , CASE WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
1194
+ AND EXISTS (
1195
+ SELECT FROM pg_attrdef ad
1196
+ WHERE ad.adrelid = a.attrelid
1197
+ AND ad.adnum = a.attnum
1198
+ AND pg_get_expr(ad.adbin, ad.adrelid)
1199
+ = 'nextval('''
1200
+ || (pg_get_serial_sequence (a.attrelid::regclass::text
1201
+ , a.attname))::regclass
1202
+ || '''::regclass)'
1203
+ )
1204
+ THEN CASE a.atttypid
1205
+ WHEN 'int'::regtype THEN 'serial'
1206
+ WHEN 'int8'::regtype THEN 'bigserial'
1207
+ WHEN 'int2'::regtype THEN 'smallserial'
1208
+ END
1209
+ ELSE format_type(a.atttypid, a.atttypmod)
1210
+ END AS data_type, INFORMATION_SCHEMA.COLUMNS.table_name, INFORMATION_SCHEMA.COLUMNS.column_name, INFORMATION_SCHEMA.COLUMNS.column_default, INFORMATION_SCHEMA.COLUMNS.data_type as additional_dt
1211
+ FROM pg_attribute a
1212
+ JOIN INFORMATION_SCHEMA.COLUMNS ON INFORMATION_SCHEMA.COLUMNS.column_name = a.attname
1213
+ WHERE a.attrelid = '"${tableSchema}"."${tableName}"'::regclass and INFORMATION_SCHEMA.COLUMNS.table_name = '${tableName}'
1214
+ AND a.attnum > 0
1215
+ AND NOT a.attisdropped
1216
+ ORDER BY a.attnum;`
1217
+ );
1218
+ const tableConstraints = await db.query(
1219
+ `SELECT c.column_name, c.data_type, constraint_type, constraint_name, constraint_schema
1220
+ FROM information_schema.table_constraints tc
1221
+ JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
1222
+ JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
1223
+ AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
1224
+ WHERE tc.table_name = '${tableName}' and constraint_schema = '${tableSchema}';`
1225
+ );
1226
+ columnsCount += tableResponse.length;
1227
+ if (progressCallback) {
1228
+ progressCallback("columns", columnsCount, "fetching");
1229
+ }
1230
+ const tableForeignKeys = await db.query(
1231
+ `SELECT
1232
+ tc.table_schema,
1233
+ tc.constraint_name,
1234
+ tc.table_name,
1235
+ kcu.column_name,
1236
+ ccu.table_schema AS foreign_table_schema,
1237
+ ccu.table_name AS foreign_table_name,
1238
+ ccu.column_name AS foreign_column_name,
1239
+ rc.delete_rule, rc.update_rule
1240
+ FROM
1241
+ information_schema.table_constraints AS tc
1242
+ JOIN information_schema.key_column_usage AS kcu
1243
+ ON tc.constraint_name = kcu.constraint_name
1244
+ AND tc.table_schema = kcu.table_schema
1245
+ JOIN information_schema.constraint_column_usage AS ccu
1246
+ ON ccu.constraint_name = tc.constraint_name
1247
+ AND ccu.table_schema = tc.table_schema
1248
+ JOIN information_schema.referential_constraints AS rc
1249
+ ON ccu.constraint_name = rc.constraint_name
1250
+ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${tableName}' and tc.table_schema='${tableSchema}';`
1251
+ );
1252
+ foreignKeysCount += tableForeignKeys.length;
1253
+ if (progressCallback) {
1254
+ progressCallback("fks", foreignKeysCount, "fetching");
1255
+ }
1256
+ for (const fk of tableForeignKeys) {
1257
+ const columnFrom = fk.column_name;
1258
+ const tableTo = fk.foreign_table_name;
1259
+ const columnTo = fk.foreign_column_name;
1260
+ const foreignKeyName = fk.constraint_name;
1261
+ const onUpdate = fk.update_rule.toLowerCase();
1262
+ const onDelete = fk.delete_rule.toLowerCase();
1263
+ if (typeof foreignKeysToReturn[foreignKeyName] !== "undefined") {
1264
+ foreignKeysToReturn[foreignKeyName].columnsFrom.push(columnFrom);
1265
+ foreignKeysToReturn[foreignKeyName].columnsTo.push(columnTo);
1266
+ } else {
1267
+ foreignKeysToReturn[foreignKeyName] = {
1268
+ name: foreignKeyName,
1269
+ tableFrom: tableName,
1270
+ tableTo,
1271
+ columnsFrom: [columnFrom],
1272
+ columnsTo: [columnTo],
1273
+ onDelete,
1274
+ onUpdate
1275
+ };
1276
+ }
1277
+ foreignKeysToReturn[foreignKeyName].columnsFrom = [
1278
+ ...new Set(foreignKeysToReturn[foreignKeyName].columnsFrom)
1279
+ ];
1280
+ foreignKeysToReturn[foreignKeyName].columnsTo = [
1281
+ ...new Set(foreignKeysToReturn[foreignKeyName].columnsTo)
1282
+ ];
1283
+ }
1284
+ const uniqueConstrainsRows = tableConstraints.filter(
1285
+ (mapRow) => mapRow.constraint_type === "UNIQUE"
1286
+ );
1287
+ for (const unqs of uniqueConstrainsRows) {
1288
+ const columnName = unqs.column_name;
1289
+ const constraintName = unqs.constraint_name;
1290
+ if (typeof uniqueConstrains[constraintName] !== "undefined") {
1291
+ uniqueConstrains[constraintName].columns.push(columnName);
1292
+ } else {
1293
+ uniqueConstrains[constraintName] = {
1294
+ columns: [columnName],
1295
+ nullsNotDistinct: false,
1296
+ name: constraintName
1297
+ };
1298
+ }
1299
+ }
1300
+ for (const columnResponse of tableResponse) {
1301
+ const columnName = columnResponse.attname;
1302
+ const columnAdditionalDT = columnResponse.additional_dt;
1303
+ const columnDimensions = columnResponse.array_dimensions;
1304
+ let columnType = columnResponse.data_type;
1305
+ const primaryKey = tableConstraints.filter(
1306
+ (mapRow) => columnName === mapRow.column_name && mapRow.constraint_type === "PRIMARY KEY"
1307
+ );
1308
+ const cprimaryKey = tableConstraints.filter(
1309
+ (mapRow) => mapRow.constraint_type === "PRIMARY KEY"
1310
+ );
1311
+ if (cprimaryKey.length > 1) {
1312
+ const tableCompositePkName = await db.query(
1313
+ `SELECT conname AS primary_key
1314
+ FROM pg_constraint join pg_class on (pg_class.oid = conrelid)
1315
+ WHERE contype = 'p'
1316
+ AND connamespace = $1::regnamespace
1317
+ AND pg_class.relname = $2;`,
1318
+ [tableSchema, tableName]
1319
+ );
1320
+ primaryKeys[tableCompositePkName[0].primary_key] = {
1321
+ name: tableCompositePkName[0].primary_key,
1322
+ columns: cprimaryKey.map((c) => c.column_name)
1323
+ };
1324
+ }
1325
+ const defaultValue = defaultForColumn(columnResponse);
1326
+ const isSerial = columnType === "serial";
1327
+ let columnTypeMapped = columnType;
1328
+ if (columnTypeMapped.startsWith("numeric(")) {
1329
+ columnTypeMapped = columnTypeMapped.replace(",", ", ");
1330
+ }
1331
+ if (columnAdditionalDT === "ARRAY") {
1332
+ if (typeof internals.tables[tableName] === "undefined") {
1333
+ internals.tables[tableName] = {
1334
+ columns: {
1335
+ [columnName]: {
1336
+ isArray: true,
1337
+ dimensions: columnDimensions,
1338
+ rawType: columnTypeMapped.substring(
1339
+ 0,
1340
+ columnTypeMapped.length - 2
1341
+ )
1342
+ }
1343
+ }
1344
+ };
1345
+ } else {
1346
+ if (typeof internals.tables[tableName].columns[columnName] === "undefined") {
1347
+ internals.tables[tableName].columns[columnName] = {
1348
+ isArray: true,
1349
+ dimensions: columnDimensions,
1350
+ rawType: columnTypeMapped.substring(
1351
+ 0,
1352
+ columnTypeMapped.length - 2
1353
+ )
1354
+ };
1355
+ }
1356
+ }
1357
+ }
1358
+ if (columnAdditionalDT === "ARRAY") {
1359
+ for (let i = 1; i < Number(columnDimensions); i++) {
1360
+ columnTypeMapped += "[]";
1361
+ }
1362
+ }
1363
+ columnTypeMapped = columnTypeMapped.replace("character varying", "varchar").replace(" without time zone", "").replace("character", "char");
1364
+ columnTypeMapped = trimChar(columnTypeMapped, '"');
1365
+ columnToReturn[columnName] = {
1366
+ name: columnName,
1367
+ type: columnTypeMapped,
1368
+ primaryKey: primaryKey.length === 1 && cprimaryKey.length < 2,
1369
+ // default: isSerial ? undefined : defaultValue,
1370
+ notNull: columnResponse.is_nullable === "NO"
1371
+ };
1372
+ if (!isSerial && typeof defaultValue !== "undefined") {
1373
+ columnToReturn[columnName].default = defaultValue;
1374
+ }
1375
+ }
1376
+ const dbIndexes = await db.query(
1377
+ `SELECT t.relname as table_name, i.relname AS index_name, ix.indisunique AS is_unique, a.attname AS column_name
1378
+ FROM pg_class t
1379
+ JOIN pg_index ix ON t.oid = ix.indrelid
1380
+ JOIN pg_class i ON i.oid = ix.indexrelid
1381
+ JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
1382
+ JOIN pg_namespace ns ON ns.oid = t.relnamespace
1383
+ WHERE ns.nspname = '${tableSchema}'
1384
+ AND t.relname = '${tableName}'
1385
+ and ix.indisprimary = false;`
1386
+ );
1387
+ const dbIndexFromConstraint = await db.query(
1388
+ `SELECT
1389
+ idx.indexrelname AS index_name,
1390
+ idx.relname AS table_name,
1391
+ schemaname,
1392
+ CASE WHEN con.conname IS NOT NULL THEN 1 ELSE 0 END AS generated_by_constraint
1393
+ FROM
1394
+ pg_stat_user_indexes idx
1395
+ LEFT JOIN
1396
+ pg_constraint con ON con.conindid = idx.indexrelid
1397
+ WHERE idx.relname = '${tableName}' and schemaname = '${tableSchema}'
1398
+ group by index_name, table_name,schemaname, generated_by_constraint;`
1399
+ );
1400
+ const idxsInConsteraint = dbIndexFromConstraint.filter((it) => it.generated_by_constraint === 1).map((it) => it.index_name);
1401
+ for (const dbIndex of dbIndexes) {
1402
+ const indexName2 = dbIndex.index_name;
1403
+ const indexColumnName = dbIndex.column_name;
1404
+ const indexIsUnique = dbIndex.is_unique;
1405
+ if (idxsInConsteraint.includes(indexName2))
1406
+ continue;
1407
+ if (typeof indexToReturn[indexName2] !== "undefined") {
1408
+ indexToReturn[indexName2].columns.push(indexColumnName);
1409
+ } else {
1410
+ indexToReturn[indexName2] = {
1411
+ name: indexName2,
1412
+ columns: [indexColumnName],
1413
+ isUnique: indexIsUnique
1414
+ };
1415
+ }
1416
+ }
1417
+ indexesCount += Object.keys(indexToReturn).length;
1418
+ if (progressCallback) {
1419
+ progressCallback("indexes", indexesCount, "fetching");
1420
+ }
1421
+ result[tableName] = {
1422
+ name: tableName,
1423
+ schema: tableSchema !== "public" ? tableSchema : "",
1424
+ columns: columnToReturn,
1425
+ indexes: indexToReturn,
1426
+ foreignKeys: foreignKeysToReturn,
1427
+ compositePrimaryKeys: primaryKeys,
1428
+ uniqueConstraints: uniqueConstrains
1429
+ };
1430
+ } catch (e) {
1431
+ rej(e);
1432
+ return;
1433
+ }
1434
+ res("");
1435
+ });
1436
+ });
1437
+ if (progressCallback) {
1438
+ progressCallback("tables", tableCount, "done");
1439
+ }
1440
+ for await (const _ of all) {
1441
+ }
1442
+ if (progressCallback) {
1443
+ progressCallback("columns", columnsCount, "done");
1444
+ progressCallback("indexes", indexesCount, "done");
1445
+ progressCallback("fks", foreignKeysCount, "done");
1446
+ }
1447
+ const allEnums = await db.query(
1448
+ `select n.nspname as enum_schema,
1449
+ t.typname as enum_name,
1450
+ e.enumlabel as enum_value
1451
+ from pg_type t
1452
+ join pg_enum e on t.oid = e.enumtypid
1453
+ join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;`
1454
+ );
1455
+ const enumsToReturn = {};
1456
+ for (const dbEnum of allEnums) {
1457
+ const enumName = dbEnum.enum_name;
1458
+ const enumValue = dbEnum.enum_value;
1459
+ if (enumsToReturn[enumName] !== void 0 && enumsToReturn[enumName] !== null) {
1460
+ enumsToReturn[enumName].values[enumValue] = enumValue;
1461
+ } else {
1462
+ enumsToReturn[enumName] = {
1463
+ name: enumName,
1464
+ values: { [enumValue]: enumValue }
1465
+ };
1466
+ }
1467
+ }
1468
+ if (progressCallback) {
1469
+ progressCallback("enums", Object.keys(enumsToReturn).length, "done");
1470
+ }
1471
+ const schemasObject = Object.fromEntries([...schemas].map((it) => [it, it]));
1472
+ return {
1473
+ version: "5",
1474
+ dialect: "pg",
1475
+ tables: result,
1476
+ enums: enumsToReturn,
1477
+ schemas: schemasObject,
1478
+ _meta: {
1479
+ schemas: {},
1480
+ tables: {},
1481
+ columns: {}
1482
+ },
1483
+ internal: internals
1484
+ };
1485
+ };
1486
+ columnToDefault = {
1487
+ "numeric(": "::numeric",
1488
+ // text: "::text",
1489
+ // "character varying": "::character varying",
1490
+ // "double precision": "::double precision",
1491
+ // "time with time zone": "::time with time zone",
1492
+ "time without time zone": "::time without time zone",
1493
+ // "timestamp with time zone": "::timestamp with time zone",
1494
+ "timestamp without time zone": "::timestamp without time zone",
1495
+ // date: "::date",
1496
+ // interval: "::interval",
1497
+ // character: "::bpchar",
1498
+ // macaddr8: "::macaddr8",
1499
+ // macaddr: "::macaddr",
1500
+ // inet: "::inet",
1501
+ // cidr: "::cidr",
1502
+ // jsonb: "::jsonb",
1503
+ // json: "::json",
1504
+ "character(": "::bpchar"
1505
+ };
1506
+ defaultForColumn = (column) => {
1507
+ if (column.data_type === "serial" || column.data_type === "smallserial" || column.data_type === "bigserial") {
1508
+ return void 0;
1509
+ }
1510
+ const hasDifferentDefaultCast = Object.keys(columnToDefault).find(
1511
+ (it) => column.data_type.startsWith(it)
1512
+ );
1513
+ if (column.column_default === null) {
1514
+ return void 0;
1515
+ }
1516
+ const columnDefaultAsString = column.column_default.toString();
1517
+ if (columnDefaultAsString.endsWith(
1518
+ hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column.data_type
1519
+ )) {
1520
+ const nonPrefixPart = column.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`).length - 1;
1521
+ const rt = column.column_default.toString().substring(1, nonPrefixPart);
1522
+ if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column.data_type.startsWith("numeric")) {
1523
+ return Number(rt);
1524
+ } else if (column.data_type === "json" || column.data_type === "jsonb") {
1525
+ const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
1526
+ return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`}`;
1527
+ } else if (column.data_type === "boolean") {
1528
+ return column.column_default === "true";
1529
+ } else {
1530
+ return `'${rt}'`;
1531
+ }
1532
+ } else {
1533
+ if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !column.data_type.startsWith("numeric")) {
1534
+ return Number(columnDefaultAsString);
1535
+ } else if (column.data_type === "boolean") {
1536
+ return column.column_default === "true";
1537
+ } else {
1538
+ return `${columnDefaultAsString}`;
1539
+ }
1540
+ }
1541
+ };
1542
+ toDrizzle = (schema, schemaName) => {
1543
+ const tables = {};
1544
+ Object.values(schema.tables).forEach((t) => {
1545
+ const columns = {};
1546
+ Object.values(t.columns).forEach((c) => {
1547
+ const columnName = c.name;
1548
+ const type = c.type;
1549
+ let columnBuilder;
1550
+ if (type === "bigint") {
1551
+ columnBuilder = new PgBigInt53Builder(columnName);
1552
+ } else if (type === "bigserial") {
1553
+ columnBuilder = new PgBigSerial53Builder(columnName);
1554
+ } else if (type === "boolean") {
1555
+ columnBuilder = new PgBooleanBuilder(columnName);
1556
+ } else if (type === "cidr") {
1557
+ columnBuilder = new PgCidrBuilder(columnName);
1558
+ } else if (type === "date") {
1559
+ columnBuilder = new PgDateBuilder(columnName);
1560
+ } else if (type === "double precision") {
1561
+ columnBuilder = new PgDoublePrecisionBuilder(columnName);
1562
+ } else if (type === "inet") {
1563
+ columnBuilder = new PgInetBuilder(columnName);
1564
+ } else if (type === "integer") {
1565
+ columnBuilder = new PgIntegerBuilder(columnName);
1566
+ } else if (type === "interval" || type.startsWith("interval ")) {
1567
+ columnBuilder = new PgIntervalBuilder(columnName, {});
1568
+ } else if (type === "json") {
1569
+ columnBuilder = new PgJsonBuilder(columnName);
1570
+ } else if (type === "jsonb") {
1571
+ columnBuilder = new PgJsonbBuilder(columnName);
1572
+ } else if (type === "macaddr") {
1573
+ columnBuilder = new PgMacaddrBuilder(columnName);
1574
+ } else if (type === "macaddr8") {
1575
+ columnBuilder = new PgMacaddr8Builder(columnName);
1576
+ } else if (type === "numeric" || type.startsWith("numeric(")) {
1577
+ columnBuilder = new PgNumericBuilder(columnName);
1578
+ } else if (type === "real") {
1579
+ columnBuilder = new PgRealBuilder(columnName);
1580
+ } else if (type === "serial") {
1581
+ columnBuilder = new PgSerialBuilder(columnName);
1582
+ } else if (type === "smallint") {
1583
+ columnBuilder = new PgSmallIntBuilder(columnName);
1584
+ } else if (type === "smallserial") {
1585
+ columnBuilder = new PgSmallSerialBuilder(columnName);
1586
+ } else if (type === "text") {
1587
+ columnBuilder = new PgTextBuilder(columnName, {});
1588
+ } else if (type === "time" || type.startsWith("time(") || type === "time with time zone") {
1589
+ columnBuilder = new PgTimeBuilder(columnName, false, void 0);
1590
+ } else if (type === "timestamp" || type.startsWith("timestamp(") || type === "timestamp with time zone") {
1591
+ columnBuilder = new PgTimestampBuilder(columnName, false, void 0);
1592
+ } else if (type === "uuid") {
1593
+ columnBuilder = new PgUUIDBuilder(columnName);
1594
+ } else if (type === "varchar" || type.startsWith("varchar(")) {
1595
+ columnBuilder = new PgVarcharBuilder(columnName, {});
1596
+ } else if (type === "char" || type.startsWith("char(")) {
1597
+ columnBuilder = new PgCharBuilder(columnName, {});
1598
+ } else {
1599
+ columnBuilder = customType({
1600
+ dataType() {
1601
+ return type;
1602
+ }
1603
+ })(columnName);
1604
+ }
1605
+ if (c.notNull) {
1606
+ columnBuilder = columnBuilder.notNull();
1607
+ }
1608
+ if (c.default) {
1609
+ columnBuilder = columnBuilder.default(c.default);
1610
+ }
1611
+ if (c.primaryKey) {
1612
+ columnBuilder = columnBuilder.primaryKey();
1613
+ }
1614
+ columns[columnName] = columnBuilder;
1615
+ });
1616
+ if (schemaName === "public") {
1617
+ tables[t.name] = pgTable(t.name, columns, (cb) => {
1618
+ const res = {};
1619
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1620
+ const gh = cpk.columns.map((c) => cb[c]);
1621
+ res[cpk.name] = new PrimaryKeyBuilder(
1622
+ gh,
1623
+ cpk.name
1624
+ );
1625
+ });
1626
+ return res;
1627
+ });
1628
+ } else {
1629
+ tables[t.name] = pgSchema(schemaName).table(t.name, columns, (cb) => {
1630
+ const res = {};
1631
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1632
+ const gh = cpk.columns.map((c) => cb[c]);
1633
+ res[cpk.name] = new PrimaryKeyBuilder(
1634
+ gh,
1635
+ cpk.name
1636
+ );
1637
+ });
1638
+ return res;
1639
+ });
1640
+ }
1641
+ });
1642
+ return tables;
1643
+ };
1644
+ }
1645
+ });
1646
+
1102
1647
  // src/serializer/sqliteSerializer.ts
1103
- import { getTableName, is, SQL } from "drizzle-orm";
1648
+ import { getTableName as getTableName2, is as is2, SQL as SQL2 } from "drizzle-orm";
1104
1649
  import {
1105
- getTableConfig,
1650
+ getTableConfig as getTableConfig2,
1651
+ PrimaryKeyBuilder as PrimaryKeyBuilder2,
1106
1652
  SQLiteBaseInteger,
1653
+ SQLiteBlobBufferBuilder,
1654
+ SQLiteIntegerBuilder,
1655
+ SQLiteNumericBuilder,
1656
+ SQLiteRealBuilder,
1107
1657
  SQLiteSyncDialect,
1108
- uniqueKeyName
1658
+ sqliteTable,
1659
+ SQLiteTextBuilder,
1660
+ uniqueKeyName as uniqueKeyName2
1109
1661
  } from "drizzle-orm/sqlite-core";
1110
1662
  function mapSqlToSqliteType(sqlType) {
1111
1663
  if ([
@@ -1142,14 +1694,14 @@ function mapSqlToSqliteType(sqlType) {
1142
1694
  return "numeric";
1143
1695
  }
1144
1696
  }
1145
- var dialect, fromDatabase;
1697
+ var dialect2, fromDatabase2, toDrizzle2;
1146
1698
  var init_sqliteSerializer = __esm({
1147
1699
  "src/serializer/sqliteSerializer.ts"() {
1148
1700
  init_serializer();
1149
1701
  init_outputs();
1150
1702
  init_source();
1151
- dialect = new SQLiteSyncDialect();
1152
- fromDatabase = async (db, tablesFilter = (table) => true, progressCallback) => {
1703
+ dialect2 = new SQLiteSyncDialect();
1704
+ fromDatabase2 = async (db, tablesFilter = (table) => true, progressCallback) => {
1153
1705
  const result = {};
1154
1706
  const columns = await db.query(
1155
1707
  `SELECT
@@ -1343,6 +1895,50 @@ WHERE
1343
1895
  }
1344
1896
  };
1345
1897
  };
1898
+ toDrizzle2 = (schema) => {
1899
+ const tables = {};
1900
+ Object.values(schema.tables).forEach((t) => {
1901
+ const columns = {};
1902
+ Object.values(t.columns).forEach((c) => {
1903
+ const columnName = c.name;
1904
+ const type = c.type;
1905
+ let columnBuilder;
1906
+ if (type === "integer") {
1907
+ columnBuilder = new SQLiteIntegerBuilder(columnName);
1908
+ } else if (type === "text") {
1909
+ columnBuilder = new SQLiteTextBuilder(columnName, {});
1910
+ } else if (type === "blob") {
1911
+ columnBuilder = new SQLiteBlobBufferBuilder(columnName);
1912
+ } else if (type === "real") {
1913
+ columnBuilder = new SQLiteRealBuilder(columnName);
1914
+ } else {
1915
+ columnBuilder = new SQLiteNumericBuilder(columnName);
1916
+ }
1917
+ if (c.notNull) {
1918
+ columnBuilder = columnBuilder.notNull();
1919
+ }
1920
+ if (c.default) {
1921
+ columnBuilder = columnBuilder.default(c.default);
1922
+ }
1923
+ if (c.primaryKey) {
1924
+ columnBuilder = columnBuilder.primaryKey();
1925
+ }
1926
+ columns[columnName] = columnBuilder;
1927
+ });
1928
+ tables[t.name] = sqliteTable(t.name, columns, (cb) => {
1929
+ const res = {};
1930
+ Object.values(t.compositePrimaryKeys).forEach((cpk) => {
1931
+ const gh = cpk.columns.map((c) => cb[c]);
1932
+ res[cpk.name] = new PrimaryKeyBuilder2(
1933
+ gh,
1934
+ cpk.name
1935
+ );
1936
+ });
1937
+ return res;
1938
+ });
1939
+ });
1940
+ return tables;
1941
+ };
1346
1942
  }
1347
1943
  });
1348
1944
 
@@ -1529,446 +2125,31 @@ var require_brace_expansion = __commonJS({
1529
2125
  var need = width - c.length;
1530
2126
  if (need > 0) {
1531
2127
  var z = new Array(need + 1).join("0");
1532
- if (i < 0)
1533
- c = "-" + z + c.slice(1);
1534
- else
1535
- c = z + c;
1536
- }
1537
- }
1538
- }
1539
- N.push(c);
1540
- }
1541
- } else {
1542
- N = [];
1543
- for (var j = 0; j < n.length; j++) {
1544
- N.push.apply(N, expand2(n[j], false));
1545
- }
1546
- }
1547
- for (var j = 0; j < N.length; j++) {
1548
- for (var k = 0; k < post.length; k++) {
1549
- var expansion = pre + N[j] + post[k];
1550
- if (!isTop || isSequence || expansion)
1551
- expansions.push(expansion);
1552
- }
1553
- }
1554
- }
1555
- return expansions;
1556
- }
1557
- }
1558
- });
1559
-
1560
- // src/serializer/pgSerializer.ts
1561
- import {
1562
- PgDialect,
1563
- uniqueKeyName as uniqueKeyName2
1564
- } from "drizzle-orm/pg-core";
1565
- import { getTableConfig as getTableConfig2 } from "drizzle-orm/pg-core";
1566
- import { is as is2, SQL as SQL2, getTableName as getTableName2 } from "drizzle-orm";
1567
- var dialect2, trimChar, fromDatabase2, columnToDefault, defaultForColumn;
1568
- var init_pgSerializer = __esm({
1569
- "src/serializer/pgSerializer.ts"() {
1570
- init_serializer();
1571
- init_source();
1572
- init_outputs();
1573
- dialect2 = new PgDialect();
1574
- trimChar = (str, char) => {
1575
- let start = 0;
1576
- let end = str.length;
1577
- while (start < end && str[start] === char)
1578
- ++start;
1579
- while (end > start && str[end - 1] === char)
1580
- --end;
1581
- return start > 0 || end < str.length ? str.substring(start, end) : str.toString();
1582
- };
1583
- fromDatabase2 = async (db, tablesFilter = (table) => true, schemaFilters, progressCallback) => {
1584
- const result = {};
1585
- const internals = { tables: {} };
1586
- const where = schemaFilters.map((t) => `table_schema = '${t}'`).join(" or ");
1587
- const allTables = await db.query(
1588
- `SELECT table_schema, table_name FROM information_schema.tables WHERE ${where};`
1589
- );
1590
- const schemas = new Set(allTables.map((it) => it.table_schema));
1591
- schemas.delete("public");
1592
- const allSchemas = await db.query(`select s.nspname as table_schema
1593
- from pg_catalog.pg_namespace s
1594
- join pg_catalog.pg_user u on u.usesysid = s.nspowner
1595
- where nspname not in ('information_schema', 'pg_catalog', 'public')
1596
- and nspname not like 'pg_toast%'
1597
- and nspname not like 'pg_temp_%'
1598
- order by table_schema;`);
1599
- allSchemas.forEach((item) => {
1600
- if (schemaFilters.includes(item.table_schema)) {
1601
- schemas.add(item.table_schema);
1602
- }
1603
- });
1604
- let columnsCount = 0;
1605
- let indexesCount = 0;
1606
- let foreignKeysCount = 0;
1607
- let tableCount = 0;
1608
- const all = allTables.map((row) => {
1609
- return new Promise(async (res, rej) => {
1610
- const tableName = row.table_name;
1611
- if (!tablesFilter(tableName))
1612
- return res("");
1613
- tableCount += 1;
1614
- const tableSchema = row.table_schema;
1615
- try {
1616
- const columnToReturn = {};
1617
- const indexToReturn = {};
1618
- const foreignKeysToReturn = {};
1619
- const primaryKeys = {};
1620
- const uniqueConstrains = {};
1621
- const tableResponse = await db.query(
1622
- `SELECT a.attrelid::regclass::text, a.attname, is_nullable, a.attndims as array_dimensions
1623
- , CASE WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
1624
- AND EXISTS (
1625
- SELECT FROM pg_attrdef ad
1626
- WHERE ad.adrelid = a.attrelid
1627
- AND ad.adnum = a.attnum
1628
- AND pg_get_expr(ad.adbin, ad.adrelid)
1629
- = 'nextval('''
1630
- || (pg_get_serial_sequence (a.attrelid::regclass::text
1631
- , a.attname))::regclass
1632
- || '''::regclass)'
1633
- )
1634
- THEN CASE a.atttypid
1635
- WHEN 'int'::regtype THEN 'serial'
1636
- WHEN 'int8'::regtype THEN 'bigserial'
1637
- WHEN 'int2'::regtype THEN 'smallserial'
1638
- END
1639
- ELSE format_type(a.atttypid, a.atttypmod)
1640
- END AS data_type, INFORMATION_SCHEMA.COLUMNS.table_name, INFORMATION_SCHEMA.COLUMNS.column_name, INFORMATION_SCHEMA.COLUMNS.column_default, INFORMATION_SCHEMA.COLUMNS.data_type as additional_dt
1641
- FROM pg_attribute a
1642
- JOIN INFORMATION_SCHEMA.COLUMNS ON INFORMATION_SCHEMA.COLUMNS.column_name = a.attname
1643
- WHERE a.attrelid = '"${tableSchema}"."${tableName}"'::regclass and INFORMATION_SCHEMA.COLUMNS.table_name = '${tableName}'
1644
- AND a.attnum > 0
1645
- AND NOT a.attisdropped
1646
- ORDER BY a.attnum;`
1647
- );
1648
- const tableConstraints = await db.query(
1649
- `SELECT c.column_name, c.data_type, constraint_type, constraint_name, constraint_schema
1650
- FROM information_schema.table_constraints tc
1651
- JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
1652
- JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
1653
- AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
1654
- WHERE tc.table_name = '${tableName}' and constraint_schema = '${tableSchema}';`
1655
- );
1656
- columnsCount += tableResponse.length;
1657
- if (progressCallback) {
1658
- progressCallback("columns", columnsCount, "fetching");
1659
- }
1660
- const tableForeignKeys = await db.query(
1661
- `SELECT
1662
- tc.table_schema,
1663
- tc.constraint_name,
1664
- tc.table_name,
1665
- kcu.column_name,
1666
- ccu.table_schema AS foreign_table_schema,
1667
- ccu.table_name AS foreign_table_name,
1668
- ccu.column_name AS foreign_column_name,
1669
- rc.delete_rule, rc.update_rule
1670
- FROM
1671
- information_schema.table_constraints AS tc
1672
- JOIN information_schema.key_column_usage AS kcu
1673
- ON tc.constraint_name = kcu.constraint_name
1674
- AND tc.table_schema = kcu.table_schema
1675
- JOIN information_schema.constraint_column_usage AS ccu
1676
- ON ccu.constraint_name = tc.constraint_name
1677
- AND ccu.table_schema = tc.table_schema
1678
- JOIN information_schema.referential_constraints AS rc
1679
- ON ccu.constraint_name = rc.constraint_name
1680
- WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${tableName}' and tc.table_schema='${tableSchema}';`
1681
- );
1682
- foreignKeysCount += tableForeignKeys.length;
1683
- if (progressCallback) {
1684
- progressCallback("fks", foreignKeysCount, "fetching");
1685
- }
1686
- for (const fk of tableForeignKeys) {
1687
- const columnFrom = fk.column_name;
1688
- const tableTo = fk.foreign_table_name;
1689
- const columnTo = fk.foreign_column_name;
1690
- const foreignKeyName = fk.constraint_name;
1691
- const onUpdate = fk.update_rule.toLowerCase();
1692
- const onDelete = fk.delete_rule.toLowerCase();
1693
- if (typeof foreignKeysToReturn[foreignKeyName] !== "undefined") {
1694
- foreignKeysToReturn[foreignKeyName].columnsFrom.push(columnFrom);
1695
- foreignKeysToReturn[foreignKeyName].columnsTo.push(columnTo);
1696
- } else {
1697
- foreignKeysToReturn[foreignKeyName] = {
1698
- name: foreignKeyName,
1699
- tableFrom: tableName,
1700
- tableTo,
1701
- columnsFrom: [columnFrom],
1702
- columnsTo: [columnTo],
1703
- onDelete,
1704
- onUpdate
1705
- };
1706
- }
1707
- foreignKeysToReturn[foreignKeyName].columnsFrom = [
1708
- ...new Set(foreignKeysToReturn[foreignKeyName].columnsFrom)
1709
- ];
1710
- foreignKeysToReturn[foreignKeyName].columnsTo = [
1711
- ...new Set(foreignKeysToReturn[foreignKeyName].columnsTo)
1712
- ];
1713
- }
1714
- const uniqueConstrainsRows = tableConstraints.filter(
1715
- (mapRow) => mapRow.constraint_type === "UNIQUE"
1716
- );
1717
- for (const unqs of uniqueConstrainsRows) {
1718
- const columnName = unqs.column_name;
1719
- const constraintName = unqs.constraint_name;
1720
- if (typeof uniqueConstrains[constraintName] !== "undefined") {
1721
- uniqueConstrains[constraintName].columns.push(columnName);
1722
- } else {
1723
- uniqueConstrains[constraintName] = {
1724
- columns: [columnName],
1725
- nullsNotDistinct: false,
1726
- name: constraintName
1727
- };
1728
- }
1729
- }
1730
- for (const columnResponse of tableResponse) {
1731
- const columnName = columnResponse.attname;
1732
- const columnAdditionalDT = columnResponse.additional_dt;
1733
- const columnDimensions = columnResponse.array_dimensions;
1734
- let columnType = columnResponse.data_type;
1735
- const primaryKey = tableConstraints.filter(
1736
- (mapRow) => columnName === mapRow.column_name && mapRow.constraint_type === "PRIMARY KEY"
1737
- );
1738
- const cprimaryKey = tableConstraints.filter(
1739
- (mapRow) => mapRow.constraint_type === "PRIMARY KEY"
1740
- );
1741
- if (cprimaryKey.length > 1) {
1742
- const tableCompositePkName = await db.query(
1743
- `SELECT conname AS primary_key
1744
- FROM pg_constraint join pg_class on (pg_class.oid = conrelid)
1745
- WHERE contype = 'p'
1746
- AND connamespace = $1::regnamespace
1747
- AND pg_class.relname = $2;`,
1748
- [tableSchema, tableName]
1749
- );
1750
- primaryKeys[tableCompositePkName[0].primary_key] = {
1751
- name: tableCompositePkName[0].primary_key,
1752
- columns: cprimaryKey.map((c) => c.column_name)
1753
- };
1754
- }
1755
- const defaultValue = defaultForColumn(columnResponse);
1756
- const isSerial = columnType === "serial";
1757
- let columnTypeMapped = columnType;
1758
- if (columnTypeMapped.startsWith("numeric(")) {
1759
- columnTypeMapped = columnTypeMapped.replace(",", ", ");
1760
- }
1761
- if (columnAdditionalDT === "ARRAY") {
1762
- if (typeof internals.tables[tableName] === "undefined") {
1763
- internals.tables[tableName] = {
1764
- columns: {
1765
- [columnName]: {
1766
- isArray: true,
1767
- dimensions: columnDimensions,
1768
- rawType: columnTypeMapped.substring(
1769
- 0,
1770
- columnTypeMapped.length - 2
1771
- )
1772
- }
1773
- }
1774
- };
1775
- } else {
1776
- if (typeof internals.tables[tableName].columns[columnName] === "undefined") {
1777
- internals.tables[tableName].columns[columnName] = {
1778
- isArray: true,
1779
- dimensions: columnDimensions,
1780
- rawType: columnTypeMapped.substring(
1781
- 0,
1782
- columnTypeMapped.length - 2
1783
- )
1784
- };
1785
- }
1786
- }
1787
- }
1788
- if (columnAdditionalDT === "ARRAY") {
1789
- for (let i = 1; i < Number(columnDimensions); i++) {
1790
- columnTypeMapped += "[]";
2128
+ if (i < 0)
2129
+ c = "-" + z + c.slice(1);
2130
+ else
2131
+ c = z + c;
1791
2132
  }
1792
2133
  }
1793
- columnTypeMapped = columnTypeMapped.replace("character varying", "varchar").replace(" without time zone", "").replace("character", "char");
1794
- columnTypeMapped = trimChar(columnTypeMapped, '"');
1795
- columnToReturn[columnName] = {
1796
- name: columnName,
1797
- type: columnTypeMapped,
1798
- primaryKey: primaryKey.length === 1 && cprimaryKey.length < 2,
1799
- // default: isSerial ? undefined : defaultValue,
1800
- notNull: columnResponse.is_nullable === "NO"
1801
- };
1802
- if (!isSerial && typeof defaultValue !== "undefined") {
1803
- columnToReturn[columnName].default = defaultValue;
1804
- }
1805
- }
1806
- const dbIndexes = await db.query(
1807
- `SELECT t.relname as table_name, i.relname AS index_name, ix.indisunique AS is_unique, a.attname AS column_name
1808
- FROM pg_class t
1809
- JOIN pg_index ix ON t.oid = ix.indrelid
1810
- JOIN pg_class i ON i.oid = ix.indexrelid
1811
- JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
1812
- JOIN pg_namespace ns ON ns.oid = t.relnamespace
1813
- WHERE ns.nspname = '${tableSchema}'
1814
- AND t.relname = '${tableName}'
1815
- and ix.indisprimary = false;`
1816
- );
1817
- const dbIndexFromConstraint = await db.query(
1818
- `SELECT
1819
- idx.indexrelname AS index_name,
1820
- idx.relname AS table_name,
1821
- schemaname,
1822
- CASE WHEN con.conname IS NOT NULL THEN 1 ELSE 0 END AS generated_by_constraint
1823
- FROM
1824
- pg_stat_user_indexes idx
1825
- LEFT JOIN
1826
- pg_constraint con ON con.conindid = idx.indexrelid
1827
- WHERE idx.relname = '${tableName}' and schemaname = '${tableSchema}'
1828
- group by index_name, table_name,schemaname, generated_by_constraint;`
1829
- );
1830
- const idxsInConsteraint = dbIndexFromConstraint.filter((it) => it.generated_by_constraint === 1).map((it) => it.index_name);
1831
- for (const dbIndex of dbIndexes) {
1832
- const indexName2 = dbIndex.index_name;
1833
- const indexColumnName = dbIndex.column_name;
1834
- const indexIsUnique = dbIndex.is_unique;
1835
- if (idxsInConsteraint.includes(indexName2))
1836
- continue;
1837
- if (typeof indexToReturn[indexName2] !== "undefined") {
1838
- indexToReturn[indexName2].columns.push(indexColumnName);
1839
- } else {
1840
- indexToReturn[indexName2] = {
1841
- name: indexName2,
1842
- columns: [indexColumnName],
1843
- isUnique: indexIsUnique
1844
- };
1845
- }
1846
- }
1847
- indexesCount += Object.keys(indexToReturn).length;
1848
- if (progressCallback) {
1849
- progressCallback("indexes", indexesCount, "fetching");
1850
2134
  }
1851
- result[tableName] = {
1852
- name: tableName,
1853
- schema: tableSchema !== "public" ? tableSchema : "",
1854
- columns: columnToReturn,
1855
- indexes: indexToReturn,
1856
- foreignKeys: foreignKeysToReturn,
1857
- compositePrimaryKeys: primaryKeys,
1858
- uniqueConstraints: uniqueConstrains
1859
- };
1860
- } catch (e) {
1861
- rej(e);
1862
- return;
2135
+ N.push(c);
1863
2136
  }
1864
- res("");
1865
- });
1866
- });
1867
- if (progressCallback) {
1868
- progressCallback("tables", tableCount, "done");
1869
- }
1870
- for await (const _ of all) {
1871
- }
1872
- if (progressCallback) {
1873
- progressCallback("columns", columnsCount, "done");
1874
- progressCallback("indexes", indexesCount, "done");
1875
- progressCallback("fks", foreignKeysCount, "done");
1876
- }
1877
- const allEnums = await db.query(
1878
- `select n.nspname as enum_schema,
1879
- t.typname as enum_name,
1880
- e.enumlabel as enum_value
1881
- from pg_type t
1882
- join pg_enum e on t.oid = e.enumtypid
1883
- join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;`
1884
- );
1885
- const enumsToReturn = {};
1886
- for (const dbEnum of allEnums) {
1887
- const enumName = dbEnum.enum_name;
1888
- const enumValue = dbEnum.enum_value;
1889
- if (enumsToReturn[enumName] !== void 0 && enumsToReturn[enumName] !== null) {
1890
- enumsToReturn[enumName].values[enumValue] = enumValue;
1891
- } else {
1892
- enumsToReturn[enumName] = {
1893
- name: enumName,
1894
- values: { [enumValue]: enumValue }
1895
- };
1896
- }
1897
- }
1898
- if (progressCallback) {
1899
- progressCallback("enums", Object.keys(enumsToReturn).length, "done");
1900
- }
1901
- const schemasObject = Object.fromEntries([...schemas].map((it) => [it, it]));
1902
- return {
1903
- version: "5",
1904
- dialect: "pg",
1905
- tables: result,
1906
- enums: enumsToReturn,
1907
- schemas: schemasObject,
1908
- _meta: {
1909
- schemas: {},
1910
- tables: {},
1911
- columns: {}
1912
- },
1913
- internal: internals
1914
- };
1915
- };
1916
- columnToDefault = {
1917
- "numeric(": "::numeric",
1918
- // text: "::text",
1919
- // "character varying": "::character varying",
1920
- // "double precision": "::double precision",
1921
- // "time with time zone": "::time with time zone",
1922
- "time without time zone": "::time without time zone",
1923
- // "timestamp with time zone": "::timestamp with time zone",
1924
- "timestamp without time zone": "::timestamp without time zone",
1925
- // date: "::date",
1926
- // interval: "::interval",
1927
- // character: "::bpchar",
1928
- // macaddr8: "::macaddr8",
1929
- // macaddr: "::macaddr",
1930
- // inet: "::inet",
1931
- // cidr: "::cidr",
1932
- // jsonb: "::jsonb",
1933
- // json: "::json",
1934
- "character(": "::bpchar"
1935
- };
1936
- defaultForColumn = (column) => {
1937
- if (column.data_type === "serial" || column.data_type === "smallserial" || column.data_type === "bigserial") {
1938
- return void 0;
1939
- }
1940
- const hasDifferentDefaultCast = Object.keys(columnToDefault).find(
1941
- (it) => column.data_type.startsWith(it)
1942
- );
1943
- if (column.column_default === null) {
1944
- return void 0;
1945
- }
1946
- const columnDefaultAsString = column.column_default.toString();
1947
- if (columnDefaultAsString.endsWith(
1948
- hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column.data_type
1949
- )) {
1950
- const nonPrefixPart = column.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`).length - 1;
1951
- const rt = column.column_default.toString().substring(1, nonPrefixPart);
1952
- if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column.data_type.startsWith("numeric")) {
1953
- return Number(rt);
1954
- } else if (column.data_type === "json" || column.data_type === "jsonb") {
1955
- const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
1956
- return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column.data_type}`}`;
1957
- } else if (column.data_type === "boolean") {
1958
- return column.column_default === "true";
1959
2137
  } else {
1960
- return `'${rt}'`;
2138
+ N = [];
2139
+ for (var j = 0; j < n.length; j++) {
2140
+ N.push.apply(N, expand2(n[j], false));
2141
+ }
1961
2142
  }
1962
- } else {
1963
- if (/^-?[\d.]+(?:e-?\d+)?$/.test(columnDefaultAsString) && !column.data_type.startsWith("numeric")) {
1964
- return Number(columnDefaultAsString);
1965
- } else if (column.data_type === "boolean") {
1966
- return column.column_default === "true";
1967
- } else {
1968
- return `${columnDefaultAsString}`;
2143
+ for (var j = 0; j < N.length; j++) {
2144
+ for (var k = 0; k < post.length; k++) {
2145
+ var expansion = pre + N[j] + post[k];
2146
+ if (!isTop || isSequence || expansion)
2147
+ expansions.push(expansion);
2148
+ }
1969
2149
  }
1970
2150
  }
1971
- };
2151
+ return expansions;
2152
+ }
1972
2153
  }
1973
2154
  });
1974
2155
 
@@ -2004,192 +2185,9 @@ var init_drivers = __esm({
2004
2185
  }
2005
2186
  });
2006
2187
 
2007
- // src/serializer/schemaToDrizzle.ts
2008
- import {
2009
- customType,
2010
- PgBigInt53Builder,
2011
- PgBigSerial53Builder,
2012
- PgBooleanBuilder,
2013
- PgCharBuilder,
2014
- PgCidrBuilder,
2015
- PgDateBuilder,
2016
- PgDoublePrecisionBuilder,
2017
- PgInetBuilder,
2018
- PgIntegerBuilder,
2019
- PgIntervalBuilder,
2020
- PgJsonbBuilder,
2021
- PgJsonBuilder,
2022
- PgMacaddr8Builder,
2023
- PgMacaddrBuilder,
2024
- PgNumericBuilder,
2025
- PgRealBuilder,
2026
- pgSchema,
2027
- PgSerialBuilder,
2028
- PgSmallIntBuilder,
2029
- PgSmallSerialBuilder,
2030
- pgTable,
2031
- PgTextBuilder,
2032
- PgTimeBuilder,
2033
- PgTimestampBuilder,
2034
- PgUUIDBuilder,
2035
- PgVarcharBuilder,
2036
- PrimaryKeyBuilder as PgPrimaryKeyBuilder
2037
- } from "drizzle-orm/pg-core";
2038
- import {
2039
- SQLiteIntegerBuilder,
2040
- SQLiteTextBuilder,
2041
- SQLiteBlobBufferBuilder,
2042
- SQLiteRealBuilder,
2043
- SQLiteNumericBuilder,
2044
- sqliteTable,
2045
- PrimaryKeyBuilder as SQLitePrimaryKeyBuilder
2046
- } from "drizzle-orm/sqlite-core";
2047
- var pgSchemaToDrizzle = (schema, schemaName) => {
2048
- const tables = {};
2049
- Object.values(schema.tables).forEach((t) => {
2050
- const columns = {};
2051
- Object.values(t.columns).forEach((c) => {
2052
- const columnName = c.name;
2053
- const type = c.type;
2054
- let columnBuilder;
2055
- if (type === "bigint") {
2056
- columnBuilder = new PgBigInt53Builder(columnName);
2057
- } else if (type === "bigserial") {
2058
- columnBuilder = new PgBigSerial53Builder(columnName);
2059
- } else if (type === "boolean") {
2060
- columnBuilder = new PgBooleanBuilder(columnName);
2061
- } else if (type === "cidr") {
2062
- columnBuilder = new PgCidrBuilder(columnName);
2063
- } else if (type === "date") {
2064
- columnBuilder = new PgDateBuilder(columnName);
2065
- } else if (type === "double precision") {
2066
- columnBuilder = new PgDoublePrecisionBuilder(columnName);
2067
- } else if (type === "inet") {
2068
- columnBuilder = new PgInetBuilder(columnName);
2069
- } else if (type === "integer") {
2070
- columnBuilder = new PgIntegerBuilder(columnName);
2071
- } else if (type === "interval" || type.startsWith("interval ")) {
2072
- columnBuilder = new PgIntervalBuilder(columnName, {});
2073
- } else if (type === "json") {
2074
- columnBuilder = new PgJsonBuilder(columnName);
2075
- } else if (type === "jsonb") {
2076
- columnBuilder = new PgJsonbBuilder(columnName);
2077
- } else if (type === "macaddr") {
2078
- columnBuilder = new PgMacaddrBuilder(columnName);
2079
- } else if (type === "macaddr8") {
2080
- columnBuilder = new PgMacaddr8Builder(columnName);
2081
- } else if (type === "numeric" || type.startsWith("numeric(")) {
2082
- columnBuilder = new PgNumericBuilder(columnName);
2083
- } else if (type === "real") {
2084
- columnBuilder = new PgRealBuilder(columnName);
2085
- } else if (type === "serial") {
2086
- columnBuilder = new PgSerialBuilder(columnName);
2087
- } else if (type === "smallint") {
2088
- columnBuilder = new PgSmallIntBuilder(columnName);
2089
- } else if (type === "smallserial") {
2090
- columnBuilder = new PgSmallSerialBuilder(columnName);
2091
- } else if (type === "text") {
2092
- columnBuilder = new PgTextBuilder(columnName, {});
2093
- } else if (type === "time" || type.startsWith("time(") || type === "time with time zone") {
2094
- columnBuilder = new PgTimeBuilder(columnName, false, void 0);
2095
- } else if (type === "timestamp" || type.startsWith("timestamp(") || type === "timestamp with time zone") {
2096
- columnBuilder = new PgTimestampBuilder(columnName, false, void 0);
2097
- } else if (type === "uuid") {
2098
- columnBuilder = new PgUUIDBuilder(columnName);
2099
- } else if (type === "varchar" || type.startsWith("varchar(")) {
2100
- columnBuilder = new PgVarcharBuilder(columnName, {});
2101
- } else if (type === "char" || type.startsWith("char(")) {
2102
- columnBuilder = new PgCharBuilder(columnName, {});
2103
- } else {
2104
- columnBuilder = customType({
2105
- dataType() {
2106
- return type;
2107
- }
2108
- })(columnName);
2109
- }
2110
- if (c.notNull) {
2111
- columnBuilder = columnBuilder.notNull();
2112
- }
2113
- if (c.default) {
2114
- columnBuilder = columnBuilder.default(c.default);
2115
- }
2116
- if (c.primaryKey) {
2117
- columnBuilder = columnBuilder.primaryKey();
2118
- }
2119
- columns[columnName] = columnBuilder;
2120
- });
2121
- if (schemaName === "public") {
2122
- tables[t.name] = pgTable(t.name, columns, (cb) => {
2123
- const res = {};
2124
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2125
- const gh = cpk.columns.map((c) => cb[c]);
2126
- res[cpk.name] = new PgPrimaryKeyBuilder(
2127
- gh,
2128
- cpk.name
2129
- );
2130
- });
2131
- return res;
2132
- });
2133
- } else {
2134
- tables[t.name] = pgSchema(schemaName).table(t.name, columns, (cb) => {
2135
- const res = {};
2136
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2137
- const gh = cpk.columns.map((c) => cb[c]);
2138
- res[cpk.name] = new PgPrimaryKeyBuilder(
2139
- gh,
2140
- cpk.name
2141
- );
2142
- });
2143
- return res;
2144
- });
2145
- }
2146
- });
2147
- return tables;
2148
- };
2149
- var sqliteSchemaToDrizzle = (schema) => {
2150
- const tables = {};
2151
- Object.values(schema.tables).forEach((t) => {
2152
- const columns = {};
2153
- Object.values(t.columns).forEach((c) => {
2154
- const columnName = c.name;
2155
- const type = c.type;
2156
- let columnBuilder;
2157
- if (type === "integer") {
2158
- columnBuilder = new SQLiteIntegerBuilder(columnName);
2159
- } else if (type === "text") {
2160
- columnBuilder = new SQLiteTextBuilder(columnName, {});
2161
- } else if (type === "blob") {
2162
- columnBuilder = new SQLiteBlobBufferBuilder(columnName);
2163
- } else if (type === "real") {
2164
- columnBuilder = new SQLiteRealBuilder(columnName);
2165
- } else {
2166
- columnBuilder = new SQLiteNumericBuilder(columnName);
2167
- }
2168
- if (c.notNull) {
2169
- columnBuilder = columnBuilder.notNull();
2170
- }
2171
- if (c.default) {
2172
- columnBuilder = columnBuilder.default(c.default);
2173
- }
2174
- if (c.primaryKey) {
2175
- columnBuilder = columnBuilder.primaryKey();
2176
- }
2177
- columns[columnName] = columnBuilder;
2178
- });
2179
- tables[t.name] = sqliteTable(t.name, columns, (cb) => {
2180
- const res = {};
2181
- Object.values(t.compositePrimaryKeys).forEach((cpk) => {
2182
- const gh = cpk.columns.map((c) => cb[c]);
2183
- res[cpk.name] = new SQLitePrimaryKeyBuilder(
2184
- gh,
2185
- cpk.name
2186
- );
2187
- });
2188
- return res;
2189
- });
2190
- });
2191
- return tables;
2192
- };
2188
+ // src/utils-studio.ts
2189
+ init_pgSerializer();
2190
+ init_sqliteSerializer();
2193
2191
 
2194
2192
  // src/cli/commands/sqliteIntrospect.ts
2195
2193
  init_views();
@@ -3367,7 +3365,7 @@ var sqlitePushIntrospect = async (client, filters) => {
3367
3365
  }
3368
3366
  return false;
3369
3367
  };
3370
- const res = await fromDatabase(client, filter2);
3368
+ const res = await fromDatabase2(client, filter2);
3371
3369
  const schema = { id: originUUID, prevId: "", ...res };
3372
3370
  return { schema };
3373
3371
  };
@@ -3403,7 +3401,7 @@ var pgPushIntrospect = async (connection, filters, schemaFilters) => {
3403
3401
  }
3404
3402
  return false;
3405
3403
  };
3406
- const res = await fromDatabase2(client, filter2, schemaFilters);
3404
+ const res = await fromDatabase(client, filter2, schemaFilters);
3407
3405
  const schema = { id: originUUID, prevId: "", ...res };
3408
3406
  const { internal, ...schemaWithoutInternals } = schema;
3409
3407
  return { schema: schemaWithoutInternals };
@@ -3414,8 +3412,8 @@ init_drivers();
3414
3412
  export {
3415
3413
  DrizzleORMPgClient,
3416
3414
  TursoSqlite,
3417
- pgSchemaToDrizzle as drizzleSchemaPg,
3418
- sqliteSchemaToDrizzle as drizzleSchemaSQLite,
3415
+ toDrizzle as drizzleSchemaPg,
3416
+ toDrizzle2 as drizzleSchemaSQLite,
3419
3417
  pgPushIntrospect,
3420
3418
  sqlitePushIntrospect
3421
3419
  };