diff --git a/data_init/data_init_tw_v0.py b/data_init/data_init_tw_v0.py index ffbcaa7..b783f74 100644 --- a/data_init/data_init_tw_v0.py +++ b/data_init/data_init_tw_v0.py @@ -5,16 +5,28 @@ import numpy as np import psycopg2 from psycopg2.extras import execute_values from tqdm import tqdm +from config import SQL_CONFIG +import os + with open('data_init/assets_tw.json') as f: data_tw = json.load(f) -#SQLใ€€setting -SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='thiispassword1qaz!QAZ', host='db', port='5432') - - # TW Stocks conn = psycopg2.connect(**SQL_CONFIG) cursor = conn.cursor() + +# Create schema if not exists +try: + cursor.execute("SELECT 1 FROM stock_price_tw LIMIT 1;") +except psycopg2.errors.UndefinedTable: + print("๐Ÿ“‹ Tables not found, creating schema...") + conn.rollback() + sql_path = '/flask/sql_script/create_all.sql' if os.path.exists('/flask/sql_script/create_all.sql') else 'sql_script/create_all.sql' + with open(sql_path, 'r') as f: + cursor.execute(f.read()) + conn.commit() + print("โœ… Schema created successfully") + cursor.execute("SELECT COUNT(*) FROM stock_price_tw;") row_count = cursor.fetchone()[0] if row_count <= 0 : diff --git a/data_init/data_init_us_v0.py b/data_init/data_init_us_v0.py index d9f4d16..b0346ec 100644 --- a/data_init/data_init_us_v0.py +++ b/data_init/data_init_us_v0.py @@ -5,15 +5,16 @@ import numpy as np import psycopg2 from psycopg2.extras import execute_values from tqdm import tqdm +from config import SQL_CONFIG + with open('data_init/assets_us.json') as f: data_us = json.load(f) -#SQLใ€€setting -SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='thiispassword1qaz!QAZ', host='db', port='5432') - -# TW Stocks +# US Stocks conn = psycopg2.connect(**SQL_CONFIG) cursor = conn.cursor() + +# Schema already created by data_init_tw_v0.py, just check connection cursor.execute("SELECT COUNT(*) FROM stock_price;") row_count = cursor.fetchone()[0] if row_count <= 0 : diff --git a/init_railway_db.py b/init_railway_db.py new file mode 100644 index 0000000..014177c --- /dev/null +++ b/init_railway_db.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +""" +One-time script to initialize Railway database from local machine. +Run this ONCE after deploying to Railway. + +Usage: + export DATABASE_URL="postgresql://user:password@host:port/database" + python3 init_railway_db.py +""" +import os +import sys + +# Check DATABASE_URL +DATABASE_URL = os.environ.get('DATABASE_URL') +if not DATABASE_URL: + print("โŒ ERROR: DATABASE_URL environment variable not set") + print("\nGet your DATABASE_URL from Railway:") + print("1. Go to Railway dashboard") + print("2. Click on PostgreSQL service") + print("3. Go to 'Connect' tab") + print("4. Copy the 'Postgres Connection URL'") + print("\nThen run:") + print(' export DATABASE_URL="postgresql://..."') + print(" python3 init_railway_db.py") + sys.exit(1) + +print(f"โœ… DATABASE_URL is set") +print(f"๐Ÿ“Š Connecting to: {DATABASE_URL.split('@')[1] if '@' in DATABASE_URL else '***'}") + +# Import after checking DATABASE_URL +os.chdir('/Users/chiuyiting/Documents/GitHub/TPM') +sys.path.insert(0, '/Users/chiuyiting/Documents/GitHub/TPM/data_init') + +# Now run the initialization scripts +print("\n" + "="*60) +print("๐Ÿš€ Starting Railway Database Initialization") +print("="*60 + "\n") + +print("๐Ÿ“‹ Step 1: Initializing Taiwan stock data (this will create schema)...") +import data_init.data_init_tw_v0 +print("\nโœ… Taiwan data initialized\n") + +print("๐Ÿ“‹ Step 2: Initializing US stock data...") +import data_init.data_init_us_v0 +print("\nโœ… US data initialized\n") + +print("\n" + "="*60) +print("๐ŸŽ‰ Railway Database Initialization Complete!") +print("="*60) +print("\nYour Railway app should now work properly.") +print("Check https://nthutpm.up.railway.app")