mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -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.
|
# We will remove this step when a new version of CLI is released.
|
||||||
- name: Setup alteration database test data
|
- name: Setup alteration database test data
|
||||||
working-directory: ./fresh
|
working-directory: ./fresh
|
||||||
run: pnpm cli db seed --testOnly
|
run: pnpm cli db seed --legacy-test-data
|
||||||
env:
|
env:
|
||||||
DB_URL: postgres://postgres:postgres@localhost:5432/alteration
|
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.
|
# We will remove this step when a new version of CLI is released.
|
||||||
- name: Setup old database test data
|
- name: Setup old database test data
|
||||||
working-directory: ./fresh
|
working-directory: ./fresh
|
||||||
run: pnpm cli db seed --testOnly
|
run: pnpm cli db seed --legacy-test-data
|
||||||
env:
|
env:
|
||||||
DB_URL: postgres://postgres:postgres@localhost:5432/old
|
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';
|
import { createTables, seedCloud, seedTables, seedTest } from './tables.js';
|
||||||
|
|
||||||
export const seedByPool = async (
|
export const seedByPool = async (pool: DatabasePool, cloud = false, test = false) => {
|
||||||
pool: DatabasePool,
|
|
||||||
cloud = false,
|
|
||||||
test = false,
|
|
||||||
testOnly = false
|
|
||||||
) => {
|
|
||||||
await pool.transaction(async (connection) => {
|
await pool.transaction(async (connection) => {
|
||||||
// Check alteration scripts available in order to insert correct timestamp
|
// Check alteration scripts available in order to insert correct timestamp
|
||||||
const latestTimestamp = await getLatestAlterationTimestamp();
|
const latestTimestamp = await getLatestAlterationTimestamp();
|
||||||
|
@ -26,26 +21,30 @@ export const seedByPool = async (
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!testOnly) {
|
await oraPromise(createTables(connection), {
|
||||||
await oraPromise(createTables(connection), {
|
text: 'Create tables',
|
||||||
text: 'Create tables',
|
});
|
||||||
});
|
await seedTables(connection, latestTimestamp, cloud);
|
||||||
await seedTables(connection, latestTimestamp, cloud);
|
|
||||||
|
|
||||||
if (cloud) {
|
if (cloud) {
|
||||||
await seedCloud(connection);
|
await seedCloud(connection);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (test || testOnly) {
|
if (test) {
|
||||||
await seedTest(connection);
|
await seedTest(connection);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const seedLegacyTestData = async (pool: DatabasePool) => {
|
||||||
|
return pool.transaction(async (connection) => {
|
||||||
|
await seedTest(connection, true);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const seed: CommandModule<
|
const seed: CommandModule<
|
||||||
Record<string, unknown>,
|
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]',
|
command: 'seed [type]',
|
||||||
describe: 'Create database then seed tables and data',
|
describe: 'Create database then seed tables and data',
|
||||||
|
@ -64,13 +63,27 @@ const seed: CommandModule<
|
||||||
describe: 'Seed additional test data',
|
describe: 'Seed additional test data',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
})
|
})
|
||||||
.option('test-only', {
|
.option('legacy-test-data', {
|
||||||
describe: 'Seed test data only, this option conflicts with `--cloud`',
|
describe:
|
||||||
|
'Seed test data only for legacy Logto versions (<=1.12.0), this option conflicts with others',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
}),
|
}),
|
||||||
handler: async ({ swe, cloud, test, testOnly }) => {
|
handler: async ({ swe, cloud, test, legacyTestData }) => {
|
||||||
const pool = await createPoolAndDatabaseIfNeeded();
|
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))) {
|
if (swe && (await doesConfigsTableExist(pool))) {
|
||||||
consoleLog.info('Seeding skipped');
|
consoleLog.info('Seeding skipped');
|
||||||
await pool.end();
|
await pool.end();
|
||||||
|
@ -79,7 +92,7 @@ const seed: CommandModule<
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await seedByPool(pool, cloud, test, testOnly);
|
await seedByPool(pool, cloud, test);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
consoleLog.error(error);
|
consoleLog.error(error);
|
||||||
consoleLog.error(
|
consoleLog.error(
|
||||||
|
|
|
@ -25,7 +25,6 @@ import {
|
||||||
Users,
|
Users,
|
||||||
OrganizationRoleUserRelations,
|
OrganizationRoleUserRelations,
|
||||||
TenantRole,
|
TenantRole,
|
||||||
Organizations,
|
|
||||||
} from '@logto/schemas';
|
} from '@logto/schemas';
|
||||||
import { getTenantRole } from '@logto/schemas';
|
import { getTenantRole } from '@logto/schemas';
|
||||||
import { Tenants } from '@logto/schemas/models';
|
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-1` will be assigned the management roles for both `default` and `admin` tenant.
|
||||||
* - `test-2` will be assigned the management role for `default` 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 roles = convertToIdentifiers(Roles);
|
||||||
const getManagementRole = async (tenantId: string) =>
|
const getManagementRole = async (tenantId: string) =>
|
||||||
connection.one<Role>(sql`
|
connection.one<Role>(sql`
|
||||||
|
@ -245,18 +244,11 @@ export const seedTest = async (connection: DatabaseTransactionConnection) => {
|
||||||
]);
|
]);
|
||||||
consoleLog.succeed('Assigned tenant management roles to the test users');
|
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.
|
// This check is for older versions (<=v.12.0) that don't have tenant organization initialized.
|
||||||
if (!isTenantOrganizationInitialized) {
|
if (forLegacy) {
|
||||||
consoleLog.warn('Tenant organization is not enabled, skip seeding tenant organization data');
|
consoleLog.warn(
|
||||||
|
'Tenant organization is not enabled in legacy Logto versions, skip seeding tenant organization data'
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,4 +284,5 @@ export const seedTest = async (connection: DatabaseTransactionConnection) => {
|
||||||
assignOrganizationRole(userIds[0], defaultTenantId, TenantRole.Owner),
|
assignOrganizationRole(userIds[0], defaultTenantId, TenantRole.Owner),
|
||||||
assignOrganizationRole(userIds[1], 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