@strapi/database 5.0.0-rc.0 → 5.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +79 -53
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -53
- package/dist/index.mjs.map +1 -1
- package/dist/schema/diff.d.ts +6 -1
- package/dist/schema/diff.d.ts.map +1 -1
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/storage.d.ts +6 -1
- package/dist/schema/storage.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1388,27 +1388,28 @@ const createSchemaDiff = (db) => {
|
|
|
1388
1388
|
}
|
|
1389
1389
|
};
|
|
1390
1390
|
};
|
|
1391
|
-
const diffTableColumns = (
|
|
1391
|
+
const diffTableColumns = (diffCtx) => {
|
|
1392
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1392
1393
|
const addedColumns = [];
|
|
1393
1394
|
const updatedColumns = [];
|
|
1394
1395
|
const unchangedColumns = [];
|
|
1395
1396
|
const removedColumns = [];
|
|
1396
|
-
for (const
|
|
1397
|
-
const
|
|
1398
|
-
if (
|
|
1399
|
-
const { status, diff } = diffColumns(
|
|
1397
|
+
for (const userSchemaColumn of userSchemaTable.columns) {
|
|
1398
|
+
const databaseColumn = helpers.findColumn(databaseTable, userSchemaColumn.name);
|
|
1399
|
+
if (databaseColumn) {
|
|
1400
|
+
const { status, diff } = diffColumns(databaseColumn, userSchemaColumn);
|
|
1400
1401
|
if (status === statuses.CHANGED) {
|
|
1401
1402
|
updatedColumns.push(diff);
|
|
1402
1403
|
} else {
|
|
1403
|
-
unchangedColumns.push(
|
|
1404
|
+
unchangedColumns.push(databaseColumn);
|
|
1404
1405
|
}
|
|
1405
1406
|
} else {
|
|
1406
|
-
addedColumns.push(
|
|
1407
|
+
addedColumns.push(userSchemaColumn);
|
|
1407
1408
|
}
|
|
1408
1409
|
}
|
|
1409
|
-
for (const
|
|
1410
|
-
if (!helpers.hasColumn(
|
|
1411
|
-
removedColumns.push(
|
|
1410
|
+
for (const databaseColumn of databaseTable.columns) {
|
|
1411
|
+
if (!helpers.hasColumn(userSchemaTable, databaseColumn.name) && previousTable && helpers.hasColumn(previousTable, databaseColumn.name)) {
|
|
1412
|
+
removedColumns.push(databaseColumn);
|
|
1412
1413
|
}
|
|
1413
1414
|
}
|
|
1414
1415
|
const hasChanged = [addedColumns, updatedColumns, removedColumns].some((arr) => arr.length > 0);
|
|
@@ -1422,27 +1423,28 @@ const createSchemaDiff = (db) => {
|
|
|
1422
1423
|
}
|
|
1423
1424
|
};
|
|
1424
1425
|
};
|
|
1425
|
-
const diffTableIndexes = (
|
|
1426
|
+
const diffTableIndexes = (diffCtx) => {
|
|
1427
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1426
1428
|
const addedIndexes = [];
|
|
1427
1429
|
const updatedIndexes = [];
|
|
1428
1430
|
const unchangedIndexes = [];
|
|
1429
1431
|
const removedIndexes = [];
|
|
1430
|
-
for (const
|
|
1431
|
-
const
|
|
1432
|
-
if (
|
|
1433
|
-
const { status, diff } = diffIndexes(
|
|
1432
|
+
for (const userSchemaIndex of userSchemaTable.indexes) {
|
|
1433
|
+
const databaseIndex = helpers.findIndex(databaseTable, userSchemaIndex.name);
|
|
1434
|
+
if (databaseIndex) {
|
|
1435
|
+
const { status, diff } = diffIndexes(databaseIndex, userSchemaIndex);
|
|
1434
1436
|
if (status === statuses.CHANGED) {
|
|
1435
1437
|
updatedIndexes.push(diff);
|
|
1436
1438
|
} else {
|
|
1437
|
-
unchangedIndexes.push(
|
|
1439
|
+
unchangedIndexes.push(databaseIndex);
|
|
1438
1440
|
}
|
|
1439
1441
|
} else {
|
|
1440
|
-
addedIndexes.push(
|
|
1442
|
+
addedIndexes.push(userSchemaIndex);
|
|
1441
1443
|
}
|
|
1442
1444
|
}
|
|
1443
|
-
for (const
|
|
1444
|
-
if (!helpers.hasIndex(
|
|
1445
|
-
removedIndexes.push(
|
|
1445
|
+
for (const databaseIndex of databaseTable.indexes) {
|
|
1446
|
+
if (!helpers.hasIndex(userSchemaTable, databaseIndex.name) && previousTable && helpers.hasIndex(previousTable, databaseIndex.name)) {
|
|
1447
|
+
removedIndexes.push(databaseIndex);
|
|
1446
1448
|
}
|
|
1447
1449
|
}
|
|
1448
1450
|
const hasChanged = [addedIndexes, updatedIndexes, removedIndexes].some((arr) => arr.length > 0);
|
|
@@ -1456,7 +1458,8 @@ const createSchemaDiff = (db) => {
|
|
|
1456
1458
|
}
|
|
1457
1459
|
};
|
|
1458
1460
|
};
|
|
1459
|
-
const diffTableForeignKeys = (
|
|
1461
|
+
const diffTableForeignKeys = (diffCtx) => {
|
|
1462
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1460
1463
|
const addedForeignKeys = [];
|
|
1461
1464
|
const updatedForeignKeys = [];
|
|
1462
1465
|
const unchangedForeignKeys = [];
|
|
@@ -1472,22 +1475,22 @@ const createSchemaDiff = (db) => {
|
|
|
1472
1475
|
}
|
|
1473
1476
|
};
|
|
1474
1477
|
}
|
|
1475
|
-
for (const
|
|
1476
|
-
const
|
|
1477
|
-
if (
|
|
1478
|
-
const { status, diff } = diffForeignKeys(
|
|
1478
|
+
for (const userSchemaForeignKeys of userSchemaTable.foreignKeys) {
|
|
1479
|
+
const databaseForeignKeys = helpers.findForeignKey(databaseTable, userSchemaForeignKeys.name);
|
|
1480
|
+
if (databaseForeignKeys) {
|
|
1481
|
+
const { status, diff } = diffForeignKeys(databaseForeignKeys, userSchemaForeignKeys);
|
|
1479
1482
|
if (status === statuses.CHANGED) {
|
|
1480
1483
|
updatedForeignKeys.push(diff);
|
|
1481
1484
|
} else {
|
|
1482
|
-
unchangedForeignKeys.push(
|
|
1485
|
+
unchangedForeignKeys.push(databaseForeignKeys);
|
|
1483
1486
|
}
|
|
1484
1487
|
} else {
|
|
1485
|
-
addedForeignKeys.push(
|
|
1488
|
+
addedForeignKeys.push(userSchemaForeignKeys);
|
|
1486
1489
|
}
|
|
1487
1490
|
}
|
|
1488
|
-
for (const
|
|
1489
|
-
if (!helpers.hasForeignKey(
|
|
1490
|
-
removedForeignKeys.push(
|
|
1491
|
+
for (const databaseForeignKeys of databaseTable.foreignKeys) {
|
|
1492
|
+
if (!helpers.hasForeignKey(userSchemaTable, databaseForeignKeys.name) && previousTable && helpers.hasForeignKey(previousTable, databaseForeignKeys.name)) {
|
|
1493
|
+
removedForeignKeys.push(databaseForeignKeys);
|
|
1491
1494
|
}
|
|
1492
1495
|
}
|
|
1493
1496
|
const hasChanged = [addedForeignKeys, updatedForeignKeys, removedForeignKeys].some(
|
|
@@ -1503,37 +1506,44 @@ const createSchemaDiff = (db) => {
|
|
|
1503
1506
|
}
|
|
1504
1507
|
};
|
|
1505
1508
|
};
|
|
1506
|
-
const diffTables = (
|
|
1507
|
-
const
|
|
1508
|
-
const
|
|
1509
|
-
const
|
|
1509
|
+
const diffTables = (diffCtx) => {
|
|
1510
|
+
const { databaseTable } = diffCtx;
|
|
1511
|
+
const columnsDiff = diffTableColumns(diffCtx);
|
|
1512
|
+
const indexesDiff = diffTableIndexes(diffCtx);
|
|
1513
|
+
const foreignKeysDiff = diffTableForeignKeys(diffCtx);
|
|
1510
1514
|
const hasChanged = [columnsDiff, indexesDiff, foreignKeysDiff].some(hasChangedStatus);
|
|
1511
1515
|
return {
|
|
1512
1516
|
status: hasChanged ? statuses.CHANGED : statuses.UNCHANGED,
|
|
1513
1517
|
diff: {
|
|
1514
|
-
name:
|
|
1518
|
+
name: databaseTable.name,
|
|
1515
1519
|
indexes: indexesDiff.diff,
|
|
1516
1520
|
foreignKeys: foreignKeysDiff.diff,
|
|
1517
1521
|
columns: columnsDiff.diff
|
|
1518
1522
|
}
|
|
1519
1523
|
};
|
|
1520
1524
|
};
|
|
1521
|
-
const diffSchemas = async (
|
|
1525
|
+
const diffSchemas = async (schemaDiffCtx) => {
|
|
1526
|
+
const { previousSchema, databaseSchema, userSchema } = schemaDiffCtx;
|
|
1522
1527
|
const addedTables = [];
|
|
1523
1528
|
const updatedTables = [];
|
|
1524
1529
|
const unchangedTables = [];
|
|
1525
1530
|
const removedTables = [];
|
|
1526
|
-
for (const
|
|
1527
|
-
const
|
|
1528
|
-
|
|
1529
|
-
|
|
1531
|
+
for (const userSchemaTable of userSchema.tables) {
|
|
1532
|
+
const databaseTable = helpers.findTable(databaseSchema, userSchemaTable.name);
|
|
1533
|
+
const previousTable = previousSchema && helpers.findTable(previousSchema, userSchemaTable.name);
|
|
1534
|
+
if (databaseTable) {
|
|
1535
|
+
const { status, diff } = diffTables({
|
|
1536
|
+
previousTable,
|
|
1537
|
+
databaseTable,
|
|
1538
|
+
userSchemaTable
|
|
1539
|
+
});
|
|
1530
1540
|
if (status === statuses.CHANGED) {
|
|
1531
1541
|
updatedTables.push(diff);
|
|
1532
1542
|
} else {
|
|
1533
|
-
unchangedTables.push(
|
|
1543
|
+
unchangedTables.push(databaseTable);
|
|
1534
1544
|
}
|
|
1535
1545
|
} else {
|
|
1536
|
-
addedTables.push(
|
|
1546
|
+
addedTables.push(userSchemaTable);
|
|
1537
1547
|
}
|
|
1538
1548
|
}
|
|
1539
1549
|
const parsePersistedTable = (persistedTable) => {
|
|
@@ -1542,23 +1552,34 @@ const createSchemaDiff = (db) => {
|
|
|
1542
1552
|
}
|
|
1543
1553
|
return persistedTable.name;
|
|
1544
1554
|
};
|
|
1545
|
-
const persistedTables = helpers.hasTable(
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1555
|
+
const persistedTables = helpers.hasTable(databaseSchema, "strapi_core_store_settings") ? (
|
|
1556
|
+
// TODO: replace with low level db query instead
|
|
1557
|
+
await strapi.store.get({
|
|
1558
|
+
type: "core",
|
|
1559
|
+
key: "persisted_tables"
|
|
1560
|
+
}) ?? []
|
|
1561
|
+
) : [];
|
|
1549
1562
|
const reservedTables = [...RESERVED_TABLE_NAMES, ...persistedTables.map(parsePersistedTable)];
|
|
1550
|
-
for (const
|
|
1551
|
-
|
|
1563
|
+
for (const databaseTable of databaseSchema.tables) {
|
|
1564
|
+
const isInUserSchema = helpers.hasTable(userSchema, databaseTable.name);
|
|
1565
|
+
const wasTracked = previousSchema && helpers.hasTable(previousSchema, databaseTable.name);
|
|
1566
|
+
const isReserved = reservedTables.includes(databaseTable.name);
|
|
1567
|
+
if (!isInUserSchema && !wasTracked) {
|
|
1568
|
+
continue;
|
|
1569
|
+
}
|
|
1570
|
+
if (!isInUserSchema && wasTracked && !isReserved) {
|
|
1552
1571
|
const dependencies = persistedTables.filter((table) => {
|
|
1553
1572
|
const dependsOn = table?.dependsOn;
|
|
1554
1573
|
if (!___default.default.isArray(dependsOn)) {
|
|
1555
1574
|
return;
|
|
1556
1575
|
}
|
|
1557
|
-
return dependsOn.some((table2) => table2.name ===
|
|
1576
|
+
return dependsOn.some((table2) => table2.name === databaseTable.name);
|
|
1558
1577
|
}).map((dependsOnTable) => {
|
|
1559
|
-
return
|
|
1578
|
+
return databaseSchema.tables.find(
|
|
1579
|
+
(databaseTable2) => databaseTable2.name === dependsOnTable.name
|
|
1580
|
+
);
|
|
1560
1581
|
}).filter((table) => !___default.default.isNil(table));
|
|
1561
|
-
removedTables.push(
|
|
1582
|
+
removedTables.push(databaseTable, ...dependencies);
|
|
1562
1583
|
}
|
|
1563
1584
|
}
|
|
1564
1585
|
const hasChanged = [addedTables, updatedTables, removedTables].some((arr) => arr.length > 0);
|
|
@@ -2204,8 +2225,13 @@ const createSchemaProvider = (db) => {
|
|
|
2204
2225
|
},
|
|
2205
2226
|
async syncSchema() {
|
|
2206
2227
|
debug$1("Synchronizing database schema");
|
|
2207
|
-
const
|
|
2208
|
-
const
|
|
2228
|
+
const databaseSchema = await db.dialect.schemaInspector.getSchema();
|
|
2229
|
+
const storedSchema = await this.schemaStorage.read();
|
|
2230
|
+
const { status, diff } = await this.schemaDiff.diff({
|
|
2231
|
+
previousSchema: storedSchema?.schema,
|
|
2232
|
+
databaseSchema,
|
|
2233
|
+
userSchema: this.schema
|
|
2234
|
+
});
|
|
2209
2235
|
if (status === "CHANGED") {
|
|
2210
2236
|
await this.builder.updateSchema(diff);
|
|
2211
2237
|
}
|