본문으로 건너뛰기
Skillpedia
데이터베이스9.7k · #12214

sqlite-schema-design

하는 일

이 가이드는 여러 기기에서 SQLite 데이터베이스를 같은 상태로 유지하는 시스템인 SQLite Sync용 테이블 설계를 다룹니다. 일반적인 데이터베이스 조언이 아니라 동기화 규칙이 필요한 앱을 만드는 개발자를 위한 가이드입니다.

사용하는 경우

  • ✓기기 간에 동기화되어야 하는 새 테이블을 추가할 때.
  • ✓이미 동기화되는 테이블의 열을 바꿀 때.
  • ✓SQLite Sync와 함께 작동해야 하는 데이터베이스 마이그레이션을 계획하는 경우.

스킬 링크

https://github.com/fastrepl/anarlog/blob/main/.agents/skills/sqlite-schema-design/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/fastrepl/anarlog/blob/main/.agents/skills/sqlite-schema-design/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“SQLite Sync로 사용자 기기 간에 동기화되는 메모 테이블을 설계해 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

스킬은 먼저 테이블이 동기화되어야 하는지 확인한 뒤 동기화 규칙을 적용합니다. 동기화 UUID 도우미로 만든 안정적인 텍스트 기본 키, 자동 증가 정수 ID 사용 금지, 동기화의 변경 단계를 통한 스키마 변경이 포함됩니다. 로컬과 클라우드 데이터베이스는 같은 스키마를 유지해야 합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Goal

Design tables that behave correctly under SQLite Sync's CRDT replication model.

This skill is specifically for CloudSync-backed schemas:

  • tables are initialized through cloudsync_init(...)
  • sync is enabled with cloudsync_enable(...)
  • identifiers should be generated with cloudsync_uuid()
  • schema changes must go through cloudsync_begin_alter(...) and cloudsync_commit_alter(...)
  • local and cloud databases must keep the same schema

Do not treat this as ordinary SQLite schema design. SQLite Sync imposes extra rules around keys, defaults, foreign keys, and schema evolution.

Workflow

1. Decide Whether The Table Is Synced

Before proposing DDL, classify the table:

  • synced application data: must satisfy SQLite Sync constraints
  • local-only cache or ephemeral state: should usually stay out of CloudSync

Only apply this skill to synced tables or to tables that may become synced soon.

2. Require A Stable, Globally Unique Primary Key

For synced tables:

  • always declare an explicit primary key
  • prefer TEXT PRIMARY KEY NOT NULL
  • generate ids with cloudsync_uuid()
  • do not use auto-incrementing integer ids

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.

3. Make Inserts Merge-Safe With Real Defaults

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:

  • every non-primary-key NOT NULL column should have a meaningful DEFAULT
  • avoid required columns that only application code knows how to populate
  • prefer simple scalar defaults over nullable columns when the field is logically always present

Good:

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.

4. Keep The Local And Cloud Schemas Identical

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:

  • treat local and remote DDL as one contract
  • do not introduce "client-only" columns on synced tables
  • do not rely on drift being harmless
  • ensure migrations are applied consistently before sync resumes

If a field is only needed locally, it likely belongs in a separate non-synced table.

5. Be Conservative With Foreign Keys

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:

  • make child foreign key columns nullable when the relationship is optional
  • if a foreign key column has a DEFAULT, that default must be NULL or reference an actually valid parent row
  • avoid fake sentinel ids such as 'root' unless that parent row is guaranteed to exist everywhere
  • index the child foreign key columns

Prefer ownership patterns that tolerate out-of-order arrival between related rows.

6. Avoid Triggers And Implicit Write Logic On Synced Tables

SQLite Sync best practices advise minimizing triggers because they make replicated writes harder to reason about.

For synced tables:

  • avoid triggers that mutate synced columns
  • avoid hidden side effects on insert or update
  • prefer explicit application writes
  • keep derived or bookkeeping writes in non-synced tables if possible

If a trigger is unavoidable, review it as part of the replication design, not as a local SQLite convenience.

7. Scope Uniqueness For RLS And Multi-Tenant Sync

The introduction docs emphasize row-level security and multi-tenant access patterns.

That changes uniqueness design:

  • if the real rule is "unique per workspace/user/team", enc
자주 묻는 질문

자동 증가 ID를 쓰지 않는 이유는 무엇인가요?

여러 기기가 동시에 행을 만들 수 있어서 정수 카운터가 충돌하기 때문입니다. 가이드는 전역적으로 고유한 ID를 쓰라고 합니다.

모든 테이블에 적용되나요?

아닙니다. 동기화되는 테이블만 이 규칙이 필요하며, 로컬 전용 캐시 테이블은 보통 동기화에서 제외합니다.

동기화되는 테이블은 어떻게 바꾸나요?

begin과 commit 변경 단계를 거치므로, 모든 곳에서 스키마가 같게 유지됩니다.

상세 정보

작성자의 원본 설명

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.

repo: fastrepl/anarlog

인기도

GitHub 스타
9.7k
순위
#12214
라이선스
MIT
업데이트
2026. 11. 1.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