From c324e29df3b4b80b6487839fd4ed63b0c9db70d6 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 7 Oct 2022 20:26:50 +0800 Subject: [PATCH] refactor(core): use config key enum --- packages/core/src/alteration/constants.ts | 3 --- packages/core/src/alteration/index.test.ts | 14 ++++++++------ packages/core/src/alteration/index.ts | 13 +++++++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/core/src/alteration/constants.ts b/packages/core/src/alteration/constants.ts index 76d18ba7d..077a1b68b 100644 --- a/packages/core/src/alteration/constants.ts +++ b/packages/core/src/alteration/constants.ts @@ -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/'; diff --git a/packages/core/src/alteration/index.test.ts b/packages/core/src/alteration/index.test.ts index 6098fe329..651736ff7 100644 --- a/packages/core/src/alteration/index.test.ts +++ b/packages/core/src/alteration/index.test.ts @@ -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 = 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([]); }); diff --git a/packages/core/src/alteration/index.ts b/packages/core/src/alteration/index.ts index e593faaf0..bb9468b94 100644 --- a/packages/core/src/alteration/index.ts +++ b/packages/core/src/alteration/index.ts @@ -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( - 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} ` );