@@ -7,7 +7,11 @@ import com.powersync.db.ActiveDatabaseGroup
77import com.powersync.db.crud.CrudEntry
88import com.powersync.db.crud.CrudTransaction
99import com.powersync.db.getString
10+ import com.powersync.db.schema.PendingStatement
11+ import com.powersync.db.schema.PendingStatementParameter
12+ import com.powersync.db.schema.RawTable
1013import com.powersync.db.schema.Schema
14+ import com.powersync.sync.LegacySyncImplementation
1115import com.powersync.test.getTempDir
1216import com.powersync.test.waitFor
1317import com.powersync.testutils.UserRow
@@ -548,4 +552,56 @@ class DatabaseTest {
548552 job.cancelAndJoin()
549553 hadOtherWrite.await()
550554 }
555+
556+ @Test
557+ fun testSoftClear () =
558+ databaseTest {
559+ database.execute(" INSERT INTO users (id, name) VALUES (uuid(), ?)" , listOf (" testuser" ))
560+ database.execute(" INSERT INTO ps_buckets (name, last_applied_op) VALUES (?, ?)" , listOf (" bkt" , 10 ))
561+
562+ // Doing a soft-clear should delete data but keep the bucket around.
563+ database.disconnectAndClear(soft = true )
564+ database.getAll(" SELECT name FROM ps_buckets" ) { it.getString(" name" ) } shouldHaveSize 1
565+
566+ // Doing a default clear also deletes buckets
567+ database.disconnectAndClear()
568+ database.getAll(" SELECT name FROM ps_buckets" ) { it.getString(" name" ) } shouldHaveSize 0
569+ }
570+
571+ @Test
572+ @OptIn(ExperimentalPowerSyncAPI ::class )
573+ fun testRawTablesClear () =
574+ databaseTest(createInitialDatabase = false ) {
575+ val db =
576+ openDatabase(
577+ Schema (
578+ listOf (
579+ RawTable (
580+ name = " lists" ,
581+ put =
582+ PendingStatement (
583+ " INSERT OR REPLACE INTO lists (id, name) VALUES (?, ?)" ,
584+ listOf (
585+ PendingStatementParameter .Id ,
586+ PendingStatementParameter .Column (" name" ),
587+ ),
588+ ),
589+ delete =
590+ PendingStatement (
591+ " DELETE FROM lists WHERE id = ?" ,
592+ listOf (PendingStatementParameter .Id ),
593+ ),
594+ clear = " DELETE FROM lists" ,
595+ ),
596+ ),
597+ ),
598+ )
599+
600+ db.execute(" CREATE TABLE lists (id TEXT NOT NULL PRIMARY KEY, name TEXT)" )
601+ db.execute(" INSERT INTO lists (id, name) VALUES (uuid(), ?)" , listOf (" list" ))
602+
603+ db.getAll(" SELECT * FROM lists" ) { } shouldHaveSize 1
604+ db.disconnectAndClear()
605+ db.getAll(" SELECT * FROM lists" ) { } shouldHaveSize 0
606+ }
551607}
0 commit comments