feat(db): add guests table and claimed_by_guest_id column
This commit is contained in:
@@ -103,6 +103,16 @@ export async function initializeDatabase() {
|
||||
)
|
||||
`);
|
||||
|
||||
// Create guests table
|
||||
sqlite.exec(`
|
||||
CREATE TABLE IF NOT EXISTS guests (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
created_at INTEGER DEFAULT (unixepoch()) NOT NULL,
|
||||
updated_at INTEGER DEFAULT (unixepoch()) NOT NULL
|
||||
)
|
||||
`);
|
||||
|
||||
// Run migrations for existing databases
|
||||
try {
|
||||
const columns = sqlite.pragma('table_info(wishlists)') as Array<{ name: string }>;
|
||||
@@ -127,6 +137,26 @@ export async function initializeDatabase() {
|
||||
sqlite.exec('ALTER TABLE wishlists ADD COLUMN preferences TEXT');
|
||||
console.log('✅ Added preferences column to wishlists table');
|
||||
}
|
||||
|
||||
const itemColumns = sqlite.pragma('table_info(wishlist_items)') as Array<{ name: string }>;
|
||||
|
||||
const hasClaimedByGuestId = itemColumns.some((col) => col.name === 'claimed_by_guest_id');
|
||||
if (!hasClaimedByGuestId) {
|
||||
sqlite.exec('ALTER TABLE wishlist_items ADD COLUMN claimed_by_guest_id TEXT REFERENCES guests(id) ON DELETE SET NULL');
|
||||
console.log('✅ Added claimed_by_guest_id column to wishlist_items');
|
||||
}
|
||||
|
||||
const hasClaimedByName = itemColumns.some((col) => col.name === 'claimed_by_name');
|
||||
const hasClaimedByToken = itemColumns.some((col) => col.name === 'claimed_by_token');
|
||||
if (hasClaimedByName || hasClaimedByToken) {
|
||||
if (hasClaimedByName) {
|
||||
sqlite.exec('UPDATE wishlist_items SET claimed_by_name = NULL');
|
||||
}
|
||||
if (hasClaimedByToken) {
|
||||
sqlite.exec('UPDATE wishlist_items SET claimed_by_token = NULL');
|
||||
}
|
||||
console.log('ℹ️ Legacy claim columns nullified (claimed_by_name / claimed_by_token)');
|
||||
}
|
||||
} catch (migrationError) {
|
||||
console.log('Migration already applied or not needed');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user