diff --git a/packages/cli/src/commands/database/alteration/utils.ts b/packages/cli/src/commands/database/alteration/utils.ts index 38f04337c..5416f220b 100644 --- a/packages/cli/src/commands/database/alteration/utils.ts +++ b/packages/cli/src/commands/database/alteration/utils.ts @@ -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 => { - const alterationDirectory = getPathInModule('@logto/schemas', 'alterations-js'); + const alterationDirectory = getAlterationDirectory(); /** * We copy all alteration scripts to the CLI package root directory, diff --git a/packages/cli/src/commands/database/seed/index.ts b/packages/cli/src/commands/database/seed/index.ts index 34ebbb014..4977b2b79 100644 --- a/packages/cli/src/commands/database/seed/index.ts +++ b/packages/cli/src/commands/database/seed/index.ts @@ -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]'), });