|
| 1 | +""" |
| 2 | +Split off non-runner related things into separate db columns. |
| 3 | +""" |
| 4 | +__depends__ = {'20250506_01_38PkG-add-index-on-runs-runner-score'} |
| 5 | + |
| 6 | +import json |
| 7 | +from yoyo import step |
| 8 | + |
| 9 | + |
| 10 | +def apply_step(conn): |
| 11 | + cursor = conn.cursor() |
| 12 | + |
| 13 | + # Add new table for code templates |
| 14 | + cursor.execute(""" |
| 15 | + CREATE TABLE IF NOT EXISTS leaderboard.templates ( |
| 16 | + id SERIAL PRIMARY KEY, |
| 17 | + leaderboard_id INTEGER NOT NULL REFERENCES leaderboard.leaderboard(id), |
| 18 | + lang TEXT NOT NULL, |
| 19 | + code TEXT NOT NULL |
| 20 | + ) |
| 21 | + """) |
| 22 | + |
| 23 | + # Extract the description column |
| 24 | + cursor.execute(""" |
| 25 | + ALTER TABLE leaderboard.leaderboard |
| 26 | + ADD COLUMN description TEXT, |
| 27 | + """) |
| 28 | + |
| 29 | + # Extract data from JSON column and populate new columns |
| 30 | + cursor.execute("SELECT id, task FROM leaderboard.leaderboard") |
| 31 | + rows = cursor.fetchall() |
| 32 | + |
| 33 | + for row in rows: |
| 34 | + row_id, json_data = row |
| 35 | + if json_data: |
| 36 | + try: |
| 37 | + parsed_data = json.loads(json_data) if isinstance(json_data, str) else json_data |
| 38 | + description = parsed_data.get('description', '') |
| 39 | + templates = parsed_data.get('templates', {}) |
| 40 | + |
| 41 | + # Remove description and templates from original JSON |
| 42 | + parsed_data.pop('description', None) |
| 43 | + parsed_data.pop('templates', None) |
| 44 | + |
| 45 | + cursor.execute(""" |
| 46 | + UPDATE leaderboard.leaderboard |
| 47 | + SET description = %s, task = %s |
| 48 | + WHERE id = %s |
| 49 | + """, (description, json.dumps(templates), json.dumps(parsed_data), row_id)) |
| 50 | + |
| 51 | + for lang, code in templates.items(): |
| 52 | + cursor.execute( |
| 53 | + """ |
| 54 | + INSERT INTO leaderboard.templates (leaderboard_id, lang, code) |
| 55 | + VALUES (%s, %s, %s) |
| 56 | + """, |
| 57 | + (row_id, lang, code), |
| 58 | + ) |
| 59 | + except (json.JSONDecodeError, TypeError) as e: |
| 60 | + print(f"Failed to parse JSON for row {row_id}: {e}") |
| 61 | + |
| 62 | + # Now make description NOT NULL |
| 63 | + cursor.execute("ALTER TABLE leaderboard.leaderboard ALTER COLUMN description SET NOT NULL") |
| 64 | + |
| 65 | + conn.commit() |
| 66 | + |
| 67 | + |
| 68 | +def rollback_step(conn): |
| 69 | + cursor = conn.cursor() |
| 70 | + cursor.execute("SELECT id, description, task FROM leaderboard.leaderboard") |
| 71 | + for lb_id, description, task_json in cursor.fetchall(): |
| 72 | + task_data = json.loads(task_json) |
| 73 | + task_data['description'] = description |
| 74 | + |
| 75 | + cursor.execute(""" |
| 76 | + SELECT lang, code |
| 77 | + FROM leaderboard.templates |
| 78 | + WHERE leaderboard_id = %s |
| 79 | + """, (lb_id,)) |
| 80 | + |
| 81 | + templates = {} |
| 82 | + for lang, code in cursor.fetchall(): |
| 83 | + templates[lang] = code |
| 84 | + task_data['templates'] = templates |
| 85 | + |
| 86 | + cursor.execute(""" |
| 87 | + UPDATE leaderboard.leaderboard |
| 88 | + SET task = %s |
| 89 | + WHERE id = %s |
| 90 | + """, (json.dumps(task_data), lb_id)) |
| 91 | + |
| 92 | + cursor.execute("ALTER TABLE leaderboard.leaderboard DROP COLUMN description") |
| 93 | + cursor.execute("DROP TABLE IF EXISTS leaderboard.templates") |
| 94 | + |
| 95 | + conn.commit() |
| 96 | + |
| 97 | + |
| 98 | +steps = [ |
| 99 | + step(apply_step, rollback_step) |
| 100 | +] |
0 commit comments