Skip to content

[BUG] CREATE TABLE workspace migration silently recorded but not executed — crashes on v1.2.17 upgrade #16026

@flitzrrr

Description

@flitzrrr

Root Cause (resolved)

This was caused by running a forked dev build (feature/agent-teams) against the production database on both affected machines. The dev build wrote the workspace migration with a different timestamp (1772134200000) than the official release (1772056728000). Due to a known Drizzle migration timestamp ordering bug, when the official v1.2.16 release ran, it skipped the workspace CREATE TABLE migration because a newer timestamp was already recorded -- but the table had never actually been created.

This is not a bug in the official OpenCode releases. However, the underlying Drizzle bug means any contributor testing a local build with divergent migration timestamps will silently corrupt their database.

Workaround: Manually create the missing table (see below) or delete the database and start fresh.


Original issue description (outdated -- kept for reference)

Description

After upgrading to v1.2.17, OpenCode crashes on every startup:

Failed to run the query 'ALTER TABLE `workspace` ADD `type` text NOT NULL;'

Root cause: Migration 20260225215848_workspace (CREATE TABLE workspace) was recorded as applied in __drizzle_migrations during v1.2.16 startup but never actually executed. When v1.2.17 tries to ALTER TABLE workspace (migration 20260303231226_add_workspace_fields), it fails because the table doesn't exist.

Why it failed silently: The exact mechanism is still under investigation. The initial hypothesis was that PRAGMA foreign_keys = ON (set before migrations in db.ts) might cause the CREATE TABLE with a FK constraint to fail silently. However, as discussed in the comments, SQLite FK enforcement only applies to DML operations, not DDL -- so this likely does not explain the failure. The actual cause may be related to how Drizzle's migrator handles errors or how it interacts with databases that have accumulated data over multiple prior versions.

Reproduced on two machines:

Machine Chip Result
Mac Studio Apple M2 Max Silent migration failure on v1.2.16, crash on v1.2.17
MacBook Pro Apple M3 Max Silent migration failure on v1.2.16, crash on v1.2.17

Both machines show the same DB state: migration hash recorded in __drizzle_migrations, but workspace table does not exist. Both databases were built up over several weeks (v1.2.13 through v1.2.15) with real data before hitting v1.2.16.

Additional observation: OpenCode CLI v1.2.15 (installed via Homebrew on the MacBook Pro) still starts fine because it predates the workspace migration entirely (only ships 3 migrations). The bug is only triggered when upgrading through versions that include migration 20260225215848_workspace.

Evidence from DB (MacBook Pro):

  • __drizzle_migrations: 5 entries (including workspace migration timestamp) -- all "applied"
  • workspace table: does not exist
  • session table: has workspace_id column (migration tweak: glob should be able to read certain hidden files/dirs #5 succeeded -- simple ALTER, no FK)
  • All other data intact (sessions, messages, projects)

Log timeline (MacBook Pro):

Date Version Bundled Migrations Result
2026-02-26 v1.2.13 3 OK
2026-02-27 v1.2.15 3 OK
2026-03-03 v1.2.15 3 OK
2026-03-04 10:35 v1.2.16 5 No error logged, but workspace table not created
2026-03-04 16:01 v1.2.17 6 Crash: ALTER TABLE workspace fails

Migration #4 SQL (20260225215848_workspace/migration.sql):

CREATE TABLE `workspace` (
    `id` text PRIMARY KEY,
    `branch` text,
    `project_id` text NOT NULL,
    `config` text NOT NULL,
    CONSTRAINT `fk_workspace_project_id_project_id_fk`
        FOREIGN KEY (`project_id`) REFERENCES `project`(`id`) ON DELETE CASCADE
);

Workaround: Manually create the table, then start opencode:

cp ~/.local/share/opencode/opencode.db ~/.local/share/opencode/opencode.db.bak
sqlite3 ~/.local/share/opencode/opencode.db "CREATE TABLE \`workspace\` (\`id\` text PRIMARY KEY, \`branch\` text, \`project_id\` text NOT NULL, \`config\` text NOT NULL, CONSTRAINT \`fk_workspace_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE);"
opencode

Suggested fix: Verify DDL execution before recording the migration hash, or add a pre-migration check that detects missing tables whose hashes are already recorded and re-runs them.

Plugins

None

OpenCode version

v1.2.17 (crash) -- silent failure occurred during v1.2.16

Steps to reproduce

  1. Fork the repo and build a local dev build with divergent migration timestamps
  2. Run the dev build against a production database -- migrations are recorded with different timestamps
  3. Upgrade to official v1.2.16 -- Drizzle skips workspace migration (known timestamp ordering bug)
  4. Upgrade to v1.2.17 -- crash on startup

Screenshot and/or share link

Image Image Image

Operating System

macOS 15.x (Apple Silicon) -- reproduced on Mac Studio (M2 Max) and MacBook Pro (M3 Max)

Terminal

zsh (Homebrew Cask install)

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions