0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor: check alteration timestamp when seed database (#2721)

This commit is contained in:
Gao Sun 2022-12-24 17:18:37 +08:00 committed by GitHub
parent c52a66a32d
commit 8879880feb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View file

@ -22,8 +22,10 @@ export const getTimestampFromFilename = (filename: string) => {
return Number(match[1]);
};
export const getAlterationDirectory = () => getPathInModule('@logto/schemas', 'alterations-js');
export const getAlterationFiles = async (): Promise<AlterationFile[]> => {
const alterationDirectory = getPathInModule('@logto/schemas', 'alterations-js');
const alterationDirectory = getAlterationDirectory();
/**
* We copy all alteration scripts to the CLI package root directory,

View file

@ -28,6 +28,7 @@ import {
} from '../../../queries/logto-config.js';
import { getPathInModule, log, oraPromise } from '../../../utilities.js';
import { getLatestAlterationTimestamp } from '../alteration/index.js';
import { getAlterationDirectory } from '../alteration/utils.js';
import { oidcConfigReaders } from './oidc-config.js';
const createTables = async (connection: DatabaseTransactionConnection) => {
@ -53,14 +54,14 @@ const createTables = async (connection: DatabaseTransactionConnection) => {
}
};
const seedTables = async (connection: DatabaseTransactionConnection) => {
const seedTables = async (connection: DatabaseTransactionConnection, latestTimestamp: number) => {
await Promise.all([
connection.query(insertInto(managementResource, 'resources')),
connection.query(insertInto(createDefaultSetting(), 'settings')),
connection.query(insertInto(defaultSignInExperience, 'sign_in_experiences')),
connection.query(insertInto(createDemoAppApplication(generateStandardId()), 'applications')),
connection.query(insertInto(defaultRole, 'roles')),
updateDatabaseTimestamp(connection, await getLatestAlterationTimestamp()),
updateDatabaseTimestamp(connection, latestTimestamp),
]);
};
@ -118,11 +119,21 @@ type SeedChoice = typeof seedChoices[number];
export const seedByPool = async (pool: DatabasePool, type: SeedChoice) => {
await pool.transaction(async (connection) => {
if (type !== 'oidc') {
// Check alteration scripts available in order to insert correct timestamp
const latestTimestamp = await getLatestAlterationTimestamp();
if (latestTimestamp < 1) {
throw new Error(
`No alteration script found when seeding the database.\n` +
`Please check \`${getAlterationDirectory()}\` to see if there are alteration scripts available.\n`
);
}
await oraPromise(createTables(connection), {
text: 'Create tables',
prefixText: chalk.blue('[info]'),
});
await oraPromise(seedTables(connection), {
await oraPromise(seedTables(connection, latestTimestamp), {
text: 'Seed data',
prefixText: chalk.blue('[info]'),
});