-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopulate_hotels.py
More file actions
84 lines (65 loc) · 2.17 KB
/
populate_hotels.py
File metadata and controls
84 lines (65 loc) · 2.17 KB
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
#!/usr/bin/env python3
"""
Manual script to populate hotels from hotels.csv into the database.
Run this once on the production server if the automatic import didn't work.
"""
import sys
import os
import csv
# Add app to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
from app.database import (
create_tables,
get_db,
Hotel,
DATABASE_URL,
engine
)
def populate_hotels():
"""Populate hotels from CSV"""
print("Populating hotels from hotels.csv...")
# Create tables if they don't exist
create_tables()
# Get database session
db = next(get_db())
try:
# Check existing hotels
existing_count = db.query(Hotel).count()
print(f"Found {existing_count} existing hotels in database")
if existing_count > 0:
print("Hotels already exist. Skipping import.")
return
# Read hotels.csv and import
hotels_imported = 0
with open("hotels.csv", "r") as file:
reader = csv.DictReader(file)
print("CSV columns:", reader.fieldnames)
for row in reader:
print(f"Processing: {row}")
# Check if already exists
existing = db.query(Hotel).filter(
Hotel.hotel_name == row["hotel_name"]
).first()
if not existing:
hotel = Hotel(
hotel_name=row["hotel_name"],
password=row["password"]
)
db.add(hotel)
hotels_imported += 1
print(f"Added hotel: {row['hotel_name']}")
db.commit()
print(f"Successfully imported {hotels_imported} hotels")
# Verify
all_hotels = db.query(Hotel).all()
print("Hotels in database:")
for hotel in all_hotels:
print(f" {hotel.hotel_name}: {hotel.password}")
except Exception as e:
print(f"Error: {e}")
db.rollback()
sys.exit(1)
finally:
db.close()
if __name__ == "__main__":
populate_hotels()