Skip to content

Commit 4977420

Browse files
committed
WIP Simplify triggers
1 parent 1643621 commit 4977420

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

demos/supabase-todolist/lib/raw_tables_helper.dart

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ Future<void> initializeRawTablesSchema(PowerSyncDatabase db) async {
1717
}
1818

1919
if (schemaVersion > latestSchemaVersion) {
20-
throw Exception(
21-
"Database is in a newer version than expected ($schemaVersion)");
20+
throw Exception("Database is in a newer version than expected ($schemaVersion)");
2221
}
2322

2423
print("Migrating from custom db schema version $schemaVersion to $latestSchemaVersion");
2524
for (var i = schemaVersion; i < latestSchemaVersion; i++) {
26-
assert(_migrationsMap.containsKey(i),
27-
'Migrations map is missing migration from version $i');
25+
assert(_migrationsMap.containsKey(i), 'Migrations map is missing migration from version $i');
2826
await _migrationsMap[i]!(ctx);
2927
}
3028

@@ -57,34 +55,29 @@ CREATE TABLE IF NOT EXISTS $listsRawTable(
5755

5856
Future<void> _createInsertListTriggers(SqliteWriteContext ctx) async {
5957
final table = listsRawTable;
60-
final dataJsonExpr = 'json(${_buildJsonObjectExpression(
58+
final dataJsonExpr = _buildJsonObjectExpression(
6159
columns: [
6260
'created_at',
6361
'name',
6462
'owner_id',
6563
],
6664
columnPrefix: 'NEW',
67-
)})';
65+
);
6866

6967
await ctx.execute('''
7068
CREATE TRIGGER IF NOT EXISTS ${table}_insert
7169
AFTER INSERT ON $table
7270
FOR EACH ROW
73-
WHEN NOT powersync_in_sync_operation()
7471
BEGIN
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);
72+
INSERT INTO powersync_crud (op, id, type, data) VALUES('PUT',NEW.id, '$table', $dataJsonExpr);
7873
END;
7974
''');
8075
}
8176

82-
String _buildJsonObjectExpression(
83-
{required List<String> columns, String? columnPrefix}) {
77+
String _buildJsonObjectExpression({required List<String> columns, String? columnPrefix}) {
8478
final list = columns.map((columnName) {
8579
final String key = "'$columnName'";
86-
final String value =
87-
columnPrefix != null ? '$columnPrefix.$columnName' : columnName;
80+
final String value = columnPrefix != null ? '$columnPrefix.$columnName' : columnName;
8881
return '$key, $value';
8982
}).join(', ');
9083

@@ -151,8 +144,7 @@ Future<void> _setupSchemaVersionTable(PowerSyncDatabase db) async {
151144
''');
152145

153146
// If no version is recorded, insert the initial version.
154-
final result =
155-
await ctx.get('SELECT COUNT(*) as count FROM $_versionTable;');
147+
final result = await ctx.get('SELECT COUNT(*) as count FROM $_versionTable;');
156148
final count = result['count'] as int;
157149
if (count == 0) {
158150
await ctx.execute('INSERT INTO $_versionTable (version) VALUES (0);');

0 commit comments

Comments
 (0)