mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
feat(cli): support seeding legacy test data
This commit is contained in:
parent
b8fd594fea
commit
74e5975be5
3 changed files with 41 additions and 35 deletions
4
.github/workflows/main.yml
vendored
4
.github/workflows/main.yml
vendored
|
@ -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
|
||||
|
||||
|
|
|
@ -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,7 +21,6 @@ export const seedByPool = async (
|
|||
);
|
||||
}
|
||||
|
||||
if (!testOnly) {
|
||||
await oraPromise(createTables(connection), {
|
||||
text: 'Create tables',
|
||||
});
|
||||
|
@ -35,17 +29,22 @@ export const seedByPool = async (
|
|||
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<string, unknown>,
|
||||
{ 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(
|
||||
|
|
|
@ -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<Role>(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');
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue