Files
waiting-system/apply_barcode_migration.py
Jun-dev f699a29a85 Add waiting system application files
- Add main application files (main.py, models.py, schemas.py, etc.)
- Add routers for all features (waiting, attendance, members, etc.)
- Add HTML templates for admin and user interfaces
- Add migration scripts and utility files
- Add Docker configuration
- Add documentation files
- Add .gitignore to exclude database and cache files

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-14 00:29:39 +09:00

43 lines
1.6 KiB
Python

import sqlite3
def migrate():
db_path = "waiting_system.db"
print(f"Connecting to {db_path}...")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# 1. Check for duplicate barcodes
print("Checking for duplicate barcodes...")
cursor.execute("SELECT barcode, COUNT(*) FROM members WHERE barcode IS NOT NULL AND barcode != '' GROUP BY barcode HAVING COUNT(*) > 1")
duplicates = cursor.fetchall()
if duplicates:
print("ERROR: Found duplicates in barcode. Cannot apply unique constraint.")
for code, count in duplicates:
print(f" Barcode '{code}': {count} entries")
print("Please resolve duplicates manually before applying constraint.")
return
# 2. Drop existing index if exists (SQLAlchemy likely named it ix_members_barcode)
print("Dropping old index 'ix_members_barcode' if exists...")
cursor.execute("DROP INDEX IF EXISTS ix_members_barcode")
# 3. Create Unique Index
print("Creating UNIQUE INDEX 'ix_members_barcode'...")
# Note: SQLite allows NULLs in UNIQUE columns to be distinct (multiple NULLs ok).
# But we also want to ensure empty strings are handled?
# Usually barcode is NULL if empty.
cursor.execute("CREATE UNIQUE INDEX ix_members_barcode ON members (barcode)")
print("Success! Unique constraint applied to 'barcode'.")
except Exception as e:
print(f"Migration failed: {e}")
finally:
conn.close()
if __name__ == "__main__":
migrate()