Fix PR feedback

This commit is contained in:
Jim Laurie 2018-01-11 15:00:34 +01:00
parent 054df448a9
commit f545f373aa
7 changed files with 60 additions and 66 deletions

View File

@ -235,93 +235,87 @@ module.exports = {
syncSchema: (cb) => { syncSchema: (cb) => {
const Model = strapi.plugins['users-permissions'].models.user; const Model = strapi.plugins['users-permissions'].models.user;
if (Model.orm === 'bookshelf') { if (Model.orm !== 'bookshelf') {
const tableName = Model.collectionName; return cb();
}
let queue = new Promise((resolve, reject) => { const tableName = Model.collectionName;
strapi.connections[Model.connection].schema.hasTable(tableName)
.then(exist => { new Promise((resolve, reject) => {
if (!exist) { strapi.connections[Model.connection].schema.hasTable(tableName)
strapi.log.warn(` .then(exist => {
if (!exist) {
strapi.log.warn(`
TABLE \`${tableName}\` DOESN'T EXIST TABLE \`${tableName}\` DOESN'T EXIST
1 EXECUTE THE FOLLOWING SQL QUERY 1 EXECUTE THE FOLLOWING SQL QUERY
CREATE TABLE "${tableName}" ( CREATE TABLE "${tableName}" (
id integer NOT NULL, id integer NOT NULL,
username text, username text,
email text, email text,
role text, role text,
"resetPasswordToken" text, "resetPasswordToken" text,
password text, password text,
updated_at timestamp with time zone, updated_at timestamp with time zone,
created_at timestamp with time zone created_at timestamp with time zone
); );
2 RESTART YOUR SERVER 2 RESTART YOUR SERVER
`); `);
strapi.stop(); strapi.stop();
} }
resolve(); resolve();
});
}); });
})
.then(() => {
const attributes = _.cloneDeep(Model.attributes);
attributes.id = {
type: 'integer'
};
attributes.updated_at = attributes.created_at = {
type: 'timestamp with time zone'
};
queue = queue.then(() => { let commands = '';
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) => {
const columnExist = (description, attribute) => { strapi.connections[Model.connection].schema.hasColumn(tableName, attribute)
return new Promise((resolve, reject) => { .then(exist => {
strapi.connections[Model.connection].schema.hasColumn(tableName, attribute) if (!exist) {
.then(exist => { if (description.type === 'string') {
if (!exist) { description.type = 'text';
if (description.type === 'string') {
description.type = 'text';
}
commands += `\r\nALTER TABLE "${tableName}" ADD "${attribute}" ${description.type};`;
} }
resolve(); commands += `\r\nALTER TABLE "${tableName}" ADD "${attribute}" ${description.type};`;
}); }
resolve();
}); });
};
const testsColumns = [];
_.forEach(attributes, (description, attribute) => {
testsColumns.push(columnExist(description, attribute));
}); });
};
Promise.all(testsColumns) const testsColumns = Object.entries(attributes).map(([attribute, description]) => columnExist(description, attribute));
.then(() => { Promise.all(testsColumns)
if (!_.isEmpty(commands)) { .then(() => {
strapi.log.warn(` if (!_.isEmpty(commands)) {
strapi.log.warn(`
TABLE \`${tableName}\` HAS MISSING COLUMNS TABLE \`${tableName}\` HAS MISSING COLUMNS
1 EXECUTE THE FOLLOWING SQL QUERIES 1 EXECUTE THE FOLLOWING SQL QUERIES
${commands} ${commands}
2 RESTART YOUR SERVER 2 RESTART YOUR SERVER
`); `);
strapi.stop(); strapi.stop();
} }
cb(); cb();
});
}); });
} else { });
cb();
}
} }
}; };