diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 811ddfd42..669ba5729 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -148,7 +148,7 @@ jobs: # We will remove this step when a new version of CLI is released. - name: Setup alteration database test data working-directory: ./fresh - run: pnpm cli db seed --testOnly + run: pnpm cli db seed --legacy-test-data env: DB_URL: postgres://postgres:postgres@localhost:5432/alteration @@ -177,7 +177,7 @@ jobs: # We will remove this step when a new version of CLI is released. - name: Setup old database test data working-directory: ./fresh - run: pnpm cli db seed --testOnly + run: pnpm cli db seed --legacy-test-data env: DB_URL: postgres://postgres:postgres@localhost:5432/old diff --git a/packages/cli/src/commands/database/seed/index.ts b/packages/cli/src/commands/database/seed/index.ts index 7cff7ac9c..c5ef9b2ac 100644 --- a/packages/cli/src/commands/database/seed/index.ts +++ b/packages/cli/src/commands/database/seed/index.ts @@ -9,12 +9,7 @@ import { getAlterationDirectory } from '../alteration/utils.js'; import { createTables, seedCloud, seedTables, seedTest } from './tables.js'; -export const seedByPool = async ( - pool: DatabasePool, - cloud = false, - test = false, - testOnly = false -) => { +export const seedByPool = async (pool: DatabasePool, cloud = false, test = false) => { await pool.transaction(async (connection) => { // Check alteration scripts available in order to insert correct timestamp const latestTimestamp = await getLatestAlterationTimestamp(); @@ -26,26 +21,30 @@ export const seedByPool = async ( ); } - if (!testOnly) { - await oraPromise(createTables(connection), { - text: 'Create tables', - }); - await seedTables(connection, latestTimestamp, cloud); + await oraPromise(createTables(connection), { + text: 'Create tables', + }); + await seedTables(connection, latestTimestamp, cloud); - if (cloud) { - await seedCloud(connection); - } + if (cloud) { + await seedCloud(connection); } - if (test || testOnly) { + if (test) { await seedTest(connection); } }); }; +const seedLegacyTestData = async (pool: DatabasePool) => { + return pool.transaction(async (connection) => { + await seedTest(connection, true); + }); +}; + const seed: CommandModule< Record, - { swe?: boolean; cloud?: boolean; test?: boolean; 'test-only'?: boolean } + { swe?: boolean; cloud?: boolean; test?: boolean; 'legacy-test-data'?: boolean } > = { command: 'seed [type]', describe: 'Create database then seed tables and data', @@ -64,13 +63,27 @@ const seed: CommandModule< describe: 'Seed additional test data', type: 'boolean', }) - .option('test-only', { - describe: 'Seed test data only, this option conflicts with `--cloud`', + .option('legacy-test-data', { + describe: + 'Seed test data only for legacy Logto versions (<=1.12.0), this option conflicts with others', type: 'boolean', }), - handler: async ({ swe, cloud, test, testOnly }) => { + handler: async ({ swe, cloud, test, legacyTestData }) => { const pool = await createPoolAndDatabaseIfNeeded(); + if (legacyTestData) { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + if (swe || cloud || test) { + throw new Error( + 'The `legacy-test-data` option conflicts with other options, please use it alone.' + ); + } + + await seedLegacyTestData(pool); + await pool.end(); + return; + } + if (swe && (await doesConfigsTableExist(pool))) { consoleLog.info('Seeding skipped'); await pool.end(); @@ -79,7 +92,7 @@ const seed: CommandModule< } try { - await seedByPool(pool, cloud, test, testOnly); + await seedByPool(pool, cloud, test); } catch (error: unknown) { consoleLog.error(error); consoleLog.error( diff --git a/packages/cli/src/commands/database/seed/tables.ts b/packages/cli/src/commands/database/seed/tables.ts index 1e50e1ed5..ed088863e 100644 --- a/packages/cli/src/commands/database/seed/tables.ts +++ b/packages/cli/src/commands/database/seed/tables.ts @@ -25,7 +25,6 @@ import { Users, OrganizationRoleUserRelations, TenantRole, - Organizations, } from '@logto/schemas'; import { getTenantRole } from '@logto/schemas'; import { Tenants } from '@logto/schemas/models'; @@ -206,7 +205,7 @@ export const seedCloud = async (connection: DatabaseTransactionConnection) => { * - `test-1` will be assigned the management roles for both `default` and `admin` tenant. * - `test-2` will be assigned the management role for `default` tenant. */ -export const seedTest = async (connection: DatabaseTransactionConnection) => { +export const seedTest = async (connection: DatabaseTransactionConnection, forLegacy = false) => { const roles = convertToIdentifiers(Roles); const getManagementRole = async (tenantId: string) => connection.one(sql` @@ -245,18 +244,11 @@ export const seedTest = async (connection: DatabaseTransactionConnection) => { ]); consoleLog.succeed('Assigned tenant management roles to the test users'); - const organizations = convertToIdentifiers(Organizations); - const isTenantOrganizationInitialized = await connection.exists( - sql` - select 1 - from ${organizations.table} - where ${organizations.fields.tenantId} = ${getTenantOrganizationId(adminTenantId)} - ` - ); - // This check is for older versions (<=v.12.0) that don't have tenant organization initialized. - if (!isTenantOrganizationInitialized) { - consoleLog.warn('Tenant organization is not enabled, skip seeding tenant organization data'); + if (forLegacy) { + consoleLog.warn( + 'Tenant organization is not enabled in legacy Logto versions, skip seeding tenant organization data' + ); return; } @@ -292,4 +284,5 @@ export const seedTest = async (connection: DatabaseTransactionConnection) => { assignOrganizationRole(userIds[0], defaultTenantId, TenantRole.Owner), assignOrganizationRole(userIds[1], defaultTenantId, TenantRole.Owner), ]); + consoleLog.succeed('Assigned tenant organization membership and roles to the test users'); };