From 8037d7abcf5d20a57d0f99c06b0f01f70b657a9b Mon Sep 17 00:00:00 2001 From: AliGebily Date: Tue, 14 Apr 2020 17:31:05 +0200 Subject: [PATCH 1/2] update database sequencers, after importing, to be set to max(autoincrement column) from table, so that when next insertions don't provide value of autoincrement column, as in case of using APIs, it should be set automatically based on lastvalue of sequencers. --- scripts/data/import/importData.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/scripts/data/import/importData.js b/scripts/data/import/importData.js index 9958c3b5..cfb471e6 100644 --- a/scripts/data/import/importData.js +++ b/scripts/data/import/importData.js @@ -21,14 +21,32 @@ async function writeDataToDatabase(filePath, logger) { for (let index = 0; index < dataModels.length; index += 1) { const modelName = dataModels[index]; currentModelName = modelName; + const model = models[modelName]; const modelRecords = jsonData[modelName]; if (modelRecords && modelRecords.length > 0) { - await models[modelName].bulkCreate(modelRecords, { + logger.info( + `Importing data for model: ${modelName}`, + ); + await model.bulkCreate(modelRecords, { transaction, }); logger.info( - `Records to save for model: ${modelName} = ${modelRecords.length}`, + `Records imported for model: ${modelName} = ${modelRecords.length}`, ); + const modelAttributes = Object.keys(model.rawAttributes); + const tableName = model.getTableName(); + /* eslint-disable no-await-in-loop */ + for (let attributeIndex = 0; attributeIndex < modelAttributes.length; attributeIndex += 1) { + const field = modelAttributes[attributeIndex]; + const fieldInfo = model.rawAttributes[field]; + if (fieldInfo.autoIncrement) { + const query = `SELECT setval('${tableName}_${field}_seq', (SELECT MAX(${field}) FROM ${tableName}))`; + const setValue = (await models.sequelize.query(query, { + transaction, + }))[0][0].setval; + logger.info(`Updated autoIncrement for ${modelName}.${field} with max value = ${setValue}`); + } + } } else { logger.info(`No records to save for model: ${modelName}`); } From f04409398ee025a022d0de8eadc4c438ba12d907 Mon Sep 17 00:00:00 2001 From: AliGebily Date: Thu, 16 Apr 2020 18:04:30 +0200 Subject: [PATCH 2/2] Identify sequencer name by querying it from database using table name and automincrement column --- scripts/data/import/importData.js | 39 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/scripts/data/import/importData.js b/scripts/data/import/importData.js index cfb471e6..593ae74e 100644 --- a/scripts/data/import/importData.js +++ b/scripts/data/import/importData.js @@ -24,29 +24,48 @@ async function writeDataToDatabase(filePath, logger) { const model = models[modelName]; const modelRecords = jsonData[modelName]; if (modelRecords && modelRecords.length > 0) { - logger.info( - `Importing data for model: ${modelName}`, - ); + logger.info(`Importing data for model: ${modelName}`); await model.bulkCreate(modelRecords, { transaction, }); logger.info( `Records imported for model: ${modelName} = ${modelRecords.length}`, ); + + // Set autoincrement sequencers in the database to be set to max of the autoincrement column, + // so that, when next insertions don't provide value of autoincrement column, as in case of using APIs, + // it should be set automatically based on last value of sequencers. const modelAttributes = Object.keys(model.rawAttributes); const tableName = model.getTableName(); /* eslint-disable no-await-in-loop */ - for (let attributeIndex = 0; attributeIndex < modelAttributes.length; attributeIndex += 1) { + for ( + let attributeIndex = 0; + attributeIndex < modelAttributes.length; + attributeIndex += 1 + ) { const field = modelAttributes[attributeIndex]; const fieldInfo = model.rawAttributes[field]; if (fieldInfo.autoIncrement) { - const query = `SELECT setval('${tableName}_${field}_seq', (SELECT MAX(${field}) FROM ${tableName}))`; - const setValue = (await models.sequelize.query(query, { - transaction, - }))[0][0].setval; - logger.info(`Updated autoIncrement for ${modelName}.${field} with max value = ${setValue}`); + // Get sequence name corresponding to automincrment column in a table + const selectSequenceQuery = `SELECT pg_get_serial_sequence('${tableName}', '${field}')`; + const sequenceName = ( + await models.sequelize.query(selectSequenceQuery, { + transaction, + }) + )[0][0].pg_get_serial_sequence; + + // update sequence value to be set to max value of the autoincrement column in the table + const query = `SELECT setval('${sequenceName}', (SELECT MAX(${field}) FROM ${tableName}))`; + const setValue = ( + await models.sequelize.query(query, { + transaction, + }) + )[0][0].setval; + logger.info( + `Updated autoIncrement for ${modelName}.${field} with max value = ${setValue}`, + ); + } } - } } else { logger.info(`No records to save for model: ${modelName}`); }