| 4 | | In this phase we implement advanced database mechanisms that enforce correctness and improve performance in the Wedding Planner system. |
| 5 | | Instead of relying only on application logic, we move important rules directly into PostgreSQL using indexes, constraints, triggers and views. |
| | 6 | In this phase we implement: |
| | 7 | * Indexes – faster searching/filtering in large tables |
| | 8 | * Constraints – enforce validity and prevent inconsistent records |
| | 9 | * Triggers – automate rules (price calculation, venue capacity control) |
| | 10 | * Views – simplify reporting queries and provide reusable reports |
| 8 | | * Indexes for faster searching/filtering (availability checks, RSVP reports) |
| 9 | | * Constraints for preventing invalid/inconsistent data |
| 10 | | * Triggers for automation of derived logic (pricing, capacity checks) |
| 11 | | * Views for simplified reporting (RSVP summary, confirmed bookings overview) |
| | 13 | * Indexes for frequent query patterns (events, bookings, RSVP) |
| | 14 | * Constraints for data validation + preventing overlapping bookings |
| | 15 | * Triggers to automate derived values (venue price) and enforce capacity |
| | 16 | * Views for simplified summaries (RSVP counts, confirmed bookings, guest counts) |
| 15 | | == 1) Indexes (Query Performance) == |
| 16 | | Indexes speed up database queries by avoiding full table scans. |
| 17 | | They are especially important in our system because availability checks and RSVP reports are executed frequently. |
| 18 | | |
| 19 | | === Index 1: Event date/time search === |
| 20 | | This index improves performance when searching or filtering events by date + start/end time. |
| 21 | | |
| 22 | | *Table:* `event` |
| | 20 | == Indexes (Performance Improvement) == |
| | 21 | |
| | 22 | === Index: Event Date and Time Search Optimization === |
| | 23 | This improves performance when searching or filtering events by date and time interval. |
| | 24 | |
| | 25 | *Tables affected:* `event` |
| | 26 | *Index type:* B-tree composite indexes |
| 29 | | *Why it matters:* |
| 30 | | When the system loads the schedule for a wedding day, the database can quickly find events in a time window (without scanning the whole table). |
| 31 | | |
| 32 | | === Index 2: Booking status filtering (availability checks) === |
| 33 | | Availability endpoints frequently filter bookings by date + status (only CONFIRMED bookings should block resources). |
| 34 | | |
| 35 | | *Tables:* `venue_booking`, `photographer_booking`, `band_booking` |
| | 33 | *Explanation:* |
| | 34 | The indexes store pre-sorted combinations of (`date`, `start_time`) and (`date`, `end_time`), which allows fast retrieval of events for a specific day/time window without scanning the entire table. |
| | 35 | |
| | 36 | --- |
| | 37 | |
| | 38 | === Index: Booking Status Filtering === |
| | 39 | These indexes optimize availability checks and booking filtering by date and status. |
| | 40 | |
| | 41 | *Tables affected:* `venue_booking`, `photographer_booking`, `band_booking` |
| | 42 | *Index type:* B-tree composite indexes |
| 43 | | *Reason:* |
| 44 | | These indexes make queries like “all confirmed bookings on date X” significantly faster. |
| 45 | | |
| 46 | | === Index 3: RSVP status report === |
| 47 | | RSVP summaries filter guests by status per event. |
| 48 | | |
| 49 | | *Table:* `event_rsvp` |
| | 50 | *Explanation:* |
| | 51 | Booking records are organized by (`date`, `status`), allowing fast retrieval of rows such as: “confirmed bookings on a given date”. |
| | 52 | |
| | 53 | --- |
| | 54 | |
| | 55 | === Index: RSVP Status Filtering === |
| | 56 | This index speeds up reports of RSVP responses per event. |
| | 57 | |
| | 58 | *Table affected:* `event_rsvp` |
| | 59 | *Index type:* Composite B-tree index |
| 60 | | == 2) Constraints (Consistency & Integrity) == |
| 61 | | Constraints guarantee correctness even if invalid data reaches the database. |
| 62 | | In Wedding Planner, this is critical for avoiding double bookings and enforcing valid status values. |
| 63 | | |
| 64 | | === Constraint 1: Prevent overlapping bookings (NO double booking) === |
| 65 | | A venue / photographer / band must not be booked in overlapping time intervals. |
| 66 | | |
| 67 | | To support this, we first create generated timestamp columns and enable GiST indexing. |
| | 70 | == Constraints (Data Integrity) == |
| | 71 | |
| | 72 | === Constraint: Prevent Overlapping Bookings === |
| | 73 | This prevents double-booking of the same venue/photographer/band at overlapping time intervals. |
| | 74 | |
| | 75 | To support this constraint, we use: |
| | 76 | * `btree_gist` extension |
| | 77 | * generated timestamp columns (`start_ts`, `end_ts`) |
| | 78 | * GiST exclusion constraints |
| 107 | | *Why this is important:* |
| 108 | | This enforces a real-world rule at database level: |
| 109 | | even if two users try to insert bookings at the same time, PostgreSQL blocks conflicts automatically. |
| 110 | | |
| 111 | | === Constraint 2: Status validation (CHECK constraints) === |
| 112 | | CHECK constraints restrict allowed values and prevent “random text statuses”. |
| | 120 | *Explanation:* |
| | 121 | The database rejects inserting a booking if the interval overlaps with another booking for the same resource (venue/band/photographer). |
| | 122 | |
| | 123 | --- |
| | 124 | |
| | 125 | === Constraint: Status and Value Validation === |
| | 126 | We prevent invalid values for status/role/budget using CHECK constraints. |
| 144 | | CHECK (role IN ('GUEST', 'BRIDE', 'GROOM', 'BEST_MAN', 'MAID_OF_HONOR')); |
| 145 | | }}} |
| 146 | | |
| 147 | | *Result:* |
| 148 | | The database rejects invalid states automatically, ensuring consistent data across the whole system. |
| 149 | | |
| 150 | | |
| 151 | | ---- |
| 152 | | |
| 153 | | == 3) Triggers (Automation of Business Logic) == |
| 154 | | Triggers automate derived logic and enforce rules without relying on the application. |
| 155 | | |
| 156 | | === Trigger 1: Automatic venue booking price calculation === |
| 157 | | The total venue booking price is derived from: |
| 158 | | `venue.price_per_guest * number_of_guests_for_wedding`. |
| | 159 | CHECK (role IN ('Guest', 'Bride', 'Groom', 'Best Man', 'Maid of Honor')); |
| | 160 | }}} |
| | 161 | |
| | 162 | *Explanation:* |
| | 163 | If an invalid value is inserted or updated, PostgreSQL rejects the operation and keeps the database consistent. |
| | 164 | |
| | 165 | == Triggers (Automation of Business Logic) == |
| | 166 | |
| | 167 | === Trigger: Automatic Venue Price Calculation === |
| | 168 | This trigger calculates the total venue booking price based on: |
| | 169 | * venue price per guest |
| | 170 | * current number of guests for that wedding |
| | 171 | |
| | 172 | *Tables involved:* `venue_booking`, `venue`, `guest` |
| | 173 | *Trigger type:* BEFORE INSERT on `venue_booking` |
| 181 | | *Why it matters:* |
| 182 | | This ensures consistent pricing and avoids manual calculation mistakes. |
| 183 | | |
| 184 | | === Trigger 2: Venue capacity enforcement === |
| 185 | | This trigger prevents inserting guests if venue capacity would be exceeded. |
| | 196 | *Reason for use:* |
| | 197 | This ensures price is always correct and prevents manual calculation errors inside the application. |
| | 198 | |
| | 199 | --- |
| | 200 | |
| | 201 | === Trigger: Venue Capacity Enforcement === |
| | 202 | This trigger prevents adding guests if the confirmed venue capacity will be exceeded. |
| | 203 | |
| | 204 | *Tables involved:* `guest`, `venue_booking`, `venue` |
| | 205 | *Trigger type:* BEFORE INSERT OR UPDATE on `guest` |
| 292 | | This phase upgrades our PostgreSQL database with advanced mechanisms used in real systems. |
| 293 | | Indexes improve performance for scheduling, booking checks and RSVP reports. |
| 294 | | Constraints enforce correctness and prevent double bookings and invalid statuses. |
| 295 | | Triggers automate business rules such as venue pricing and capacity limits. |
| 296 | | Views simplify reporting and provide reusable queries for dashboards and analytics. |
| 297 | | |
| | 321 | In this phase we implemented advanced database mechanisms that improve scalability and correctness of our Wedding Planner system. |
| | 322 | Indexes help queries run faster as the number of bookings/events grows. |
| | 323 | Constraints enforce strict data rules and prevent invalid states, such as double-booking. |
| | 324 | Triggers automate derived values and business rules like venue capacity control. |
| | 325 | Finally, views provide reusable reports and simplify querying for common summaries. |