Add logs if missing thing in user table

This commit is contained in:
Jim Laurie 2018-01-04 16:36:56 +01:00
parent 4fec48fee1
commit 1a1b083436
4 changed files with 98 additions and 3 deletions

View File

@ -55,4 +55,4 @@
"npm": ">= 5.3.0"
},
"license": "MIT"
}
}

View File

@ -46,4 +46,4 @@
"npm": ">= 5.0.0"
},
"license": "MIT"
}
}

View File

@ -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);
});
};

View File

@ -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();
}
}
};