add migration for 1.6.0

This commit is contained in:
miloschwartz 2025-06-19 15:58:05 -04:00
parent 1bf2e23f5d
commit f300838f8e
No known key found for this signature in database
3 changed files with 37 additions and 2 deletions

View file

@ -211,6 +211,10 @@ export async function validateOidcCallback(
logger.debug("User email", { email }); logger.debug("User email", { email });
logger.debug("User name", { name }); logger.debug("User name", { name });
if (email) {
email = email.toLowerCase();
}
const [existingUser] = await db const [existingUser] = await db
.select() .select()
.from(users) .from(users)

View file

@ -1,4 +1,6 @@
import { db } from "@server/db/pg/driver";
import { configFilePath1, configFilePath2 } from "@server/lib/consts"; import { configFilePath1, configFilePath2 } from "@server/lib/consts";
import { sql } from "drizzle-orm";
import fs from "fs"; import fs from "fs";
import yaml from "js-yaml"; import yaml from "js-yaml";
@ -7,6 +9,15 @@ const version = "1.6.0";
export default async function migration() { export default async function migration() {
console.log(`Running setup script ${version}...`); console.log(`Running setup script ${version}...`);
try {
db.execute(sql`UPDATE 'user' SET email = LOWER(email);`);
db.execute(sql`UPDATE 'user' SET username = LOWER(username);`);
console.log(`Migrated database schema`);
} catch (e) {
console.log("Unable to make all usernames and emails lowercase");
console.log(e);
}
try { try {
// Determine which config file exists // Determine which config file exists
const filePaths = [configFilePath1, configFilePath2]; const filePaths = [configFilePath1, configFilePath2];

View file

@ -1,12 +1,32 @@
import { configFilePath1, configFilePath2 } from "@server/lib/consts"; import { APP_PATH, configFilePath1, configFilePath2 } from "@server/lib/consts";
import Database from "better-sqlite3";
import fs from "fs"; import fs from "fs";
import yaml from "js-yaml"; import yaml from "js-yaml";
import path from "path";
const version = "1.6.0"; const version = "1.6.0";
export default async function migration() { export default async function migration() {
console.log(`Running setup script ${version}...`); console.log(`Running setup script ${version}...`);
const location = path.join(APP_PATH, "db", "db.sqlite");
const db = new Database(location);
try {
db.pragma("foreign_keys = OFF");
db.transaction(() => {
db.exec(`
UPDATE 'user' SET email = LOWER(email);
UPDATE 'user' SET username = LOWER(username);
`);
})(); // <-- executes the transaction immediately
db.pragma("foreign_keys = ON");
console.log(`Migrated database schema`);
} catch (e) {
console.log("Unable to make all usernames and emails lowercase");
console.log(e);
}
try { try {
// Determine which config file exists // Determine which config file exists
const filePaths = [configFilePath1, configFilePath2]; const filePaths = [configFilePath1, configFilePath2];
@ -39,7 +59,7 @@ export default async function migration() {
console.log(`Set trust_proxy to 1 in config file`); console.log(`Set trust_proxy to 1 in config file`);
} catch (e) { } catch (e) {
console.log(`Unable to migrate config file. Error: ${e}`); console.log(`Unable to migrate config file. Please do it manually. Error: ${e}`);
} }
console.log(`${version} migration complete`); console.log(`${version} migration complete`);