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:
parent
0c4fd3c7be
commit
c0eaf66c26
2 changed files with 31 additions and 8 deletions
|
@ -12,6 +12,7 @@ import { z } from 'zod';
|
||||||
import { createPoolAndDatabaseIfNeeded, insertInto } from '../../../database';
|
import { createPoolAndDatabaseIfNeeded, insertInto } from '../../../database';
|
||||||
import {
|
import {
|
||||||
getRowsByKeys,
|
getRowsByKeys,
|
||||||
|
isConfigsTableExists,
|
||||||
updateDatabaseTimestamp,
|
updateDatabaseTimestamp,
|
||||||
updateValueByKey,
|
updateValueByKey,
|
||||||
} from '../../../queries/logto-config';
|
} 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]',
|
command: 'seed [type]',
|
||||||
describe: 'Create database then seed tables and data',
|
describe: 'Create database then seed tables and data',
|
||||||
builder: (yargs) =>
|
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',
|
describe: 'Optional seed type',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
choices: seedChoices,
|
choices: seedChoices,
|
||||||
default: 'all',
|
default: 'all',
|
||||||
}),
|
}),
|
||||||
handler: async ({ type }) => {
|
handler: async ({ type, swe }) => {
|
||||||
const pool = await createPoolAndDatabaseIfNeeded();
|
const pool = await createPoolAndDatabaseIfNeeded();
|
||||||
|
|
||||||
|
if (swe && (await isConfigsTableExists(pool))) {
|
||||||
|
log.info('Seeding skipped');
|
||||||
|
await pool.end();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Cannot avoid `as` since the official type definition of `yargs` doesn't work.
|
// 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.
|
// The value of `type` can be ensured, so it's safe to use `as` here.
|
||||||
|
|
|
@ -7,11 +7,20 @@ import {
|
||||||
AlterationStateKey,
|
AlterationStateKey,
|
||||||
} from '@logto/schemas';
|
} from '@logto/schemas';
|
||||||
import { convertToIdentifiers } from '@logto/shared';
|
import { convertToIdentifiers } from '@logto/shared';
|
||||||
|
import { Nullable } from '@silverhand/essentials';
|
||||||
import { DatabasePool, DatabaseTransactionConnection, sql } from 'slonik';
|
import { DatabasePool, DatabaseTransactionConnection, sql } from 'slonik';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
const { table, fields } = convertToIdentifiers(LogtoConfigs);
|
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 (
|
export const getRowsByKeys = async (
|
||||||
pool: DatabasePool | DatabaseTransactionConnection,
|
pool: DatabasePool | DatabaseTransactionConnection,
|
||||||
keys: LogtoConfigKey[]
|
keys: LogtoConfigKey[]
|
||||||
|
|
Loading…
Add table
Reference in a new issue