File size: 2,262 Bytes
d3ecfe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Migration 004: Add route_directions column to assignments table
Adds JSONB column to store turn-by-turn navigation instructions from Google Routes API
"""

import sys
import os

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from database.connection import get_db_connection

MIGRATION_SQL = """
-- Add route_directions column to store turn-by-turn navigation steps
ALTER TABLE assignments
ADD COLUMN IF NOT EXISTS route_directions JSONB;

-- Add comment to explain the column
COMMENT ON COLUMN assignments.route_directions IS 'Turn-by-turn navigation instructions from Google Routes API (array of steps with instructions, distance, duration)';
"""

ROLLBACK_SQL = """
-- Drop route_directions column
ALTER TABLE assignments
DROP COLUMN IF EXISTS route_directions;
"""


def up():
    """Apply migration - add route_directions column"""
    print("Running migration 004: Add route_directions column to assignments table...")

    try:
        conn = get_db_connection()
        cursor = conn.cursor()

        # Execute migration SQL
        cursor.execute(MIGRATION_SQL)

        conn.commit()
        cursor.close()
        conn.close()

        print("SUCCESS: Migration 004 applied successfully")
        print("  - Added route_directions JSONB column to assignments table")
        print("  - Column will store turn-by-turn navigation instructions")
        return True

    except Exception as e:
        print(f"ERROR: Migration 004 failed: {e}")
        return False


def down():
    """Rollback migration - drop route_directions column"""
    print("Rolling back migration 004: Drop route_directions column...")

    try:
        conn = get_db_connection()
        cursor = conn.cursor()

        # Execute rollback SQL
        cursor.execute(ROLLBACK_SQL)

        conn.commit()
        cursor.close()
        conn.close()

        print("SUCCESS: Migration 004 rolled back successfully")
        return True

    except Exception as e:
        print(f"ERROR: Migration 004 rollback failed: {e}")
        return False


if __name__ == "__main__":
    import sys

    if len(sys.argv) > 1 and sys.argv[1] == "down":
        down()
    else:
        up()