fix: Handle partially migrated databases in migration 002 detection

CRITICAL HOTFIX for production deployment failure

Problem:
- Production database had migration 001 applied but not migration 002
- Migration 002's tables (tokens, authorization_codes) already existed from SCHEMA_SQL
- Smart detection only checked when migration_count == 0 (fresh database)
- For partially migrated databases (count > 0), tried to run full migration
- This failed with "table already exists" error

Solution:
- Always check migration 002's state, regardless of migration_count
- If tables exist with correct structure, skip table creation
- Create missing indexes only
- Mark migration as applied

Testing:
- Manual verification with production scenario: SUCCESS
- 561 automated tests passing
- test_run_migrations_partial_applied confirms fix works

Impact:
- Fixes deployment on partially migrated production databases
- No impact on fresh or fully migrated databases
- Backwards compatible with all database states

Version: 1.0.0-rc.2 → 1.0.0-rc.3

Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-24 13:26:15 -07:00
parent baf799120e
commit 605681de42
4 changed files with 219 additions and 6 deletions

View File

@@ -153,5 +153,5 @@ def create_app(config=None):
# Package version (Semantic Versioning 2.0.0)
# See docs/standards/versioning-strategy.md for details
__version__ = "1.0.0-rc.2"
__version_info__ = (1, 0, 0, "rc", 2)
__version__ = "1.0.0-rc.3"
__version_info__ = (1, 0, 0, "rc", 3)

View File

@@ -370,14 +370,20 @@ def run_migrations(db_path, logger=None):
# Get already-applied migrations
applied = get_applied_migrations(conn)
# Apply pending migrations (using smart detection for fresh databases)
# Apply pending migrations (using smart detection for fresh databases and migration 002)
pending_count = 0
skipped_count = 0
for migration_name, migration_path in migration_files:
if migration_name not in applied:
# For fresh databases (migration_count == 0), check if migration is actually needed
# Some migrations may have been included in SCHEMA_SQL
if migration_count == 0 and not is_migration_needed(conn, migration_name):
# Check if migration is actually needed
# For fresh databases (migration_count == 0), check all migrations
# For migration 002, ALWAYS check (handles partially migrated databases)
should_check_needed = (
migration_count == 0 or
migration_name == "002_secure_tokens_and_authorization_codes.sql"
)
if should_check_needed and not is_migration_needed(conn, migration_name):
# Special handling for migration 002: if tables exist but indexes don't,
# create just the indexes
if migration_name == "002_secure_tokens_and_authorization_codes.sql":