Skip to content

Commit 1f0771b

Browse files
committed
Use separate array for raw tables
1 parent 87516b3 commit 1f0771b

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 {
@@ -228,12 +233,6 @@ impl<'de> Deserialize<'de> for TableInfoFlags {
228233
}
229234
}
230235

231-
#[derive(Deserialize)]
232-
pub struct RawTableDefinition {
233-
pub put: PendingStatement,
234-
pub delete: PendingStatement,
235-
}
236-
237236
#[derive(Deserialize)]
238237
pub struct PendingStatement {
239238
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
@@ -681,26 +681,24 @@ void _syncTests<T>({
681681
'start',
682682
json.encode({
683683
'schema': {
684-
'tables': [
684+
'raw_tables': [
685685
{
686686
'name': 'users',
687-
'raw': {
688-
'put': {
689-
'sql':
690-
'INSERT OR REPLACE INTO users (id, name) VALUES (?, ?);',
691-
'params': [
692-
'Id',
693-
{'Column': 'name'}
694-
],
695-
},
696-
'delete': {
697-
'sql': 'DELETE FROM users WHERE id = ?',
698-
'params': ['Id'],
699-
},
687+
'put': {
688+
'sql':
689+
'INSERT OR REPLACE INTO users (id, name) VALUES (?, ?);',
690+
'params': [
691+
'Id',
692+
{'Column': 'name'}
693+
],
694+
},
695+
'delete': {
696+
'sql': 'DELETE FROM users WHERE id = ?',
697+
'params': ['Id'],
700698
},
701-
'columns': [],
702699
}
703700
],
701+
'tables': [],
704702
},
705703
}),
706704
);

0 commit comments

Comments
 (0)