0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

ci: check alteration sequence (#2746)

This commit is contained in:
wangsijie 2022-12-30 10:50:56 +08:00 committed by GitHub
parent 74c1532a38
commit b5b1e5f258
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 22 deletions

View file

@ -90,3 +90,22 @@ jobs:
uses: docker/build-push-action@v3
with:
context: .
main-alteration-sequence:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Setup Node and pnpm
uses: silverhand-io/actions-node-pnpm-run-steps@v2
with:
node-version: 18
- name: Build
run: pnpm prepack
- name: Check alteration sequence
run: node .scripts/check-alterations-sequence.js

View file

@ -0,0 +1,36 @@
/**
* This script runs a task to check alteration files seqence:
* new files should come last
*
*/
const { execSync } = require("child_process");
const alterationFilePrefix = "packages/schemas/alterations/";
const allAlterations = execSync("pnpm cli db alter list", {
encoding: "utf-8",
})
.split("\n")
.filter((filename) => Boolean(filename))
.map((filename) => filename.replace(".js", ""));
const diffFiles = execSync("git diff --name-only HEAD HEAD~1", {
encoding: "utf-8",
});
const committedAlterations = diffFiles
.split("\n")
.filter((filename) => filename.startsWith(alterationFilePrefix))
.map((filename) =>
filename.replace(alterationFilePrefix, "").replace(".ts", "")
);
for (const alteration of committedAlterations) {
const index = allAlterations.indexOf(alteration);
if (index < allAlterations.length - committedAlterations.length) {
throw new Error(
`Wrong alteration sequence for commited file: ${alteration}\nAll timestamps of committed alteration files should be greater than the biggest one in the base branch.`
);
}
}

View file

@ -72,7 +72,7 @@ const alteration: CommandModule<unknown, { action: string; target?: string }> =
builder: (yargs) =>
yargs
.positional('action', {
describe: 'The action to perform, now it only accepts `deploy`',
describe: 'The action to perform, now it only accepts `deploy` and `list`',
type: 'string',
demandOption: true,
})
@ -81,29 +81,35 @@ const alteration: CommandModule<unknown, { action: string; target?: string }> =
type: 'string',
}),
handler: async ({ action, target }) => {
if (action !== 'deploy') {
if (action === 'list') {
const files = await getAlterationFiles();
for (const file of files) {
console.log(file.filename);
}
} else if (action === 'deploy') {
const pool = await createPoolFromConfig();
const alterations = await chooseAlterationsByVersion(
await getUndeployedAlterations(pool),
target
);
log.info(
`Found ${alterations.length} alteration${conditionalString(
alterations.length > 1 && 's'
)} to deploy`
);
// The await inside the loop is intended, alterations should run in order
for (const alteration of alterations) {
// eslint-disable-next-line no-await-in-loop
await deployAlteration(pool, alteration);
}
await pool.end();
} else {
log.error('Unsupported action');
}
const pool = await createPoolFromConfig();
const alterations = await chooseAlterationsByVersion(
await getUndeployedAlterations(pool),
target
);
log.info(
`Found ${alterations.length} alteration${conditionalString(
alterations.length > 1 && 's'
)} to deploy`
);
// The await inside the loop is intended, alterations should run in order
for (const alteration of alterations) {
// eslint-disable-next-line no-await-in-loop
await deployAlteration(pool, alteration);
}
await pool.end();
},
};