Design or review schemas for `crates/cloudsync` using SQLite Sync constraints, not generic SQLite advice. Use when adding synced tables, changing synced columns, or planning CloudSync-safe migrations.
Design tables that behave correctly under SQLite Sync's CRDT replication model.
This skill is specifically for CloudSync-backed schemas:
cloudsync_init(...)cloudsync_enable(...)cloudsync_uuid()cloudsync_begin_alter(...) and cloudsync_commit_alter(...)Do not treat this as ordinary SQLite schema design. SQLite Sync imposes extra rules around keys, defaults, foreign keys, and schema evolution.
Before proposing DDL, classify the table:
Only apply this skill to synced tables or to tables that may become synced soon.
For synced tables:
TEXT PRIMARY KEY NOT NULLcloudsync_uuid()SQLite Sync docs explicitly recommend UUIDv7-style globally unique ids for CRDT workloads. Integer autoincrement ids are a bad fit because multiple devices can create rows independently.
CREATE TABLE document (
id TEXT PRIMARY KEY NOT NULL DEFAULT (cloudsync_uuid()),
workspace_id TEXT NOT NULL,
title TEXT NOT NULL DEFAULT '',
body TEXT NOT NULL DEFAULT '',
archived INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
) STRICT;
If the environment cannot use a function call in DEFAULT, generate the id in application code, but still use cloudsync_uuid() as the canonical id strategy.
SQLite Sync best practices call out a non-obvious CRDT constraint: merges can happen column-by-column, so missing values are much more dangerous than in a single-node SQLite app.
For synced tables:
NOT NULL column should have a meaningful DEFAULTGood:
title TEXT NOT NULL DEFAULT ''
archived INTEGER NOT NULL DEFAULT 0
sort_order INTEGER NOT NULL DEFAULT 0
Bad:
title TEXT NOT NULL
archived INTEGER NOT NULL
without defaults on a synced table.
The getting-started docs require the local synced database and the SQLite Cloud database to share the same schema.
When designing or reviewing a schema:
If a field is only needed locally, it likely belongs in a separate non-synced table.
SQLite Sync best practices explicitly warn that foreign keys can interact poorly with CRDT replication.
Use foreign keys on synced tables only when the integrity guarantee is worth the operational cost.
If you keep them:
DEFAULT, that default must be NULL or reference an actually valid parent row'root' unless that parent row is guaranteed to exist everywherePrefer ownership patterns that tolerate out-of-order arrival between related rows.
SQLite Sync best practices advise minimizing triggers because they make replicated writes harder to reason about.
For synced tables:
If a trigger is unavoidable, review it as part of the replication design, not as a local SQLite convenience.
The introduction docs emphasize row-level security and multi-tenant access patterns.
That changes uniqueness design:
Prefer:
UNIQUE (workspace_id, slug)
Spring Boot中的JPA/Hibernate模式,用于实体设计、关系处理、查询优化、事务管理、审计、索引、分页和连接池。