@strapi/database 4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8 → 4.10.0-beta.0

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.
@@ -96,8 +96,15 @@ const createRepository = (uid, db) => {
96
96
  return db.entityManager.attachRelations(uid, id, data);
97
97
  },
98
98
 
99
- updateRelations(id, data) {
100
- return db.entityManager.updateRelations(uid, id, data);
99
+ async updateRelations(id, data) {
100
+ const trx = await db.transaction();
101
+ try {
102
+ await db.entityManager.updateRelations(uid, id, data, { transaction: trx.get() });
103
+ return trx.commit();
104
+ } catch (e) {
105
+ await trx.rollback();
106
+ throw e;
107
+ }
101
108
  },
102
109
 
103
110
  deleteRelations(id) {
@@ -14,9 +14,15 @@ describe('diffSchemas', () => {
14
14
  });
15
15
 
16
16
  diffSchemas = schemaDiff.diff.bind(schemaDiff);
17
+
18
+ global.strapi = {
19
+ store: {
20
+ get: () => [],
21
+ },
22
+ };
17
23
  });
18
24
 
19
- test('New Table', () => {
25
+ test('New Table', async () => {
20
26
  const testTable = {
21
27
  name: 'my_table',
22
28
  };
@@ -29,7 +35,7 @@ describe('diffSchemas', () => {
29
35
  tables: [testTable],
30
36
  };
31
37
 
32
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
38
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
33
39
  status: 'CHANGED',
34
40
  diff: {
35
41
  tables: {
@@ -42,7 +48,7 @@ describe('diffSchemas', () => {
42
48
  });
43
49
  });
44
50
 
45
- test('Removed Table', () => {
51
+ test('Removed Table', async () => {
46
52
  const testTable = {
47
53
  name: 'my_table',
48
54
  };
@@ -55,7 +61,7 @@ describe('diffSchemas', () => {
55
61
  tables: [],
56
62
  };
57
63
 
58
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
64
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
59
65
  status: 'CHANGED',
60
66
  diff: {
61
67
  tables: {
@@ -68,7 +74,7 @@ describe('diffSchemas', () => {
68
74
  });
69
75
  });
70
76
 
71
- test('Unchanged Table', () => {
77
+ test('Unchanged Table', async () => {
72
78
  const testTable = {
73
79
  name: 'my_table',
74
80
  columns: [],
@@ -84,7 +90,7 @@ describe('diffSchemas', () => {
84
90
  tables: [testTable],
85
91
  };
86
92
 
87
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
93
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
88
94
  status: 'UNCHANGED',
89
95
  diff: {
90
96
  tables: {
@@ -98,7 +104,7 @@ describe('diffSchemas', () => {
98
104
  });
99
105
 
100
106
  describe('Changed table', () => {
101
- test('added column', () => {
107
+ test('added column', async () => {
102
108
  const srcSchema = {
103
109
  tables: [
104
110
  {
@@ -125,7 +131,7 @@ describe('diffSchemas', () => {
125
131
  ],
126
132
  };
127
133
 
128
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
134
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
129
135
  status: 'CHANGED',
130
136
  diff: {
131
137
  tables: {
@@ -178,4 +184,48 @@ describe('diffSchemas', () => {
178
184
  test.todo('unchanged foreign key');
179
185
  test.todo('removed foreign key');
180
186
  });
187
+
188
+ test('With persisted DB tables', async () => {
189
+ const testTables = [
190
+ {
191
+ name: 'my_table',
192
+ },
193
+ {
194
+ name: 'my_table_1',
195
+ },
196
+ ];
197
+
198
+ const coreStoreTable = {
199
+ name: 'strapi_core_store_settings',
200
+ columns: [],
201
+ indexes: [],
202
+ foreignKeys: [],
203
+ };
204
+
205
+ global.strapi = {
206
+ store: {
207
+ get: async () => [testTables[0].name, 'table2'],
208
+ },
209
+ };
210
+
211
+ const srcSchema = {
212
+ tables: [...testTables, coreStoreTable],
213
+ };
214
+
215
+ const destSchema = {
216
+ tables: [coreStoreTable],
217
+ };
218
+
219
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
220
+ status: 'CHANGED',
221
+ diff: {
222
+ tables: {
223
+ added: [],
224
+ updated: [],
225
+ unchanged: [coreStoreTable],
226
+ removed: [testTables[1]],
227
+ },
228
+ },
229
+ });
230
+ });
181
231
  });
@@ -322,7 +322,7 @@ module.exports = (db) => {
322
322
  };
323
323
  };
324
324
 
325
- const diffSchemas = (srcSchema, destSchema) => {
325
+ const diffSchemas = async (srcSchema, destSchema) => {
326
326
  const addedTables = [];
327
327
  const updatedTables = [];
328
328
  const unchangedTables = [];
@@ -344,10 +344,17 @@ module.exports = (db) => {
344
344
  }
345
345
  }
346
346
 
347
+ const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings')
348
+ ? (await strapi.store.get({
349
+ type: 'core',
350
+ key: 'persisted_tables',
351
+ })) ?? []
352
+ : [];
353
+
347
354
  for (const srcTable of srcSchema.tables) {
348
355
  if (
349
356
  !helpers.hasTable(destSchema, srcTable.name) &&
350
- !RESERVED_TABLE_NAMES.includes(srcTable.name)
357
+ ![...RESERVED_TABLE_NAMES, ...persistedTables].includes(srcTable.name)
351
358
  ) {
352
359
  removedTables.push(srcTable);
353
360
  }
@@ -50,7 +50,7 @@ const createSchemaProvider = (db) => {
50
50
 
51
51
  const DBSchema = await db.dialect.schemaInspector.getSchema();
52
52
 
53
- const { status, diff } = this.schemaDiff.diff(DBSchema, schema);
53
+ const { status, diff } = await this.schemaDiff.diff(DBSchema, schema);
54
54
 
55
55
  if (status === 'CHANGED') {
56
56
  await this.builder.updateSchema(diff);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8",
3
+ "version": "4.10.0-beta.0",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -43,5 +43,5 @@
43
43
  "node": ">=14.19.1 <=18.x.x",
44
44
  "npm": ">=6.0.0"
45
45
  },
46
- "gitHead": "366eb8a0d0f06935914854c6d9c4b3fe859468e0"
46
+ "gitHead": "1519ef0e56d27b738f24fc88223797651ad47aaf"
47
47
  }