Spaces:
Sleeping
Sleeping
| """ | |
| Script para executar migracao 003: Adicionar metadata JSONB. | |
| Uso: | |
| python scripts/run_migration_003.py | |
| """ | |
| import sys | |
| import os | |
| from pathlib import Path | |
| # Adicionar src ao path | |
| sys.path.insert(0, str(Path(__file__).parent.parent)) | |
| from src.database import DatabaseManager | |
| from src.config import DATABASE_URL | |
| def run_migration(): | |
| """Executa migracao 003.""" | |
| print("Executando migracao 003: Adicionar metadata JSONB...") | |
| # Conectar ao banco | |
| db = DatabaseManager(DATABASE_URL) | |
| # Ler arquivo de migracao | |
| migration_file = Path(__file__).parent.parent / "db" / "migrations" / "003_add_metadata.sql" | |
| if not migration_file.exists(): | |
| print(f"Erro: Arquivo de migracao nao encontrado: {migration_file}") | |
| return False | |
| with open(migration_file, 'r', encoding='utf-8') as f: | |
| migration_sql = f.read() | |
| # Executar migracao | |
| try: | |
| with db.get_connection() as conn: | |
| with conn.cursor() as cur: | |
| # Executar cada statement | |
| statements = [s.strip() for s in migration_sql.split(';') if s.strip() and not s.strip().startswith('--')] | |
| for i, statement in enumerate(statements, 1): | |
| if statement: | |
| print(f" Executando statement {i}/{len(statements)}...") | |
| cur.execute(statement) | |
| conn.commit() | |
| print("Migracao 003 executada com sucesso!") | |
| return True | |
| except Exception as e: | |
| print(f"Erro ao executar migracao: {e}") | |
| return False | |
| def verify_migration(): | |
| """Verifica se migracao foi aplicada.""" | |
| print("\nVerificando migracao...") | |
| db = DatabaseManager(DATABASE_URL) | |
| try: | |
| with db.get_connection() as conn: | |
| with conn.cursor() as cur: | |
| # Verificar se coluna existe | |
| cur.execute(""" | |
| SELECT column_name | |
| FROM information_schema.columns | |
| WHERE table_name = 'documents' | |
| AND column_name = 'metadata' | |
| """) | |
| if cur.fetchone(): | |
| print(" Coluna 'metadata' existe") | |
| # Verificar indices | |
| cur.execute(""" | |
| SELECT indexname | |
| FROM pg_indexes | |
| WHERE tablename = 'documents' | |
| AND indexname LIKE '%metadata%' | |
| """) | |
| indices = [row[0] for row in cur.fetchall()] | |
| print(f" Indices criados: {len(indices)}") | |
| for idx in indices: | |
| print(f" - {idx}") | |
| print("\nMigracao verificada com sucesso!") | |
| return True | |
| else: | |
| print(" Erro: Coluna 'metadata' nao encontrada") | |
| return False | |
| except Exception as e: | |
| print(f"Erro ao verificar migracao: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| success = run_migration() | |
| if success: | |
| verify_migration() | |
| else: | |
| sys.exit(1) | |