Skip to content

Fix reverting failed deletes #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/powersync/lib/src/schema_logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ FOR EACH ROW
BEGIN
DELETE FROM $internalNameE WHERE id = OLD.id;
INSERT INTO ps_crud(tx_id, data) SELECT current_tx, json_object('op', 'DELETE', 'type', ${quoteString(type)}, 'id', OLD.id) FROM ps_tx WHERE id = 1;
INSERT INTO ps_oplog(bucket, op_id, op, row_type, row_id, hash, superseded)
SELECT '\$local',
1,
'REMOVE',
${quoteString(type)},
OLD.id,
0,
0;
INSERT OR REPLACE INTO ps_buckets(name, pending_delete, last_op, target_op) VALUES('\$local', 1, 0, $maxOpId);
END""",
"""
CREATE TRIGGER ${quoteIdentifier('ps_view_insert_$viewName')}
Expand Down
113 changes: 113 additions & 0 deletions packages/powersync/test/bucket_storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -674,5 +674,118 @@ void main() {
{'description': 'server updated'}
]));
});

test('should revert a failing update', () async {
await bucketStorage.saveSyncData(SyncDataBatch([
SyncBucketData(
bucket: 'bucket1',
data: [putAsset1_1, putAsset2_2, putAsset1_3],
),
]));

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '3',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

// Local save
db.execute('INSERT INTO assets(id, description) VALUES(?, ?)',
['O3', 'inserted']);
final batch = bucketStorage.getCrudBatch();
await batch!.complete();
await bucketStorage.updateLocalTarget(() async {
return '4';
});

expect(
db.select('SELECT description FROM assets WHERE id = \'O3\''),
equals([
{'description': 'inserted'}
]));

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '4',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

expect(db.select('SELECT description FROM assets WHERE id = \'O3\''),
equals([]));
});

test('should revert a failing delete', () async {
await bucketStorage.saveSyncData(SyncDataBatch([
SyncBucketData(
bucket: 'bucket1',
data: [putAsset1_1, putAsset2_2, putAsset1_3],
),
]));

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '3',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

// Local save
db.execute('DELETE FROM assets WHERE id = ?', ['O2']);

expect(db.select('SELECT description FROM assets WHERE id = \'O2\''),
equals([]));
// Simulate a permissions error when uploading - data should be preserved.
final batch = bucketStorage.getCrudBatch();
await batch!.complete();

await bucketStorage.updateLocalTarget(() async {
return '4';
});

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '4',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

expect(
db.select('SELECT description FROM assets WHERE id = \'O2\''),
equals([
{'description': 'bar'}
]));
});

test('should revert a failing insert', () async {
await bucketStorage.saveSyncData(SyncDataBatch([
SyncBucketData(
bucket: 'bucket1',
data: [putAsset1_1, putAsset2_2, putAsset1_3],
),
]));

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '3',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

// Local save
db.execute('DELETE FROM assets WHERE id = ?', ['O2']);

expect(db.select('SELECT description FROM assets WHERE id = \'O2\''),
equals([]));
// Simulate a permissions error when uploading - data should be preserved.
final batch = bucketStorage.getCrudBatch();
await batch!.complete();

await bucketStorage.updateLocalTarget(() async {
return '4';
});

await syncLocalChecked(Checkpoint(
lastOpId: '3',
writeCheckpoint: '4',
checksums: [BucketChecksum(bucket: 'bucket1', checksum: 6)]));

expect(
db.select('SELECT description FROM assets WHERE id = \'O2\''),
equals([
{'description': 'bar'}
]));
});
});
}