Backup before cleanup

This commit is contained in:
Joan
2026-02-05 15:00:49 +01:00
parent e6747b1d05
commit 1b7ffd614d
60 changed files with 3013 additions and 460 deletions

View File

@@ -1,18 +1,24 @@
-- Add persistent status effects table
CREATE TABLE IF NOT EXISTS player_status_effects (
DROP TABLE IF EXISTS player_status_effects CASCADE;
CREATE TABLE player_status_effects (
id SERIAL PRIMARY KEY,
player_id INTEGER NOT NULL REFERENCES players(telegram_id) ON DELETE CASCADE,
character_id INTEGER NOT NULL REFERENCES characters(id) ON DELETE CASCADE,
effect_name VARCHAR(50) NOT NULL,
effect_icon VARCHAR(10) NOT NULL,
damage_per_tick INTEGER NOT NULL DEFAULT 0,
effect_type VARCHAR(20) DEFAULT 'damage',
value INTEGER DEFAULT 0,
ticks_remaining INTEGER NOT NULL,
persist_after_combat BOOLEAN DEFAULT FALSE,
source VARCHAR(50),
applied_at FLOAT NOT NULL,
CONSTRAINT valid_ticks CHECK (ticks_remaining >= 0),
CONSTRAINT valid_damage CHECK (damage_per_tick >= 0)
);
-- Create index for efficient querying by player
CREATE INDEX IF NOT EXISTS idx_status_effects_player ON player_status_effects(player_id);
CREATE INDEX IF NOT EXISTS idx_status_effects_player ON player_status_effects(character_id);
-- Create index for background processor to find active effects
CREATE INDEX IF NOT EXISTS idx_status_effects_active ON player_status_effects(player_id, ticks_remaining) WHERE ticks_remaining > 0;
CREATE INDEX IF NOT EXISTS idx_status_effects_active ON player_status_effects(character_id, ticks_remaining) WHERE ticks_remaining > 0;

View File

@@ -7,6 +7,9 @@ import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from dotenv import load_dotenv
load_dotenv()
# Database connection
DB_USER = os.getenv("POSTGRES_USER")

View File

@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""
Migration: Add combat effect fields to player_status_effects table.
Adds effect_type, value, persist_after_combat, and source columns.
"""
import asyncio
import os
import sys
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
DB_USER = os.getenv("POSTGRES_USER")
DB_PASS = os.getenv("POSTGRES_PASSWORD")
DB_NAME = os.getenv("POSTGRES_DB")
DB_HOST = os.getenv("POSTGRES_HOST", "echoes_of_the_ashes_db")
DB_PORT = os.getenv("POSTGRES_PORT", "5432")
DATABASE_URL = f"postgresql+psycopg://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
async def run_migration():
engine = create_async_engine(DATABASE_URL, echo=True)
async with engine.begin() as conn:
print("Adding new columns to player_status_effects table...")
# Add effect_type column (damage, buff, debuff)
try:
await conn.execute(text("""
ALTER TABLE player_status_effects
ADD COLUMN IF NOT EXISTS effect_type VARCHAR(20) DEFAULT 'damage'
"""))
print("✓ Added effect_type column")
except Exception as e:
print(f"Note: effect_type column may already exist: {e}")
# Add value column (generic value for effect - damage amount, buff %, etc.)
# This replaces/supplements damage_per_tick for more flexibility
try:
await conn.execute(text("""
ALTER TABLE player_status_effects
ADD COLUMN IF NOT EXISTS value INTEGER DEFAULT 0
"""))
print("✓ Added value column")
except Exception as e:
print(f"Note: value column may already exist: {e}")
# Add persist_after_combat column
try:
await conn.execute(text("""
ALTER TABLE player_status_effects
ADD COLUMN IF NOT EXISTS persist_after_combat BOOLEAN DEFAULT FALSE
"""))
print("✓ Added persist_after_combat column")
except Exception as e:
print(f"Note: persist_after_combat column may already exist: {e}")
# Add source column to track where effect came from
try:
await conn.execute(text("""
ALTER TABLE player_status_effects
ADD COLUMN IF NOT EXISTS source VARCHAR(50) DEFAULT NULL
"""))
print("✓ Added source column")
except Exception as e:
print(f"Note: source column may already exist: {e}")
# Create index on persist_after_combat for background task queries
try:
await conn.execute(text("""
CREATE INDEX IF NOT EXISTS idx_status_effects_persist
ON player_status_effects(persist_after_combat)
WHERE persist_after_combat = TRUE
"""))
print("✓ Created persist_after_combat index")
except Exception as e:
print(f"Note: Index may already exist: {e}")
print("\n✓ Migration completed successfully!")
await engine.dispose()
if __name__ == "__main__":
asyncio.run(run_migration())

View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""
Migration: Drop valid_damage constraint from player_status_effects table.
This constraint prevents negative damage (healing) for status effects.
"""
import asyncio
import os
import sys
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
DB_USER = os.getenv("POSTGRES_USER")
DB_PASS = os.getenv("POSTGRES_PASSWORD")
DB_NAME = os.getenv("POSTGRES_DB")
DB_HOST = os.getenv("POSTGRES_HOST", "echoes_of_the_ashes_db")
DB_PORT = os.getenv("POSTGRES_PORT", "5432")
DATABASE_URL = f"postgresql+psycopg://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
async def run_migration():
engine = create_async_engine(DATABASE_URL, echo=True)
async with engine.begin() as conn:
print("Removing restrictive constraint from player_status_effects table...")
try:
await conn.execute(text("""
ALTER TABLE player_status_effects
DROP CONSTRAINT IF EXISTS valid_damage
"""))
print("✓ Dropped valid_damage constraint")
except Exception as e:
print(f"Error dropping constraint: {e}")
print("\n✓ Migration completed successfully!")
await engine.dispose()
if __name__ == "__main__":
asyncio.run(run_migration())