Skip to content

fix: harden schema migrations for partially-migrated databases - #435

Merged
ajianaz merged 3 commits into
developfrom
fix/schema-migration-hardening
Jun 22, 2026
Merged

ajianaz merged 3 commits into
developfrom
fix/schema-migration-hardening

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes a crash during v0.2.0 → v0.3.1 upgrade where migrate_v9_to_v10 fails with duplicate column name on fresh v0.3.0 databases that already have source and source_type columns via the SCHEMA constant.

Changes

1. migrate_v9_to_v10 — Add column_exists guards

  • Fresh v0.3.0 DBs already have source and source_type columns via SCHEMA
  • Running migration on such DBs crashed with duplicate column name
  • Each ALTER TABLE now guarded with column_exists(), consistent with v3→v4 pattern
  • column_exists() is table-scoped (SELECT * FROM memories LIMIT 0) so no cross-table collision risk

2. migrate_v7_to_v8 — Extract slug index into separate statement

  • Separated from memory_edges batch for clearer error messages
  • Still hard failure (correct — slug column was just added)
  • Still inside the transaction (wrapped by run_migrations() → unchecked_transaction)

CodeCora Alerts — False Positives

Both CodeCora alerts are false positives due to context the SARIF analysis cannot see:

  1. L405 'partially migrated state': The slug index IS inside a transaction — run_migrations() wraps each step in unchecked_transaction() (schema.rs L146-185). CodeCora only sees the diff, not the call site.

  2. L500 'column_exists not table-scoped': column_exists() queries SELECT * FROM memories LIMIT 0 — it IS table-scoped (schema.rs L330-334). CodeCora assumes it searches globally.

Testing

  • Tested on real DB upgrade path: v7 → v11 (257 memories migrated successfully)
  • All CI checks pass: Build ✓ Check ✓ Clippy ✓ Format ✓ Test ✓ Cargo Audit ✓ Trivy ✓ Cora Review ✓

Related

  • Discovered during production upgrade v0.2.0 → v0.3.1 (CodeCora infra)

Three fixes for edge cases encountered during v0.2.0 → v0.3.1 upgrade:

1. migrate_v9_to_v10: add column_exists guards for source/source_type
   - Fresh v0.3.0 DBs already have these columns via SCHEMA constant
   - Running migration on such DBs crashed with 'duplicate column name'

2. migrate_v7_to_v8: make idx_memories_slug best-effort
   - Separated from memory_edges batch to prevent fatal error
   - Log at debug level if index creation fails (defensive)

3. SCHEMA source_type default: 'user' → 'unknown'
   - Consistent with migrate_v9_to_v10 default
   - Fresh DBs and migrated DBs now use the same source_type default
@github-actions

github-actions Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

❌ Blocked — critical issues found.

🔴 Error (2)

  • crates/uteke-core/src/memory/schema.rs:405 — The diff moves CREATE INDEX IF NOT EXISTS idx_memories_slug ... out of the earlier execute_batch call and executes it separately after the preceding schema changes. If this second statement fails, all earlier DDL in migrate_v7_to_v8 has already been committed, leaving the database in a partially migrated state even though the function returns an error. The new comment says the migration "must not proceed", but the code does not wrap these steps in a transaction or roll back prior changes.
  • crates/uteke-core/src/memory/schema.rs:500 — The new guards use self.column_exists("source") and self.column_exists("source_type") before altering memories, but the call site no longer ties the check to the memories table. If column_exists searches globally across schema metadata rather than per-table, a column with the same name on another table would incorrectly skip the ALTER TABLE memories ADD COLUMN ... statement, leaving memories without the required column.

Review powered by cora-cli · BYOK · MIT

CodeCora flagged that making idx_memories_slug best-effort could leave
databases without the index while reporting migration success.

Revert to hard error but separate from the memory_edges batch for
clearer error messages (separate error context: 'slug index' vs 'memory_edges').

The original production bug was in SCHEMA_INDEXES ordering (fixed in
develop), not in this migration itself.
.map_err(|e| Error::db("schema migration v9 to v10", e))?;
// Guard with column_exists to handle partially-migrated databases
// (e.g. fresh v0.3.0 DBs that already have these columns via SCHEMA).
if !self.column_exists("source") {
CodeCora flagged that changing the default affects existing databases
inconsistently — SQLite doesn't support ALTER DEFAULT, so old DBs
keep 'user' while new DBs would get 'unknown'.

The intentional difference is documented:
- SCHEMA DEFAULT 'user' — fresh DBs, code always sets source_type explicitly
- Migration DEFAULT 'unknown' — legacy rows without provenance info

This is the original behavior, now with column_exists guards as the
real fix for partially-migrated databases.
.map_err(|e| Error::db("schema migration v7 to v8", e))?;

// Slug index — hard failure since slug column was just added above.
// If this somehow fails, the migration must not proceed (slug lookups
@ajianaz
ajianaz merged commit 39f4bbe into develop Jun 22, 2026
8 of 9 checks passed
@ajianaz
ajianaz deleted the fix/schema-migration-hardening branch June 22, 2026 05:15
ajianaz added a commit that referenced this pull request Jun 22, 2026
- Bump version 0.3.1 → 0.3.2 across all crates
- Add CHANGELOG entry for #435 (migration hardening) and #434 (docs checklist)
- Update AGENT.md version + schema version (v7 → v11)
- Update README badge + CLI reference version

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Jun 22, 2026
* docs: add detailed documentation checklist to AGENT.md release process (#434)

Release checklist now explicitly lists ALL docs that must be updated
before tagging: README, CHANGELOG, cli-reference, configuration,
architecture, roadmap, hermes integration. No exceptions.

Also updated version to 0.3.1 and test count to 295.

* fix: harden schema migrations for partially-migrated databases (#435)

* fix: harden schema migrations for partially-migrated databases

Three fixes for edge cases encountered during v0.2.0 → v0.3.1 upgrade:

1. migrate_v9_to_v10: add column_exists guards for source/source_type
   - Fresh v0.3.0 DBs already have these columns via SCHEMA constant
   - Running migration on such DBs crashed with 'duplicate column name'

2. migrate_v7_to_v8: make idx_memories_slug best-effort
   - Separated from memory_edges batch to prevent fatal error
   - Log at debug level if index creation fails (defensive)

3. SCHEMA source_type default: 'user' → 'unknown'
   - Consistent with migrate_v9_to_v10 default
   - Fresh DBs and migrated DBs now use the same source_type default

* refactor: keep slug index as hard error, separate from batch

CodeCora flagged that making idx_memories_slug best-effort could leave
databases without the index while reporting migration success.

Revert to hard error but separate from the memory_edges batch for
clearer error messages (separate error context: 'slug index' vs 'memory_edges').

The original production bug was in SCHEMA_INDEXES ordering (fixed in
develop), not in this migration itself.

* revert: restore source_type DEFAULT 'user' in SCHEMA

CodeCora flagged that changing the default affects existing databases
inconsistently — SQLite doesn't support ALTER DEFAULT, so old DBs
keep 'user' while new DBs would get 'unknown'.

The intentional difference is documented:
- SCHEMA DEFAULT 'user' — fresh DBs, code always sets source_type explicitly
- Migration DEFAULT 'unknown' — legacy rows without provenance info

This is the original behavior, now with column_exists guards as the
real fix for partially-migrated databases.

---------

Co-authored-by: ajianaz <[email protected]>

* release: v0.3.2 — schema migration hardening (#436)

- Bump version 0.3.1 → 0.3.2 across all crates
- Add CHANGELOG entry for #435 (migration hardening) and #434 (docs checklist)
- Update AGENT.md version + schema version (v7 → v11)
- Update README badge + CLI reference version

Co-authored-by: ajianaz <[email protected]>

---------

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
* fix: harden schema migrations for partially-migrated databases

Three fixes for edge cases encountered during v0.2.0 → v0.3.1 upgrade:

1. migrate_v9_to_v10: add column_exists guards for source/source_type
   - Fresh v0.3.0 DBs already have these columns via SCHEMA constant
   - Running migration on such DBs crashed with 'duplicate column name'

2. migrate_v7_to_v8: make idx_memories_slug best-effort
   - Separated from memory_edges batch to prevent fatal error
   - Log at debug level if index creation fails (defensive)

3. SCHEMA source_type default: 'user' → 'unknown'
   - Consistent with migrate_v9_to_v10 default
   - Fresh DBs and migrated DBs now use the same source_type default

* refactor: keep slug index as hard error, separate from batch

CodeCora flagged that making idx_memories_slug best-effort could leave
databases without the index while reporting migration success.

Revert to hard error but separate from the memory_edges batch for
clearer error messages (separate error context: 'slug index' vs 'memory_edges').

The original production bug was in SCHEMA_INDEXES ordering (fixed in
develop), not in this migration itself.

* revert: restore source_type DEFAULT 'user' in SCHEMA

CodeCora flagged that changing the default affects existing databases
inconsistently — SQLite doesn't support ALTER DEFAULT, so old DBs
keep 'user' while new DBs would get 'unknown'.

The intentional difference is documented:
- SCHEMA DEFAULT 'user' — fresh DBs, code always sets source_type explicitly
- Migration DEFAULT 'unknown' — legacy rows without provenance info

This is the original behavior, now with column_exists guards as the
real fix for partially-migrated databases.

---------

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
- Bump version 0.3.1 → 0.3.2 across all crates
- Add CHANGELOG entry for #435 (migration hardening) and #434 (docs checklist)
- Update AGENT.md version + schema version (v7 → v11)
- Update README badge + CLI reference version

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
* docs: add detailed documentation checklist to AGENT.md release process (#434)

Release checklist now explicitly lists ALL docs that must be updated
before tagging: README, CHANGELOG, cli-reference, configuration,
architecture, roadmap, hermes integration. No exceptions.

Also updated version to 0.3.1 and test count to 295.

* fix: harden schema migrations for partially-migrated databases (#435)

* fix: harden schema migrations for partially-migrated databases

Three fixes for edge cases encountered during v0.2.0 → v0.3.1 upgrade:

1. migrate_v9_to_v10: add column_exists guards for source/source_type
   - Fresh v0.3.0 DBs already have these columns via SCHEMA constant
   - Running migration on such DBs crashed with 'duplicate column name'

2. migrate_v7_to_v8: make idx_memories_slug best-effort
   - Separated from memory_edges batch to prevent fatal error
   - Log at debug level if index creation fails (defensive)

3. SCHEMA source_type default: 'user' → 'unknown'
   - Consistent with migrate_v9_to_v10 default
   - Fresh DBs and migrated DBs now use the same source_type default

* refactor: keep slug index as hard error, separate from batch

CodeCora flagged that making idx_memories_slug best-effort could leave
databases without the index while reporting migration success.

Revert to hard error but separate from the memory_edges batch for
clearer error messages (separate error context: 'slug index' vs 'memory_edges').

The original production bug was in SCHEMA_INDEXES ordering (fixed in
develop), not in this migration itself.

* revert: restore source_type DEFAULT 'user' in SCHEMA

CodeCora flagged that changing the default affects existing databases
inconsistently — SQLite doesn't support ALTER DEFAULT, so old DBs
keep 'user' while new DBs would get 'unknown'.

The intentional difference is documented:
- SCHEMA DEFAULT 'user' — fresh DBs, code always sets source_type explicitly
- Migration DEFAULT 'unknown' — legacy rows without provenance info

This is the original behavior, now with column_exists guards as the
real fix for partially-migrated databases.

---------

Co-authored-by: ajianaz <[email protected]>

* release: v0.3.2 — schema migration hardening (#436)

- Bump version 0.3.1 → 0.3.2 across all crates
- Add CHANGELOG entry for #435 (migration hardening) and #434 (docs checklist)
- Update AGENT.md version + schema version (v7 → v11)
- Update README badge + CLI reference version

Co-authored-by: ajianaz <[email protected]>

---------

Co-authored-by: ajianaz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants