chore: docs and scripts organized. clutter cleared.
This commit is contained in:
57
archive/legacy_migrations/migrate_add_counter.py
Executable file
57
archive/legacy_migrations/migrate_add_counter.py
Executable file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Database migration: Add counter field to nl43_status table
|
||||
|
||||
This adds the d0 (measurement interval counter) field to track the device's
|
||||
actual measurement progress for accurate timer synchronization.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
DB_PATH = "data/slmm.db"
|
||||
|
||||
def migrate():
|
||||
print(f"Adding counter field to: {DB_PATH}")
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if counter column already exists
|
||||
cursor.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if 'counter' in columns:
|
||||
print("✓ Counter column already exists, no migration needed")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
print("Starting migration...")
|
||||
|
||||
# Add counter column
|
||||
cursor.execute("""
|
||||
ALTER TABLE nl43_status
|
||||
ADD COLUMN counter TEXT
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
print("✓ Added counter column")
|
||||
|
||||
# Verify
|
||||
cursor.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if 'counter' not in columns:
|
||||
raise Exception("Counter column was not added successfully")
|
||||
|
||||
print("✓ Migration completed successfully")
|
||||
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Migration failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate()
|
||||
55
archive/legacy_migrations/migrate_add_ftp_port.py
Normal file
55
archive/legacy_migrations/migrate_add_ftp_port.py
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Migration script to add ftp_port column to nl43_config table.
|
||||
|
||||
Usage:
|
||||
python migrate_add_ftp_port.py
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
def migrate():
|
||||
db_path = Path("data/slmm.db")
|
||||
|
||||
if not db_path.exists():
|
||||
print(f"❌ Database not found at {db_path}")
|
||||
print(" Run this script from the slmm directory")
|
||||
return False
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if column already exists
|
||||
cursor.execute("PRAGMA table_info(nl43_config)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if "ftp_port" in columns:
|
||||
print("✓ ftp_port column already exists")
|
||||
conn.close()
|
||||
return True
|
||||
|
||||
print("Adding ftp_port column to nl43_config table...")
|
||||
|
||||
# Add the ftp_port column with default value of 21
|
||||
cursor.execute("""
|
||||
ALTER TABLE nl43_config
|
||||
ADD COLUMN ftp_port INTEGER DEFAULT 21
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
print("✓ Migration completed successfully")
|
||||
print(" Added ftp_port column (default: 21)")
|
||||
|
||||
conn.close()
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Migration failed: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = migrate()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Database migration: Add measurement_start_time field to nl43_status table
|
||||
|
||||
This tracks when a measurement session started by detecting the state transition
|
||||
from "Stop" to "Measure", enabling accurate elapsed time display even for
|
||||
manually-started measurements.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
DB_PATH = "data/slmm.db"
|
||||
|
||||
def migrate():
|
||||
print(f"Adding measurement_start_time field to: {DB_PATH}")
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if measurement_start_time column already exists
|
||||
cursor.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if 'measurement_start_time' in columns:
|
||||
print("✓ measurement_start_time column already exists, no migration needed")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
print("Starting migration...")
|
||||
|
||||
# Add measurement_start_time column
|
||||
cursor.execute("""
|
||||
ALTER TABLE nl43_status
|
||||
ADD COLUMN measurement_start_time TEXT
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
print("✓ Added measurement_start_time column")
|
||||
|
||||
# Verify
|
||||
cursor.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if 'measurement_start_time' not in columns:
|
||||
raise Exception("measurement_start_time column was not added successfully")
|
||||
|
||||
print("✓ Migration completed successfully")
|
||||
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Migration failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate()
|
||||
111
archive/legacy_migrations/migrate_field_names.py
Normal file
111
archive/legacy_migrations/migrate_field_names.py
Normal file
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Migration script to rename NL43 measurement field names to match actual device output.
|
||||
|
||||
Changes:
|
||||
- lp -> laeq (A-weighted equivalent continuous sound level)
|
||||
- leq -> lae (A-weighted sound exposure level)
|
||||
- lmax -> lasmax (A-weighted slow maximum)
|
||||
- lmin -> lasmin (A-weighted slow minimum)
|
||||
- lpeak -> lapeak (A-weighted peak)
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
def migrate_database(db_path: str):
|
||||
"""Migrate the database schema to use correct field names."""
|
||||
|
||||
print(f"Migrating database: {db_path}")
|
||||
|
||||
# Connect to database
|
||||
conn = sqlite3.connect(db_path)
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
# Check if migration is needed
|
||||
cur.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cur.fetchall()]
|
||||
|
||||
if 'laeq' in columns:
|
||||
print("✓ Database already migrated")
|
||||
return
|
||||
|
||||
if 'lp' not in columns:
|
||||
print("✗ Database schema does not match expected format")
|
||||
sys.exit(1)
|
||||
|
||||
print("Starting migration...")
|
||||
|
||||
# SQLite doesn't support column renaming directly, so we need to:
|
||||
# 1. Create new table with correct column names
|
||||
# 2. Copy data from old table
|
||||
# 3. Drop old table
|
||||
# 4. Rename new table
|
||||
|
||||
# Create new table with correct column names
|
||||
cur.execute("""
|
||||
CREATE TABLE nl43_status_new (
|
||||
unit_id VARCHAR PRIMARY KEY,
|
||||
last_seen DATETIME,
|
||||
measurement_state VARCHAR,
|
||||
laeq VARCHAR,
|
||||
lae VARCHAR,
|
||||
lasmax VARCHAR,
|
||||
lasmin VARCHAR,
|
||||
lapeak VARCHAR,
|
||||
battery_level VARCHAR,
|
||||
power_source VARCHAR,
|
||||
sd_remaining_mb VARCHAR,
|
||||
sd_free_ratio VARCHAR,
|
||||
raw_payload TEXT
|
||||
)
|
||||
""")
|
||||
print("✓ Created new table with correct column names")
|
||||
|
||||
# Copy data from old table to new table
|
||||
cur.execute("""
|
||||
INSERT INTO nl43_status_new
|
||||
(unit_id, last_seen, measurement_state, laeq, lae, lasmax, lasmin, lapeak,
|
||||
battery_level, power_source, sd_remaining_mb, sd_free_ratio, raw_payload)
|
||||
SELECT
|
||||
unit_id, last_seen, measurement_state, lp, leq, lmax, lmin, lpeak,
|
||||
battery_level, power_source, sd_remaining_mb, sd_free_ratio, raw_payload
|
||||
FROM nl43_status
|
||||
""")
|
||||
rows_copied = cur.rowcount
|
||||
print(f"✓ Copied {rows_copied} rows from old table")
|
||||
|
||||
# Drop old table
|
||||
cur.execute("DROP TABLE nl43_status")
|
||||
print("✓ Dropped old table")
|
||||
|
||||
# Rename new table
|
||||
cur.execute("ALTER TABLE nl43_status_new RENAME TO nl43_status")
|
||||
print("✓ Renamed new table to nl43_status")
|
||||
|
||||
# Commit changes
|
||||
conn.commit()
|
||||
print("✓ Migration completed successfully")
|
||||
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
print(f"✗ Migration failed: {e}")
|
||||
sys.exit(1)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Default database path
|
||||
db_path = Path(__file__).parent / "data" / "slmm.db"
|
||||
|
||||
# Allow custom path as command line argument
|
||||
if len(sys.argv) > 1:
|
||||
db_path = Path(sys.argv[1])
|
||||
|
||||
if not db_path.exists():
|
||||
print(f"✗ Database not found: {db_path}")
|
||||
sys.exit(1)
|
||||
|
||||
migrate_database(str(db_path))
|
||||
116
archive/legacy_migrations/migrate_revert_field_names.py
Normal file
116
archive/legacy_migrations/migrate_revert_field_names.py
Normal file
@@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Migration script to revert NL43 measurement field names back to correct DRD format.
|
||||
|
||||
The previous migration was incorrect. According to NL43 DRD documentation:
|
||||
- d0 = counter (1-600) - NOT a measurement!
|
||||
- d1 = Lp (instantaneous sound pressure level)
|
||||
- d2 = Leq (equivalent continuous sound level)
|
||||
- d3 = Lmax (maximum level)
|
||||
- d4 = Lmin (minimum level)
|
||||
- d5 = Lpeak (peak level)
|
||||
|
||||
Changes:
|
||||
- laeq -> lp (was incorrectly mapped to counter field!)
|
||||
- lae -> leq
|
||||
- lasmax -> lmax
|
||||
- lasmin -> lmin
|
||||
- lapeak -> lpeak
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
def migrate_database(db_path: str):
|
||||
"""Revert database schema to correct DRD field names."""
|
||||
|
||||
print(f"Reverting database migration: {db_path}")
|
||||
|
||||
# Connect to database
|
||||
conn = sqlite3.connect(db_path)
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
# Check if migration is needed
|
||||
cur.execute("PRAGMA table_info(nl43_status)")
|
||||
columns = [row[1] for row in cur.fetchall()]
|
||||
|
||||
if 'lp' in columns:
|
||||
print("✓ Database already has correct field names")
|
||||
return
|
||||
|
||||
if 'laeq' not in columns:
|
||||
print("✗ Database schema does not match expected format")
|
||||
sys.exit(1)
|
||||
|
||||
print("Starting revert migration...")
|
||||
|
||||
# Create new table with correct column names
|
||||
cur.execute("""
|
||||
CREATE TABLE nl43_status_new (
|
||||
unit_id VARCHAR PRIMARY KEY,
|
||||
last_seen DATETIME,
|
||||
measurement_state VARCHAR,
|
||||
lp VARCHAR,
|
||||
leq VARCHAR,
|
||||
lmax VARCHAR,
|
||||
lmin VARCHAR,
|
||||
lpeak VARCHAR,
|
||||
battery_level VARCHAR,
|
||||
power_source VARCHAR,
|
||||
sd_remaining_mb VARCHAR,
|
||||
sd_free_ratio VARCHAR,
|
||||
raw_payload TEXT
|
||||
)
|
||||
""")
|
||||
print("✓ Created new table with correct DRD field names")
|
||||
|
||||
# Copy data from old table to new table
|
||||
# Note: laeq was incorrectly mapped to d0 (counter), so we discard it
|
||||
# The actual measurements start from d1
|
||||
cur.execute("""
|
||||
INSERT INTO nl43_status_new
|
||||
(unit_id, last_seen, measurement_state, lp, leq, lmax, lmin, lpeak,
|
||||
battery_level, power_source, sd_remaining_mb, sd_free_ratio, raw_payload)
|
||||
SELECT
|
||||
unit_id, last_seen, measurement_state, lae, lasmax, lasmin, lapeak, NULL,
|
||||
battery_level, power_source, sd_remaining_mb, sd_free_ratio, raw_payload
|
||||
FROM nl43_status
|
||||
""")
|
||||
rows_copied = cur.rowcount
|
||||
print(f"✓ Copied {rows_copied} rows (note: discarded incorrect 'laeq' counter field)")
|
||||
|
||||
# Drop old table
|
||||
cur.execute("DROP TABLE nl43_status")
|
||||
print("✓ Dropped old table")
|
||||
|
||||
# Rename new table
|
||||
cur.execute("ALTER TABLE nl43_status_new RENAME TO nl43_status")
|
||||
print("✓ Renamed new table to nl43_status")
|
||||
|
||||
# Commit changes
|
||||
conn.commit()
|
||||
print("✓ Revert migration completed successfully")
|
||||
print("\nNote: The 'lp' field will be populated correctly on next device measurement")
|
||||
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
print(f"✗ Migration failed: {e}")
|
||||
sys.exit(1)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Default database path
|
||||
db_path = Path(__file__).parent / "data" / "slmm.db"
|
||||
|
||||
# Allow custom path as command line argument
|
||||
if len(sys.argv) > 1:
|
||||
db_path = Path(sys.argv[1])
|
||||
|
||||
if not db_path.exists():
|
||||
print(f"✗ Database not found: {db_path}")
|
||||
sys.exit(1)
|
||||
|
||||
migrate_database(str(db_path))
|
||||
Reference in New Issue
Block a user