mirror of
https://github.com/strapi/strapi.git
synced 2025-09-09 16:47:06 +00:00
Merge pull request #16084 from strapi/feature/persist-ee-tables
This commit is contained in:
commit
cd29d8ba6b
3
packages/core/admin/ee/server/bootstrap.js
vendored
3
packages/core/admin/ee/server/bootstrap.js
vendored
@ -5,6 +5,7 @@ const { features } = require('@strapi/strapi/lib/utils/ee');
|
|||||||
const executeCEBootstrap = require('../../server/bootstrap');
|
const executeCEBootstrap = require('../../server/bootstrap');
|
||||||
const { getService } = require('../../server/utils');
|
const { getService } = require('../../server/utils');
|
||||||
const actions = require('./config/admin-actions');
|
const actions = require('./config/admin-actions');
|
||||||
|
const { persistTablesWithPrefix } = require('./utils/persisted-tables');
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async () => {
|
||||||
const { actionProvider } = getService('permission');
|
const { actionProvider } = getService('permission');
|
||||||
@ -14,6 +15,8 @@ module.exports = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (features.isEnabled('audit-logs')) {
|
if (features.isEnabled('audit-logs')) {
|
||||||
|
await persistTablesWithPrefix('strapi_audit_logs');
|
||||||
|
|
||||||
await actionProvider.registerMany(actions.auditLogs);
|
await actionProvider.registerMany(actions.auditLogs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
49
packages/core/admin/ee/server/utils/persisted-tables.js
Normal file
49
packages/core/admin/ee/server/utils/persisted-tables.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all tables in the database that start with a prefix
|
||||||
|
* @param {string} prefix
|
||||||
|
* @returns {Array}
|
||||||
|
*/
|
||||||
|
const findTablesThatStartWithPrefix = async (prefix) => {
|
||||||
|
const tables = await strapi.db.dialect.schemaInspector.getTables();
|
||||||
|
return tables.filter((tableName) => tableName.startsWith(prefix));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all reserved table names from the core store
|
||||||
|
* @returns {Array}
|
||||||
|
*/
|
||||||
|
const getPersistedTables = async () =>
|
||||||
|
(await strapi.store.get({
|
||||||
|
type: 'core',
|
||||||
|
key: 'persisted_tables',
|
||||||
|
})) ?? [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add all table names that start with a prefix to the reserved tables in
|
||||||
|
* core store
|
||||||
|
* @param {string} tableNamePrefix
|
||||||
|
*/
|
||||||
|
|
||||||
|
const persistTablesWithPrefix = async (tableNamePrefix) => {
|
||||||
|
const persistedTables = await getPersistedTables();
|
||||||
|
const tableNames = await findTablesThatStartWithPrefix(tableNamePrefix);
|
||||||
|
const notReservedTableNames = tableNames.filter((name) => !persistedTables.includes(name));
|
||||||
|
|
||||||
|
if (!notReservedTableNames.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
persistedTables.push(...notReservedTableNames);
|
||||||
|
await strapi.store.set({
|
||||||
|
type: 'core',
|
||||||
|
key: 'persisted_tables',
|
||||||
|
value: persistedTables,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
persistTablesWithPrefix,
|
||||||
|
findTablesThatStartWithPrefix,
|
||||||
|
};
|
@ -14,9 +14,15 @@ describe('diffSchemas', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
diffSchemas = schemaDiff.diff.bind(schemaDiff);
|
diffSchemas = schemaDiff.diff.bind(schemaDiff);
|
||||||
|
|
||||||
|
global.strapi = {
|
||||||
|
store: {
|
||||||
|
get: () => [],
|
||||||
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
test('New Table', () => {
|
test('New Table', async () => {
|
||||||
const testTable = {
|
const testTable = {
|
||||||
name: 'my_table',
|
name: 'my_table',
|
||||||
};
|
};
|
||||||
@ -29,7 +35,7 @@ describe('diffSchemas', () => {
|
|||||||
tables: [testTable],
|
tables: [testTable],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
||||||
status: 'CHANGED',
|
status: 'CHANGED',
|
||||||
diff: {
|
diff: {
|
||||||
tables: {
|
tables: {
|
||||||
@ -42,7 +48,7 @@ describe('diffSchemas', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Removed Table', () => {
|
test('Removed Table', async () => {
|
||||||
const testTable = {
|
const testTable = {
|
||||||
name: 'my_table',
|
name: 'my_table',
|
||||||
};
|
};
|
||||||
@ -55,7 +61,7 @@ describe('diffSchemas', () => {
|
|||||||
tables: [],
|
tables: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
||||||
status: 'CHANGED',
|
status: 'CHANGED',
|
||||||
diff: {
|
diff: {
|
||||||
tables: {
|
tables: {
|
||||||
@ -68,7 +74,7 @@ describe('diffSchemas', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Unchanged Table', () => {
|
test('Unchanged Table', async () => {
|
||||||
const testTable = {
|
const testTable = {
|
||||||
name: 'my_table',
|
name: 'my_table',
|
||||||
columns: [],
|
columns: [],
|
||||||
@ -84,7 +90,7 @@ describe('diffSchemas', () => {
|
|||||||
tables: [testTable],
|
tables: [testTable],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
||||||
status: 'UNCHANGED',
|
status: 'UNCHANGED',
|
||||||
diff: {
|
diff: {
|
||||||
tables: {
|
tables: {
|
||||||
@ -98,7 +104,7 @@ describe('diffSchemas', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Changed table', () => {
|
describe('Changed table', () => {
|
||||||
test('added column', () => {
|
test('added column', async () => {
|
||||||
const srcSchema = {
|
const srcSchema = {
|
||||||
tables: [
|
tables: [
|
||||||
{
|
{
|
||||||
@ -125,7 +131,7 @@ describe('diffSchemas', () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
||||||
status: 'CHANGED',
|
status: 'CHANGED',
|
||||||
diff: {
|
diff: {
|
||||||
tables: {
|
tables: {
|
||||||
@ -178,4 +184,48 @@ describe('diffSchemas', () => {
|
|||||||
test.todo('unchanged foreign key');
|
test.todo('unchanged foreign key');
|
||||||
test.todo('removed foreign key');
|
test.todo('removed foreign key');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('With persisted DB tables', async () => {
|
||||||
|
const testTables = [
|
||||||
|
{
|
||||||
|
name: 'my_table',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'my_table_1',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const coreStoreTable = {
|
||||||
|
name: 'strapi_core_store_settings',
|
||||||
|
columns: [],
|
||||||
|
indexes: [],
|
||||||
|
foreignKeys: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
global.strapi = {
|
||||||
|
store: {
|
||||||
|
get: async () => [testTables[0].name, 'table2'],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const srcSchema = {
|
||||||
|
tables: [...testTables, coreStoreTable],
|
||||||
|
};
|
||||||
|
|
||||||
|
const destSchema = {
|
||||||
|
tables: [coreStoreTable],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
|
||||||
|
status: 'CHANGED',
|
||||||
|
diff: {
|
||||||
|
tables: {
|
||||||
|
added: [],
|
||||||
|
updated: [],
|
||||||
|
unchanged: [coreStoreTable],
|
||||||
|
removed: [testTables[1]],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -322,7 +322,7 @@ module.exports = (db) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const diffSchemas = (srcSchema, destSchema) => {
|
const diffSchemas = async (srcSchema, destSchema) => {
|
||||||
const addedTables = [];
|
const addedTables = [];
|
||||||
const updatedTables = [];
|
const updatedTables = [];
|
||||||
const unchangedTables = [];
|
const unchangedTables = [];
|
||||||
@ -344,10 +344,17 @@ module.exports = (db) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings')
|
||||||
|
? (await strapi.store.get({
|
||||||
|
type: 'core',
|
||||||
|
key: 'persisted_tables',
|
||||||
|
})) ?? []
|
||||||
|
: [];
|
||||||
|
|
||||||
for (const srcTable of srcSchema.tables) {
|
for (const srcTable of srcSchema.tables) {
|
||||||
if (
|
if (
|
||||||
!helpers.hasTable(destSchema, srcTable.name) &&
|
!helpers.hasTable(destSchema, srcTable.name) &&
|
||||||
!RESERVED_TABLE_NAMES.includes(srcTable.name)
|
![...RESERVED_TABLE_NAMES, ...persistedTables].includes(srcTable.name)
|
||||||
) {
|
) {
|
||||||
removedTables.push(srcTable);
|
removedTables.push(srcTable);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ const createSchemaProvider = (db) => {
|
|||||||
|
|
||||||
const DBSchema = await db.dialect.schemaInspector.getSchema();
|
const DBSchema = await db.dialect.schemaInspector.getSchema();
|
||||||
|
|
||||||
const { status, diff } = this.schemaDiff.diff(DBSchema, schema);
|
const { status, diff } = await this.schemaDiff.diff(DBSchema, schema);
|
||||||
|
|
||||||
if (status === 'CHANGED') {
|
if (status === 'CHANGED') {
|
||||||
await this.builder.updateSchema(diff);
|
await this.builder.updateSchema(diff);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user