0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor(cli): add skip when exists option (#2172)

This commit is contained in:
Gao Sun 2022-10-17 21:51:32 +08:00 committed by GitHub
parent 0c4fd3c7be
commit c0eaf66c26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View file

@ -12,6 +12,7 @@ import { z } from 'zod';
import { createPoolAndDatabaseIfNeeded, insertInto } from '../../../database';
import {
getRowsByKeys,
isConfigsTableExists,
updateDatabaseTimestamp,
updateValueByKey,
} from '../../../queries/logto-config';
@ -126,19 +127,32 @@ export const seedByPool = async (pool: DatabasePool, type: SeedChoice) => {
});
};
const seed: CommandModule<Record<string, unknown>, { type: string }> = {
const seed: CommandModule<Record<string, unknown>, { type: string; swe?: boolean }> = {
command: 'seed [type]',
describe: 'Create database then seed tables and data',
builder: (yargs) =>
yargs.positional('type', {
yargs
.option('swe', {
describe: 'Skip the seeding process when Logto configs table exists',
alias: 'skip-when-exists',
type: 'boolean',
})
.positional('type', {
describe: 'Optional seed type',
type: 'string',
choices: seedChoices,
default: 'all',
}),
handler: async ({ type }) => {
handler: async ({ type, swe }) => {
const pool = await createPoolAndDatabaseIfNeeded();
if (swe && (await isConfigsTableExists(pool))) {
log.info('Seeding skipped');
await pool.end();
return;
}
try {
// Cannot avoid `as` since the official type definition of `yargs` doesn't work.
// The value of `type` can be ensured, so it's safe to use `as` here.

View file

@ -7,11 +7,20 @@ import {
AlterationStateKey,
} from '@logto/schemas';
import { convertToIdentifiers } from '@logto/shared';
import { Nullable } from '@silverhand/essentials';
import { DatabasePool, DatabaseTransactionConnection, sql } from 'slonik';
import { z } from 'zod';
const { table, fields } = convertToIdentifiers(LogtoConfigs);
export const isConfigsTableExists = async (pool: DatabasePool) => {
const { rows } = await pool.query<Nullable<string>>(
sql`select to_regclass(${LogtoConfigs.table})`
);
return Boolean(rows[0]);
};
export const getRowsByKeys = async (
pool: DatabasePool | DatabaseTransactionConnection,
keys: LogtoConfigKey[]