feat: removed transaction from init

This commit is contained in:
=
2025-02-10 15:46:39 +05:30
parent 1b15cb4c35
commit ddc819dda1
3 changed files with 30 additions and 22 deletions
@@ -1,10 +1,24 @@
import { TDbClient } from "@app/db"; import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas"; import { TableName } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { ormify } from "@app/lib/knex"; import { ormify } from "@app/lib/knex";
import { Knex } from "knex";
export type TKmsRootConfigDALFactory = ReturnType<typeof kmsRootConfigDALFactory>; export type TKmsRootConfigDALFactory = ReturnType<typeof kmsRootConfigDALFactory>;
export const kmsRootConfigDALFactory = (db: TDbClient) => { export const kmsRootConfigDALFactory = (db: TDbClient) => {
const kmsOrm = ormify(db, TableName.KmsServerRootConfig); const kmsOrm = ormify(db, TableName.KmsServerRootConfig);
return kmsOrm;
const findById = async (id: string, tx?: Knex) => {
try {
const result = await (tx || db)(TableName.KmsServerRootConfig)
.where({ id } as never)
.first("*");
return result;
} catch (error) {
throw new DatabaseError({ error, name: "Find by id" });
}
};
return { ...kmsOrm, findById };
}; };
+7 -10
View File
@@ -875,7 +875,7 @@ export const kmsServiceFactory = ({
const kmsRootConfig = await kmsRootConfigDAL.transaction(async (tx) => { const kmsRootConfig = await kmsRootConfigDAL.transaction(async (tx) => {
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsRootKeyInit]); await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsRootKeyInit]);
// check if KMS root key was already generated and saved in DB // check if KMS root key was already generated and saved in DB
const existingRootConfig = await kmsRootConfigDAL.findById(KMS_ROOT_CONFIG_UUID, tx); const existingRootConfig = await kmsRootConfigDAL.findById(KMS_ROOT_CONFIG_UUID);
if (existingRootConfig) return existingRootConfig; if (existingRootConfig) return existingRootConfig;
logger.info("KMS: Generating new ROOT Key"); logger.info("KMS: Generating new ROOT Key");
@@ -885,15 +885,12 @@ export const kmsServiceFactory = ({
throw err; throw err;
}); });
const newRootConfig = await kmsRootConfigDAL.create( const newRootConfig = await kmsRootConfigDAL.create({
{ // @ts-expect-error id is kept as fixed for idempotence and to avoid race condition
// @ts-expect-error id is kept as fixed for idempotence and to avoid race condition id: KMS_ROOT_CONFIG_UUID,
id: KMS_ROOT_CONFIG_UUID, encryptedRootKey,
encryptedRootKey, encryptionStrategy: RootKeyEncryptionStrategy.Software
encryptionStrategy: RootKeyEncryptionStrategy.Software });
},
tx
);
return newRootConfig; return newRootConfig;
}); });
@@ -89,19 +89,16 @@ export const superAdminServiceFactory = ({
await keyStore.deleteItem(ADMIN_CONFIG_KEY); await keyStore.deleteItem(ADMIN_CONFIG_KEY);
const serverCfg = await serverCfgDAL.transaction(async (tx) => { const serverCfg = await serverCfgDAL.transaction(async (tx) => {
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.SuperAdminInit]); await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.SuperAdminInit]);
const serverCfgInDB = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID, tx); const serverCfgInDB = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
if (serverCfgInDB) return serverCfgInDB; if (serverCfgInDB) return serverCfgInDB;
const newCfg = await serverCfgDAL.create( const newCfg = await serverCfgDAL.create({
{ // @ts-expect-error id is kept as fixed for idempotence and to avoid race condition
// @ts-expect-error id is kept as fixed for idempotence and to avoid race condition id: ADMIN_CONFIG_DB_UUID,
id: ADMIN_CONFIG_DB_UUID, initialized: false,
initialized: false, allowSignUp: true,
allowSignUp: true, defaultAuthOrgId: null
defaultAuthOrgId: null });
},
tx
);
return newCfg; return newCfg;
}); });
return serverCfg; return serverCfg;