Skip to content

Commit bcb4b98

Browse files
committed
delete trigger
1 parent eb01e38 commit bcb4b98

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

demos/supabase-todolist/lib/powersync.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class SupabaseConnector extends PowerSyncBackendConnector {
9898
for (var op in transaction.crud) {
9999
lastOp = op;
100100

101+
log.info("Upload op: ${op.op} ${op.table} ${op.id} ${op.opData}");
102+
101103
final table = rest.from(op.table);
102104
if (op.op == UpdateType.put) {
103105
var data = Map<String, dynamic>.of(op.opData!);

demos/supabase-todolist/lib/raw_tables_helper.dart

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:powersync/powersync.dart';
22
import 'package:powersync_flutter_demo/models/schema.dart';
33
import 'package:sqlite_async/sqlite_async.dart';
44

5+
// A table to hold a custom schema version number
56
const _versionTable = 'custom_schema_version';
67
const latestSchemaVersion = 1;
78

@@ -20,6 +21,7 @@ Future<void> initializeRawTablesSchema(PowerSyncDatabase db) async {
2021
"Database is in a newer version than expected ($schemaVersion)");
2122
}
2223

24+
print("Migrating from custom db schema version $schemaVersion to $latestSchemaVersion");
2325
for (var i = schemaVersion; i < latestSchemaVersion; i++) {
2426
assert(_migrationsMap.containsKey(i),
2527
'Migrations map is missing migration from version $i');
@@ -36,8 +38,10 @@ Map<int, Future<void> Function(SqliteWriteContext)> _migrationsMap = {
3638

3739
Future<void> _migrateFrom0(SqliteWriteContext ctx) async {
3840
await _createListsRawTable(ctx);
39-
// await insertListTriggers(ctx);
40-
await _updateListTrigger(ctx);
41+
// Triggers
42+
await _createInsertListTriggers(ctx);
43+
await _createUpdateListTrigger(ctx);
44+
await _createDeleteListTrigger(ctx);
4145
}
4246

4347
Future<void> _createListsRawTable(SqliteWriteContext ctx) async {
@@ -51,7 +55,7 @@ CREATE TABLE IF NOT EXISTS $listsRawTable(
5155
''');
5256
}
5357

54-
Future<void> insertListTriggers(SqliteWriteContext ctx) async {
58+
Future<void> _createInsertListTriggers(SqliteWriteContext ctx) async {
5559
final table = listsRawTable;
5660
final dataJsonExpr = 'json(${_buildJsonObjectExpression(
5761
columns: [
@@ -61,34 +65,18 @@ Future<void> insertListTriggers(SqliteWriteContext ctx) async {
6165
],
6266
columnPrefix: 'NEW',
6367
)})';
64-
// final dataJsonExpr =
65-
// "json(json_object('created_at', NEW.created_at, 'name', NEW.name, 'owner_id', NEW.owner_id))";
66-
67-
final insert = '''
68-
INSERT INTO powersync_crud_(data)
69-
VALUES(json_object('op', 'PUT', 'type', '$table', 'id', NEW.id, 'data', $dataJsonExpr));
70-
''';
7168

7269
await ctx.execute('''
7370
CREATE TRIGGER IF NOT EXISTS ${table}_insert
7471
AFTER INSERT ON $table
7572
FOR EACH ROW
76-
--WHEN NOT powersync_in_sync_operation()
73+
WHEN NOT powersync_in_sync_operation()
7774
BEGIN
78-
$insert
75+
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'PUT', 'type', '$table', 'id', NEW.id, 'data', $dataJsonExpr));
76+
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('$table', NEW.id);
77+
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('\$local', 0, 9223372036854775807);
7978
END;
8079
''');
81-
82-
// Default insert trigger from powersync
83-
/*
84-
await ctx.execute('''
85-
CREATE TRIGGER fts_insert_trigger_todos AFTER INSERT ON ps_data__todos
86-
BEGIN
87-
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'PUT', 'type', 'todos', 'id', NEW.id, 'data', json(powersync_diff('{}', json_object('list_id', NEW."list_id", 'photo_id', NEW."photo_id", 'created_at', NEW."created_at", 'completed_at', NEW."completed_at", 'description', NEW."description", 'completed', NEW."completed", 'created_by', NEW."created_by", 'completed_by', NEW."completed_by")))));
88-
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('todos', NEW.id);
89-
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, 9223372036854775807);
90-
END
91-
'''); */
9280
}
9381

9482
String _buildJsonObjectExpression(
@@ -103,7 +91,7 @@ String _buildJsonObjectExpression(
10391
return "json_object($list)";
10492
}
10593

106-
Future<void> _updateListTrigger(SqliteWriteContext ctx) async {
94+
Future<void> _createUpdateListTrigger(SqliteWriteContext ctx) async {
10795
final table = listsRawTable;
10896
final columns = [
10997
'created_at',
@@ -131,8 +119,22 @@ BEGIN
131119
END;
132120
INSERT INTO powersync_crud_(data, options)
133121
VALUES(json_object('op', 'PATCH', 'type', '$table', 'id', NEW.id, 'data', json(powersync_diff($oldRowJsonObj, $newRowJsonObj))), 0);
134-
--INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('todos', NEW.id);
135-
--INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('\$local', 0, 9223372036854775807);
122+
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('$table', NEW.id);
123+
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('\$local', 0, 9223372036854775807);
124+
END
125+
''');
126+
}
127+
128+
Future<void> _createDeleteListTrigger(SqliteWriteContext ctx) async {
129+
final table = listsRawTable;
130+
await ctx.execute('''
131+
CREATE TRIGGER ${table}_delete
132+
AFTER DELETE ON $table
133+
FOR EACH ROW
134+
BEGIN
135+
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'DELETE', 'type', '$table', 'id', OLD.id));
136+
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('$table', OLD.id);
137+
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('\$local', 0, 9223372036854775807);
136138
END
137139
''');
138140
}

0 commit comments

Comments
 (0)