Commit Graph

6464 Commits

Author SHA1 Message Date
PThorpe92
eff455fb03 Add READONLY const property to virtual table module trait 2025-07-23 16:44:04 -04:00
Jussi Saurio
1e38202084 Merge 'WAL insert API' from Nikita Sivukhin
This PR implements missing raw WAL API from LibSQL for future use for
offline-sync feature:
1. `wal_insert_begin` - begin WAL session by opening WAL read/write
transaction
2. `wal_insert_end` - finish WAL session by closing WAL transaction
opened by `wal_insert_begin` call
3. `wal_insert_frame` - insert frame `frame_no` with raw content `frame`
(WAL frame included)
For now any schema changes will not be reflected after
`wal_insert_frame` because `turso-db` do not re-parse schema without
need. I will fix this in follow up PR.

Reviewed-by: Pekka Enberg <penberg@iki.fi>

Closes #2231
2025-07-23 14:08:15 +03:00
Jussi Saurio
63f488a1cc Merge 'Pager: clear overflow cells when freeing page' from Jussi Saurio
## Background
The `balance_non_root` procedure can end up freeing a page if the pages
to be balanced can fit the required combined number of cells in less
pages, even if the page that triggered balancing is overfull. This can
then free the originally overfull pages, leaving a non-zero
`overflow_cells` on the in-mem representation of the page.
```rust
balance_non_root: page=305, overflow_cells=0
balance_non_root: page=304, overflow_cells=0
balance_non_root: page=302, overflow_cells=1
pre_edit_page(page=304, page_idx=0, new_cells=4, old_cells=1, cells_per_page_old=[1, 3, 9, 0, 0], cells_per_page_new=[4, 9, 9, 0, 0], cell_array_count=9)
edit_page start_old_cells=0 start_new_cells=0 number_new_cells=4 cell_array=9 end_old_cells=1 end_new_cells=4
pre_edit_page(page=305, page_idx=1, new_cells=4, old_cells=1, cells_per_page_old=[1, 3, 9, 0, 0], cells_per_page_new=[4, 9, 9, 0, 0], cell_array_count=9)
edit_page start_old_cells=2 start_new_cells=5 number_new_cells=4 cell_array=9 end_old_cells=3 end_new_cells=9
balance_non_root: sibling_count_new=2, sibling_count=3

// Custom assertion to demonstrate this:
thread 'main' panicked at core/storage/pager.rs:1127:29:
Pager::free_page: In memory page with id 302 has overflow cells
```
## Why is this a problem
Right now this is not an immediate problem, because we always allocate
brand new pages. However, in #2233 we begin to reuse pages from the
freelist for page allocation to improve performance and reduce database
size bloat. In that PR, the `balance_non_root` procedure will calculate
cell counts incorrectly in `edit_page()` and panic if: 1. a new
allocated page is taken from the freelist, 2. the page is still in
memory, and 3. and it still contains `overflow_cells`.
## Solution
Clear `page_contents.overflow_cells` when an in-memory page is freed.

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #2238
2025-07-23 13:38:31 +03:00
Jussi Saurio
ab92aabb70 Merge 'types: less noisy Debug implementation for ImmutableRecord' from Jussi Saurio
This PR truncates the Debug output of ImmutableRecord so that blobs and
texts only show the first 20 bytes or chars respectively. Debugging gets
much better when we don't print a huge blob or text a million times.

Closes #2237
2025-07-23 12:20:54 +03:00
Jussi Saurio
f98a9e8939 Pager: don't assume page is necessarily in memory anymore 2025-07-23 11:08:34 +03:00
Jussi Saurio
ecb5fce1bd Pager: clear overflow cells when freeing page 2025-07-23 10:58:10 +03:00
Jussi Saurio
ffd2299aa1 types: less noisy Debug implementation for ImmutableRecord 2025-07-23 10:56:41 +03:00
Nikita Sivukhin
60eaa11add hide new methods behind fs feature 2025-07-23 11:51:39 +04:00
Nikita Sivukhin
a85283a84f add trailing comma 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
3c0af3e389 small adjustments 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
2283a04aab add more tests 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
73761a8983 rollback non-commited changes 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
bf2bfbe978 fix clippy 2025-07-23 11:31:00 +04:00
Nikita Sivukhin
16763e1500 implement raw WAL write api 2025-07-23 11:30:59 +04:00
Nikita Sivukhin
bc09ea6e98 make end_write_txn/end_read_txn function non-failing 2025-07-23 11:30:29 +04:00
PThorpe92
cb42102a6e Merge 'silence clippy errors with features disabled' from Glauber Costa
When compiling with features disabled, there are lots of clippy
warnings. This PR silences them.
For the utils file, I am using a bit of a hammer and just allowing
unused stuff in the whole file. Due to the box of utilities nature of
this file, it'll always be the case that things will be unused depending
on the feature-set.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2236
2025-07-22 21:51:27 -04:00
PThorpe92
a13fc3515e Fix cargo fmt warning 2025-07-22 21:47:15 -04:00
Glauber Costa
a10d8d7f94 silence clippy errors with features disabled
When compiling with features disabled, there are lots of clippy
warnings. This PR silences them.

For the utils file, I am using a bit of a hammer and just allowing
unused stuff in the whole file. Due to the box of utilities nature of
this file, it'll always be the case that things will be unused depending
on the feature-set.
2025-07-22 20:37:45 -05:00
PThorpe92
9c785ea0ea Merge 'make readonly a property of the database' from Glauber Costa
There's no such thing as a read-only connection.
In a normal connection, you can have many attached databases. Some r/o,
some r/w.
To properly fix that, we also need to fix the OpenWrite opcode. Right
now we are passing a name, which is the name of the table. That
parameter is not used anywhere. That is also not what the SQLite opcode
specifies. Same as OpenRead, the p3 register should be the database
index.
With that change, we can - for now - pass the index 0, which is all we
support anyway, and then use that to test if we are r/o.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2232
2025-07-22 21:12:08 -04:00
PThorpe92
dcd677ab6c Merge 'Safe AtomicUsize wrapper for db_state: add DbState and AtomicDbState' from Levy A.
Makes invalid states impossible, removes magic numbers. Functionally
equivalent.
> [!NOTE]
> ~`DbState` is implemented as a transparent wrapper over `usize` to
avoid undefined behavior with `mem::transmute`~
> Switched to a regular enum, by @Shourya742's suggestion.

Reviewed-by: bit-aloo (@Shourya742)
Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2174
2025-07-22 19:21:10 -04:00
Levy A.
e6ad88cc18 refactor: constified enum -> regular enum 2025-07-22 17:20:30 -03:00
Levy A.
203239ff30 refactor: safer db_state 2025-07-22 17:20:29 -03:00
Glauber Costa
57a1113460 make readonly a property of the database
There's no such thing as a read-only connection.
In a normal connection, you can have many attached databases. Some
r/o, some r/w.

To properly fix that, we also need to fix the OpenWrite opcode. Right
now we are passing a name, which is the name of the table. That
parameter is not used anywhere. That is also not what the SQLite opcode
specifies. Same as OpenRead, the p3 register should be the database
index.

With that change, we can - for now - pass the index 0, which is all
we support anyway, and then use that to test if we are r/o.
2025-07-22 09:41:32 -05:00
Jussi Saurio
60beeaefd9 Merge 'Add Github workflow for Turso serverless package' from Pekka Enberg
Closes #2226
2025-07-22 16:12:04 +03:00
Jussi Saurio
72b4318fa1 Merge 'fix raw read frame WAL API' from Nikita Sivukhin
This PR fixes `wal_read_frame_raw` API
Before, implementation of raw read API read only page content - which is
not enough as we also need page_no and size_after fields from the
header. This PR fixes that and also make few adjustments in the
signatures.

Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #2229
2025-07-22 16:10:55 +03:00
Jussi Saurio
fbed082257 Merge 'use default hasher for the sake of determinism' from Nikita Sivukhin
Make `cacheflush` / `commit_dirty_pages` functions deterministic. This
can help to mitigate some simulator non-reproducible seeds + simplify
life a bit.

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #2228
2025-07-22 16:10:43 +03:00
Nikita Sivukhin
b34d081d35 cargo fmt 2025-07-22 16:23:04 +04:00
Nikita Sivukhin
d617d1d21e fix raw read frame WAL API 2025-07-22 16:21:04 +04:00
Nikita Sivukhin
a730136564 use default hasher for the sake of determinism 2025-07-22 16:18:42 +04:00
Pekka Enberg
710b43d2c4 Add Github workflow for Turso serverless package 2025-07-22 13:38:51 +03:00
Pekka Enberg
68169a7b58 Merge 'Work around CREATE TABLE whitespace issue' from Jussi Saurio
`BTreeTable::to_sql` makes us incompatible with SQLite by losing e.g.
the original whitespace provided during the CREATE TABLE command. For
example:
`CREATE TABLE t            (x)` gets saved as `CREATE TABLE t (x)`.
Sqlite preserves the user-provided whitespace.
For now let's fix our tests by regex-replacing every CREATE TABLE in the
entire repo to have exactly 1 space after the table name in the CREATE
TABLE statement.
I also added a FIXME in the doc comment for `BTreeTable::to_sql`, and
opened https://github.com/tursodatabase/turso/issues/2224

Closes #2223
2025-07-22 13:38:20 +03:00
Jussi Saurio
022f679fab chore: make every CREATE TABLE stmt in entire repo have 1 space after tbl name
`BTreeTable::to_sql` makes us incompatible with SQLite by losing e.g. the original whitespace provided during the CREATE TABLE command.

For now let's fix our tests by regex-replacing every CREATE TABLE in
the entire repo to have exactly 1 space after the table name in the
CREATE TABLE statement.
2025-07-22 11:35:21 +03:00
Pekka Enberg
f83870731d testing/sqlite3: Import ALTER TABLE tests
Imported from SQLite 3.50.2
2025-07-22 11:27:52 +03:00
Pekka Enberg
d634a28890 Merge 'Fix issues in alter table tests and fix a small error in BTreeTable::to_sql' from Jussi Saurio
Closes #2186
```git
commit 2095e5dde1 (HEAD -> fix-alter-table, origin/fix-alter-table, main)
Author: Jussi Saurio <jussi.saurio@gmail.com>
Date:   Tue Jul 22 11:12:02 2025 +0300

    test/tcl: run alter_table.test in all.test

commit 58f98391d0
Author: Jussi Saurio <jussi.saurio@gmail.com>
Date:   Tue Jul 22 11:11:50 2025 +0300

    test/tcl: fix alter table test assertions and require index flag on some tests

commit 13d40c6a73
Author: Jussi Saurio <jussi.saurio@gmail.com>
Date:   Tue Jul 22 11:11:08 2025 +0300

    schema: fix extra whitespace in BTreeTable::from_sql
```

Closes #2222
2025-07-22 11:20:48 +03:00
Pekka Enberg
eab71d2ee2 Update README.md 2025-07-22 11:20:01 +03:00
Pekka Enberg
b6d3371d46 Update README.md 2025-07-22 11:12:46 +03:00
Jussi Saurio
2095e5dde1 test/tcl: run alter_table.test in all.test 2025-07-22 11:12:02 +03:00
Jussi Saurio
58f98391d0 test/tcl: fix alter table test assertions and require index flag on some tests 2025-07-22 11:11:50 +03:00
Jussi Saurio
13d40c6a73 schema: fix extra whitespace in BTreeTable::from_sql 2025-07-22 11:11:08 +03:00
Pekka Enberg
0a6f99f3d9 Merge 'Add Rickrolling Turso blog post to contrib' from Avinash Sajjanshetty
Closes #2218
2025-07-22 11:10:31 +03:00
Avinash Sajjanshetty
4e91ed1201 Add Rickrolling Turso blog post to contrib 2025-07-22 13:07:33 +05:30
Pekka Enberg
f10723779d bindings/javascript: Add Drizzle example 2025-07-22 10:35:30 +03:00
Pekka Enberg
f979903633 Merge 'Load static extensions once and store on Database instead of once per connection' from Preston Thorpe
To help make the connection speed faster, we don't need to register
these every time as they are compiled in.

Closes #2213
2025-07-22 09:16:22 +03:00
Pekka Enberg
830f3c7276 Fix typo in insert.ts 2025-07-22 09:12:30 +03:00
Pekka Enberg
30de02d526 testing: Add test case for duplicate SET in UPDATE
Refs #2117
2025-07-22 09:08:14 +03:00
Pekka Enberg
afc22da07c Merge 'Fix duplicate SET statement compatibility with SQLite' from Ihor Andrianov
For duplicate SET statements, SQLite uses last one

Closes #2117
2025-07-22 09:02:16 +03:00
Pekka Enberg
b03fe4669a Merge 'wal: write txn fail in case max_frame change midway' from Pere Diaz Bou
A write txn can only start if the current snapshot held by writer is
consistent with the one in shared state

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #2204
2025-07-22 09:01:44 +03:00
Pekka Enberg
c1ee91f500 Merge 'Add @tursodatabase/serverless package' from Pekka Enberg
This package is for serverless access to the Turso Cloud using SQL over
HTTP protocol. The purpose of this package is to provide the same
interface as `@tursodatabase/turso`, but for serverless environments
that cannot host the database engine.
The package also provides a `@libsql/client` compatibility layer in the
`@tursodatabase/serverless/compat` module for drop-in replacement for
existing clients.

Closes #2209
2025-07-22 08:56:22 +03:00
PThorpe92
a92126961d Remove duplicate case and just send Mutex along with schema for extension registrations 2025-07-21 20:06:14 -04:00
PThorpe92
d514304ac2 Remove unneeded changes 2025-07-21 19:24:24 -04:00