Files
StarPunk/starpunk/database.py
Phil Skentelbery 9a805ec316 Implement automatic database migration system
Following design in ADR-020, implementation guidance, and quick reference.

Phase 1: Migration System Core (starpunk/migrations.py)
- Create migration runner with fresh database detection
- Implement is_schema_current() heuristic for fresh DB detection
- Add helper functions (table_exists, column_exists, index_exists)
- Complete error handling with MigrationError exception
- 315 lines of production code

Phase 2: Database Integration (starpunk/database.py)
- Modify init_db() to call run_migrations()
- Add logger parameter handling
- 5 lines changed for integration

Phase 3: Comprehensive Testing (tests/test_migrations.py)
- 26 tests covering all scenarios (100% pass rate)
- Tests for fresh DB, legacy DB, helpers, error handling
- Integration test with actual migration file
- 560 lines of test code

Phase 4: Version and Documentation
- Bump version to 0.9.0 (MINOR increment per versioning strategy)
- Update CHANGELOG.md with comprehensive v0.9.0 entry
- Create implementation report documenting all details

Features:
- Fresh database detection prevents unnecessary migrations
- Legacy database detection applies pending migrations automatically
- Migration tracking table records all applied migrations
- Idempotent execution safe for multiple runs
- Fail-safe: app won't start if migrations fail
- Container deployments now fully automatic

Testing:
- All 26 migration tests passing (100%)
- Fresh database scenario verified (auto-skip)
- Legacy database scenario verified (migrations applied)
- Idempotent behavior confirmed

Documentation:
- Implementation report in docs/reports/
- CHANGELOG.md updated with v0.9.0 entry
- All architecture decisions from ADR-020 implemented

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-19 16:08:33 -07:00

119 lines
3.2 KiB
Python

"""
Database initialization and operations for StarPunk
SQLite database for metadata, sessions, and tokens
"""
import sqlite3
from pathlib import Path
# Database schema
SCHEMA_SQL = """
-- Notes metadata (content is in files)
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT UNIQUE NOT NULL,
file_path TEXT UNIQUE NOT NULL,
published BOOLEAN DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,
content_hash TEXT
);
CREATE INDEX IF NOT EXISTS idx_notes_created_at ON notes(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notes_published ON notes(published);
CREATE INDEX IF NOT EXISTS idx_notes_slug ON notes(slug);
CREATE INDEX IF NOT EXISTS idx_notes_deleted_at ON notes(deleted_at);
-- Authentication sessions (IndieLogin)
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_token_hash TEXT UNIQUE NOT NULL,
me TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
last_used_at TIMESTAMP,
user_agent TEXT,
ip_address TEXT
);
CREATE INDEX IF NOT EXISTS idx_sessions_token_hash ON sessions(session_token_hash);
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at);
CREATE INDEX IF NOT EXISTS idx_sessions_me ON sessions(me);
-- Micropub access tokens
CREATE TABLE IF NOT EXISTS tokens (
token TEXT PRIMARY KEY,
me TEXT NOT NULL,
client_id TEXT,
scope TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_tokens_me ON tokens(me);
-- CSRF state tokens (for IndieAuth flow)
CREATE TABLE IF NOT EXISTS auth_state (
state TEXT PRIMARY KEY,
code_verifier TEXT NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
redirect_uri TEXT
);
CREATE INDEX IF NOT EXISTS idx_auth_state_expires ON auth_state(expires_at);
"""
def init_db(app=None):
"""
Initialize database schema and run migrations
Args:
app: Flask application instance (optional, for config access)
"""
if app:
db_path = app.config["DATABASE_PATH"]
logger = app.logger
else:
# Fallback to default path
db_path = Path("./data/starpunk.db")
logger = None
# Ensure parent directory exists
db_path.parent.mkdir(parents=True, exist_ok=True)
# Create database and initial schema
conn = sqlite3.connect(db_path)
try:
conn.executescript(SCHEMA_SQL)
conn.commit()
if logger:
logger.info(f"Database initialized: {db_path}")
else:
print(f"Database initialized: {db_path}")
finally:
conn.close()
# Run migrations
from starpunk.migrations import run_migrations
run_migrations(db_path, logger=logger)
def get_db(app):
"""
Get database connection
Args:
app: Flask application instance
Returns:
sqlite3.Connection
"""
db_path = app.config["DATABASE_PATH"]
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row # Return rows as dictionaries
return conn