Skip to content

Commit 4faf33b

Browse files
committed
Fix migration ordering and test detection for branch databases
- Add score_locks migration for the editing feature - Detect Vitest runs via VITEST env var to skip branch-specific database suffix in unit and e2e tests - Move checkOrphans before main in db-cleanup.ts to fix const hoisting Signed-off-by: Mike Lischke <mike@lischke-online.de>
1 parent 2e035bb commit 4faf33b

3 files changed

Lines changed: 37 additions & 36 deletions

File tree

build/db-cleanup.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ const getLocalBranches = (): Set<string> => {
5151
}
5252
};
5353

54+
const checkOrphans = (dbNames: string[], expectedDbs: Set<string>, prefix: string): void => {
55+
const orphans: string[] = [];
56+
57+
for (const dbName of dbNames) {
58+
if (dbName.includes("__") && !expectedDbs.has(dbName)) {
59+
orphans.push(dbName);
60+
}
61+
}
62+
63+
if (orphans.length === 0) {
64+
console.log("No orphaned branch databases found.");
65+
66+
return;
67+
}
68+
69+
console.log("Orphaned branch databases (branch no longer exists locally):");
70+
71+
for (const db of orphans) {
72+
console.log(` - ${db}`);
73+
}
74+
75+
console.log(
76+
"\nTo drop these databases manually, run:\n"
77+
+ orphans.map((db) => {
78+
return " echo 'DROP DATABASE `" + db + "`;' | mysql -u root -p";
79+
}).join("\n"),
80+
);
81+
};
82+
5483
const main = async (): Promise<void> => {
5584
const config = loadConfig();
5685

@@ -128,40 +157,6 @@ const main = async (): Promise<void> => {
128157
}
129158
};
130159

131-
const checkOrphans = (
132-
dbNames: string[],
133-
expectedDbs: Set<string>,
134-
prefix: string,
135-
): void => {
136-
const orphans: string[] = [];
137-
138-
for (const dbName of dbNames) {
139-
// Only check branch-specific databases (those with the __ prefix).
140-
if (dbName.includes("__") && !expectedDbs.has(dbName)) {
141-
orphans.push(dbName);
142-
}
143-
}
144-
145-
if (orphans.length === 0) {
146-
console.log("No orphaned branch databases found.");
147-
148-
return;
149-
}
150-
151-
console.log("Orphaned branch databases (branch no longer exists locally):");
152-
153-
for (const db of orphans) {
154-
console.log(` - ${db}`);
155-
}
156-
157-
console.log(
158-
"\nTo drop these databases manually, run:\n"
159-
+ orphans.map((db) => {
160-
return " echo 'DROP DATABASE `" + db + "`;' | mysql -u root -p";
161-
}).join("\n"),
162-
);
163-
};
164-
165160
main().catch((e: unknown) => {
166161
console.error("Cleanup failed:", e);
167162
process.exit(1);

build/migration.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const getCurrentBranch = (): string => {
141141
* For feature branches the pattern is `<baseName>__<branchName>`.
142142
*
143143
* @param baseName The database name from backend-config.json.
144-
* @param branch The current Git branch name.
144+
* @param branch The branch name to derive the suffix from.
145145
*
146146
* @returns The effective database name.
147147
*/
@@ -162,7 +162,12 @@ export const deriveDbName = (baseName: string, branch: string): string => {
162162
*/
163163
export const runMigrations = async (config: IDatabaseConfig): Promise<string> => {
164164
const branch = getCurrentBranch();
165-
const effectiveDb = deriveDbName(config.database, branch);
165+
166+
// When running under Vitest (unit or e2e), use the base database name directly —
167+
// no branch-specific suffix.
168+
const effectiveDb = process.env.VITEST
169+
? config.database
170+
: deriveDbName(config.database, branch);
166171

167172
console.log(`Branch: ${branch}, database: ${effectiveDb}`);
168173

tests/e2e/setup-real-db.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ test.describe.serial("Setup: real database integration", () => {
8484
DB_NAME: testDbName,
8585
DB_USER: dbUser,
8686
DB_PASSWORD: dbPassword,
87+
VITEST: "true",
8788
},
8889
stdio: "pipe",
8990
});

0 commit comments

Comments
 (0)