Skip to content

Commit 227b585

Browse files
committed
Use separate array for raw tables
1 parent f28900a commit 227b585

File tree

5 files changed

+59
-33
lines changed

5 files changed

+59
-33
lines changed

crates/core/src/schema/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use serde::Deserialize;
66
use sqlite::ResultCode;
77
use sqlite_nostd as sqlite;
88
pub use table_info::{
9-
DiffIncludeOld, PendingStatement, PendingStatementValue, RawTableDefinition, Table,
10-
TableInfoFlags,
9+
DiffIncludeOld, PendingStatement, PendingStatementValue, RawTable, Table, TableInfoFlags,
1110
};
1211

1312
#[derive(Deserialize, Default)]
1413
pub struct Schema {
1514
pub tables: Vec<table_info::Table>,
15+
#[serde(default)]
16+
pub raw_tables: Vec<table_info::RawTable>,
1617
}
1718

1819
pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {

crates/core/src/schema/table_info.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ pub struct Table {
1717
pub diff_include_old: Option<DiffIncludeOld>,
1818
#[serde(flatten)]
1919
pub flags: TableInfoFlags,
20-
#[serde(default)]
21-
pub raw: Option<RawTableDefinition>,
20+
}
21+
22+
#[derive(Deserialize)]
23+
pub struct RawTable {
24+
pub name: String,
25+
pub put: PendingStatement,
26+
pub delete: PendingStatement,
2227
}
2328

2429
impl Table {
@@ -232,12 +237,6 @@ impl<'de> Deserialize<'de> for TableInfoFlags {
232237
}
233238
}
234239

235-
#[derive(Deserialize)]
236-
pub struct RawTableDefinition {
237-
pub put: PendingStatement,
238-
pub delete: PendingStatement,
239-
}
240-
241240
#[derive(Deserialize)]
242241
pub struct PendingStatement {
243242
pub sql: String,

crates/core/src/sync_local.rs

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

77
use crate::error::{PSResult, SQLiteError};
8-
use crate::schema::{PendingStatement, PendingStatementValue, RawTableDefinition, Schema};
8+
use crate::schema::{PendingStatement, PendingStatementValue, RawTable, Schema};
99
use crate::sync::BucketPriority;
1010
use sqlite_nostd::{self as sqlite, Destructor, ManagedStmt, Value};
1111
use sqlite_nostd::{ColumnType, Connection, ResultCode};
@@ -392,11 +392,9 @@ impl<'a> ParsedDatabaseSchema<'a> {
392392
}
393393

394394
fn add_from_schema(&mut self, schema: &'a Schema) {
395-
for table in &schema.tables {
396-
if let Some(raw) = &table.raw {
397-
self.tables
398-
.insert(table.name.clone(), ParsedSchemaTable::raw(raw));
399-
}
395+
for raw in &schema.raw_tables {
396+
self.tables
397+
.insert(raw.name.clone(), ParsedSchemaTable::raw(raw));
400398
}
401399
}
402400

@@ -426,7 +424,7 @@ struct ParsedSchemaTable<'a> {
426424
}
427425

428426
struct RawTableWithCachedStatements<'a> {
429-
definition: &'a RawTableDefinition,
427+
definition: &'a RawTable,
430428
cached_put: Option<PreparedPendingStatement<'a>>,
431429
cached_delete: Option<PreparedPendingStatement<'a>>,
432430
}
@@ -464,7 +462,7 @@ impl<'a> ParsedSchemaTable<'a> {
464462
Self { raw: None }
465463
}
466464

467-
pub fn raw(definition: &'a RawTableDefinition) -> Self {
465+
pub fn raw(definition: &'a RawTable) -> Self {
468466
Self {
469467
raw: Some(RawTableWithCachedStatements {
470468
definition,

dart/test/schema_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ void main() {
120120
);
121121
});
122122
});
123+
124+
test('raw tables', () {
125+
db.execute('SELECT powersync_replace_schema(?)', [
126+
json.encode({
127+
'raw_tables': [
128+
{
129+
'name': 'users',
130+
'put': {
131+
'sql': 'INSERT OR REPLACE INTO users (id, name) VALUES (?, ?);',
132+
'params': [
133+
'Id',
134+
{'Column': 'name'}
135+
],
136+
},
137+
'delete': {
138+
'sql': 'DELETE FROM users WHERE id = ?',
139+
'params': ['Id'],
140+
},
141+
}
142+
],
143+
'tables': [],
144+
})
145+
]);
146+
147+
expect(
148+
db.select(
149+
"SELECT * FROM sqlite_schema WHERE type = 'table' AND name LIKE 'ps_data%'"),
150+
isEmpty,
151+
);
152+
});
123153
});
124154
}
125155

dart/test/sync_test.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -686,26 +686,24 @@ void _syncTests<T>({
686686
'start',
687687
json.encode({
688688
'schema': {
689-
'tables': [
689+
'raw_tables': [
690690
{
691691
'name': 'users',
692-
'raw': {
693-
'put': {
694-
'sql':
695-
'INSERT OR REPLACE INTO users (id, name) VALUES (?, ?);',
696-
'params': [
697-
'Id',
698-
{'Column': 'name'}
699-
],
700-
},
701-
'delete': {
702-
'sql': 'DELETE FROM users WHERE id = ?',
703-
'params': ['Id'],
704-
},
692+
'put': {
693+
'sql':
694+
'INSERT OR REPLACE INTO users (id, name) VALUES (?, ?);',
695+
'params': [
696+
'Id',
697+
{'Column': 'name'}
698+
],
699+
},
700+
'delete': {
701+
'sql': 'DELETE FROM users WHERE id = ?',
702+
'params': ['Id'],
705703
},
706-
'columns': [],
707704
}
708705
],
706+
'tables': [],
709707
},
710708
}),
711709
);

0 commit comments

Comments
 (0)