mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
refactor(core): use config key enum
This commit is contained in:
parent
a5280a2afd
commit
c324e29df3
3 changed files with 17 additions and 13 deletions
|
@ -1,6 +1,3 @@
|
|||
import { LogtoConfigKey } from '@logto/schemas';
|
||||
|
||||
export const alterationStateKey: LogtoConfigKey = 'alterationState';
|
||||
export const logtoConfigsTableFilePath = 'node_modules/@logto/schemas/tables/logto_configs.sql';
|
||||
export const alterationFilesDirectorySource = 'node_modules/@logto/schemas/alterations';
|
||||
export const alterationFilesDirectory = 'alterations/';
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
import { LogtoConfigs } from '@logto/schemas';
|
||||
import { LogtoConfigKey, LogtoConfigs } from '@logto/schemas';
|
||||
import { createMockPool, createMockQueryResult, sql } from 'slonik';
|
||||
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
import { QueryType, expectSqlAssert } from '@/utils/test-utils';
|
||||
|
||||
import * as functions from '.';
|
||||
import { alterationStateKey } from './constants';
|
||||
|
||||
const mockQuery: jest.MockedFunction<QueryType> = jest.fn();
|
||||
const {
|
||||
|
@ -59,7 +58,7 @@ describe('getCurrentDatabaseTimestamp()', () => {
|
|||
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql.sql);
|
||||
expect(values).toEqual([alterationStateKey]);
|
||||
expect(values).toEqual([LogtoConfigKey.AlterationState]);
|
||||
|
||||
return createMockQueryResult([]);
|
||||
});
|
||||
|
@ -74,7 +73,7 @@ describe('getCurrentDatabaseTimestamp()', () => {
|
|||
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql.sql);
|
||||
expect(values).toEqual([alterationStateKey]);
|
||||
expect(values).toEqual([LogtoConfigKey.AlterationState]);
|
||||
|
||||
return createMockQueryResult([{ value: 'some_value' }]);
|
||||
});
|
||||
|
@ -89,7 +88,7 @@ describe('getCurrentDatabaseTimestamp()', () => {
|
|||
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql.sql);
|
||||
expect(values).toEqual([alterationStateKey]);
|
||||
expect(values).toEqual([LogtoConfigKey.AlterationState]);
|
||||
|
||||
// @ts-expect-error createMockQueryResult doesn't support jsonb
|
||||
return createMockQueryResult([{ value: { timestamp, updatedAt: 'now' } }]);
|
||||
|
@ -148,7 +147,10 @@ describe('updateDatabaseTimestamp()', () => {
|
|||
it('sends upsert sql with timestamp and updatedAt', async () => {
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql.sql);
|
||||
expect(values).toEqual([alterationStateKey, JSON.stringify({ timestamp, updatedAt })]);
|
||||
expect(values).toEqual([
|
||||
LogtoConfigKey.AlterationState,
|
||||
JSON.stringify({ timestamp, updatedAt }),
|
||||
]);
|
||||
|
||||
return createMockQueryResult([]);
|
||||
});
|
||||
|
|
|
@ -2,7 +2,13 @@ import { existsSync } from 'fs';
|
|||
import { readdir, readFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
import { LogtoConfig, LogtoConfigs, AlterationState, alterationStateGuard } from '@logto/schemas';
|
||||
import {
|
||||
LogtoConfig,
|
||||
LogtoConfigs,
|
||||
AlterationState,
|
||||
alterationStateGuard,
|
||||
LogtoConfigKey,
|
||||
} from '@logto/schemas';
|
||||
import { AlterationScript } from '@logto/schemas/lib/types/alteration';
|
||||
import { conditionalString } from '@silverhand/essentials';
|
||||
import chalk from 'chalk';
|
||||
|
@ -14,7 +20,6 @@ import { convertToIdentifiers } from '@/database/utils';
|
|||
|
||||
import {
|
||||
logtoConfigsTableFilePath,
|
||||
alterationStateKey,
|
||||
alterationFilesDirectory,
|
||||
alterationFilesDirectorySource,
|
||||
} from './constants';
|
||||
|
@ -38,7 +43,7 @@ export const isLogtoConfigsTableExists = async (pool: DatabasePool) => {
|
|||
export const getCurrentDatabaseTimestamp = async (pool: DatabasePool) => {
|
||||
try {
|
||||
const query = await pool.maybeOne<LogtoConfig>(
|
||||
sql`select * from ${table} where ${fields.key}=${alterationStateKey}`
|
||||
sql`select * from ${table} where ${fields.key}=${LogtoConfigKey.AlterationState}`
|
||||
);
|
||||
const { timestamp } = alterationStateGuard.parse(query?.value);
|
||||
|
||||
|
@ -66,7 +71,7 @@ export const updateDatabaseTimestamp = async (pool: DatabasePool, timestamp?: nu
|
|||
await pool.query(
|
||||
sql`
|
||||
insert into ${table} (${fields.key}, ${fields.value})
|
||||
values (${alterationStateKey}, ${sql.jsonb(value)})
|
||||
values (${LogtoConfigKey.AlterationState}, ${sql.jsonb(value)})
|
||||
on conflict (${fields.key}) do update set ${fields.value}=excluded.${fields.value}
|
||||
`
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue