fix: database migrations FK relations.

fix: database columns indexing.
This commit is contained in:
Ahmed Bouhuolia
2020-10-03 12:08:11 +02:00
parent 1250eccc0d
commit 0114ed9f8b
86 changed files with 788 additions and 801 deletions

View File

@@ -1,8 +1,8 @@
exports.up = (knex) => knex.schema.createTable('password_resets', (table) => {
table.increments();
table.string('email');
table.string('token');
table.string('email').index();
table.string('token').index();
table.timestamp('created_at');
});

View File

@@ -2,7 +2,7 @@
exports.up = function(knex) {
return knex.schema.createTable('tenants', (table) => {
table.bigIncrements();
table.string('organization_id');
table.string('organization_id').index();
table.dateTime('under_maintenance_since').nullable();
table.dateTime('initialized_at').nullable();

View File

@@ -4,18 +4,15 @@ exports.up = function (knex) {
table.increments();
table.string('first_name');
table.string('last_name');
table.string('email').unique();
table.string('phone_number').unique();
table.string('email').unique().index();
table.string('phone_number').unique().index();
table.string('password');
table.boolean('active');
table.boolean('active').index();
table.string('language');
table.integer('tenant_id').unsigned();
table.date('invite_accepted_at');
table.date('last_login_at');
table.dateTime('deleted_at');
table.bigInteger('tenant_id').unsigned().index().references('id').inTable('tenants');
table.date('invite_accepted_at').index();
table.date('last_login_at').index();
table.dateTime('deleted_at').index();
table.timestamps();
});
};

View File

@@ -2,9 +2,9 @@
exports.up = function(knex) {
return knex.schema.createTable('user_invites', (table) => {
table.increments();
table.string('email');
table.string('token').unique();
table.integer('tenant_id').unsigned();
table.string('email').index();
table.string('token').unique().index();
table.bigInteger('tenant_id').unsigned().index().references('id').inTable('tenants');
table.datetime('created_at');
});
};

View File

@@ -1,18 +0,0 @@
exports.up = function(knex) {
return knex.schema.createTable('subscriptions_usage', table => {
table.increments();
table.integer('user_id');
table.integer('plan_id');
table.dateTime('trial_ends_at');
table.dateTime('subscription_starts_at');
table.dateTime('subscription_ends_at');
table.timestamps();
});
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('subscriptions_usage');
};

View File

@@ -17,7 +17,6 @@ exports.up = function(knex) {
table.string('invoice_interval').nullable();
table.integer('index').unsigned();
table.timestamps();
}).then(() => {
return knex.seed.run({

View File

@@ -2,12 +2,10 @@
exports.up = function(knex) {
return knex.schema.createTable('subscription_plan_features', table => {
table.increments();
table.integer('plan_id').unsigned();
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
table.string('slug');
table.string('name');
table.string('description');
table.timestamps();
});
};

View File

@@ -4,8 +4,8 @@ exports.up = function(knex) {
table.increments('id');
table.string('slug');
table.integer('plan_id').unsigned();
table.integer('tenant_id').unsigned();
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
table.bigInteger('tenant_id').unsigned().index().references('id').inTable('tenants');
table.dateTime('trial_started_at').nullable();
table.dateTime('trial_ends_at').nullable();

View File

@@ -3,15 +3,15 @@ exports.up = function(knex) {
return knex.schema.createTable('subscription_licenses', table => {
table.increments();
table.string('license_code').unique();
table.integer('plan_id').unsigned();
table.string('license_code').unique().index();
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
table.integer('license_period').unsigned();
table.string('period_interval');
table.dateTime('sent_at');
table.dateTime('disabled_at');
table.dateTime('used_at');
table.dateTime('sent_at').index();
table.dateTime('disabled_at').index();
table.dateTime('used_at').index();
table.timestamps();
})

View File

@@ -12,21 +12,22 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Patches the last login date to the given system user.
* @param {number} userId
* @return {Promise<void>}
*/
async patchLastLoginAt(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, {
async patchLastLoginAt(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, {
last_login_at: moment().toMySqlDateTime()
});
this.flushUserCache(user);
return user;
this.flushCache();
}
/**
* Finds system user by crediential.
* @param {string} crediential - Phone number or email.
* @return {ISystemUser}
* @return {Promise<ISystemUser>}
*/
findByCrediential(crediential: string) {
findByCrediential(crediential: string): Promise<ISystemUser> {
return SystemUser.query().whereNotDeleted()
.findOne('email', crediential)
.orWhere('phone_number', crediential);
@@ -34,9 +35,10 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Retrieve system user details of the given id.
* @param {number} userId
* @param {number} userId - User id.
* @return {Promise<ISystemUser>}
*/
getById(userId: number) {
getById(userId: number): Promise<ISystemUser> {
return this.cache.get(`systemUser.id.${userId}`, () => {
return SystemUser.query().whereNotDeleted().findById(userId);
});
@@ -44,10 +46,11 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Retrieve user by id and tenant id.
* @param {number} userId
* @param {number} tenantId
* @param {number} userId - User id.
* @param {number} tenantId - Tenant id.
* @return {Promise<ISystemUser>}
*/
getByIdAndTenant(userId: number, tenantId: number) {
getByIdAndTenant(userId: number, tenantId: number): Promise<ISystemUser> {
return this.cache.get(`systemUser.id.${userId}.tenant.${tenantId}`, () => {
return SystemUser.query().whereNotDeleted()
.findOne({ id: userId, tenant_id: tenantId });
@@ -56,9 +59,10 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Retrieve system user details by the given email.
* @param {string} email
* @param {string} email - Email
* @return {Promise<ISystemUser>}
*/
getByEmail(email: string) {
getByEmail(email: string): Promise<ISystemUser> {
return this.cache.get(`systemUser.email.${email}`, () => {
return SystemUser.query().whereNotDeleted().findOne('email', email);
});
@@ -66,9 +70,10 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Retrieve user by phone number.
* @param {string} phoneNumber
* @param {string} phoneNumber - Phone number
* @return {Promise<ISystemUser>}
*/
getByPhoneNumber(phoneNumber: string) {
getByPhoneNumber(phoneNumber: string): Promise<ISystemUser> {
return this.cache.get(`systemUser.phoneNumber.${phoneNumber}`, () => {
return SystemUser.query().whereNotDeleted().findOne('phoneNumber', phoneNumber);
});
@@ -76,62 +81,61 @@ export default class SystemUserRepository extends SystemRepository {
/**
* Edits details.
* @param {number} userId
* @param {number} user
* @param {number} userId - User id.
* @param {number} user - User input.
* @return {Promise<void>}
*/
edit(userId: number, userInput: ISystemUser) {
const user = SystemUser.query().patchAndFetchById(userId, { ...userInput });
this.flushUserCache(user);
return user;
async edit(userId: number, userInput: ISystemUser): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { ...userInput });
this.flushCache();
}
/**
* Creates a new user.
* @param {IUser} userInput
* @param {IUser} userInput - User input.
* @return {Promise<ISystemUser>}
*/
create(userInput: ISystemUser) {
return SystemUser.query().insert({ ...userInput });
async create(userInput: ISystemUser): Promise<ISystemUser> {
const systemUser = await SystemUser.query().insert({ ...userInput });
this.flushCache();
return systemUser;
}
/**
* Deletes user by the given id.
* @param {number} userId
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async deleteById(userId: number) {
const user = await this.getById(userId);
async deleteById(userId: number): Promise<void> {
await SystemUser.query().where('id', userId).delete();
this.flushUserCache(user);
this.flushCache();
}
/**
* Activate user by the given id.
* @param {number} userId
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async activateById(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, { active: 1 });
this.flushUserCache(user);
return user;
async activateById(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { active: 1 });
this.flushCache();
}
/**
* Inactivate user by the given id.
* @param {number} userId
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async inactivateById(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, { active: 0 });
this.flushUserCache(user);
return user;
async inactivateById(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { active: 0 });
this.flushCache();
}
/**
* Flush user cache.
* @param {IUser} user
* Flushes user repository cache.
*/
flushUserCache(user: ISystemUser) {
this.cache.del(`systemUser.phoneNumber.${user.phoneNumber}`);
this.cache.del(`systemUser.email.${user.email}`);
this.cache.del(`systemUser.id.${user.id}`);
this.cache.del(`systemUser.id.${user.id}.tenant.${user.tenantId}`);
flushCache() {
this.cache.delStartWith('systemUser');
}
}