Revamp workspace archive handling with async support, input validation improvements and better session management
This commit is contained in:
@@ -2,6 +2,7 @@ export function createAccountStatements(db) {
|
||||
return {
|
||||
insertUser: db.prepare("INSERT INTO users (username, password) VALUES (?, ?)"),
|
||||
getUser: db.prepare("SELECT username, password, role, session_version FROM users WHERE username = ?"),
|
||||
getUserCaseInsensitive: db.prepare("SELECT username, password, role, session_version FROM users WHERE LOWER(username) = LOWER(?)"),
|
||||
updateUsername: db.prepare("UPDATE users SET username = ? WHERE username = ?"),
|
||||
updatePassword: db.prepare("UPDATE users SET password = ? WHERE username = ?"),
|
||||
incrementSessionVersion: db.prepare("UPDATE users SET session_version = COALESCE(session_version, 0) + 1 WHERE username = ?"),
|
||||
@@ -68,6 +69,7 @@ const deleteUserTransaction = db.transaction((username) => {
|
||||
});
|
||||
return {
|
||||
getUser: (username) => statements.getUser.get(username),
|
||||
getUserCaseInsensitive: (username) => statements.getUserCaseInsensitive.get(username),
|
||||
insertUser: (username, passwordHash) => statements.insertUser.run(username, passwordHash),
|
||||
updatePassword: (passwordHash, username) => statements.updatePassword.run(passwordHash, username),
|
||||
incrementSessionVersion(username) {
|
||||
|
||||
@@ -33,6 +33,11 @@ export function initializeAttachments(db) {
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS upload_leases_user ON upload_leases(username);
|
||||
CREATE TABLE IF NOT EXISTS workspace_operation_leases (
|
||||
username TEXT PRIMARY KEY REFERENCES users(username) ON DELETE CASCADE,
|
||||
token TEXT NOT NULL UNIQUE,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TRIGGER IF NOT EXISTS release_message_attachment AFTER DELETE ON message_attachments
|
||||
BEGIN
|
||||
DELETE FROM attachments WHERE id = OLD.attachment_id AND status = 'attached'
|
||||
|
||||
+5
-1
@@ -240,7 +240,7 @@ export function initializeSchema(db) {
|
||||
`).run();
|
||||
}
|
||||
|
||||
export function runMigrations(db, {dropLegacyChats = true} = {}) {
|
||||
export function runMigrations(db, {dropLegacyChats = false} = {}) {
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version INTEGER PRIMARY KEY,
|
||||
applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
@@ -265,6 +265,7 @@ export function runMigrations(db, {dropLegacyChats = true} = {}) {
|
||||
ensureColumn("users", userColumns, "role", "ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('user', 'admin'))");
|
||||
ensureColumn("users", userColumns, "session_version", "ALTER TABLE users ADD COLUMN session_version INTEGER DEFAULT 0");
|
||||
db.prepare("UPDATE users SET session_version = 0 WHERE session_version IS NULL").run();
|
||||
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS users_username_lower ON users (LOWER(username))");
|
||||
|
||||
const personaColumns = db.prepare("PRAGMA table_info(personas)").all();
|
||||
ensureColumn("personas", personaColumns, "custom_fields", "ALTER TABLE personas ADD COLUMN custom_fields TEXT");
|
||||
@@ -500,6 +501,9 @@ export function runMigrations(db, {dropLegacyChats = true} = {}) {
|
||||
|
||||
if (dropLegacyChats) {
|
||||
db.prepare("DROP TABLE chats").run();
|
||||
} else {
|
||||
const backupName = `chats_legacy_backup_${Date.now()}`;
|
||||
db.prepare(`ALTER TABLE chats RENAME TO "${backupName}"`).run();
|
||||
}
|
||||
db.prepare("INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)").run(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user