Skip to content

Commit

Permalink
Fix upgrade types.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Apr 26, 2024
1 parent b1437a0 commit cb5654c
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 521 deletions.
134 changes: 71 additions & 63 deletions scripts/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ const { config } = require(`${process.cwd()}/config`);
const reset = '\x1b[0m';
const underline = '\x1b[4m';

const seed = async (knex, profilePath) => {
await seedProfile(knex, profilePath);
await seedPermission(knex, profilePath);
await seedRole(knex, profilePath);
await seedGroup(knex, profilePath);
await seedUser(knex, profilePath);
await seedWorkflow(knex, profilePath);
await seedType(knex, profilePath);
await seedCatalog(knex, profilePath);
await seedDocument(knex, profilePath);
await seedRedirect(knex, profilePath);
await seedAction(knex, profilePath);
await seedControlpanel(knex, profilePath);
await seedVocabulary(knex, profilePath);
const seed = async (trx, profilePath) => {
await seedProfile(trx, profilePath);
await seedPermission(trx, profilePath);
await seedRole(trx, profilePath);
await seedGroup(trx, profilePath);
await seedUser(trx, profilePath);
await seedWorkflow(trx, profilePath);
await seedType(trx, profilePath);
await seedCatalog(trx, profilePath);
await seedDocument(trx, profilePath);
await seedRedirect(trx, profilePath);
await seedAction(trx, profilePath);
await seedControlpanel(trx, profilePath);
await seedVocabulary(trx, profilePath);
};

/**
Expand All @@ -53,61 +53,69 @@ const seed = async (knex, profilePath) => {
*/
async function main() {
const command = last(process.argv);
const trx = await knex.transaction();

await mapAsync(config.profiles, async (profilePath, index) => {
if (fileExists(`${profilePath}/metadata`)) {
const metadata = stripI18n(require(`${profilePath}/metadata`));
const profile = await Profile.fetchOne({ id: metadata.id }, {}, knex);
try {
await mapAsync(config.profiles, async (profilePath, index) => {
if (fileExists(`${profilePath}/metadata`)) {
const metadata = stripI18n(require(`${profilePath}/metadata`));
const profile = await Profile.fetchOne({ id: metadata.id }, {}, knex);

switch (command) {
case 'status':
if (index === 0) {
switch (command) {
case 'status':
if (index === 0) {
console.log(
`${padEnd(`${underline}Profile${reset}`, 58)}${padEnd(
`${underline}Current${reset}`,
18,
)}${padEnd(`${underline}Latest${reset}`, 18)}`,
);
}
console.log(
`${padEnd(`${underline}Profile${reset}`, 58)}${padEnd(
`${underline}Current${reset}`,
18,
)}${padEnd(`${underline}Latest${reset}`, 18)}`,
`${padEnd(metadata.id, 50)}${padEnd(
profile ? profile.version : 'Not found',
10,
)}${padEnd(metadata.version, 10)}`,
);
}
console.log(
`${padEnd(metadata.id, 50)}${padEnd(
profile ? profile.version : 'Not found',
10,
)}${padEnd(metadata.version, 10)}`,
);
break;
case 'upgrade':
if (!profile) {
console.log('Profile is not installed yet');
} else if (metadata.version === parseInt(profile.version)) {
console.log('Profile already up to date');
} else {
return mapAsync(
Array.apply(null, {
length: metadata.version - parseInt(profile.version),
}),
async (value, index) => {
const version = parseInt(profile.version) + 1 + index;
console.log(`Upgrading ${profilePath} to ${version}`);
return await seed(knex, `${profilePath}/upgrades/${version}`);
},
);
}
break;
default:
console.log(`Applying profile ${metadata.id}`);
if (profile && metadata.version === parseInt(profile.version)) {
console.log('Profile already up to date');
} else {
return await seed(knex, profilePath);
}
break;
break;
case 'upgrade':
if (!profile) {
console.log('Profile is not installed yet');
} else if (metadata.version === parseInt(profile.version)) {
console.log('Profile already up to date');
} else {
return mapAsync(
Array.apply(null, {
length: metadata.version - parseInt(profile.version),
}),
async (value, index) => {
const version = parseInt(profile.version) + 1 + index;
console.log(`Upgrading ${profilePath} to ${version}`);
return await seed(trx, `${profilePath}/upgrades/${version}`);
},
);
}
break;
default:
console.log(`Applying profile ${metadata.id}`);
if (profile && metadata.version === parseInt(profile.version)) {
console.log('Profile already up to date');
} else {
return await seed(trx, profilePath);
}
break;
}
}
}
});
});

// Disconnect from db
knex.destroy();
// Commit changes
await trx.commit();
knex.destroy();
} catch (err) {
await trx.rollback();
knex.destroy();
console.log(err);
}
}

main();
2 changes: 2 additions & 0 deletions src/models/_model/_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export class Model extends mixin(ObjectionModel, [
* @param {string} id Id of the model.
* @param {Object} data Model data.
* @param {Object} trx Transaction object.
* @returns {Object} Model of the updated record
*/
static async update(id, data, trx) {
const relationObjects = this.getRelations();
Expand Down Expand Up @@ -369,6 +370,7 @@ export class Model extends mixin(ObjectionModel, [
}
}),
);
return model;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/models/document/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ export class Document extends Model {
async fetchWorkflowHistory(req, trx) {
return await Promise.all(
map(this.workflow_history, async (item) => {
const user = await User.fetchById(item.actor);
const user = await User.fetchById(item.actor, {}, trx);
return {
...item,
actor: {
Expand Down Expand Up @@ -727,7 +727,7 @@ export class Document extends Model {
*/
async allowedUsersGroupsRoles(trx) {
// Get global roles
const view = await Permission.fetchById('View');
const view = await Permission.fetchById('View', {}, trx);
await view.fetchRelated('_roles', trx);
const globalRoles = view._roles.map((role) => role.id);

Expand Down
2 changes: 1 addition & 1 deletion src/models/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Type extends Model {
} else {
schema = this.schema;
}
await this.update({ _schema: schema });
await this.update({ _schema: schema }, trx);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/routes/site/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default [
view: '/@site',
permission: 'View',
handler: async (req, trx) => {
const controlpanel = await Controlpanel.fetchById('site');
const controlpanel = await Controlpanel.fetchById('site', {}, trx);
const config = controlpanel.data;

// Return database information
Expand Down
48 changes: 22 additions & 26 deletions src/seeds/action/action.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { fileExists, mapAsync, stripI18n } from '../../helpers';
import { Action } from '../../models';

export const seedAction = async (knex, profilePath) => {
try {
if (fileExists(`${profilePath}/actions`)) {
const profile = stripI18n(require(`${profilePath}/actions`));
if (profile.purge) {
await Action.delete(knex);
}
await mapAsync(
['object', 'site_actions', 'object_buttons', 'user'],
async (category) => {
await mapAsync(profile[category], async (action, index) => {
await Action.create(
{
...action,
category,
order: action.order || index,
},
{},
knex,
);
});
},
);
console.log('Actions imported');
export const seedAction = async (trx, profilePath) => {
if (fileExists(`${profilePath}/actions`)) {
const profile = stripI18n(require(`${profilePath}/actions`));
if (profile.purge) {
await Action.delete(trx);
}
} catch (err) {
console.log(err);
await mapAsync(
['object', 'site_actions', 'object_buttons', 'user'],
async (category) => {
await mapAsync(profile[category], async (action, index) => {
await Action.create(
{
...action,
category,
order: action.order || index,
},
{},
trx,
);
});
},
);
console.log('Actions imported');
}
};
Loading

0 comments on commit cb5654c

Please sign in to comment.