|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What This Project Does |
| 6 | + |
| 7 | +`aiven-mysql-migrate` is a CLI tool for migrating MySQL databases between servers. It supports two methods: |
| 8 | +- **replication**: mysqldump + GTID-based replication (preferred for production, minimal downtime) |
| 9 | +- **dump**: mysqldump-only (fallback when replication preconditions aren't met) |
| 10 | + |
| 11 | +Each method supports two dump tools: `mysqldump` (MySQL native) and `mydumper` (third-party, parallel). |
| 12 | + |
| 13 | +## Build & Test Commands |
| 14 | + |
| 15 | +```bash |
| 16 | +# Install dev dependencies (requires Python 3.10-3.14) |
| 17 | +pip install -e ".[dev]" |
| 18 | + |
| 19 | +# Unit tests |
| 20 | +make test |
| 21 | +# or: python3 -m pytest -v test/unit/ |
| 22 | + |
| 23 | +# Run a single unit test |
| 24 | +python3 -m pytest -v test/unit/test_utils.py::test_mysql_dump_processor_extract_gtid |
| 25 | + |
| 26 | +# System tests (requires Docker/Podman; spins up MySQL 5.7/8.0/8.4 containers) |
| 27 | +make systest |
| 28 | +make systest DOCKER=podman |
| 29 | + |
| 30 | +# Linting & type checking |
| 31 | +make flake8 |
| 32 | +make pylint |
| 33 | +make mypy |
| 34 | +make static-checks # all three above |
| 35 | + |
| 36 | +# Auto-format |
| 37 | +make isort yapf |
| 38 | +``` |
| 39 | + |
| 40 | +## Architecture |
| 41 | + |
| 42 | +The migration flow is: `main.py` (CLI/arg parsing) -> `migration.py` (orchestration) -> `dump_tools.py` (dump/import commands) -> `migration_executor.py` (subprocess piping). |
| 43 | + |
| 44 | +### Key modules in `aiven_mysql_migrate/` |
| 45 | + |
| 46 | +- **`main.py`** — CLI entry point (`mysql_migrate` command). Parses args, reads env vars, calls `MySQLMigration`. |
| 47 | +- **`migration.py`** — `MySQLMigration` class. Runs pre-checks (version compat, GTID mode, engine support, server IDs, grants, binlog format), then orchestrates dump -> import -> replication setup. |
| 48 | +- **`dump_tools.py`** — `MySQLDumpTool` and `MyDumperTool` (both extend `MySQLMigrationToolBase`). Build CLI commands for mysqldump/mysql and mydumper/myloader respectively. Factory: `get_dump_tool()`. |
| 49 | +- **`migration_executor.py`** — `ProcessExecutor`. Runs dump and import as piped subprocesses, with a `DumpProcessor` reading stdout line-by-line. |
| 50 | +- **`utils.py`** — `MySQLConnectionInfo` (connection/URI parsing), `DumpProcessor` hierarchy (`MySQLDumpProcessor` for mysqldump, `MydumperDumpProcessor` for mydumper). Also contains regex patterns for GTID extraction, definer stripping, and SQL mode filtering. |
| 51 | +- **`config.py`** — Constants and env var reads (`SOURCE_SERVICE_URI`, `TARGET_SERVICE_URI`, `TARGET_MASTER_SERVICE_URI`). |
| 52 | +- **`enums.py`** — `MySQLMigrateTool` (mysqldump/mydumper) and `MySQLMigrateMethod` (dump/replication). |
| 53 | +- **`exceptions.py`** — Exception hierarchy. `ReplicationNotAvailableException` subclasses cause fallback to dump method. `MigrationPreCheckException` subclasses are fatal pre-check failures. |
| 54 | + |
| 55 | +### Tests |
| 56 | + |
| 57 | +- **`test/unit/`** — Pure unit tests. `test_utils.py` covers dump processors, URI parsing, password validation. `test_dump_tools.py` covers command generation and temp file handling. |
| 58 | +- **`test/sys/`** — System tests against real MySQL containers (via `docker-compose.test.yaml`). Tests run inside a container built from `Dockerfile`. The `db_name` fixture generates random 10-char alphanumeric names. |
| 59 | + |
| 60 | +## Conventions |
| 61 | + |
| 62 | +- SQL identifiers derived from variables (like `db_name`) must be backtick-quoted in queries to handle names starting with digits. |
| 63 | +- The project uses `pymysql` with `DictCursor` — query results are dicts keyed by column name. |
| 64 | +- Formatting: `yapf` + `isort`. Linting: `flake8` + `pylint`. Types: `mypy`. |
| 65 | +- `setup.py` is a shim; all config is in `pyproject.toml`. |
0 commit comments