178 lines
5.7 KiB
YAML
178 lines
5.7 KiB
YAML
name: Database Setup
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
action:
|
|
description: "Database action to perform"
|
|
required: true
|
|
default: "setup"
|
|
type: choice
|
|
options:
|
|
- setup
|
|
- migrate
|
|
- reset
|
|
confirm:
|
|
description: 'Type "yes" to confirm destructive operations'
|
|
required: false
|
|
default: "no"
|
|
type: string
|
|
|
|
jobs:
|
|
database-setup:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v4
|
|
with:
|
|
python-version: "3.12"
|
|
|
|
- name: Install uv
|
|
run: |
|
|
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
|
|
|
|
- name: Install dependencies
|
|
run: |
|
|
uv sync
|
|
|
|
- name: Validate inputs
|
|
run: |
|
|
if [[ "${{ github.event.inputs.action }}" == "reset" && "${{ github.event.inputs.confirm }}" != "yes" ]]; then
|
|
echo "❌ Reset operation requires confirmation. Please type 'yes' in the confirm field."
|
|
exit 1
|
|
fi
|
|
|
|
- name: Database Setup
|
|
if: github.event.inputs.action == 'setup'
|
|
env:
|
|
ELOVERBLIK_API_TOKEN: ${{ secrets.ELOVERBLIK_API_TOKEN }}
|
|
DB_HOST: ${{ secrets.DB_HOST }}
|
|
DB_NAME: ${{ secrets.DB_NAME }}
|
|
DB_USER: ${{ secrets.DB_USER }}
|
|
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
|
DB_PORT: ${{ secrets.DB_PORT }}
|
|
run: |
|
|
echo "🏗️ Setting up database tables..."
|
|
uv run python -c "
|
|
from energy_consumption_ingester import DatabaseStorage
|
|
from dotenv import load_dotenv
|
|
import sys
|
|
|
|
load_dotenv()
|
|
db = DatabaseStorage.from_env()
|
|
|
|
if db.connect():
|
|
print('✅ Database connection successful')
|
|
if db.create_tables():
|
|
print('✅ Database tables created successfully')
|
|
else:
|
|
print('❌ Failed to create tables')
|
|
sys.exit(1)
|
|
else:
|
|
print('❌ Failed to connect to database')
|
|
sys.exit(1)
|
|
|
|
db.close()
|
|
print('🎉 Database setup completed!')
|
|
"
|
|
|
|
- name: Database Migration
|
|
if: github.event.inputs.action == 'migrate'
|
|
env:
|
|
ELOVERBLIK_API_TOKEN: ${{ secrets.ELOVERBLIK_API_TOKEN }}
|
|
DB_HOST: ${{ secrets.DB_HOST }}
|
|
DB_NAME: ${{ secrets.DB_NAME }}
|
|
DB_USER: ${{ secrets.DB_USER }}
|
|
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
|
DB_PORT: ${{ secrets.DB_PORT }}
|
|
run: |
|
|
echo "🔄 Running database migrations..."
|
|
uv run python -c "
|
|
from energy_consumption_ingester import DatabaseStorage
|
|
from dotenv import load_dotenv
|
|
import sys
|
|
|
|
load_dotenv()
|
|
db = DatabaseStorage.from_env()
|
|
|
|
if db.connect():
|
|
print('✅ Database connection successful')
|
|
if db.create_tables():
|
|
print('✅ Database migrations completed')
|
|
else:
|
|
print('❌ Migration failed')
|
|
sys.exit(1)
|
|
else:
|
|
print('❌ Failed to connect to database')
|
|
sys.exit(1)
|
|
|
|
db.close()
|
|
print('🎉 Migration completed!')
|
|
"
|
|
|
|
- name: Database Reset (DESTRUCTIVE)
|
|
if: github.event.inputs.action == 'reset' && github.event.inputs.confirm == 'yes'
|
|
env:
|
|
ELOVERBLIK_API_TOKEN: ${{ secrets.ELOVERBLIK_API_TOKEN }}
|
|
DB_HOST: ${{ secrets.DB_HOST }}
|
|
DB_NAME: ${{ secrets.DB_NAME }}
|
|
DB_USER: ${{ secrets.DB_USER }}
|
|
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
|
DB_PORT: ${{ secrets.DB_PORT }}
|
|
run: |
|
|
echo "🚨 RESETTING DATABASE - THIS WILL DELETE ALL DATA!"
|
|
uv run python -c "
|
|
from energy_consumption_ingester import DatabaseStorage
|
|
from dotenv import load_dotenv
|
|
import sys
|
|
|
|
load_dotenv()
|
|
db = DatabaseStorage.from_env()
|
|
|
|
if db.connect():
|
|
print('✅ Database connection successful')
|
|
|
|
# Drop tables
|
|
cursor = db.connection.cursor()
|
|
cursor.execute('DROP TABLE IF EXISTS consumption_readings CASCADE;')
|
|
cursor.execute('DROP TABLE IF EXISTS metering_points CASCADE;')
|
|
cursor.execute('DROP FUNCTION IF EXISTS update_updated_at_column() CASCADE;')
|
|
db.connection.commit()
|
|
print('🗑️ Existing tables dropped')
|
|
|
|
# Recreate tables
|
|
if db.create_tables():
|
|
print('✅ Database tables recreated')
|
|
else:
|
|
print('❌ Failed to recreate tables')
|
|
sys.exit(1)
|
|
else:
|
|
print('❌ Failed to connect to database')
|
|
sys.exit(1)
|
|
|
|
db.close()
|
|
print('🎉 Database reset completed!')
|
|
"
|
|
|
|
- name: Test Connection
|
|
env:
|
|
ELOVERBLIK_API_TOKEN: ${{ secrets.ELOVERBLIK_API_TOKEN }}
|
|
DB_HOST: ${{ secrets.DB_HOST }}
|
|
DB_NAME: ${{ secrets.DB_NAME }}
|
|
DB_USER: ${{ secrets.DB_USER }}
|
|
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
|
DB_PORT: ${{ secrets.DB_PORT }}
|
|
run: |
|
|
echo "🔍 Testing system health..."
|
|
uv run energy-ingester health-check --verbose
|
|
|
|
- name: Summary
|
|
run: |
|
|
echo "✅ Database operation '${{ github.event.inputs.action }}' completed successfully!"
|
|
echo "💡 You can now run the hourly sync or manual sync operations."
|