Database backends and official sync

How to choose between SQLite, MySQL, and three-database mode, what to back up before switching, the migration order, and how official Skills and scenes are synchronized.

1. Determine which deployment you have

Topology Where data lives Suited to Switch risk
Single-machine SQLite Local data/sessions.db Trials, single machine, small teams Low; this is the default
Single MySQL One MySQL database Multiple instances, centralized backup Medium; both connections and migrations change
Three-database mode Three databases: mgmt / runtime / insight Production four-process deployment, cross-domain isolation High; you must migrate in order and fix views

Do not switch to MySQL only to appear more production-ready. Single-machine SQLite also works when the filesystem is reliable and backups are in place. The real signals for switching are:

  • Multiple service instances need to share data
  • Database-level backup, auditing, or high availability is required
  • Writes on single-machine SQLite have become the bottleneck

2. One thing to know before switching databases

Changing backend only changes the connection; it does not move data from the old database.

  • Switching from SQLite to an empty MySQL: the new database initializes with the new schema, and old data does not appear automatically
  • To keep old data: export and import offline first, then verify row counts and key objects in a test environment
  • The repository ships an export script for MySQL → SQLite; SQLite → MySQL requires your own migration tool or a manual migration during a maintenance window

The correct order is therefore:

Back up the old database
  → Prepare the new database
    → Migrate and verify in a test environment
      → Stop writes in production
        → Switch the connection
          → Start and migrate the schema
            → Verify the data
              → Keep the old database for a while

3. Single-database switch (SQLite / MySQL)

Step 1: Back up

Back up at least:

  • data/ (SQLite database, logs)
  • configs/db-backend.json
  • The environment file that actually takes effect (/etc/grape/grape.env or .env)

For backup commands, see Upgrade, backup, and rollback.

Step 2: Prepare MySQL

  1. Create the database and a dedicated account
  2. Grant only the permissions that database needs
  3. Test the connection from the application server
  4. Confirm that the character set and time settings match the existing data

Step 3: Change the configuration

{
  "backend": "mysql",
  "mysql": {
    "host": "<mysql-host>",
    "port": 3306,
    "user": "<mysql-user>",
    "password": "<mysql-password>",
    "database": "<grape-database>"
  }
}

Precedence:

Environment variable DEVOPS_DB_BACKEND
  > backend in configs/db-backend.json
    > default sqlite

Environment variables take precedence. During troubleshooting, first confirm that no leftover DEVOPS_DB_BACKEND=sqlite remains.

Step 4: Start and migrate the schema

# Restart the application; the first start runs schema initialization/migration
sudo systemctl restart grape-server

# Or Docker
cd deploy
docker compose --env-file ../.env up -d
docker compose --env-file ../.env ps

Check the startup log to confirm:

  • The backend in use is mysql
  • The connection pool initialized successfully
  • There are no connection refused, permission, or character set errors

Step 5: Verify the business flows

Do not rely only on the process being alive:

  1. Sign in to the console
  2. Open the repository list
  3. Open a historical Issue/Pipeline
  4. Create a test Issue and confirm that it can be written
  5. Check that tasks can be enqueued normally

Step 6: Roll back

  1. Stop the service
  2. Change backend back to sqlite
  3. Restore the backed-up data/
  4. Start and verify

Keep the old MySQL database for now; do not delete it immediately. Clean it up only after confirming that rollback no longer needs it.

4. Three-database mode

Three-database mode splits data into:

Database Contents Migration owner
grape_mgmt_db Accounts, repositories, scenes, Skills, credentials mgmt
grape_runtime_db Tasks, events, sessions, Workers event
grape_insight_db Logs, metrics, SLOs, dashboards insight

The entry point is the DSN file referenced by GRAPE_3DB_ENV, which provides:

GRAPE_DSN_MGMT
GRAPE_DSN_RUNTIME
GRAPE_DSN_INSIGHT

The launcher selects the DSN for its own domain according to the process role and sets DEVOPS_DB_NAME.

The upgrade order cannot be reversed

Migrate
  → Fix views
    → Re-check
      → Restart

Run:

bash scripts/deploy-after-migrate.sh

Exit code meanings:

Exit code Meaning Can you restart
0 Migration, views, and re-check all passed Yes
2 Unregistered duplicate base tables with the same name exist and the owner is undefined No
3 Database unavailable, the database family cannot be determined, or CREATE VIEW permission is missing No
Any other non-zero Migration or views still have problems No

When the script returns non-zero, do not "restart first and see". After a restart the application runs with old views and a new schema, which easily produces missing columns, 1146, and failed cross-domain reads.

5. Official Skill / scene sync

This is separate from the database switch: it determines whether the platform can fetch official Skills and scene templates.

Configuration

Copy the example:

cp configs/scene-sync.example.json configs/scene-sync.json
{
  "repo_url": "https://github.com/grape-dev/grape-skills",
  "repo_branch": "main",
  "gitcode_api_token": ""
}

Environment variables override the file:

GRAPESKILLS_REPO_URL
GRAPESKILLS_REPO_BRANCH
GRAPE_DEV_TOKEN
SKILL_SYNC_INTERVAL

Notes:

  • SKILL_SYNC_INTERVAL defaults to 1800 seconds (30 minutes)
  • SCENE_SYNC_INTERVAL is retired; if set, it only raises a warning and has no effect
  • gitcode_api_token / GRAPE_DEV_TOKEN are needed only for private repositories

Verification

  1. Check that the startup log contains scene-sync loading information
  2. Wait for one sync cycle, or temporarily lower SKILL_SYNC_INTERVAL for the test
  3. In the console, confirm that the newly added official entries appear in the Skill/scene lists
  4. Confirm that there are no fetch failures, authentication failures, or missing branches

After verification, restore the sync interval to its normal value; do not keep a very short interval long-term.

6. Common failures

Symptom Likely cause Handling
The startup log still shows sqlite An environment variable overrode the file Check DEVOPS_DB_BACKEND
MySQL connection refused Wrong host/port/account/permissions Test with the mysql client from the application machine
Old data is missing after startup Only the connection was switched; data was not migrated Re-plan around "switching is not migrating"
Cross-domain pages show missing columns or 1146 Views were not fixed after migration Run deploy-after-migrate.sh; do not restart directly
Skills/scenes never update Sync disabled, wrong branch, or expired token Check the sync log and GRAPE_DEV_TOKEN
Changing the sync interval has no effect The retired SCENE_SYNC_INTERVAL was changed Use SKILL_SYNC_INTERVAL

7. Completion criteria

  • [ ] The old database, configuration, and environment file are backed up
  • [ ] The startup log shows the expected backend and no connection errors
  • [ ] Historical data can be read and new data can be written
  • [ ] Three-database mode follows migrate → fix views → re-check → restart
  • [ ] Official Skills/scenes sync, and failures are logged
  • [ ] The rollback path is known and the old database has not been deleted

Architecture and deployment topologies | Upgrade, backup, and rollback | Observability stack