From 2b9f7fd7cd7339708e2763da3af73fc51526400c Mon Sep 17 00:00:00 2001 From: Georgii Petrov Date: Fri, 22 Mar 2024 20:44:49 +0300 Subject: [PATCH] [feature] Compability check of changes table added --- Common/sources/constants.js | 4 +- DocService/sources/DocsCoServer.js | 64 ++++++++++++------- .../databaseTests/baseConnector.tests.js | 28 +------- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/Common/sources/constants.js b/Common/sources/constants.js index 56767287..c766513d 100644 --- a/Common/sources/constants.js +++ b/Common/sources/constants.js @@ -294,7 +294,7 @@ exports.SUPPORTED_TEMPLATES_EXTENSIONS = { 'Excel': ['xlsx'], 'PowerPoint': ['pptx'] }; -exports.TASK_RESULT_SCHEMA = [ +exports.TABLE_RESULT_SCHEMA = [ 'tenant', 'id', 'status', @@ -308,7 +308,7 @@ exports.TASK_RESULT_SCHEMA = [ 'password', 'additional' ]; -exports.DOC_CHANGES_SCHEMA = [ +exports.TABLE_CHANGES_SCHEMA = [ 'tenant', 'id', 'change_id', diff --git a/DocService/sources/DocsCoServer.js b/DocService/sources/DocsCoServer.js index 00b93988..afb8f923 100644 --- a/DocService/sources/DocsCoServer.js +++ b/DocService/sources/DocsCoServer.js @@ -144,6 +144,7 @@ const cfgExpDocumentsCron = config.get('services.CoAuthoring.expire.documentsCro const cfgRefreshLockInterval = ms(config.get('wopi.refreshLockInterval')); const cfgSocketIoConnection = config.get('services.CoAuthoring.socketio.connection'); const cfgTableResult = config.get('services.CoAuthoring.sql.tableResult'); +const cfgTableChanges = config.get('services.CoAuthoring.sql.tableChanges'); const EditorTypes = { document : 0, @@ -1476,6 +1477,26 @@ function getOpenFormatByEditor(editorType) { return res; } +async function isSchemaCompatible([tableName, tableSchema]) { + const resultSchema = await sqlBase.getTableColumns(operationContext.global, tableName); + + if (resultSchema.length === 0) { + operationContext.global.logger.error('DB table "%s" does not exist', tableName); + return false; + } + + const columnArray = resultSchema.map(row => row['column_name']); + const hashedResult = new Set(columnArray); + const schemaDiff = tableSchema.filter(column => !hashedResult.has(column)); + + if (schemaDiff.length > 0) { + operationContext.global.logger.error(`DB table "${tableName}" does not contain columns: ${schemaDiff}, columns info: ${columnArray}`); + return false; + } + + return true; +} + exports.c_oAscServerStatus = c_oAscServerStatus; exports.editorData = editorData; exports.sendData = sendData; @@ -3935,31 +3956,28 @@ exports.install = function(server, callbackFunction) { gc.startGC(); //check data base compatibility - sqlBase.getTableColumns(operationContext.global, cfgTableResult).then(function(result) { - if (result.length === 0) { - operationContext.global.logger.error('DB table "%s" does not exist', cfgTableResult); - return; - } + const tables = [ + [cfgTableResult, constants.TABLE_RESULT_SCHEMA], + [cfgTableChanges, constants.TABLE_CHANGES_SCHEMA] + ]; + const requestPromises = tables.map(table => isSchemaCompatible(table)); - const columnArray = result.map(row => row['column_name']); - const hashedResult = new Set(columnArray); - const schemaDiff = constants.TASK_RESULT_SCHEMA.filter(column => !hashedResult.has(column)); + Promise.all(requestPromises).then( + checkResult => { + if (checkResult.includes(false)) { + return; + } - if (schemaDiff.length > 0) { - operationContext.global.logger.error(`DB table "${cfgTableResult}" does not contain columns: ${schemaDiff}, columns info: ${columnArray}`); - return; - } - - return editorData.connect().then( - () => { - callbackFunction(); - isServerStartedUp = true; - }, - error => operationContext.global.logger.error('editorData error: %s', error.stack) - ); - }).catch(err => { - operationContext.global.logger.error('getTableColumns error: %s', err.stack); - }); + editorData.connect().then( + () => { + callbackFunction(); + isServerStartedUp = true; + }, + error => operationContext.global.logger.error('editorData error: %s', error.stack) + ); + }, + error => operationContext.global.logger.error('getTableColumns error: %s', error.stack) + ); }); }); }; diff --git a/tests/integration/databaseTests/baseConnector.tests.js b/tests/integration/databaseTests/baseConnector.tests.js index 08ba5c06..5b8ae1d7 100644 --- a/tests/integration/databaseTests/baseConnector.tests.js +++ b/tests/integration/databaseTests/baseConnector.tests.js @@ -218,30 +218,8 @@ describe('Base database connector', function () { describe('DB tables existence', function () { const tables = { - [cfgTableResult]: [ - { column_name: 'tenant' }, - { column_name: 'id' }, - { column_name: 'status' }, - { column_name: 'status_info' }, - { column_name: 'created_at' }, - { column_name: 'last_open_date' }, - { column_name: 'user_index' }, - { column_name: 'change_id' }, - { column_name: 'callback' }, - { column_name: 'baseurl' }, - { column_name: 'password' }, - { column_name: 'additional' } - ], - [cfgTableChanges]: [ - { column_name: 'tenant' }, - { column_name: 'id' }, - { column_name: 'change_id' }, - { column_name: 'user_id' }, - { column_name: 'user_id_original' }, - { column_name: 'user_name' }, - { column_name: 'change_data' }, - { column_name: 'change_date' } - ] + [cfgTableResult]: constants.TABLE_RESULT_SCHEMA.map(column => { return { column_name: column } }), + [cfgTableChanges]: constants.TABLE_CHANGES_SCHEMA.map(column => { return { column_name: column } }) }; for (const table in tables) { @@ -437,4 +415,4 @@ describe('Base database connector', function () { expect(updatedRow).toEqual(expectedUrlChanges); }); }); -}); \ No newline at end of file +});