Skip to content

Commit ac2c544

Browse files
committed
basic insert trigger for crud
1 parent ae36db7 commit ac2c544

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

demos/supabase-todolist/lib/migrations/fts_setup.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ Future<void> configureFts(PowerSyncDatabase db) async {
6565
// ..add(createFtsMigration(
6666
// migrationVersion: 1,
6767
// tableName: 'lists',
68-
// isRawTable: true,
6968
// columns: ['name'],
7069
// tokenizationMethod: 'porter unicode61'))
7170
..add(createFtsMigration(

demos/supabase-todolist/lib/raw_tables_helper.dart

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,43 @@ import 'package:powersync/powersync.dart';
22
import 'package:powersync_flutter_demo/models/schema.dart';
33
import 'package:sqlite_async/sqlite_async.dart';
44

5+
const _versionTable = 'custom_schema_version';
6+
const latestSchemaVersion = 1;
7+
58
Future<void> initializeRawTablesSchema(PowerSyncDatabase db) async {
9+
await _setupSchemaVersionTable(db);
10+
611
await db.writeTransaction((ctx) async {
7-
await _createListsRawTable(ctx);
12+
final schemaVersion = await _getSchemaVersion(ctx);
13+
14+
if (schemaVersion == latestSchemaVersion) {
15+
return;
16+
}
17+
18+
if (schemaVersion > latestSchemaVersion) {
19+
throw Exception(
20+
"Database is in a newer version than expected ($schemaVersion)");
21+
}
22+
23+
for (var i = schemaVersion; i < latestSchemaVersion; i++) {
24+
assert(_migrationsMap.containsKey(i),
25+
'Migrations map is missing migration from version $i');
26+
await _migrationsMap[i]!(ctx);
27+
}
28+
29+
await _setSchemaVersion(ctx, latestSchemaVersion);
830
});
931
}
1032

33+
Map<int, Future<void> Function(SqliteWriteContext)> _migrationsMap = {
34+
0: _migrateFrom0,
35+
};
36+
37+
Future<void> _migrateFrom0(SqliteWriteContext ctx) async {
38+
await _createListsRawTable(ctx);
39+
await insertListTriggers(ctx);
40+
}
41+
1142
Future<void> _createListsRawTable(SqliteWriteContext ctx) async {
1243
await ctx.execute('''
1344
CREATE TABLE IF NOT EXISTS $listsRawTable(
@@ -18,3 +49,62 @@ CREATE TABLE IF NOT EXISTS $listsRawTable(
1849
) STRICT;
1950
''');
2051
}
52+
53+
Future<void> insertListTriggers(SqliteWriteContext ctx) async {
54+
final table = listsRawTable;
55+
final dataJsonExpr = "json(json_object('created_at', NEW.created_at, 'name', NEW.name, 'owner_id', NEW.owner_id))";
56+
57+
final insert = '''
58+
INSERT INTO powersync_crud_(data)
59+
VALUES(json_object('op', 'PUT', 'type', '$table', 'id', NEW.id, 'data', $dataJsonExpr));
60+
''';
61+
62+
await ctx.execute('''
63+
CREATE TRIGGER IF NOT EXISTS ${table}_insert
64+
AFTER INSERT ON $table
65+
FOR EACH ROW
66+
--WHEN NOT powersync_in_sync_operation()
67+
BEGIN
68+
$insert
69+
END;
70+
''');
71+
72+
// Default insert trigger from powersync
73+
/*
74+
await ctx.execute('''
75+
CREATE TRIGGER fts_insert_trigger_todos AFTER INSERT ON ps_data__todos
76+
BEGIN
77+
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")))));
78+
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('todos', NEW.id);
79+
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, 9223372036854775807);
80+
END
81+
'''); */
82+
}
83+
84+
Future<void> _setupSchemaVersionTable(PowerSyncDatabase db) async {
85+
await db.writeTransaction((ctx) async {
86+
// Create a simple version table to manage migrations.
87+
await ctx.execute('''
88+
CREATE TABLE IF NOT EXISTS $_versionTable (
89+
version INTEGER PRIMARY KEY
90+
);
91+
''');
92+
93+
// If no version is recorded, insert the initial version.
94+
final result =
95+
await ctx.get('SELECT COUNT(*) as count FROM $_versionTable;');
96+
final count = result['count'] as int;
97+
if (count == 0) {
98+
await ctx.execute('INSERT INTO $_versionTable (version) VALUES (0);');
99+
}
100+
});
101+
}
102+
103+
Future<int> _getSchemaVersion(SqliteReadContext db) async {
104+
final result = await db.get('SELECT version FROM $_versionTable;');
105+
return result['version'] as int;
106+
}
107+
108+
Future<void> _setSchemaVersion(SqliteWriteContext db, int version) async {
109+
await db.execute('UPDATE $_versionTable SET version = ?;', [version]);
110+
}

0 commit comments

Comments
 (0)