Skip to content

Commit d75eb60

Browse files
eransclaude
andauthored
Feature/implement internal schema migration (#27)
* feat: Implement internal schema migration system Adds a comprehensive migration framework for managing pgsqlite's internal schema evolution. Key features: - Migration runner with transaction-based execution and automatic rollback - SHA256 checksum validation for migration integrity - Migration locking to prevent concurrent migrations - Support for SQL, SqlBatch, Function, and Combined migration types - Version tracking in __pgsqlite_metadata table - Migration history in __pgsqlite_migrations table Migration behavior: - NO automatic migrations on startup (safety first) - Schema version is checked on database initialization - Throws descriptive error if schema is outdated - Explicit --migrate CLI flag to run migrations and exit - Pre-migration databases (v1) are automatically detected Current migrations: - v1: Initial schema (creates system tables) - v2: ENUM support (enum types, values, usage tracking) Test mode handling: - In-memory databases auto-migrate during tests - Detects test environment via CARGO env variable - All test suites updated and passing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * docs: Update documentation for schema migration system Updates documentation to reflect the implemented schema migration behavior: README.md: - Added comprehensive Schema Migration section - Documented explicit --migrate flag requirement - Included usage examples and error messages - Explained migration features (transactions, checksums, history) TODO.md: - Added note about explicit migration mode in completed tasks - Confirmed all migration tasks are marked as completed docs/schema-migration-plan.md: - Added implementation status header (COMPLETED) - Updated Migration Execution Timing section - Changed from automatic to explicit migration approach - Added example commands showing migration workflow The documentation now clearly explains that migrations are never run automatically (except for test in-memory databases) and users must explicitly use the --migrate flag for safety. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: Add migration step to integration test script for file databases The GitHub Actions integration tests were failing because file-based database tests need to run migrations before starting the server. Changes: - Add migration execution for file-ssl and file-no-ssl modes - Run `pgsqlite --migrate` before starting the server - Ensure test database cleanup includes file databases - Exit with error if migration fails This fixes the CI/CD pipeline failure where tests failed with: "Database schema is outdated. Current version: 0, Required version: 2" 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: Enable auto-migration for in-memory databases in integration tests GitHub Actions was failing because in-memory databases weren't auto-migrating in the release build used by integration tests. Changes: - Add PGSQLITE_TEST_AUTO_MIGRATE environment variable support - In-memory databases auto-migrate when this env var is set - Test script sets this for in-memory test modes (tcp-ssl, tcp-no-ssl, unix-socket) - File-based test modes continue to use explicit --migrate This allows integration tests to run successfully while maintaining the safety of explicit migrations in production. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 63b158a commit d75eb60

20 files changed

+2545
-0
lines changed

CLAUDE.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ pgsqlite is a PostgreSQL protocol adapter for SQLite databases. It allows Postgr
3939
- Avoid adding comments unless necessary
4040
- Keep code concise and idiomatic
4141

42+
## Schema Migration System (2025-07-06)
43+
44+
pgsqlite includes an internal schema migration system to handle schema evolution:
45+
46+
### Migration Behavior
47+
- **No automatic migrations**: Migrations are NOT run automatically on startup
48+
- **Version checking**: Database schema version is checked on startup
49+
- **Error on outdated schema**: If the database schema is outdated, pgsqlite will exit with an error message
50+
- **Explicit migration**: Use `--migrate` command line flag to run pending migrations and exit
51+
52+
### Usage
53+
```bash
54+
# Run migrations on a database
55+
pgsqlite --database mydb.db --migrate
56+
57+
# Normal operation (will fail if schema is outdated)
58+
pgsqlite --database mydb.db
59+
```
60+
61+
### Implementation Details
62+
- Migrations are embedded in the binary using lazy_static
63+
- Each migration has a SHA256 checksum for integrity verification
64+
- Migrations run in transactions with automatic rollback on failure
65+
- Migration locking prevents concurrent migrations
66+
- Pre-migration databases (with __pgsqlite_schema table) are detected as version 1
67+
68+
### Current Migrations
69+
- **v1**: Initial schema (creates __pgsqlite_schema, metadata tables)
70+
- **v2**: ENUM support (creates enum types, values, and usage tracking tables)
71+
4272
## Recent Work (Condensed History)
4373
- Implemented comprehensive PostgreSQL type support (40+ types including ranges, network types, binary types)
4474
- Built custom DECIMAL type system with automatic query rewriting for proper numeric handling

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ config = "0.15"
5858
clap = { version = "4.5", features = ["derive", "env"] }
5959
lazy_static = "1.5"
6060

61+
# Cryptography for migration checksums
62+
sha2 = "0.10"
63+
6164
# Metrics
6265
prometheus = "0.14"
6366

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,37 @@ All SQLite functions are available, including:
237237
- Array operations (arrays are stored as JSON strings)
238238
- Advanced PostgreSQL features (stored procedures, triggers, etc.)
239239

240+
## Schema Migration
241+
242+
pgsqlite includes an internal schema migration system to manage its metadata tables. When pgsqlite evolves and requires changes to its internal schema, migrations ensure smooth upgrades.
243+
244+
### Migration Behavior
245+
246+
- **No automatic migrations**: For safety, migrations are NOT run automatically on startup
247+
- **Version checking**: Database schema version is checked on startup
248+
- **Explicit migration**: Use the `--migrate` flag to run pending migrations
249+
250+
### Running Migrations
251+
252+
```bash
253+
# Check if migrations are needed (will error if schema is outdated)
254+
cargo run -- --database mydb.db
255+
256+
# Run pending migrations and exit
257+
cargo run -- --database mydb.db --migrate
258+
259+
# Example output when migrations are needed:
260+
# Error: Failed to create database handler: Database schema is outdated.
261+
# Current version: 0, Required version: 2. Please run with --migrate to update the schema.
262+
```
263+
264+
### Migration Details
265+
266+
- Migrations run in transactions with automatic rollback on failure
267+
- Each migration has a SHA256 checksum for integrity verification
268+
- Migration history is tracked in `__pgsqlite_migrations` table
269+
- Concurrent migrations are prevented via locking
270+
240271
## Building and Testing
241272

242273
```bash

TODO.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ This file tracks all future development tasks for the pgsqlite project. It serve
215215

216216
### Storage & Optimization
217217

218+
#### Schema Migration System - COMPLETED (2025-07-06)
219+
- [x] Implement internal schema migration framework
220+
- [x] Create migration module with runner and registry
221+
- [x] Implement Migration and MigrationAction structs with SHA256 checksums
222+
- [x] Build migration registry with lazy_static for embedded migrations
223+
- [x] Create MigrationRunner with transaction-based execution
224+
- [x] Add migration locking to prevent concurrent migrations
225+
- [x] Integrate migrations into DbHandler initialization
226+
- [x] Support for SQL, SqlBatch, Function, and Combined migration types
227+
- [x] Version detection for pre-migration databases (v1 recognition)
228+
- [x] Comprehensive test coverage for all migration scenarios
229+
- [x] Migration history tracking in __pgsqlite_migrations table
230+
- [x] Idempotent migrations - can run multiple times safely
231+
- [x] Explicit migration mode - requires --migrate flag, errors if schema outdated
232+
- [x] Current migrations:
233+
- v1: Initial schema (__pgsqlite_schema, metadata tables)
234+
- v2: ENUM support (enum types, values, usage tracking)
235+
218236
#### Indexing
219237
- [ ] Support for expression indexes
220238
- [ ] Partial index support

0 commit comments

Comments
 (0)