File size: 4,354 Bytes
bf26a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- Supabase Database Migration for Voucher Generator
-- This migration restructures the 'generations' table to support comprehensive voucher logging
-- Safe to run - preserves existing data

-- ==============================================================================
-- STEP 1: Rename existing columns to follow snake_case convention (removes spaces)
-- ==============================================================================

-- Rename columns with spaces to clean snake_case format
ALTER TABLE public.generations
  RENAME COLUMN "Hotel Name" TO hotel_name;

ALTER TABLE public.generations
  RENAME COLUMN "Guest Name" TO primary_guest_name;

ALTER TABLE public.generations
  RENAME COLUMN "Check-in" TO checkin_date;

ALTER TABLE public.generations
  RENAME COLUMN "Check-out" TO checkout_date;

ALTER TABLE public.generations
  RENAME COLUMN "City" TO city;

ALTER TABLE public.generations
  RENAME COLUMN "Address" TO address_line1;

ALTER TABLE public.generations
  RENAME COLUMN "Cancellation_Days" TO cancellation_days;

ALTER TABLE public.generations
  RENAME COLUMN "Refundable" TO is_refundable;


-- ==============================================================================
-- STEP 2: Add new columns for complete voucher tracking
-- ==============================================================================

-- Add all new columns needed for comprehensive voucher logging
ALTER TABLE public.generations
  ADD COLUMN IF NOT EXISTS address_line2 text,
  ADD COLUMN IF NOT EXISTS phone text,
  ADD COLUMN IF NOT EXISTS num_nights integer NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS num_guests integer NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS num_rooms integer NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS room_type text,
  ADD COLUMN IF NOT EXISTS room_size text,
  ADD COLUMN IF NOT EXISTS meal_plan text DEFAULT 'Breakfast Only',
  ADD COLUMN IF NOT EXISTS policy text DEFAULT 'Non-Refundable',
  ADD COLUMN IF NOT EXISTS remarks text,
  ADD COLUMN IF NOT EXISTS pdf_filename text NOT NULL DEFAULT 'voucher.pdf',
  ADD COLUMN IF NOT EXISTS pdf_url text,
  ADD COLUMN IF NOT EXISTS pdf_storage_path text,
  ADD COLUMN IF NOT EXISTS tab_source text NOT NULL DEFAULT 'Search Hotel',
  ADD COLUMN IF NOT EXISTS is_bulk boolean DEFAULT false,
  ADD COLUMN IF NOT EXISTS guest_details jsonb;


-- ==============================================================================
-- STEP 3: Update data type for cancellation_days (optional)
-- ==============================================================================

-- Convert cancellation_days from numeric to integer for consistency
ALTER TABLE public.generations
  ALTER COLUMN cancellation_days TYPE integer USING cancellation_days::integer;


-- ==============================================================================
-- VERIFICATION QUERY
-- ==============================================================================

-- Run this query after migration to verify all columns are present
-- SELECT column_name, data_type, is_nullable
-- FROM information_schema.columns
-- WHERE table_name = 'generations'
-- ORDER BY ordinal_position;


-- ==============================================================================
-- FINAL SCHEMA
-- ==============================================================================

-- After running this migration, the 'generations' table will have:
-- - id                   bigint (primary key, auto-increment)
-- - created_at           timestamptz
-- - hotel_name           text
-- - primary_guest_name   text
-- - checkin_date         date
-- - checkout_date        date
-- - city                 text
-- - address_line1        text
-- - address_line2        text (NEW)
-- - phone                text (NEW)
-- - num_nights           integer (NEW)
-- - num_guests           integer (NEW)
-- - num_rooms            integer (NEW)
-- - room_type            text (NEW)
-- - room_size            text (NEW)
-- - meal_plan            text (NEW)
-- - policy               text (NEW)
-- - is_refundable        boolean
-- - cancellation_days    integer
-- - remarks              text (NEW)
-- - pdf_filename         text (NEW)
-- - pdf_url              text (NEW)
-- - pdf_storage_path     text (NEW)
-- - tab_source           text (NEW)
-- - is_bulk              boolean (NEW)
-- - guest_details        jsonb (NEW)