fix: mysql transactions do not work with run property of sqlite (#1974)

* fix: mysql transactions do not work with run property of sqlite

* fix: ci issues
This commit is contained in:
Meier Lukas
2025-01-17 12:53:01 +01:00
committed by GitHub
parent 7622f0a4d2
commit a1084f91e5
9 changed files with 677 additions and 267 deletions

View File

@@ -1,5 +1,5 @@
import { objectEntries } from "@homarr/common";
import type { Database, InferInsertModel } from "@homarr/db";
import type { Database, HomarrDatabaseMysql, InferInsertModel } from "@homarr/db";
import * as schema from "@homarr/db/schema";
type TableKey = {
@@ -29,5 +29,15 @@ export const createDbInsertCollection = <TTableKey extends TableKey>(tablesInIns
}
});
},
insertAllAsync: async (db: HomarrDatabaseMysql) => {
await db.transaction(async (transaction) => {
for (const [key, values] of objectEntries(context)) {
if (values.length >= 1) {
// Below is actually the mysqlSchema when the driver is mysql
await transaction.insert(schema[key] as never).values(values as never);
}
}
});
},
};
};

View File

@@ -1,6 +1,7 @@
import type { z } from "zod";
import { Stopwatch } from "@homarr/common";
import { handleTransactionsAsync } from "@homarr/db";
import type { Database } from "@homarr/db";
import { logger } from "@homarr/log";
@@ -34,11 +35,21 @@ export const importInitialOldmarrAsync = async (
logger.info("Inserting import data to database");
// Due to a limitation with better-sqlite it's only possible to use it synchronously
db.transaction((transaction) => {
boardInsertCollection.insertAll(transaction);
userInsertCollection.insertAll(transaction);
integrationInsertCollection.insertAll(transaction);
await handleTransactionsAsync(db, {
async handleAsync(db) {
await db.transaction(async (transaction) => {
await boardInsertCollection.insertAllAsync(transaction);
await userInsertCollection.insertAllAsync(transaction);
await integrationInsertCollection.insertAllAsync(transaction);
});
},
handleSync(db) {
db.transaction((transaction) => {
boardInsertCollection.insertAll(transaction);
userInsertCollection.insertAll(transaction);
integrationInsertCollection.insertAll(transaction);
});
},
});
logger.info(`Import successful (in ${stopwatch.getElapsedInHumanWords()})`);

View File

@@ -1,4 +1,4 @@
import { inArray } from "@homarr/db";
import { handleTransactionsAsync, inArray } from "@homarr/db";
import type { Database } from "@homarr/db";
import { apps } from "@homarr/db/schema";
import type { OldmarrConfig } from "@homarr/old-schema";
@@ -31,6 +31,12 @@ export const importSingleOldmarrConfigAsync = async (
const boardInsertCollection = createBoardInsertCollection({ preparedApps, preparedBoards }, settings);
// Due to a limitation with better-sqlite it's only possible to use it synchronously
boardInsertCollection.insertAll(db);
await handleTransactionsAsync(db, {
async handleAsync(db) {
await boardInsertCollection.insertAllAsync(db);
},
handleSync(db) {
boardInsertCollection.insertAll(db);
},
});
};