Skip to content

Commit 88e2172

Browse files
committed
Delete data from synced -> local
1 parent 427f009 commit 88e2172

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

crates/core/src/schema/management.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,16 @@ fn update_tables(db: *mut sqlite::sqlite3, schema: &Schema) -> Result<(), PowerS
3939
// In a block so that all statements are finalized before dropping tables.
4040
for table in &schema.tables {
4141
if let Some(existing) = existing_tables.remove(&*table.name) {
42-
if existing.local_only && !table.local_only() {
43-
// Migrate from a local-only to a synced table. Because none of the local writes
44-
// would have created CRUD entries, we'll have to re-create the table from
45-
// scratch.
42+
if existing.local_only != table.local_only() {
43+
// Migrating between local-only and synced tables. This works by deleting
44+
// existing and re-creating the table from scratch. We can re-create first and
45+
// delete the old table afterwards because they have a different name
46+
// (local-only tables have a ps_data_local prefix).
4647

4748
// To delete the old existing table in the end.
4849
existing_tables.insert(&existing.name, existing);
49-
} else if !existing.local_only && table.local_only() {
50-
// Migrate from a synced table to a local-only table. We can keep existing rows
51-
// and will also keep existing CRUD data to be uploaded before the switch.
52-
db.exec_safe(&format!(
53-
"ALTER TABLE {} RENAME TO {}",
54-
quote_identifier(&existing.internal_name),
55-
quote_identifier(&table.internal_name()),
56-
))
57-
.into_db_result(db)?;
58-
continue;
5950
} else {
60-
// Identical table exists already, nothing to do.
51+
// Compatible table exists already, nothing to do.
6152
continue;
6253
}
6354
}

crates/core/src/sync_local.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::vec::Vec;
55
use serde::Deserialize;
66

77
use crate::error::{PSResult, PowerSyncError};
8+
use crate::schema::inspection::ExistingTable;
89
use crate::schema::{PendingStatement, PendingStatementValue, RawTable, Schema};
910
use crate::state::DatabaseState;
1011
use crate::sync::BucketPriority;
@@ -434,22 +435,16 @@ impl<'a> ParsedDatabaseSchema<'a> {
434435
}
435436

436437
fn add_from_db(&mut self, db: *mut sqlite::sqlite3) -> Result<(), PowerSyncError> {
437-
// language=SQLite
438-
let statement = db
439-
.prepare_v2(
440-
"SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'",
441-
)
442-
.into_db_result(db)?;
443-
444-
while statement.step()? == ResultCode::ROW {
445-
let name = statement.column_text(0)?;
446-
// Strip the ps_data__ prefix so that we can lookup tables by their sync protocol name.
447-
let visible_name = name.get(9..).unwrap_or(name);
448-
449-
// Tables which haven't been passed explicitly are assumed to not be raw tables.
450-
self.tables
451-
.insert(String::from(visible_name), ParsedSchemaTable::json_table());
438+
let tables = ExistingTable::list(db)?;
439+
for table in tables {
440+
if !table.local_only {
441+
let visible_name = table.name;
442+
443+
self.tables
444+
.insert(visible_name, ParsedSchemaTable::json_table());
445+
}
452446
}
447+
453448
Ok(())
454449
}
455450
}

dart/test/schema_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ void main() {
9191
throwsA(isA<SqliteException>()));
9292

9393
// Data should still be there.
94-
expect(db.select('SELECT * FROM users'), [
95-
{'id': 'synced-id', 'name': 'name'}
96-
]);
94+
expect(db.select('SELECT * FROM ps_untyped'), hasLength(1));
95+
expect(db.select('SELECT * FROM users'), isEmpty);
9796

9897
// Inserting into local-only table should not record CRUD item.
9998
db.execute(

0 commit comments

Comments
 (0)