From 1a1b083436ce28fb2b456882521dcb31cc181592 Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Thu, 4 Jan 2018 16:36:56 +0100 Subject: [PATCH] Add logs if missing thing in user table --- packages/strapi-bookshelf/package.json | 2 +- packages/strapi-knex/package.json | 2 +- .../config/functions/bootstrap.js | 4 +- .../services/UsersPermissions.js | 93 +++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/packages/strapi-bookshelf/package.json b/packages/strapi-bookshelf/package.json index a63c20c03b..48a948fec4 100755 --- a/packages/strapi-bookshelf/package.json +++ b/packages/strapi-bookshelf/package.json @@ -55,4 +55,4 @@ "npm": ">= 5.3.0" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/packages/strapi-knex/package.json b/packages/strapi-knex/package.json index 345e619ff6..2b58627503 100755 --- a/packages/strapi-knex/package.json +++ b/packages/strapi-knex/package.json @@ -46,4 +46,4 @@ "npm": ">= 5.0.0" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/packages/strapi-plugin-users-permissions/config/functions/bootstrap.js b/packages/strapi-plugin-users-permissions/config/functions/bootstrap.js index 8c08f76159..e72719317b 100644 --- a/packages/strapi-plugin-users-permissions/config/functions/bootstrap.js +++ b/packages/strapi-plugin-users-permissions/config/functions/bootstrap.js @@ -27,5 +27,7 @@ module.exports = cb => { } } - strapi.plugins['users-permissions'].services.userspermissions.updatePermissions(cb); + strapi.plugins['users-permissions'].services.userspermissions.syncSchema(() => { + strapi.plugins['users-permissions'].services.userspermissions.updatePermissions(cb); + }); }; diff --git a/packages/strapi-plugin-users-permissions/services/UsersPermissions.js b/packages/strapi-plugin-users-permissions/services/UsersPermissions.js index 8969dcc6e6..a0ace01e23 100644 --- a/packages/strapi-plugin-users-permissions/services/UsersPermissions.js +++ b/packages/strapi-plugin-users-permissions/services/UsersPermissions.js @@ -231,4 +231,97 @@ module.exports = { strapi.log.error(err); } }, + + syncSchema: (cb) => { + const Model = strapi.plugins['users-permissions'].models.user; + + if (Model.orm === 'bookshelf') { + const tableName = Model.collectionName; + + let queu = new Promise((resolve, reject) => { + strapi.connections[Model.connection].schema.hasTable(tableName) + .then(exist => { + if (!exist) { + strapi.log.warn(` +⚠️ TABLE \`${tableName}\` DOESN'T EXIST + +1️⃣ EXECUTE THE FOLLOWING SQL QUERY + +CREATE TABLE "${tableName}" ( + id integer NOT NULL, + username text, + email text, + role text, + "resetPasswordToken" text, + password text, + updated_at timestamp with time zone, + created_at timestamp with time zone +); + +2️⃣ RESTART YOUR SERVER + `); + + strapi.stop(); + } + + resolve(); + }); + }); + + queu = queu.then(() => { + const attributes = _.cloneDeep(Model.attributes); + attributes.id = { + type: 'integer' + }; + attributes.updated_at = attributes.created_at = { + type: 'timestamp with time zone' + }; + + let commands = ''; + + const columnExist = (description, attribute) => { + return new Promise((resolve, reject) => { + strapi.connections[Model.connection].schema.hasColumn(tableName, attribute) + .then(exist => { + if (!exist) { + if (description.type === 'string') { + description.type = 'text'; + } + + commands += `\r\nALTER TABLE "${tableName}" ADD "${attribute}" ${description.type};`; + } + + resolve(); + }); + }); + }; + + const testsColumns = []; + + _.forEach(attributes, (description, attribute) => { + testsColumns.push(columnExist(description, attribute)); + }); + + Promise.all(testsColumns) + .then(() => { + if (!_.isEmpty(commands)) { + strapi.log.warn(` +⚠️ TABLE \`${tableName}\` HAS MISSING COLUMNS + +1️⃣ EXECUTE THE FOLLOWING SQL QUERIES +${commands} + +2️⃣ RESTART YOUR SERVER + `); + + strapi.stop(); + } + + cb(); + }); + }); + } else { + cb(); + } + } };