|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Drop all tutorial schemas from the database. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python drop_tutorial_schemas.py --backend mysql |
| 7 | + python drop_tutorial_schemas.py --backend postgresql |
| 8 | +
|
| 9 | +This script drops all schemas matching the 'tutorial_%' pattern, |
| 10 | +preparing for a fresh notebook execution run. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import sys |
| 16 | + |
| 17 | + |
| 18 | +def get_connection(backend: str): |
| 19 | + """ |
| 20 | + Create a database connection for the specified backend. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + backend : str |
| 25 | + Either 'mysql' or 'postgresql' |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + connection |
| 30 | + Database connection object |
| 31 | + """ |
| 32 | + if backend == "postgresql": |
| 33 | + import psycopg2 |
| 34 | + |
| 35 | + return psycopg2.connect( |
| 36 | + host=os.environ.get("DJ_HOST", "127.0.0.1"), |
| 37 | + port=int(os.environ.get("DJ_PORT", "5432")), |
| 38 | + user=os.environ.get("DJ_USER", "postgres"), |
| 39 | + password=os.environ.get("DJ_PASS", "tutorial"), |
| 40 | + dbname="postgres", |
| 41 | + ) |
| 42 | + else: # mysql |
| 43 | + import pymysql |
| 44 | + |
| 45 | + return pymysql.connect( |
| 46 | + host=os.environ.get("DJ_HOST", "127.0.0.1"), |
| 47 | + port=int(os.environ.get("DJ_PORT", "3306")), |
| 48 | + user=os.environ.get("DJ_USER", "root"), |
| 49 | + password=os.environ.get("DJ_PASS", "tutorial"), |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def drop_tutorial_schemas(backend: str, dry_run: bool = False) -> list[str]: |
| 54 | + """ |
| 55 | + Drop all tutorial schemas from the database. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + backend : str |
| 60 | + Either 'mysql' or 'postgresql' |
| 61 | + dry_run : bool |
| 62 | + If True, only list schemas without dropping |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + list[str] |
| 67 | + List of dropped schema names |
| 68 | + """ |
| 69 | + conn = get_connection(backend) |
| 70 | + cursor = conn.cursor() |
| 71 | + |
| 72 | + # Find tutorial schemas |
| 73 | + if backend == "postgresql": |
| 74 | + cursor.execute( |
| 75 | + """ |
| 76 | + SELECT schema_name FROM information_schema.schemata |
| 77 | + WHERE schema_name LIKE 'tutorial_%' |
| 78 | + ORDER BY schema_name |
| 79 | + """ |
| 80 | + ) |
| 81 | + else: # mysql |
| 82 | + cursor.execute( |
| 83 | + """ |
| 84 | + SELECT schema_name FROM information_schema.schemata |
| 85 | + WHERE schema_name LIKE 'tutorial_%' |
| 86 | + ORDER BY schema_name |
| 87 | + """ |
| 88 | + ) |
| 89 | + |
| 90 | + schemas = [row[0] for row in cursor.fetchall()] |
| 91 | + |
| 92 | + if not schemas: |
| 93 | + print("No tutorial schemas found.") |
| 94 | + return [] |
| 95 | + |
| 96 | + print(f"Found {len(schemas)} tutorial schema(s):") |
| 97 | + for schema in schemas: |
| 98 | + print(f" - {schema}") |
| 99 | + |
| 100 | + if dry_run: |
| 101 | + print("\nDry run - no schemas dropped.") |
| 102 | + return schemas |
| 103 | + |
| 104 | + print("\nDropping schemas...") |
| 105 | + |
| 106 | + for schema in schemas: |
| 107 | + if backend == "postgresql": |
| 108 | + # PostgreSQL uses double quotes for identifiers |
| 109 | + cursor.execute(f'DROP SCHEMA IF EXISTS "{schema}" CASCADE') |
| 110 | + else: # mysql |
| 111 | + # MySQL uses backticks for identifiers |
| 112 | + cursor.execute(f"DROP DATABASE IF EXISTS `{schema}`") |
| 113 | + print(f" Dropped: {schema}") |
| 114 | + |
| 115 | + conn.commit() |
| 116 | + cursor.close() |
| 117 | + conn.close() |
| 118 | + |
| 119 | + print(f"\nDropped {len(schemas)} schema(s).") |
| 120 | + return schemas |
| 121 | + |
| 122 | + |
| 123 | +def main(): |
| 124 | + parser = argparse.ArgumentParser( |
| 125 | + description="Drop all tutorial schemas from the database" |
| 126 | + ) |
| 127 | + parser.add_argument( |
| 128 | + "--backend", |
| 129 | + choices=["mysql", "postgresql"], |
| 130 | + default="mysql", |
| 131 | + help="Database backend (default: mysql)", |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--dry-run", |
| 135 | + action="store_true", |
| 136 | + help="List schemas without dropping them", |
| 137 | + ) |
| 138 | + |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + print(f"{'=' * 60}") |
| 142 | + print(f"Drop Tutorial Schemas ({args.backend.upper()})") |
| 143 | + print(f"{'=' * 60}\n") |
| 144 | + |
| 145 | + try: |
| 146 | + drop_tutorial_schemas(args.backend, args.dry_run) |
| 147 | + except Exception as e: |
| 148 | + print(f"Error: {e}", file=sys.stderr) |
| 149 | + sys.exit(1) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments