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:
parent
c52a66a32d
commit
8879880feb
2 changed files with 17 additions and 4 deletions
|
@ -22,8 +22,10 @@ export const getTimestampFromFilename = (filename: string) => {
|
||||||
return Number(match[1]);
|
return Number(match[1]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getAlterationDirectory = () => getPathInModule('@logto/schemas', 'alterations-js');
|
||||||
|
|
||||||
export const getAlterationFiles = async (): Promise<AlterationFile[]> => {
|
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,
|
* We copy all alteration scripts to the CLI package root directory,
|
||||||
|
|
|
@ -28,6 +28,7 @@ import {
|
||||||
} from '../../../queries/logto-config.js';
|
} from '../../../queries/logto-config.js';
|
||||||
import { getPathInModule, log, oraPromise } from '../../../utilities.js';
|
import { getPathInModule, log, oraPromise } from '../../../utilities.js';
|
||||||
import { getLatestAlterationTimestamp } from '../alteration/index.js';
|
import { getLatestAlterationTimestamp } from '../alteration/index.js';
|
||||||
|
import { getAlterationDirectory } from '../alteration/utils.js';
|
||||||
import { oidcConfigReaders } from './oidc-config.js';
|
import { oidcConfigReaders } from './oidc-config.js';
|
||||||
|
|
||||||
const createTables = async (connection: DatabaseTransactionConnection) => {
|
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([
|
await Promise.all([
|
||||||
connection.query(insertInto(managementResource, 'resources')),
|
connection.query(insertInto(managementResource, 'resources')),
|
||||||
connection.query(insertInto(createDefaultSetting(), 'settings')),
|
connection.query(insertInto(createDefaultSetting(), 'settings')),
|
||||||
connection.query(insertInto(defaultSignInExperience, 'sign_in_experiences')),
|
connection.query(insertInto(defaultSignInExperience, 'sign_in_experiences')),
|
||||||
connection.query(insertInto(createDemoAppApplication(generateStandardId()), 'applications')),
|
connection.query(insertInto(createDemoAppApplication(generateStandardId()), 'applications')),
|
||||||
connection.query(insertInto(defaultRole, 'roles')),
|
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) => {
|
export const seedByPool = async (pool: DatabasePool, type: SeedChoice) => {
|
||||||
await pool.transaction(async (connection) => {
|
await pool.transaction(async (connection) => {
|
||||||
if (type !== 'oidc') {
|
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), {
|
await oraPromise(createTables(connection), {
|
||||||
text: 'Create tables',
|
text: 'Create tables',
|
||||||
prefixText: chalk.blue('[info]'),
|
prefixText: chalk.blue('[info]'),
|
||||||
});
|
});
|
||||||
await oraPromise(seedTables(connection), {
|
await oraPromise(seedTables(connection, latestTimestamp), {
|
||||||
text: 'Seed data',
|
text: 'Seed data',
|
||||||
prefixText: chalk.blue('[info]'),
|
prefixText: chalk.blue('[info]'),
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue