Skip to content

Commit 994a3a9

Browse files
committed
Revert some changes
1 parent 9bf55e8 commit 994a3a9

File tree

6 files changed

+130
-85
lines changed

6 files changed

+130
-85
lines changed

crates/core/src/fix_data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloc::format;
44
use alloc::string::String;
55

66
use crate::create_sqlite_optional_text_fn;
7-
use crate::error::PowerSyncError;
7+
use crate::error::{PSResult, PowerSyncError};
88
use sqlite_nostd::{self as sqlite, ColumnType, Value};
99
use sqlite_nostd::{Connection, Context, ResultCode};
1010

@@ -20,11 +20,11 @@ use crate::util::quote_identifier;
2020
//
2121
// The fix here is to find these dangling rows, and add them to ps_updated_rows.
2222
// The next time the sync_local operation is run, these rows will be removed.
23-
pub fn apply_v035_fix(db: *mut sqlite::sqlite3) -> Result<i64, ResultCode> {
23+
pub fn apply_v035_fix(db: *mut sqlite::sqlite3) -> Result<i64, PowerSyncError> {
2424
// language=SQLite
2525
let statement = db
2626
.prepare_v2("SELECT name, powersync_external_table_name(name) FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data__*'")
27-
?;
27+
.into_db_result(db)?;
2828

2929
while statement.step()? == ResultCode::ROW {
3030
let full_name = statement.column_text(0)?;
@@ -123,7 +123,7 @@ fn powersync_remove_duplicate_key_encoding_impl(
123123
let arg = args.get(0).ok_or(ResultCode::MISUSE)?;
124124

125125
if arg.value_type() != ColumnType::Text {
126-
return Err(PowerSyncError::argument_error("Expected text"));
126+
return Err(ResultCode::MISMATCH.into());
127127
}
128128

129129
return Ok(remove_duplicate_key_encoding(arg.text()));

crates/core/src/migrations.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use sqlite::ResultCode;
88
use sqlite_nostd as sqlite;
99
use sqlite_nostd::{Connection, Context};
1010

11-
use crate::error::PowerSyncError;
11+
use crate::error::{PSResult, PowerSyncError};
1212
use crate::fix_data::apply_v035_fix;
1313
use crate::sync::BucketPriority;
1414

@@ -82,7 +82,7 @@ CREATE TABLE IF NOT EXISTS ps_migration(id INTEGER PRIMARY KEY, down_migrations
8282
}
8383
let new_version = current_version_stmt.column_int(0);
8484
if new_version >= current_version {
85-
// Database down from version $currentVersion to $version failed - version not updated after dow migration
85+
// Database down from version $currentVersion to $version failed - version not updated after down migration
8686
return Err(PowerSyncError::down_migration_did_not_update_version(
8787
current_version,
8888
));
@@ -93,8 +93,9 @@ CREATE TABLE IF NOT EXISTS ps_migration(id INTEGER PRIMARY KEY, down_migrations
9393

9494
if current_version < 1 {
9595
// language=SQLite
96-
local_db.exec_safe(
97-
"
96+
local_db
97+
.exec_safe(
98+
"
9899
CREATE TABLE ps_oplog(
99100
bucket TEXT NOT NULL,
100101
op_id INTEGER NOT NULL,
@@ -125,7 +126,8 @@ CREATE TABLE ps_crud (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);
125126
126127
INSERT INTO ps_migration(id, down_migrations) VALUES(1, NULL);
127128
",
128-
)?;
129+
)
130+
.into_db_result(local_db)?;
129131
}
130132

131133
if current_version < 2 && target_version >= 2 {
@@ -137,7 +139,7 @@ INSERT INTO ps_tx(id, current_tx, next_tx) VALUES(1, NULL, 1);
137139
ALTER TABLE ps_crud ADD COLUMN tx_id INTEGER;
138140
139141
INSERT INTO ps_migration(id, down_migrations) VALUES(2, json_array(json_object('sql', 'DELETE FROM ps_migration WHERE id >= 2', 'params', json_array()), json_object('sql', 'DROP TABLE ps_tx', 'params', json_array()), json_object('sql', 'ALTER TABLE ps_crud DROP COLUMN tx_id', 'params', json_array())));
140-
")?;
142+
").into_db_result(local_db)?;
141143
}
142144

143145
if current_version < 3 && target_version >= 3 {
@@ -147,7 +149,7 @@ CREATE TABLE ps_kv(key TEXT PRIMARY KEY NOT NULL, value BLOB);
147149
INSERT INTO ps_kv(key, value) values('client_id', uuid());
148150
149151
INSERT INTO ps_migration(id, down_migrations) VALUES(3, json_array(json_object('sql', 'DELETE FROM ps_migration WHERE id >= 3'), json_object('sql', 'DROP TABLE ps_kv')));
150-
")?;
152+
").into_db_result(local_db)?;
151153
}
152154

153155
if current_version < 4 && target_version >= 4 {
@@ -167,7 +169,7 @@ VALUES(4,
167169
json_object('sql', 'ALTER TABLE ps_buckets DROP COLUMN op_checksum'),
168170
json_object('sql', 'ALTER TABLE ps_buckets DROP COLUMN remove_operations')
169171
));
170-
")?;
172+
").into_db_result(local_db)?;
171173
}
172174

173175
if current_version < 5 && target_version >= 5 {
@@ -286,7 +288,8 @@ VALUES(5,
286288
json_object('sql', 'DELETE FROM ps_migration WHERE id >= 5')
287289
));
288290
",
289-
)?;
291+
)
292+
.into_db_result(local_db)?;
290293
}
291294

292295
if current_version < 6 && target_version >= 6 {
@@ -295,15 +298,17 @@ VALUES(5,
295298
apply_v035_fix(local_db)?;
296299
}
297300

298-
local_db.exec_safe(
299-
"\
301+
local_db
302+
.exec_safe(
303+
"\
300304
INSERT INTO ps_migration(id, down_migrations)
301305
VALUES(6,
302306
json_array(
303307
json_object('sql', 'DELETE FROM ps_migration WHERE id >= 6')
304308
));
305309
",
306-
)?;
310+
)
311+
.into_db_result(local_db)?;
307312
}
308313

309314
if current_version < 7 && target_version >= 7 {
@@ -325,7 +330,7 @@ json_object('sql', 'DELETE FROM ps_migration WHERE id >= 7')
325330
));
326331
", SENTINEL_PRIORITY, SENTINEL_PRIORITY);
327332

328-
local_db.exec_safe(&stmt)?;
333+
local_db.exec_safe(&stmt).into_db_result(local_db)?;
329334
}
330335

331336
if current_version < 8 && target_version >= 8 {
@@ -346,7 +351,7 @@ json_object('sql', 'DROP TABLE ps_sync_state_new'),
346351
json_object('sql', 'DELETE FROM ps_migration WHERE id >= 8')
347352
));
348353
";
349-
local_db.exec_safe(&stmt)?;
354+
local_db.exec_safe(&stmt).into_db_result(local_db)?;
350355
}
351356

352357
if current_version < 9 && target_version >= 9 {
@@ -360,7 +365,7 @@ json_object('sql', 'DELETE FROM ps_migration WHERE id >= 9')
360365
));
361366
";
362367

363-
local_db.exec_safe(stmt)?;
368+
local_db.exec_safe(stmt).into_db_result(local_db)?;
364369
}
365370

366371
if current_version < 10 && target_version >= 10 {
@@ -375,7 +380,8 @@ INSERT INTO ps_migration(id, down_migrations) VALUES (10, json_array(
375380
json_object('sql', 'DELETE FROM ps_migration WHERE id >= 10')
376381
));
377382
",
378-
)?;
383+
)
384+
.into_db_result(local_db)?;
379385
}
380386

381387
Ok(())

crates/core/src/operations_vtab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extern "C" fn update(
108108
let result = delete_pending_buckets(db, args[3].text());
109109
vtab_result(vtab, result)
110110
} else if op == "delete_bucket" {
111-
let result: Result<(), ResultCode> = delete_bucket(db, args[3].text());
111+
let result = delete_bucket(db, args[3].text());
112112
vtab_result(vtab, result)
113113
} else {
114114
ResultCode::MISUSE as c_int

crates/core/src/schema/management.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@ use sqlite::{Connection, ResultCode, Value};
99
use sqlite_nostd as sqlite;
1010
use sqlite_nostd::Context;
1111

12-
use crate::error::PowerSyncError;
12+
use crate::error::{PSResult, PowerSyncError};
1313
use crate::ext::ExtendedDatabase;
1414
use crate::util::{quote_identifier, quote_json_path};
1515
use crate::{create_auto_tx_function, create_sqlite_text_fn};
1616

1717
use super::Schema;
1818

19-
fn update_tables(db: *mut sqlite::sqlite3, schema: &str) -> Result<(), ResultCode> {
19+
fn update_tables(db: *mut sqlite::sqlite3, schema: &str) -> Result<(), PowerSyncError> {
2020
{
2121
// In a block so that the statement is finalized before dropping tables
2222
// language=SQLite
23-
let statement = db.prepare_v2(
24-
"\
23+
let statement = db
24+
.prepare_v2(
25+
"\
2526
SELECT
2627
json_extract(json_each.value, '$.name') as name,
2728
powersync_internal_table_name(json_each.value) as internal_name,
2829
ifnull(json_extract(json_each.value, '$.local_only'), 0) as local_only
2930
FROM json_each(json_extract(?, '$.tables'))
3031
WHERE name NOT IN (SELECT name FROM powersync_tables)",
31-
)?;
32+
)
33+
.into_db_result(db)?;
3234
statement.bind_text(1, schema, sqlite::Destructor::STATIC)?;
3335

34-
while statement.step()? == ResultCode::ROW {
36+
while statement.step().into_db_result(db)? == ResultCode::ROW {
3537
let name = statement.column_text(0)?;
3638
let internal_name = statement.column_text(1)?;
3739
let local_only = statement.column_int(2) != 0;
3840

3941
db.exec_safe(&format!(
4042
"CREATE TABLE {:}(id TEXT PRIMARY KEY NOT NULL, data TEXT)",
4143
quote_identifier(internal_name)
42-
))?;
44+
))
45+
.into_db_result(db)?;
4346

4447
if !local_only {
4548
// MOVE data if any
@@ -52,7 +55,8 @@ SELECT
5255
quote_identifier(internal_name)
5356
),
5457
name,
55-
)?;
58+
)
59+
.into_db_result(db)?;
5660

5761
// language=SQLite
5862
db.exec_text(
@@ -74,7 +78,8 @@ SELECT
7478
quote_identifier(internal_name)
7579
),
7680
name,
77-
)?;
81+
)
82+
.into_db_result(db)?;
7883

7984
// language=SQLite
8085
db.exec_text(
@@ -92,13 +97,15 @@ SELECT
9297
{
9398
// In a block so that the statement is finalized before dropping tables
9499
// language=SQLite
95-
let statement = db.prepare_v2(
96-
"\
100+
let statement = db
101+
.prepare_v2(
102+
"\
97103
SELECT name, internal_name, local_only FROM powersync_tables WHERE name NOT IN (
98104
SELECT json_extract(json_each.value, '$.name')
99105
FROM json_each(json_extract(?, '$.tables'))
100106
)",
101-
)?;
107+
)
108+
.into_db_result(db)?;
102109
statement.bind_text(1, schema, sqlite::Destructor::STATIC)?;
103110

104111
while statement.step()? == ResultCode::ROW {
@@ -115,7 +122,8 @@ SELECT name, internal_name, local_only FROM powersync_tables WHERE name NOT IN (
115122
quote_identifier(internal_name)
116123
),
117124
name,
118-
)?;
125+
)
126+
.into_db_result(db)?;
119127
}
120128
}
121129
}
@@ -124,7 +132,7 @@ SELECT name, internal_name, local_only FROM powersync_tables WHERE name NOT IN (
124132
// we get "table is locked" errors.
125133
for internal_name in tables_to_drop {
126134
let q = format!("DROP TABLE {:}", quote_identifier(&internal_name));
127-
db.exec_safe(&q)?;
135+
db.exec_safe(&q).into_db_result(db)?;
128136
}
129137

130138
Ok(())
@@ -196,18 +204,20 @@ fn update_indexes(db: *mut sqlite::sqlite3, schema: &str) -> Result<(), PowerSyn
196204

197205
// In a block so that the statement is finalized before dropping indexes
198206
// language=SQLite
199-
let statement = db.prepare_v2(
200-
"\
207+
let statement = db
208+
.prepare_v2(
209+
"\
201210
SELECT
202211
sqlite_master.name as index_name
203212
FROM sqlite_master
204213
WHERE sqlite_master.type = 'index'
205214
AND sqlite_master.name GLOB 'ps_data_*'
206215
AND sqlite_master.name NOT IN (SELECT value FROM json_each(?))
207216
",
208-
)?;
209-
let json_names =
210-
serde_json::to_string(&expected_index_names).map_err(PowerSyncError::internal)?;
217+
)
218+
.into_db_result(db)?;
219+
let json_names = serde_json::to_string(&expected_index_names)
220+
.map_err(PowerSyncError::as_argument_error)?;
211221
statement.bind_text(1, &json_names, sqlite::Destructor::STATIC)?;
212222

213223
while statement.step()? == ResultCode::ROW {
@@ -220,13 +230,13 @@ SELECT
220230
// We cannot have any open queries on sqlite_master at the point that we drop indexes, otherwise
221231
// we get "database table is locked (code 6)" errors.
222232
for statement in statements {
223-
db.exec_safe(&statement)?;
233+
db.exec_safe(&statement).into_db_result(db)?;
224234
}
225235

226236
Ok(())
227237
}
228238

229-
fn update_views(db: *mut sqlite::sqlite3, schema: &str) -> Result<(), ResultCode> {
239+
fn update_views(db: *mut sqlite::sqlite3, schema: &str) -> Result<(), PowerSyncError> {
230240
// Update existing views if modified
231241
// language=SQLite
232242
db.exec_text("\
@@ -247,7 +257,7 @@ FROM (SELECT
247257
powersync_views.delete_trigger_sql IS NOT gen.delete_trigger_sql OR
248258
powersync_views.insert_trigger_sql IS NOT gen.insert_trigger_sql OR
249259
powersync_views.update_trigger_sql IS NOT gen.update_trigger_sql)
250-
", schema)?;
260+
", schema).into_db_result(db)?;
251261

252262
// Create new views
253263
// language=SQLite
@@ -266,15 +276,15 @@ ifnull(json_extract(json_each.value, '$.view_name'), json_extract(json_each.valu
266276
powersync_trigger_insert_sql(json_each.value) as insert_trigger_sql,
267277
powersync_trigger_update_sql(json_each.value) as update_trigger_sql
268278
FROM json_each(json_extract(?, '$.tables'))
269-
WHERE name NOT IN (SELECT name FROM powersync_views)", schema)?;
279+
WHERE name NOT IN (SELECT name FROM powersync_views)", schema).into_db_result(db)?;
270280

271281
// Delete old views
272282
// language=SQLite
273283
db.exec_text("\
274284
DELETE FROM powersync_views WHERE name NOT IN (
275285
SELECT ifnull(json_extract(json_each.value, '$.view_name'), json_extract(json_each.value, '$.name'))
276286
FROM json_each(json_extract(?, '$.tables'))
277-
)", schema)?;
287+
)", schema).into_db_result(db)?;
278288

279289
Ok(())
280290
}
@@ -291,7 +301,7 @@ fn powersync_replace_schema_impl(
291301
let db = ctx.db_handle();
292302

293303
// language=SQLite
294-
db.exec_safe("SELECT powersync_init()")?;
304+
db.exec_safe("SELECT powersync_init()").into_db_result(db)?;
295305

296306
update_tables(db, schema)?;
297307
update_indexes(db, schema)?;

0 commit comments

Comments
 (0)