Skip to content

Commit

Permalink
fix: don't hide script failures
Browse files Browse the repository at this point in the history
Currently, if a script (e.g. database migrations) fails, the error is shown to the console and then swallowed. The app will then be in an unknown state, leading to strange behaviour.

This change makes sure that the script throws the error and the resulting exit code halts the process.

Refs #395
  • Loading branch information
thewilkybarkid committed Nov 25, 2021
1 parent e1ba4e6 commit a3d5588
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 74 deletions.
47 changes: 21 additions & 26 deletions src/backend/scripts/dbApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,29 @@ import {
import config from '../mikro-orm.config';

async function main() {
try {
const orm = await MikroORM.init(config);
const users = userModelWrapper(orm);
const personas = personaModelWrapper(orm);
const keys = keyModelWrapper(orm);
const orm = await MikroORM.init(config);
const users = userModelWrapper(orm);
const personas = personaModelWrapper(orm);
const keys = keyModelWrapper(orm);

const user = users.create({ orcid: process.env.TEST_ADMIN_USER_ORCID });
const publicPersona = personas.create({
identity: user,
name: 'PREreview Test User',
isAnonymous: false,
});
user.defaultPersona = publicPersona;
const key = keys.create({
owner: user,
app: process.env.TEST_ADMIN_USER_API_APP,
secret: process.env.TEST_ADMIN_USER_API_KEY,
});
const user = users.create({ orcid: process.env.TEST_ADMIN_USER_ORCID });
const publicPersona = personas.create({
identity: user,
name: 'PREreview Test User',
isAnonymous: false,
});
user.defaultPersona = publicPersona;
const key = keys.create({
owner: user,
app: process.env.TEST_ADMIN_USER_API_APP,
secret: process.env.TEST_ADMIN_USER_API_KEY,
});

await users.persistAndFlush(user);
await personas.persistAndFlush(publicPersona);
await keys.persistAndFlush(key);
await users.persistAndFlush(user);
await personas.persistAndFlush(publicPersona);
await keys.persistAndFlush(key);

await orm.close();
return;
} catch (err) {
console.error('Failed to create API key:', err);
}
await orm.close();
}

main();
void main();
41 changes: 18 additions & 23 deletions src/backend/scripts/dbInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@ import dbConfig from '../mikro-orm.config';
import { groupModelWrapper } from '../models';

async function main() {
try {
const db = await MikroORM.init(dbConfig);
const groups = groupModelWrapper(db);
const adminGroup = groups.create({ name: 'admins' });
console.log('Created admins group:', adminGroup);
const userGroup = groups.create({ name: 'users' });
console.log('Created users group:', userGroup);
const modsGroup = groups.create({ name: 'moderators' });
console.log('Created moderators group:', modsGroup);
const partnersGroup = groups.create({ name: 'partners' });
console.log('Created partners group:', partnersGroup);
await groups.persistAndFlush([
adminGroup,
userGroup,
modsGroup,
partnersGroup,
]);
db.close();
return;
} catch (err) {
console.error('Failed to run seeds:', err);
}
const db = await MikroORM.init(dbConfig);
const groups = groupModelWrapper(db);
const adminGroup = groups.create({ name: 'admins' });
console.log('Created admins group:', adminGroup);
const userGroup = groups.create({ name: 'users' });
console.log('Created users group:', userGroup);
const modsGroup = groups.create({ name: 'moderators' });
console.log('Created moderators group:', modsGroup);
const partnersGroup = groups.create({ name: 'partners' });
console.log('Created partners group:', partnersGroup);
await groups.persistAndFlush([
adminGroup,
userGroup,
modsGroup,
partnersGroup,
]);
await db.close();
}

main();
void main();
46 changes: 21 additions & 25 deletions src/backend/scripts/dbMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@ import * as migrations from '../db/migrations/postgresql';
import { SearchPostgresql } from '../db/migrations/searchIndexes';

async function main() {
try {
const migrationsList = Object.entries(migrations).map(
([migrationName, migrationClass]) => ({
name: migrationName,
class: migrationClass,
}),
);
const migrationsList = Object.entries(migrations).map(
([migrationName, migrationClass]) => ({
name: migrationName,
class: migrationClass,
}),
);

migrationsList.splice(1, 0, {
name: 'SearchPostgresql',
class: SearchPostgresql,
});
migrationsList.splice(1, 0, {
name: 'SearchPostgresql',
class: SearchPostgresql,
});

const orm = await MikroORM.init({
...options,
migrations: {
disableForeignKeys: false,
migrationsList,
},
});
const orm = await MikroORM.init({
...options,
migrations: {
disableForeignKeys: false,
migrationsList,
},
});

const migrator = orm.getMigrator();
await migrator.up(); // runs migrations up to the latest
await orm.close(true);
} catch (err) {
console.error('Failed to run migrations:', err);
}
const migrator = orm.getMigrator();
await migrator.up(); // runs migrations up to the latest
await orm.close(true);
}

main();
void main();

0 comments on commit a3d5588

Please sign in to comment.