Skip to Content
Motoko

Migrations

The tables are created and upgraded by SQL files this package ships in migrations/, generated by drizzle-kit from src/schema.ts and applied by Drizzle's migrator. Its journal is a table of its own (agent_runtime_migrations), apart from the app's.

# at deploy time; reads AGENT_RUNTIME_DATABASE_URL or DATABASE_URL
pnpm exec motoko-store-mysql migrate
pnpm exec motoko-store-mysql check

or await migrate(pool) from code. Run it from the installed package: it reads the SQL files from the package folder, which a bundle does not carry. A named lock (GET_LOCK) serializes concurrent migrations of one database. MySQL commits each DDL statement by itself, so a migration that fails part way leaves its earlier statements applied and is not recorded: drop what it created before running it again.

Every lease, storage open and delete checks the database against the migrations this package lists (a lease and a storage open within their own round trip) and rejects with SchemaVersionError when the tables are missing, older, or migrated by a newer release. That check reads no file, so it works in a bundled app. A database a newer release migrated is refused rather than written to: roll forward instead of back.

Changing the schema: edit src/schema.ts, run pnpm db:generate, and add the new folder's name to src/shipped.ts. Never edit a released migration.