invy/backend/schema.sql
2025-12-29 05:49:05 +02:00

106 lines
3.8 KiB
PL/PgSQL

-- Wedding Guest List Database Schema
-- PostgreSQL Database Setup Script
-- ============================================
-- STEP 1: Create Database User
-- ============================================
-- Create a dedicated user for the wedding guest list application
-- Change the password to something secure!
CREATE USER wedding_admin WITH PASSWORD 'your_secure_password_here';
-- Grant user the ability to create databases (optional)
ALTER USER wedding_admin CREATEDB;
-- ============================================
-- STEP 2: Create Database
-- ============================================
-- Create the wedding guests database
CREATE DATABASE wedding_guests
WITH
OWNER = wedding_admin
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
TEMPLATE = template0;
-- Connect to the new database
\c wedding_guests
-- ============================================
-- STEP 3: Grant Schema Privileges
-- ============================================
-- Grant all privileges on the public schema to the user
GRANT ALL PRIVILEGES ON SCHEMA public TO wedding_admin;
-- Grant privileges on all current and future tables
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO wedding_admin;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO wedding_admin;
-- Ensure the user has privileges on future objects
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO wedding_admin;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO wedding_admin;
-- ============================================
-- STEP 4: Create Tables
-- ============================================
-- Guests table
CREATE TABLE IF NOT EXISTS guests (
id SERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE,
phone_number VARCHAR(50),
rsvp_status VARCHAR(20) DEFAULT 'pending' CHECK (rsvp_status IN ('pending', 'accepted', 'declined')),
meal_preference VARCHAR(50),
has_plus_one BOOLEAN DEFAULT FALSE,
plus_one_name VARCHAR(200),
owner VARCHAR(50),
notes TEXT,
table_number INTEGER,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE
);
-- Create indexes for better query performance
CREATE INDEX idx_guests_email ON guests(email);
CREATE INDEX idx_guests_rsvp_status ON guests(rsvp_status);
CREATE INDEX idx_guests_last_name ON guests(last_name);
CREATE INDEX idx_guests_owner ON guests(owner);
-- Create a trigger to automatically update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_guests_updated_at
BEFORE UPDATE ON guests
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Grant table ownership to wedding_admin
ALTER TABLE guests OWNER TO wedding_admin;
-- ============================================
-- STEP 5: Insert Sample Data (Optional)
-- ============================================
-- Uncomment the lines below to insert sample guests for testing
-- INSERT INTO guests (first_name, last_name, email, phone_number, rsvp_status, meal_preference, has_plus_one)
-- VALUES
-- ('John', 'Doe', 'john.doe@example.com', '+1-555-0101', 'accepted', 'vegetarian', TRUE),
-- ('Jane', 'Smith', 'jane.smith@example.com', '+1-555-0102', 'pending', 'vegan', FALSE),
-- ('Bob', 'Johnson', 'bob.j@example.com', '+1-555-0103', 'accepted', 'gluten-free', TRUE),
-- ('Alice', 'Williams', 'alice.w@example.com', '+1-555-0104', 'declined', NULL, FALSE);
-- ============================================
-- Verification Query
-- ============================================
-- Run this to verify the setup
SELECT 'Database setup completed successfully!' AS status;
SELECT COUNT(*) AS total_guests FROM guests;