Skip to content

Commit

Permalink
Added upgrade profiles feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Jan 3, 2024
1 parent a309b8a commit 67ab66d
Show file tree
Hide file tree
Showing 30 changed files with 737 additions and 670 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Add content events @robgietema
- Add profile metadata @robgietema
- Add @site endpoint @robgietema
- Add upgrade profile functionality @robgietema

### Bugfix

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"lint": "./node_modules/eslint/bin/eslint.js --max-warnings=0 'src/**/*.{js,jsx,json}'",
"migrate": "yarn knex migrate:latest",
"prettier": "./node_modules/.bin/prettier --single-quote --check 'src/**/*.{js,jsx,ts,tsx,json}'",
"seed": "yarn knex seed:run",
"seed": "babel-node scripts/seed.js run",
"seed:status": "babel-node scripts/seed.js status",
"seed:upgrade": "babel-node scripts/seed.js upgrade",
"start": "nodemon --exec babel-node src/server.js",
"rollback": "yarn knex migrate:rollback --all",
"reset": "yarn rollback && yarn migrate && yarn seed",
Expand Down
113 changes: 113 additions & 0 deletions scripts/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint no-console: 0 */
/**
* Seed script.
* @module scripts/seed
*/

import { last, padEnd } from 'lodash';

import { Profile } from '../src/models';
import { fileExists, knex, mapAsync, stripI18n } from '../src/helpers';

import {
seedProfile,
seedPermission,
seedRole,
seedGroup,
seedUser,
seedWorkflow,
seedType,
seedCatalog,
seedDocument,
seedRedirect,
seedAction,
seedControlpanel,
seedVocabulary,
} from '../src/seeds';

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

/**
* Main function
* @function main
* @return {undefined}
*/
async function main() {
const command = last(process.argv);

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) {
console.log(
`${padEnd(`${underline}Profile${reset}`, 58)}${padEnd(
`${underline}Current${reset}`,
18,
)}${padEnd(`${underline}Latest${reset}`, 18)}`,
);
}
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;
}
}
});

// Disconnect from db
knex.destroy();
}

main();
22 changes: 0 additions & 22 deletions src/seeds/000_profile.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/seeds/001_permission.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/seeds/002_role.js

This file was deleted.

34 changes: 0 additions & 34 deletions src/seeds/003_group.js

This file was deleted.

38 changes: 0 additions & 38 deletions src/seeds/004_user.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/seeds/005_workflow.js

This file was deleted.

52 changes: 0 additions & 52 deletions src/seeds/006_type.js

This file was deleted.

Loading

0 comments on commit 67ab66d

Please sign in to comment.