Skip to content

Commit f4bf567

Browse files
committed
chore: revert acquireWriteLock function
1 parent f2c5ab5 commit f4bf567

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

src/database/index.ts

+16-41
Original file line numberDiff line numberDiff line change
@@ -95,51 +95,26 @@ export class Database {
9595
async acquireWriteLock() {
9696
const client = await this.#connectionPool.connect();
9797

98-
// Helper function to generate lock ID based on schema name
99-
const generateLockId = (schemaName: string): number => {
100-
return schemaName.split("").reduce((acc, char) => {
101-
return acc + char.charCodeAt(0);
102-
}, 0);
103-
};
104-
105-
// Helper function to acquire a lock for a specific schema
106-
const acquireLockForSchema = async (lockId: number) => {
98+
// generate lock id based on schema
99+
const lockId = this.chainDataSchemaName.split("").reduce((acc, char) => {
100+
return acc + char.charCodeAt(0);
101+
}, 0);
102+
103+
try {
107104
const result = await client.query(
108105
`SELECT pg_try_advisory_lock(${lockId}) as lock`
109106
);
110-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
111-
return result.rows[0].lock === true;
112-
};
113-
114-
// Helper function to release a lock for a specific schema
115-
const releaseLockForSchema = async (lockId: number) => {
116-
await client.query(`SELECT pg_advisory_unlock(${lockId})`);
117-
};
118-
119-
// Acquire locks for all schemas
120-
const chainDataLockId = generateLockId(this.chainDataSchemaName);
121107

122-
// Track acquired locks
123-
const acquiredLocks: number[] = [];
124-
125-
try {
126-
const chainDataLockAcquired = await acquireLockForSchema(chainDataLockId);
127-
if (chainDataLockAcquired) acquiredLocks.push(chainDataLockId);
128-
129-
this.#logger.info(`Lock Status =>
130-
Chain Data (${chainDataLockId}): ${chainDataLockAcquired}
131-
`);
132-
133-
return {
134-
release: async () => {
135-
for (const lockId of acquiredLocks) {
136-
await releaseLockForSchema(lockId);
137-
}
138-
client.release();
139-
},
140-
client,
141-
acquiredLocks,
142-
};
108+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
109+
if (result.rows[0].lock === true) {
110+
return {
111+
release: async () => {
112+
await client.query(`SELECT pg_advisory_unlock(${lockId})`);
113+
client.release();
114+
},
115+
client,
116+
};
117+
}
143118
} catch (error) {
144119
this.#logger.error({ error }, "Failed to acquire write lock");
145120
}

0 commit comments

Comments
 (0)