ddcp 0.2.4

Distributed decentralized database-to-database copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "tableinfo.h"

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "consts.h"
#include "crsqlite.h"
#include "get-table.h"
#include "util.h"

void crsql_freeColumnInfoContents(crsql_ColumnInfo *columnInfo) {
  sqlite3_free(columnInfo->name);
  sqlite3_free(columnInfo->type);
}

static void crsql_freeColumnInfos(crsql_ColumnInfo *columnInfos, int len) {
  if (columnInfos == 0) {
    return;
  }

  int i = 0;
  for (i = 0; i < len; ++i) {
    crsql_freeColumnInfoContents(&columnInfos[i]);
  }

  sqlite3_free(columnInfos);
}

int crsql_numPks(crsql_ColumnInfo *colInfos, int colInfosLen) {
  int ret = 0;
  int i = 0;

  for (i = 0; i < colInfosLen; ++i) {
    if (colInfos[i].pk > 0) {
      ++ret;
    }
  }

  return ret;
}

static int cmpPks(const void *a, const void *b) {
  return (((crsql_ColumnInfo *)a)->pk - ((crsql_ColumnInfo *)b)->pk);
}

crsql_ColumnInfo *crsql_pks(crsql_ColumnInfo *colInfos, int colInfosLen,
                            int *pPksLen) {
  int numPks = crsql_numPks(colInfos, colInfosLen);
  crsql_ColumnInfo *ret = 0;
  int i = 0;
  int j = 0;
  *pPksLen = numPks;

  if (numPks == 0) {
    return 0;
  }

  ret = sqlite3_malloc(numPks * sizeof *ret);
  for (i = 0; i < colInfosLen; ++i) {
    if (colInfos[i].pk > 0) {
      assert(j < numPks);
      ret[j] = colInfos[i];
      ++j;
    }
  }

  qsort(ret, numPks, sizeof(crsql_ColumnInfo), cmpPks);

  assert(j == numPks);
  return ret;
}

crsql_ColumnInfo *crsql_nonPks(crsql_ColumnInfo *colInfos, int colInfosLen,
                               int *pNonPksLen) {
  int nonPksLen = colInfosLen - crsql_numPks(colInfos, colInfosLen);
  crsql_ColumnInfo *ret = 0;
  int i = 0;
  int j = 0;
  *pNonPksLen = nonPksLen;

  if (nonPksLen == 0) {
    return 0;
  }

  ret = sqlite3_malloc(nonPksLen * sizeof *ret);
  for (i = 0; i < colInfosLen; ++i) {
    if (colInfos[i].pk == 0) {
      assert(j < nonPksLen);
      ret[j] = colInfos[i];
      ++j;
    }
  }

  assert(j == nonPksLen);
  return ret;
}

/**
 * Constructs a table info based on the results of pragma
 * statements against the base table.
 */
static crsql_TableInfo *crsql_tableInfo(const char *tblName,
                                        crsql_ColumnInfo *colInfos,
                                        int colInfosLen) {
  crsql_TableInfo *ret = sqlite3_malloc(sizeof *ret);

  ret->baseCols = colInfos;
  ret->baseColsLen = colInfosLen;

  ret->tblName = crsql_strdup(tblName);

  ret->nonPks =
      crsql_nonPks(ret->baseCols, ret->baseColsLen, &(ret->nonPksLen));
  ret->pks = crsql_pks(ret->baseCols, ret->baseColsLen, &(ret->pksLen));

  return ret;
}

/**
 * Given a table name, return the table info that describes that table.
 * TableInfo is a struct that represents the results
 * of pragma_table_info, pragma_index_list, pragma_index_info on a given table
 * and its inidces as well as some extra fields to facilitate crr creation.
 */
