Skip to content

fix: add migration for schedules CHECK constraint to include 'interval'#1063

Merged
threepointone merged 2 commits intomainfrom
fix-1061
Mar 4, 2026
Merged

fix: add migration for schedules CHECK constraint to include 'interval'#1063
threepointone merged 2 commits intomainfrom
fix-1061

Conversation

@threepointone
Copy link
Contributor

Fixes #1061

The cf_agents_schedules table's CHECK constraint on type only allowed ('scheduled', 'delayed', 'cron'). DOs created before the interval scheduling feature would hit CHECK constraint failed when trying to use scheduleEvery() or keepAlive().

Changes

Migration (packages/agents/src/index.ts)

  • Added migration that detects the old CHECK constraint via sqlite_master and recreates the table with 'interval' included, preserving all existing data
  • Added defensive DROP TABLE IF EXISTS cf_agents_schedules_new to handle any leftover temp table
  • Added execution_started_at and retry_options to the CREATE TABLE IF NOT EXISTS so new DOs get the full schema upfront

Tests (packages/agents/src/tests/migration.test.ts)
8 test cases covering:

  1. Old CHECK constraint rejects type='interval' (reproduces the bug)
  2. Migration updates constraint to allow 'interval'
  3. Migration preserves existing row data
  4. Migration updates DDL in sqlite_master
  5. Intermediate state — columns exist but old CHECK (most common real-world path)
  6. Leftover _new table from partial migration
  7. All schedule types (scheduled, delayed, cron) survive migration
  8. Idempotency — safe to run twice

Add migration logic to fix the cf_agents_schedules CHECK constraint to include the 'interval' type by recreating the table when the old constraint is detected. Also add new columns (execution_started_at, retry_options) and ensure data is preserved, handle leftover cf_agents_schedules_new cleanup, and make the migration idempotent. Introduce TestMigrationAgent and comprehensive Vitest tests to simulate old, intermediate, and partial-migration states and verify behavior; wire the agent into test exports, worker env, and wrangler config. Minor non-functional CHANGELOG tweak and package-lock updates.
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 990bde8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 4, 2026

Open in StackBlitz

npm i https://pkg.pr.new/agents@1063
npm i https://pkg.pr.new/@cloudflare/ai-chat@1063
npm i https://pkg.pr.new/@cloudflare/codemode@1063
npm i https://pkg.pr.new/hono-agents@1063

commit: 326d0ef

@threepointone threepointone merged commit 4ace1d4 into main Mar 4, 2026
3 checks passed
@threepointone threepointone deleted the fix-1061 branch March 4, 2026 20:50
@github-actions github-actions bot mentioned this pull request Mar 4, 2026
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.

SQL query failed: CHECK constraint failed: type IN ('scheduled', 'delayed', 'cron'): SQLITE_CONSTRAINT

1 participant