Skip to content

Commit 645917b

Browse files
committed
Update "Conversion Between SQLite and PostgreSQL Tables" in docs/postgresql/CLIENT.md
1 parent c809772 commit 645917b

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

docs/postgresql/CLIENT.md

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,114 @@ Under the hood, SQLite Sync uses advanced **CRDT (Conflict-free Replicated Data
3333

3434
## Conversion Between SQLite and PostgreSQL Tables
3535

36-
- In this version, make sure to **manually create** the same tables in the PostgreSQL database as used in the SQLite client.
37-
- Follow the Database Schema Recommendations:
38-
https://github.com/sqliteai/sqlite-sync-dev?tab=readme-ov-file#database-schema-recommendations
36+
In this version, make sure to **manually create** the same tables in the PostgreSQL database as used in the SQLite client.
37+
38+
This guide shows how to manually convert a SQLite table definition to PostgreSQL
39+
so CloudSync can sync between a PostgreSQL server and SQLite clients.
40+
41+
### 1) Primary Keys
42+
43+
- Use **TEXT NOT NULL** primary keys only (UUIDs as text).
44+
- Generate IDs with `cloudsync_uuid()` on both sides.
45+
- Avoid INTEGER auto-increment PKs.
46+
47+
SQLite:
48+
```sql
49+
id TEXT PRIMARY KEY NOT NULL
50+
```
51+
52+
PostgreSQL:
53+
```sql
54+
id TEXT PRIMARY KEY NOT NULL
55+
```
56+
57+
### 2) NOT NULL Columns Must Have DEFAULTs
58+
59+
CloudSync merges column-by-column. Any NOT NULL (non-PK) column needs a DEFAULT
60+
to avoid constraint failures during merges.
61+
62+
Example:
63+
```sql
64+
title TEXT NOT NULL DEFAULT ''
65+
count INTEGER NOT NULL DEFAULT 0
66+
```
67+
68+
### 3) Safe Type Mapping
69+
70+
Use types that map cleanly to CloudSync's DBTYPEs:
71+
72+
- INTEGER → `INTEGER` (SQLite) / `INTEGER` (Postgres)
73+
- FLOAT → `REAL` / `DOUBLE` (SQLite) / `DOUBLE PRECISION` (Postgres)
74+
- TEXT → `TEXT` (both)
75+
- BLOB → `BLOB` (SQLite) / `BYTEA` (Postgres)
76+
77+
Avoid: JSON/JSONB, UUID, INET, CIDR, RANGE, ARRAY unless you accept text-cast
78+
behavior.
79+
80+
### 4) Defaults That Match Semantics
81+
82+
Use defaults that serialize the same on both sides:
83+
84+
- TEXT: `DEFAULT ''`
85+
- INTEGER: `DEFAULT 0`
86+
- FLOAT: `DEFAULT 0.0`
87+
- BLOB: `DEFAULT X'00'` (SQLite) vs `DEFAULT E'\\x00'` (Postgres)
88+
89+
### 5) Foreign Keys and Triggers
90+
91+
- Foreign keys can cause merge conflicts; test carefully.
92+
- Application triggers will fire during merge; keep them idempotent or disable
93+
in synced tables.
94+
95+
### 6) Example Conversion
96+
97+
SQLite:
98+
```sql
99+
CREATE TABLE notes (
100+
id TEXT PRIMARY KEY NOT NULL,
101+
title TEXT NOT NULL DEFAULT '',
102+
body TEXT DEFAULT '',
103+
views INTEGER NOT NULL DEFAULT 0,
104+
rating REAL DEFAULT 0.0,
105+
data BLOB
106+
);
107+
```
108+
109+
PostgreSQL:
110+
```sql
111+
CREATE TABLE notes (
112+
id TEXT PRIMARY KEY NOT NULL,
113+
title TEXT NOT NULL DEFAULT '',
114+
body TEXT DEFAULT '',
115+
views INTEGER NOT NULL DEFAULT 0,
116+
rating DOUBLE PRECISION DEFAULT 0.0,
117+
data BYTEA
118+
);
119+
```
120+
121+
### 7) Enable CloudSync
122+
123+
SQLite:
124+
```sql
125+
.load dist/cloudsync.dylib
126+
SELECT cloudsync_init('notes');
127+
```
128+
129+
PostgreSQL:
130+
```sql
131+
CREATE EXTENSION cloudsync;
132+
SELECT cloudsync_init('notes');
133+
```
134+
135+
### Checklist
136+
137+
- [ ] PKs are TEXT + NOT NULL
138+
- [ ] All NOT NULL columns have DEFAULT
139+
- [ ] Only INTEGER/FLOAT/TEXT/BLOB-compatible types
140+
- [ ] Same column names and order
141+
- [ ] Same defaults (semantic match)
142+
143+
Database Schema Recommendations: https://github.com/sqliteai/sqlite-sync-dev?tab=readme-ov-file#database-schema-recommendations
39144

40145
---
41146

0 commit comments

Comments
 (0)