@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.mjs
CHANGED
|
@@ -1356,27 +1356,28 @@ const createSchemaDiff = (db) => {
|
|
|
1356
1356
|
}
|
|
1357
1357
|
};
|
|
1358
1358
|
};
|
|
1359
|
-
const diffTableColumns = (
|
|
1359
|
+
const diffTableColumns = (diffCtx) => {
|
|
1360
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1360
1361
|
const addedColumns = [];
|
|
1361
1362
|
const updatedColumns = [];
|
|
1362
1363
|
const unchangedColumns = [];
|
|
1363
1364
|
const removedColumns = [];
|
|
1364
|
-
for (const
|
|
1365
|
-
const
|
|
1366
|
-
if (
|
|
1367
|
-
const { status, diff } = diffColumns(
|
|
1365
|
+
for (const userSchemaColumn of userSchemaTable.columns) {
|
|
1366
|
+
const databaseColumn = helpers.findColumn(databaseTable, userSchemaColumn.name);
|
|
1367
|
+
if (databaseColumn) {
|
|
1368
|
+
const { status, diff } = diffColumns(databaseColumn, userSchemaColumn);
|
|
1368
1369
|
if (status === statuses.CHANGED) {
|
|
1369
1370
|
updatedColumns.push(diff);
|
|
1370
1371
|
} else {
|
|
1371
|
-
unchangedColumns.push(
|
|
1372
|
+
unchangedColumns.push(databaseColumn);
|
|
1372
1373
|
}
|
|
1373
1374
|
} else {
|
|
1374
|
-
addedColumns.push(
|
|
1375
|
+
addedColumns.push(userSchemaColumn);
|
|
1375
1376
|
}
|
|
1376
1377
|
}
|
|
1377
|
-
for (const
|
|
1378
|
-
if (!helpers.hasColumn(
|
|
1379
|
-
removedColumns.push(
|
|
1378
|
+
for (const databaseColumn of databaseTable.columns) {
|
|
1379
|
+
if (!helpers.hasColumn(userSchemaTable, databaseColumn.name) && previousTable && helpers.hasColumn(previousTable, databaseColumn.name)) {
|
|
1380
|
+
removedColumns.push(databaseColumn);
|
|
1380
1381
|
}
|
|
1381
1382
|
}
|
|
1382
1383
|
const hasChanged = [addedColumns, updatedColumns, removedColumns].some((arr) => arr.length > 0);
|
|
@@ -1390,27 +1391,28 @@ const createSchemaDiff = (db) => {
|
|
|
1390
1391
|
}
|
|
1391
1392
|
};
|
|
1392
1393
|
};
|
|
1393
|
-
const diffTableIndexes = (
|
|
1394
|
+
const diffTableIndexes = (diffCtx) => {
|
|
1395
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1394
1396
|
const addedIndexes = [];
|
|
1395
1397
|
const updatedIndexes = [];
|
|
1396
1398
|
const unchangedIndexes = [];
|
|
1397
1399
|
const removedIndexes = [];
|
|
1398
|
-
for (const
|
|
1399
|
-
const
|
|
1400
|
-
if (
|
|
1401
|
-
const { status, diff } = diffIndexes(
|
|
1400
|
+
for (const userSchemaIndex of userSchemaTable.indexes) {
|
|
1401
|
+
const databaseIndex = helpers.findIndex(databaseTable, userSchemaIndex.name);
|
|
1402
|
+
if (databaseIndex) {
|
|
1403
|
+
const { status, diff } = diffIndexes(databaseIndex, userSchemaIndex);
|
|
1402
1404
|
if (status === statuses.CHANGED) {
|
|
1403
1405
|
updatedIndexes.push(diff);
|
|
1404
1406
|
} else {
|
|
1405
|
-
unchangedIndexes.push(
|
|
1407
|
+
unchangedIndexes.push(databaseIndex);
|
|
1406
1408
|
}
|
|
1407
1409
|
} else {
|
|
1408
|
-
addedIndexes.push(
|
|
1410
|
+
addedIndexes.push(userSchemaIndex);
|
|
1409
1411
|
}
|
|
1410
1412
|
}
|
|
1411
|
-
for (const
|
|
1412
|
-
if (!helpers.hasIndex(
|
|
1413
|
-
removedIndexes.push(
|
|
1413
|
+
for (const databaseIndex of databaseTable.indexes) {
|
|
1414
|
+
if (!helpers.hasIndex(userSchemaTable, databaseIndex.name) && previousTable && helpers.hasIndex(previousTable, databaseIndex.name)) {
|
|
1415
|
+
removedIndexes.push(databaseIndex);
|
|
1414
1416
|
}
|
|
1415
1417
|
}
|
|
1416
1418
|
const hasChanged = [addedIndexes, updatedIndexes, removedIndexes].some((arr) => arr.length > 0);
|
|
@@ -1424,7 +1426,8 @@ const createSchemaDiff = (db) => {
|
|
|
1424
1426
|
}
|
|
1425
1427
|
};
|
|
1426
1428
|
};
|
|
1427
|
-
const diffTableForeignKeys = (
|
|
1429
|
+
const diffTableForeignKeys = (diffCtx) => {
|
|
1430
|
+
const { databaseTable, userSchemaTable, previousTable } = diffCtx;
|
|
1428
1431
|
const addedForeignKeys = [];
|
|
1429
1432
|
const updatedForeignKeys = [];
|
|
1430
1433
|
const unchangedForeignKeys = [];
|
|
@@ -1440,22 +1443,22 @@ const createSchemaDiff = (db) => {
|
|
|
1440
1443
|
}
|
|
1441
1444
|
};
|
|
1442
1445
|
}
|
|
1443
|
-
for (const
|
|
1444
|
-
const
|
|
1445
|
-
if (
|
|
1446
|
-
const { status, diff } = diffForeignKeys(
|
|
1446
|
+
for (const userSchemaForeignKeys of userSchemaTable.foreignKeys) {
|
|
1447
|
+
const databaseForeignKeys = helpers.findForeignKey(databaseTable, userSchemaForeignKeys.name);
|
|
1448
|
+
if (databaseForeignKeys) {
|
|
1449
|
+
const { status, diff } = diffForeignKeys(databaseForeignKeys, userSchemaForeignKeys);
|
|
1447
1450
|
if (status === statuses.CHANGED) {
|
|
1448
1451
|
updatedForeignKeys.push(diff);
|
|
1449
1452
|
} else {
|
|
1450
|
-
unchangedForeignKeys.push(
|
|
1453
|
+
unchangedForeignKeys.push(databaseForeignKeys);
|
|
1451
1454
|
}
|
|
1452
1455
|
} else {
|
|
1453
|
-
addedForeignKeys.push(
|
|
1456
|
+
addedForeignKeys.push(userSchemaForeignKeys);
|
|
1454
1457
|
}
|
|
1455
1458
|
}
|
|
1456
|
-
for (const
|
|
1457
|
-
if (!helpers.hasForeignKey(
|
|
1458
|
-
removedForeignKeys.push(
|
|
1459
|
+
for (const databaseForeignKeys of databaseTable.foreignKeys) {
|
|
1460
|
+
if (!helpers.hasForeignKey(userSchemaTable, databaseForeignKeys.name) && previousTable && helpers.hasForeignKey(previousTable, databaseForeignKeys.name)) {
|
|
1461
|
+
removedForeignKeys.push(databaseForeignKeys);
|
|
1459
1462
|
}
|
|
1460
1463
|
}
|
|
1461
1464
|
const hasChanged = [addedForeignKeys, updatedForeignKeys, removedForeignKeys].some(
|
|
@@ -1471,37 +1474,44 @@ const createSchemaDiff = (db) => {
|
|
|
1471
1474
|
}
|
|
1472
1475
|
};
|
|
1473
1476
|
};
|
|
1474
|
-
const diffTables = (
|
|
1475
|
-
const
|
|
1476
|
-
const
|
|
1477
|
-
const
|
|
1477
|
+
const diffTables = (diffCtx) => {
|
|
1478
|
+
const { databaseTable } = diffCtx;
|
|
1479
|
+
const columnsDiff = diffTableColumns(diffCtx);
|
|
1480
|
+
const indexesDiff = diffTableIndexes(diffCtx);
|
|
1481
|
+
const foreignKeysDiff = diffTableForeignKeys(diffCtx);
|
|
1478
1482
|
const hasChanged = [columnsDiff, indexesDiff, foreignKeysDiff].some(hasChangedStatus);
|
|
1479
1483
|
return {
|
|
1480
1484
|
status: hasChanged ? statuses.CHANGED : statuses.UNCHANGED,
|
|
1481
1485
|
diff: {
|
|
1482
|
-
name:
|
|
1486
|
+
name: databaseTable.name,
|
|
1483
1487
|
indexes: indexesDiff.diff,
|
|
1484
1488
|
foreignKeys: foreignKeysDiff.diff,
|
|
1485
1489
|
columns: columnsDiff.diff
|
|
1486
1490
|
}
|
|
1487
1491
|
};
|
|
1488
1492
|
};
|
|
1489
|
-
const diffSchemas = async (
|
|
1493
|
+
const diffSchemas = async (schemaDiffCtx) => {
|
|
1494
|
+
const { previousSchema, databaseSchema, userSchema } = schemaDiffCtx;
|
|
1490
1495
|
const addedTables = [];
|
|
1491
1496
|
const updatedTables = [];
|
|
1492
1497
|
const unchangedTables = [];
|
|
1493
1498
|
const removedTables = [];
|
|
1494
|
-
for (const
|
|
1495
|
-
const
|
|
1496
|
-
|
|
1497
|
-
|
|
1499
|
+
for (const userSchemaTable of userSchema.tables) {
|
|
1500
|
+
const databaseTable = helpers.findTable(databaseSchema, userSchemaTable.name);
|
|
1501
|
+
const previousTable = previousSchema && helpers.findTable(previousSchema, userSchemaTable.name);
|
|
1502
|
+
if (databaseTable) {
|
|
1503
|
+
const { status, diff } = diffTables({
|
|
1504
|
+
previousTable,
|
|
1505
|
+
databaseTable,
|
|
1506
|
+
userSchemaTable
|
|
1507
|
+
});
|
|
1498
1508
|
if (status === statuses.CHANGED) {
|
|
1499
1509
|
updatedTables.push(diff);
|
|
1500
1510
|
} else {
|
|
1501
|
-
unchangedTables.push(
|
|
1511
|
+
unchangedTables.push(databaseTable);
|
|
1502
1512
|
}
|
|
1503
1513
|
} else {
|
|
1504
|
-
addedTables.push(
|
|
1514
|
+
addedTables.push(userSchemaTable);
|
|
1505
1515
|
}
|
|
1506
1516
|
}
|
|
1507
1517
|
const parsePersistedTable = (persistedTable) => {
|
|
@@ -1510,23 +1520,34 @@ const createSchemaDiff = (db) => {
|
|
|
1510
1520
|
}
|
|
1511
1521
|
return persistedTable.name;
|
|
1512
1522
|
};
|
|
1513
|
-
const persistedTables = helpers.hasTable(
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1523
|
+
const persistedTables = helpers.hasTable(databaseSchema, "strapi_core_store_settings") ? (
|
|
1524
|
+
// TODO: replace with low level db query instead
|
|
1525
|
+
await strapi.store.get({
|
|
1526
|
+
type: "core",
|
|
1527
|
+
key: "persisted_tables"
|
|
1528
|
+
}) ?? []
|
|
1529
|
+
) : [];
|
|
1517
1530
|
const reservedTables = [...RESERVED_TABLE_NAMES, ...persistedTables.map(parsePersistedTable)];
|
|
1518
|
-
for (const
|
|
1519
|
-
|
|
1531
|
+
for (const databaseTable of databaseSchema.tables) {
|
|
1532
|
+
const isInUserSchema = helpers.hasTable(userSchema, databaseTable.name);
|
|
1533
|
+
const wasTracked = previousSchema && helpers.hasTable(previousSchema, databaseTable.name);
|
|
1534
|
+
const isReserved = reservedTables.includes(databaseTable.name);
|
|
1535
|
+
if (!isInUserSchema && !wasTracked) {
|
|
1536
|
+
continue;
|
|
1537
|
+
}
|
|
1538
|
+
if (!isInUserSchema && wasTracked && !isReserved) {
|
|
1520
1539
|
const dependencies = persistedTables.filter((table) => {
|
|
1521
1540
|
const dependsOn = table?.dependsOn;
|
|
1522
1541
|
if (!_.isArray(dependsOn)) {
|
|
1523
1542
|
return;
|
|
1524
1543
|
}
|
|
1525
|
-
return dependsOn.some((table2) => table2.name ===
|
|
1544
|
+
return dependsOn.some((table2) => table2.name === databaseTable.name);
|
|
1526
1545
|
}).map((dependsOnTable) => {
|
|
1527
|
-
return
|
|
1546
|
+
return databaseSchema.tables.find(
|
|
1547
|
+
(databaseTable2) => databaseTable2.name === dependsOnTable.name
|
|
1548
|
+
);
|
|
1528
1549
|
}).filter((table) => !_.isNil(table));
|
|
1529
|
-
removedTables.push(
|
|
1550
|
+
removedTables.push(databaseTable, ...dependencies);
|
|
1530
1551
|
}
|
|
1531
1552
|
}
|
|
1532
1553
|
const hasChanged = [addedTables, updatedTables, removedTables].some((arr) => arr.length > 0);
|
|
@@ -2172,8 +2193,13 @@ const createSchemaProvider = (db) => {
|
|
|
2172
2193
|
},
|
|
2173
2194
|
async syncSchema() {
|
|
2174
2195
|
debug$1("Synchronizing database schema");
|
|
2175
|
-
const
|
|
2176
|
-
const
|
|
2196
|
+
const databaseSchema = await db.dialect.schemaInspector.getSchema();
|
|
2197
|
+
const storedSchema = await this.schemaStorage.read();
|
|
2198
|
+
const { status, diff } = await this.schemaDiff.diff({
|
|
2199
|
+
previousSchema: storedSchema?.schema,
|
|
2200
|
+
databaseSchema,
|
|
2201
|
+
userSchema: this.schema
|
|
2202
|
+
});
|
|
2177
2203
|
if (status === "CHANGED") {
|
|
2178
2204
|
await this.builder.updateSchema(diff);
|
|
2179
2205
|
}
|