Replace migrate_to_mariadb.py with sbin/setup_mariadb.py

Covers all 5 models/tables, prompts for credentials, snapshots SQLite,
and prints env/service config at the end.
This commit is contained in:
2026-04-15 17:34:45 -05:00
parent 8721254476
commit f64e14e713
3 changed files with 324 additions and 145 deletions
+28 -16
View File
@@ -217,26 +217,32 @@ flask db upgrade
## 5. Run the migration script
The `migrate_to_mariadb.py` script reads every record from SQLite and inserts
it into MariaDB using the SQLAlchemy ORM — no raw SQL, no CSV exports.
`sbin/setup_mariadb.py` prompts interactively for credentials (or reads them
from environment variables), snapshots the SQLite database, creates all tables
on MariaDB, and migrates every record using the SQLAlchemy ORM.
```bash
# Interactive — prompts for host, port, user, password, database
python sbin/setup_mariadb.py
# Non-interactive — supply individual env vars
MARIADB_HOST=localhost MARIADB_PORT=3306 \
MARIADB_USER=battuser MARIADB_PASSWORD=strongpassword \
MARIADB_DATABASE=batteries \
python sbin/setup_mariadb.py
# Non-interactive — supply a full URL
MARIADB_URL='mysql+pymysql://battuser:strongpassword@localhost/batteries?charset=utf8mb4' \
python migrate_to_mariadb.py
```
Or pass the URL as a positional argument:
```bash
python migrate_to_mariadb.py 'mysql+pymysql://battuser:strongpassword@localhost/batteries?charset=utf8mb4'
python sbin/setup_mariadb.py
```
The script:
1. Creates all tables on MariaDB if they don't exist
2. Inserts all Device records (preserving primary keys)
3. Inserts all Battery records (preserving primary keys and foreign keys)
1. Snapshots `batteries.db``batteries.db.YYYY-MM-DD.snapshot`
2. Creates all tables on MariaDB if they don't exist
3. Migrates Device, Battery, CapacityTest, ChargeLog, and BatteryPctLog records (preserving primary keys and foreign keys)
4. Resets `AUTO_INCREMENT` counters past the highest migrated ID
5. Prints a verification table comparing row counts
6. Prints the exact `DATABASE_URL` and systemd service line to configure
---
@@ -246,10 +252,13 @@ The migration script prints a summary:
```
=== Verification ===
Table SQLite MariaDB OK?
--------------------------------------
device 5 5 OK
battery 40 40 OK
Table SQLite MariaDB OK?
-----------------------------------------------
device 5 5 OK
battery 40 40 OK
capacity_test 12 12 OK
charge_log 87 87 OK
battery_pct_log 320 320 OK
Migration complete. All row counts match.
```
@@ -262,6 +271,9 @@ You can also verify manually in the MariaDB shell:
```sql
SELECT COUNT(*) FROM device;
SELECT COUNT(*) FROM battery;
SELECT COUNT(*) FROM capacity_test;
SELECT COUNT(*) FROM charge_log;
SELECT COUNT(*) FROM battery_pct_log;
```
---