Spaces:
Paused
Paused
Create setup_olist_db.py
Browse files- setup_olist_db.py +49 -0
setup_olist_db.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sqlite3
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import yaml
|
| 5 |
+
|
| 6 |
+
DB_PATH = "olist.sqlite"
|
| 7 |
+
|
| 8 |
+
def initialize_db():
|
| 9 |
+
if os.path.exists(DB_PATH):
|
| 10 |
+
print("✅ DB already exists, skipping rebuild.")
|
| 11 |
+
return
|
| 12 |
+
|
| 13 |
+
conn = sqlite3.connect(DB_PATH)
|
| 14 |
+
|
| 15 |
+
csv_files = {
|
| 16 |
+
"orders": "data/olist_orders_dataset.csv",
|
| 17 |
+
"order_items": "data/olist_order_items_dataset.csv",
|
| 18 |
+
"order_payments": "data/olist_order_payments_dataset.csv",
|
| 19 |
+
"order_reviews": "data/olist_order_reviews_dataset.csv",
|
| 20 |
+
"customers": "data/olist_customers_dataset.csv",
|
| 21 |
+
"products": "data/olist_products_dataset.csv",
|
| 22 |
+
"sellers": "data/olist_sellers_dataset.csv",
|
| 23 |
+
"geolocation": "data/olist_geolocation_dataset.csv",
|
| 24 |
+
"product_category_name_translation": "data/product_category_name_translation.csv"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
for table, path in csv_files.items():
|
| 28 |
+
df = pd.read_csv(path)
|
| 29 |
+
df.to_sql(table, conn, if_exists="replace", index=False)
|
| 30 |
+
|
| 31 |
+
# Users table
|
| 32 |
+
conn.execute("""
|
| 33 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 34 |
+
user_id TEXT PRIMARY KEY,
|
| 35 |
+
email TEXT UNIQUE NOT NULL,
|
| 36 |
+
password_hash TEXT NOT NULL,
|
| 37 |
+
customer_id TEXT,
|
| 38 |
+
role TEXT CHECK(role IN ('user','admin')) NOT NULL
|
| 39 |
+
);
|
| 40 |
+
""")
|
| 41 |
+
|
| 42 |
+
conn.execute("INSERT OR IGNORE INTO users VALUES ('U001','alice@example.com','hashed_pw1','CUST12345','user');")
|
| 43 |
+
conn.execute("INSERT OR IGNORE INTO users VALUES ('A001','admin@example.com','hashed_pw_admin',NULL,'admin');")
|
| 44 |
+
|
| 45 |
+
conn.commit()
|
| 46 |
+
conn.close()
|
| 47 |
+
print("✅ DB initialized")
|
| 48 |
+
|
| 49 |
+
|