int crsql_getTableInfo(sqlite3 *db, const char *tblName,
                       crsql_TableInfo **pTableInfo, char **pErrMsg) {
  char *zSql = 0;
  int rc = SQLITE_OK;
  sqlite3_stmt *pStmt = 0;
  int numColInfos = 0;
  int i = 0;
  crsql_ColumnInfo *columnInfos = 0;

  zSql =
      sqlite3_mprintf("select count(*) from pragma_table_info('%s')", tblName);
  numColInfos = crsql_getCount(db, zSql);
  sqlite3_free(zSql);

  if (numColInfos < 0) {
    *pErrMsg = sqlite3_mprintf("Failed to find columns for crr -- %s", tblName);
    return numColInfos;
  }

  zSql = sqlite3_mprintf(
      "select \"cid\", \"name\", \"type\", \"notnull\", \"pk\" from "
      "pragma_table_info('%s') order by cid asc",
      tblName);
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);

  if (rc != SQLITE_OK) {
    *pErrMsg =
        sqlite3_mprintf("Failed to prepare select for crr -- %s", tblName);
    sqlite3_finalize(pStmt);
    return rc;
  }

  rc = sqlite3_step(pStmt);
  if (rc != SQLITE_ROW) {
    *pErrMsg = sqlite3_mprintf("Failed to parse crr definition -- %s", tblName);
    sqlite3_finalize(pStmt);
    return rc;
  }
  columnInfos = sqlite3_malloc(numColInfos * sizeof *columnInfos);
  while (rc == SQLITE_ROW) {
    if (i >= numColInfos) {
      sqlite3_finalize(pStmt);
      for (int j = 0; j < i; ++j) {
        crsql_freeColumnInfoContents(&columnInfos[j]);
      }
      sqlite3_free(columnInfos);
      return SQLITE_ERROR;
    }

    columnInfos[i].cid = sqlite3_column_int(pStmt, 0);

    columnInfos[i].name =
        crsql_strdup((const char *)sqlite3_column_text(pStmt, 1));
    columnInfos[i].type =
        crsql_strdup((const char *)sqlite3_column_text(pStmt, 2));

    columnInfos[i].notnull = sqlite3_column_int(pStmt, 3);
    columnInfos[i].pk = sqlite3_column_int(pStmt, 4);

    ++i;
    rc = sqlite3_step(pStmt);
  }
  sqlite3_finalize(pStmt);

  if (i < numColInfos) {
    for (int j = 0; j < i; ++j) {
      crsql_freeColumnInfoContents(&columnInfos[j]);
    }
    sqlite3_free(columnInfos);
    *pErrMsg = sqlite3_mprintf(
        "Number of fetched columns did not match expected number of "
        "columns");
    return SQLITE_ERROR;
  }

  *pTableInfo = crsql_tableInfo(tblName, columnInfos, numColInfos);

  return SQLITE_OK;
}

void crsql_freeTableInfo(crsql_TableInfo *tableInfo) {
  if (tableInfo == 0) {
    return;
  }
  // baseCols is a superset of all other col arrays
  // and will free their contents.
  crsql_freeColumnInfos(tableInfo->baseCols, tableInfo->baseColsLen);

  // the arrays themselves of course still need freeing
  sqlite3_free(tableInfo->tblName);
  sqlite3_free(tableInfo->pks);
  sqlite3_free(tableInfo->nonPks);

  sqlite3_free(tableInfo);
}

void crsql_freeAllTableInfos(crsql_TableInfo **tableInfos, int len) {
  for (int i = 0; i < len; ++i) {
    crsql_freeTableInfo(tableInfos[i]);
  }
  sqlite3_free(tableInfos);
}

crsql_TableInfo *crsql_findTableInfo(crsql_TableInfo **tblInfos, int len,
                                     const char *tblName) {
  for (int i = 0; i < len; ++i) {
    if (strcmp(tblInfos[i]->tblName, tblName) == 0) {
      return tblInfos[i];
    }
  }

  return 0;
}

int crsql_indexofTableInfo(crsql_TableInfo **tblInfos, int len,
                           const char *tblName) {
  for (int i = 0; i < len; ++i) {
    if (strcmp(tblInfos[i]->tblName, tblName) == 0) {
      return i;
    }
  }

  return -1;
}

sqlite3_int64 crsql_slabRowid(int idx, sqlite3_int64 rowid) {
  if (idx < 0) {
    return -1;
  }

  sqlite3_int64 modulo = rowid % ROWID_SLAB_SIZE;
  return idx * ROWID_SLAB_SIZE + modulo;
}

/**
 * Pulls all table infos for all crrs present in the database.
 * Run once at vtab initialization -- see docs on crsql_Changes_vtab
 * for the constraints this creates.
 */
int crsql_pullAllTableInfos(sqlite3 *db, crsql_TableInfo ***pzpTableInfos,
                            int *rTableInfosLen, char **errmsg) {
  char **zzClockTableNames = 0;
  int rNumCols = 0;
  int rNumRows = 0;
  int rc = SQLITE_OK;

  // Find all clock tables
  rc = crsql_get_table(db, CLOCK_TABLES_SELECT, &zzClockTableNames, &rNumRows,
                       &rNumCols, 0);

  if (rc != SQLITE_OK) {
    *errmsg = sqlite3_mprintf("crsql internal error discovering crr tables.");
    crsql_free_table(zzClockTableNames);
    return SQLITE_ERROR;
  }

  if (rNumRows == 0) {
    crsql_free_table(zzClockTableNames);
    return SQLITE_OK;
  }

  crsql_TableInfo **tableInfos =
      sqlite3_malloc(rNumRows * sizeof(crsql_TableInfo *));
  memset(tableInfos, 0, rNumRows * sizeof(crsql_TableInfo *));
  for (int i = 0; i < rNumRows; ++i) {
    // +1 since tableNames includes a row for column headers
    // Strip __crsql_clock suffix.
    char *baseTableName =
        crsql_strndup(zzClockTableNames[i + 1],
                      strlen(zzClockTableNames[i + 1]) - __CRSQL_CLOCK_LEN);
    rc = crsql_getTableInfo(db, baseTableName, &tableInfos[i], errmsg);
    sqlite3_free(baseTableName);

    if (rc != SQLITE_OK) {
      crsql_free_table(zzClockTableNames);
      crsql_freeAllTableInfos(tableInfos, rNumRows);
      return rc;
    }
  }

  crsql_free_table(zzClockTableNames);

  *pzpTableInfos = tableInfos;
  *rTableInfosLen = rNumRows;

  return SQLITE_OK;
}

