File size: 1,799 Bytes
01d5a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""
Database Migration Downgrade Tool

Usage:
    python downgrade_migration.py [version]

Arguments:
    version: Optional, version to downgrade to (inclusive).
             If not provided, all migrations will be downgraded.
"""

import os
import sys
import sqlite3
import importlib.util
from pathlib import Path

# Add project root to Python path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

# Import migration manager
from lpm_kernel.database.migration_manager import MigrationManager
from lpm_kernel.configs.config import Config

def main():
    # Get database path
    config = Config.from_env()
    db_path = config.database.db_file
    
    print(f"Database path: {db_path}")
    
    # Create migration manager
    manager = MigrationManager(db_path)
    
    # Get applied migrations
    applied = manager.get_applied_migrations()
    print(f"Applied migrations: {', '.join(applied) if applied else 'none'}")
    
    if not applied:
        print("No migrations to downgrade")
        return
    
    # Check command line arguments
    if len(sys.argv) > 1:
        target_version = sys.argv[1]
        if target_version not in applied:
            print(f"Error: Version {target_version} is not applied, cannot downgrade to this version")
            return
        
        print(f"Downgrading to version {target_version}...")
        downgraded = manager.downgrade_to_version(target_version)
    else:
        print("Downgrading all migrations...")
        downgraded = manager.downgrade_to_version()
    
    if downgraded:
        print(f"Successfully downgraded migrations: {', '.join(downgraded)}")
    else:
        print("No migrations were downgraded")

if __name__ == "__main__":
    main()