fix: use view recreate

This commit is contained in:
Morten Olsen
2025-11-04 13:17:44 +01:00
parent 5cb31324ee
commit e3398e1968

View File

@@ -75,19 +75,22 @@ class ViewsService {
const dbService = this.#services.get(DatabaseService); const dbService = this.#services.get(DatabaseService);
const db = await dbService.getInstance(); const db = await dbService.getInstance();
const subquery = db.raw(query); const subquery = db.raw(query);
await db.schema.createViewOrReplace(name, (view) => { await db.transaction(async (trx) => {
await trx.schema.dropViewIfExists(name);
await trx.schema.createViewOrReplace(name, (view) => {
// view.columns(columns); // view.columns(columns);
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
view.as(subquery as any); view.as(subquery as any);
}); });
if (description) { if (description) {
const sql = db.raw(`COMMENT ON VIEW ?? IS ?;`, [name, description]); const sql = trx.raw(`COMMENT ON VIEW ?? IS ?;`, [name, description]);
await db.raw(sql.toQuery()); await trx.raw(sql.toQuery());
} }
for (const [columnName, info] of Object.entries(columns)) { for (const [columnName, info] of Object.entries(columns)) {
const sql = db.raw(`COMMENT ON COLUMN ??.?? IS ?;`, [name, columnName, info.description || null]); const sql = trx.raw(`COMMENT ON COLUMN ??.?? IS ?;`, [name, columnName, info.description || null]);
await db.raw(sql.toQuery()); await trx.raw(sql.toQuery());
} }
});
}; };
public remove = async (name: string) => { public remove = async (name: string) => {