int crsql_isTableCompatible(sqlite3 *db, const char *tblName, char **errmsg) {
  // No unique indices besides primary key
  sqlite3_stmt *pStmt = 0;
  char *zSql = sqlite3_mprintf(
      "SELECT count(*) FROM pragma_index_list('%s') WHERE \"origin\" != 'pk' "
      "AND \"unique\" = 1",
      tblName);
  int rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);

  if (rc != SQLITE_OK) {
    *errmsg =
        sqlite3_mprintf("Failed to analyze index information for %s", tblName);
    return 0;
  }

  rc = sqlite3_step(pStmt);
  if (rc == SQLITE_ROW) {
    int count = sqlite3_column_int(pStmt, 0);
    sqlite3_finalize(pStmt);
    if (count != 0) {
      *errmsg = sqlite3_mprintf(
          "Table %s has unique indices besides the primary key. This is "
          "not "
          "allowed for CRRs",
          tblName);
      return 0;
    }
  } else {
    sqlite3_finalize(pStmt);
    return 0;
  }

  // Must have a primary key
  zSql = sqlite3_mprintf(
      // pragma_index_list does not include primary keys that alias rowid...
      // hence why we cannot use `select * from pragma_index_list where origin =
      // pk`
      "SELECT count(*) FROM pragma_table_info('%s') WHERE \"pk\" > 0", tblName);
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);

  if (rc != SQLITE_OK) {
    *errmsg = sqlite3_mprintf(
        "Failed to analyze primary key information for %s", tblName);
    return 0;
  }

  rc = sqlite3_step(pStmt);
  if (rc == SQLITE_ROW) {
    int count = sqlite3_column_int(pStmt, 0);
    sqlite3_finalize(pStmt);
    if (count == 0) {
      *errmsg = sqlite3_mprintf(
          "Table %s has no primary key. CRRs must have a primary key", tblName);
      return 0;
    }
  } else {
    sqlite3_finalize(pStmt);
    return 0;
  }

  // No auto-increment primary keys
  zSql =
      "SELECT 1 FROM sqlite_master WHERE name = ? AND type = 'table' AND sql "
      "LIKE '%autoincrement%' limit 1";
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);

  rc += sqlite3_bind_text(pStmt, 1, tblName, -1, SQLITE_STATIC);
  if (rc != SQLITE_OK) {
    *errmsg = sqlite3_mprintf("Failed to analyze autoincrement status for %s",
                              tblName);
    return 0;
  }
  rc = sqlite3_step(pStmt);
  sqlite3_finalize(pStmt);
  if (rc == SQLITE_ROW) {
    *errmsg = sqlite3_mprintf(
        "%s has auto-increment primary keys. This is likely a mistake as two "
        "concurrent nodes will assign unrelated rows the same primary key. "
        "Either use a primary key that represents the identity of your row or "
        "use a database friendly UUID such as UUIDv7",
        tblName);
    return 0;
  } else if (rc != SQLITE_DONE) {
    return 0;
  }

  // No checked foreign key constraints
  zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_foreign_key_list('%s')",
                         tblName);
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);

  if (rc != SQLITE_OK) {
    *errmsg = sqlite3_mprintf(
        "Failed to analyze primary key information for %s", tblName);
    return 0;
  }

  rc = sqlite3_step(pStmt);
  if (rc == SQLITE_ROW) {
    int count = sqlite3_column_int(pStmt, 0);
    sqlite3_finalize(pStmt);
    if (count != 0) {
      *errmsg = sqlite3_mprintf(
          "Table %s has checked foreign key constraints. CRRs may have foreign "
          "keys but must not have "
          "checked foreign key constraints as they can be violated by row "
          "level "
          "security or replication.",
          tblName);
      return 0;
    }
  } else {
    sqlite3_finalize(pStmt);
    return 0;
  }

  // check for default value or nullable
  zSql = sqlite3_mprintf(
      "SELECT count(*) FROM pragma_table_xinfo('%s') WHERE \"notnull\" = 1 "
      "AND "
      "\"dflt_value\" IS NULL AND \"pk\" = 0",
      tblName);
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);

  if (rc != SQLITE_OK) {
    *errmsg = sqlite3_mprintf(
        "Failed to analyze default value information for %s", tblName);
    return 0;
  }

  rc = sqlite3_step(pStmt);
  if (rc == SQLITE_ROW) {
    int count = sqlite3_column_int(pStmt, 0);
    sqlite3_finalize(pStmt);
    if (count != 0) {
      *errmsg = sqlite3_mprintf(
          "Table %s has a NOT NULL column without a DEFAULT VALUE. This "
          "is not "
          "allowed as it prevents forwards and backwards compatability "
          "between "
          "schema versions. Make the column nullable or assign a default "
          "value "
          "to it.",
          tblName);
      return 0;
    }
  } else {
    sqlite3_finalize(pStmt);
    return 0;
  }

  return 1;
}