feat: add auto schema initialization to data_init scripts

- Modified data_init_tw_v0.py to create database schema if tables don't exist
- Updated both data_init scripts to use centralized config.py for DATABASE_URL support
- Added init_railway_db.py script for one-time Railway database initialization from local machine

This fixes the issue where Railway only runs Flask container, not the data_init container from docker-compose.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
master
Eric0801 2 months ago
parent 32ef5e1978
commit ca52a05f19
  1. 20
      data_init/data_init_tw_v0.py
  2. 9
      data_init/data_init_us_v0.py
  3. 51
      init_railway_db.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 :

@ -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 :

@ -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")
Loading…
Cancel
Save