Initial commit
This commit is contained in:
44
init_db.py
Normal file
44
init_db.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Database initialization script"""
|
||||
import asyncio
|
||||
import asyncpg
|
||||
|
||||
async def init_database():
|
||||
"""Initialize database with schema"""
|
||||
|
||||
# Read SQL file
|
||||
with open('docker/init-db.sql', 'r') as f:
|
||||
sql = f.read()
|
||||
|
||||
# Connect to database
|
||||
conn = await asyncpg.connect(
|
||||
host='shium.net',
|
||||
port=55432,
|
||||
user='stock',
|
||||
password='Tjduddn!!12',
|
||||
database='stock'
|
||||
)
|
||||
|
||||
try:
|
||||
# Split SQL into individual statements and execute one by one
|
||||
statements = [s.strip() for s in sql.split(';') if s.strip()]
|
||||
|
||||
for statement in statements:
|
||||
try:
|
||||
await conn.execute(statement)
|
||||
print(f"✅ Executed: {statement[:50]}...")
|
||||
except Exception as e:
|
||||
# Skip if already exists
|
||||
if 'already exists' in str(e):
|
||||
print(f"⚠️ Skipped (already exists): {statement[:50]}...")
|
||||
else:
|
||||
print(f"❌ Error: {e}")
|
||||
|
||||
print("\n✅ Database initialization completed!")
|
||||
except Exception as e:
|
||||
print(f"❌ Error initializing database: {e}")
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(init_database())
|
||||
Reference in New Issue
Block a user