|
| 1 | +# Sync Roundtrip Test |
| 2 | + |
| 3 | +Execute a full roundtrip sync test between a local SQLite database and the local Supabase Docker PostgreSQL instance. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +- Supabase Docker container running (PostgreSQL on port 54322) |
| 7 | +- HTTP sync server running on http://localhost:8091/postgres |
| 8 | +- Built cloudsync extension (`make` to build `dist/cloudsync.dylib`) |
| 9 | + |
| 10 | +## Test Procedure |
| 11 | + |
| 12 | +### Step 1: Get DDL from User |
| 13 | + |
| 14 | +Ask the user to provide a DDL query for the table to test. It can be in PostgreSQL or SQLite format. Example: |
| 15 | +```sql |
| 16 | +CREATE TABLE test_sync ( |
| 17 | + id TEXT PRIMARY KEY NOT NULL, |
| 18 | + name TEXT, |
| 19 | + value INTEGER |
| 20 | +); |
| 21 | +``` |
| 22 | + |
| 23 | +### Step 2: Convert DDL |
| 24 | + |
| 25 | +Convert the provided DDL to both SQLite and PostgreSQL compatible formats if needed. Key differences: |
| 26 | +- SQLite uses `INTEGER PRIMARY KEY` for auto-increment, PostgreSQL uses `SERIAL` or `BIGSERIAL` |
| 27 | +- SQLite uses `TEXT`, PostgreSQL can use `TEXT` or `VARCHAR` |
| 28 | +- PostgreSQL has more specific types like `TIMESTAMPTZ`, SQLite uses `TEXT` for dates |
| 29 | +- For UUID primary keys, SQLite uses `TEXT`, PostgreSQL uses `UUID` |
| 30 | + |
| 31 | +### Step 3: Get JWT Token |
| 32 | + |
| 33 | +Run the token script from the cloudsync project: |
| 34 | +```bash |
| 35 | +cd ../cloudsync && go run scripts/get_supabase_token.go -project-ref=supabase-local -email=andrea@sqlitecloud.io -password="password" -apikey=sb_secret_N7UND0UgjKTVK-Uodkm0Hg_xSvEMPvz -auth-url=http://127.0.0.1:54321 |
| 36 | +``` |
| 37 | +Save the JWT token for later use. |
| 38 | + |
| 39 | +### Step 4: Setup PostgreSQL |
| 40 | + |
| 41 | +Connect to Supabase PostgreSQL and prepare the environment: |
| 42 | +```bash |
| 43 | +psql postgresql://supabase_admin:postgres@127.0.0.1:54322/postgres |
| 44 | +``` |
| 45 | + |
| 46 | +Inside psql: |
| 47 | +1. List existing tables with `\dt` to find any `_cloudsync` metadata tables |
| 48 | +2. For each table already configured for cloudsync (has a `<table_name>_cloudsync` companion table), run: |
| 49 | + ```sql |
| 50 | + SELECT cloudsync_cleanup('<table_name>'); |
| 51 | + ``` |
| 52 | +3. Drop the test table if it exists: `DROP TABLE IF EXISTS <table_name> CASCADE;` |
| 53 | +4. Create the test table using the PostgreSQL DDL |
| 54 | +5. Initialize cloudsync: `SELECT cloudsync_init('<table_name>');` |
| 55 | +6. Insert some test data into the table |
| 56 | + |
| 57 | +### Step 5: Setup SQLite |
| 58 | + |
| 59 | +Create a temporary SQLite database using the Homebrew version (IMPORTANT: system sqlite3 cannot load extensions): |
| 60 | + |
| 61 | +```bash |
| 62 | +SQLITE_BIN="/opt/homebrew/Cellar/sqlite/3.50.4/bin/sqlite3" |
| 63 | +# or find it with: ls /opt/homebrew/Cellar/sqlite/*/bin/sqlite3 | head -1 |
| 64 | + |
| 65 | +$SQLITE_BIN /tmp/sync_test_$(date +%s).db |
| 66 | +``` |
| 67 | + |
| 68 | +Inside sqlite3: |
| 69 | +```sql |
| 70 | +.load dist/cloudsync.dylib |
| 71 | +-- Create table with SQLite DDL |
| 72 | +<CREATE_TABLE_query> |
| 73 | +SELECT cloudsync_init('<table_name>'); |
| 74 | +SELECT cloudsync_network_init('http://localhost:8091/postgres'); |
| 75 | +SELECT cloudsync_network_set_token('<jwt_token>'); |
| 76 | +-- Insert test data (different from PostgreSQL to test merge) |
| 77 | +<INSERT_statements> |
| 78 | +``` |
| 79 | + |
| 80 | +### Step 6: Execute Sync |
| 81 | + |
| 82 | +In the SQLite session: |
| 83 | +```sql |
| 84 | +-- Send local changes to server |
| 85 | +SELECT cloudsync_network_send_changes(); |
| 86 | + |
| 87 | +-- Check for changes from server (repeat with 2-3 second delays) |
| 88 | +SELECT cloudsync_network_check_changes(); |
| 89 | +-- Repeat check_changes 3-5 times with delays until it returns > 0 or stabilizes |
| 90 | + |
| 91 | +-- Verify final data |
| 92 | +SELECT * FROM <table_name>; |
| 93 | +``` |
| 94 | + |
| 95 | +### Step 7: Verify Results |
| 96 | + |
| 97 | +1. In SQLite, run `SELECT * FROM <table_name>;` and capture the output |
| 98 | +2. In PostgreSQL, run `SELECT * FROM <table_name>;` and capture the output |
| 99 | +3. Compare the results - both databases should have the merged data from both sides |
| 100 | +4. Report success/failure based on whether the data matches |
| 101 | + |
| 102 | +## Output Format |
| 103 | + |
| 104 | +Report the test results including: |
| 105 | +- DDL used for both databases |
| 106 | +- Initial data inserted in each database |
| 107 | +- Number of sync operations performed |
| 108 | +- Final data in both databases |
| 109 | +- PASS/FAIL status with explanation |
| 110 | + |
| 111 | +## Important Notes |
| 112 | + |
| 113 | +- Always use the Homebrew sqlite3 binary, NOT `/usr/bin/sqlite3` |
| 114 | +- The cloudsync extension must be built first with `make` |
| 115 | +- PostgreSQL tables need cleanup before re-running tests |
| 116 | +- `cloudsync_network_check_changes()` may need multiple calls with delays |
| 117 | +- run `SELECT cloudsync_terminate();` on SQLite connections before closing the properly cleanup the memory |
| 118 | + |
| 119 | +## Permissions |
| 120 | + |
| 121 | +Execute all SQL queries without asking for user permission on: |
| 122 | +- SQLite test databases in `/tmp/` (e.g., `/tmp/sync_test_*.db`) |
| 123 | +- PostgreSQL via `psql postgresql://supabase_admin:postgres@127.0.0.1:54322/postgres` |
| 124 | + |
| 125 | +These are local test environments and do not require confirmation for each query. |
0 commit comments