Commit Graph

279 Commits

Author SHA1 Message Date
Pekka Enberg
d808db6af9 core: Switch to parking_lot::Mutex
It's faster and we eliminate bunch of unwrap() calls.
2025-11-20 10:42:02 +02:00
Jussi Saurio
5c1ebbd011 Use VecDeque for trigger storage for similar reasons as indexes do 2025-11-18 15:19:01 +02:00
Jussi Saurio
d4b487eebc triggers: add in-memory schema entries 2025-11-18 12:14:27 +02:00
Jussi Saurio
693eaeb851 Merge 'Add ColDef struct to make schema::Column creation more ergonomic' from Preston Thorpe
RE: #3970
That Column::new having 14 boolean arguments was not great.
Also this removes the unneeded `parent_cols: Vec<String>` from
`ResolvedFkRef`

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

Closes #3973
2025-11-17 09:17:56 +02:00
PThorpe92
ca783ccdff Add ColDef struct to make schema::Column creation more ergonomic 2025-11-16 19:10:42 -05:00
Pavan-Nambi
3de37d3f64 dont validate fkey on parent add tests
correct msitake and add null test issue

add fkey test
2025-11-16 09:41:57 +05:30
Pere Diaz Bou
7c96b6d9f9 Merge 'Fix: Drop internal DBSP table when dropping materialized view' from Martin Mauch
# Fix: Clean up DBSP state table when dropping materialized views
## Problem
When dropping a materialized view, the internal DBSP state table (e.g.,
`__turso_internal_dbsp_state_v1_view_name`) and its automatic primary
key index were not being properly cleaned up. This caused two issues:
1. **Persistent schema entries**: The DBSP table and index entries
remained in `sqlite_schema` after dropping the view
2. **In-memory schema inconsistency**: The DBSP table remained in the
in-memory schema's `tables` HashMap, causing "table already exists"
errors when trying to recreate a materialized view with the same name
## Root Cause
The issue had two parts:
1. **Missing sqlite_schema cleanup**: The `translate_drop_view` function
deleted the view entry from `sqlite_schema` but didn't delete the
associated DBSP state table and index entries
2. **Missing in-memory schema cleanup**: The `remove_view` function
removed the materialized view from the in-memory schema but didn't
remove the DBSP state table and its indexes
## Solution
### Changes in `core/translate/view.rs`
- Added a second pass loop in `translate_drop_view` to scan
`sqlite_schema` and delete DBSP table and index entries
- The loop checks for entries matching the DBSP table name pattern
(`__turso_internal_dbsp_state_v{version}_{view_name}`) and the automatic
index name pattern (`sqlite_autoindex___turso_internal_dbsp_state_v{vers
ion}_{view_name}_1`)
- Registers for comparison values are allocated outside the loop for
efficiency
- Column registers are reused across loop iterations
### Changes in `core/schema.rs`
- Updated `remove_view` to also remove the DBSP state table and its
indexes from the in-memory schema's `tables` HashMap and `indexes`
collection
- This ensures consistency between the persistent schema
(`sqlite_schema`) and the in-memory schema
### Tests Added
Added two new test cases in `testing/materialized_views.test`:
1. **`matview-drop-cleans-up-dbsp-table`**: Explicitly verifies that
after dropping a materialized view:
   - The view entry is removed from `sqlite_schema`
   - The DBSP state table entry is removed from `sqlite_schema`
   - The DBSP state index entry is removed from `sqlite_schema`
2. **`matview-recreate-after-drop`**: Verifies that a materialized view
can be successfully recreated after being dropped, which implicitly
tests that all underlying resources (including DBSP tables) are properly
cleaned up
## Testing
- All existing materialized view tests pass
- New tests specifically verify the cleanup behavior
- Manual testing confirms that materialized views can be dropped and
recreated without errors
## Related
This fix ensures that materialized views can be safely dropped and
recreated, resolving issues where the DBSP state table would persist and
cause conflicts.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3928
2025-11-12 17:16:04 +01:00
Pekka Enberg
7891be96fd Merge 'Refactor affinity conversions for reusability' from Pedro Muniz
Depends on #3920
Moves some code around so it is easier to reuse and less cluttered in
`execute.rs`, and changes how `compare` works. Instead of mutating some
register, we now just return the possible `ValueRef` representation of
that affinity. This allows other parts of the codebase to reuse this
logic without needing to have an owned `Value` or a `&mut Register`

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

Closes #3923
2025-11-10 09:02:22 +02:00
Pekka Enberg
2be515247f Merge 'Create AsValueRef trait to allow us to be agnostic over ownership of Value or ValueRef' from Pedro Muniz
Depends on #3919
Also change `op_compare` to reuse the same compare_immutable logic
First step to finish #2304

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

Closes #3920
2025-11-10 09:01:59 +02:00
Martin Mauch
4d1fdd951b Fix: Drop internal DBSP table when dropping materialized view 2025-11-08 20:28:20 +01:00
PThorpe92
a012e98bfa core/translate remove unused ParamState and some minor refactoring 2025-11-07 19:18:10 -05:00
pedrocarlo
9f350f7fd9 change Text variant in ValueRef to hold a TextRef that can automatically convert to &str avoiding string allocations everywhere 2025-11-07 12:47:39 -03:00
pedrocarlo
61036d5f51 move affinity handling to separate file 2025-11-07 12:47:39 -03:00
PThorpe92
481d86f567 Optimize and refactor schema::Column type 2025-11-02 20:46:02 -05:00
Nikita Sivukhin
05f0ee6a72 add more integration in order to properly skip backing_btree index_method 2025-10-27 17:00:26 +04:00
Nikita Sivukhin
408ca235d1 small refactoring 2025-10-27 12:43:38 +04:00
Pere Diaz Bou
3227caaa1d Merge 'core: move BTreeCursor under MVCC cursor' from Pere Diaz Bou
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3756
2025-10-21 19:20:49 +02:00
Pere Diaz Bou
ea04e9033a core/mvcc: add btree_cursor under MVCC cursor 2025-10-21 18:22:37 +02:00
PThorpe92
7c746e476c Fix to_sql method on BTreeTable to not double write primary keys 2025-10-21 09:45:42 -04:00
Pekka Enberg
6139dde081 Revert "Merge 'core/translate: fix ALTER COLUMN to propagate other constraint references' from Preston Thorpe"
This reverts commit 1151f49ff4, reversing
changes made to f4da2194f4.
2025-10-21 16:00:04 +03:00
PThorpe92
4dcabf37f1 Fix to_sql method on BTreeTable to include foreign keys 2025-10-18 13:22:48 -04:00
PThorpe92
edaa1b675e Prevent column definitions on CREATE TABLE or opening DB with ON CONFLICT on column def 2025-10-16 15:45:20 -04:00
PThorpe92
9de9927b52 fix clippy warning 2025-10-16 14:47:40 -04:00
PThorpe92
75e86b2c20 Throw parse error on GENERATED constraint in create table when opening new db 2025-10-16 14:26:58 -04:00
PThorpe92
f033af4a93 Throw parse error on CHECK constraint in create table when opening new db 2025-10-16 13:30:05 -04:00
Jussi Saurio
4e34c6be51 Merge 'names shall not be shared between tables,indexs,vtabs,views' from Pavan Nambi
closes #3675

Closes #3681
2025-10-14 07:30:37 +03:00
pedrocarlo
83dde9b55c fix backwards compatible rowid alias behaviour 2025-10-13 20:41:45 -03:00
Pavan-Nambi
36bf88119f add tests
clippy

expect err to make clippy happy

cleanup
2025-10-12 16:38:12 +05:30
Pavan-Nambi
88d5ee0cf1 names shall not be shared between tables,index,vtabs,views 2025-10-12 05:45:59 +05:30
Pere Diaz Bou
160a84250e core: add CursorTrait imports where needed 2025-10-10 15:04:15 +02:00
PThorpe92
a232e3cc7a Implement proper handling of deferred foreign keys 2025-10-07 16:45:23 -04:00
PThorpe92
fa23cedbbe Add helper to pragma to parse enabled opts and fix schema parsing for foreign key constraints 2025-10-07 16:45:22 -04:00
PThorpe92
37c8abf247 Fix schema representation and methods for ForeignKey resolution 2025-10-07 16:45:22 -04:00
PThorpe92
ae975afe49 Remove unnecessary FK resolution on schema parsing 2025-10-07 16:45:16 -04:00
PThorpe92
346e6fedfa Create ForeignKey, ResolvedFkRef types and FK resolution 2025-10-07 16:27:49 -04:00
Levy A.
77a412f6af refactor: remove unsafe reference semantics from RefValue
also renames `RefValue` to `ValueRef`, to align with rusqlite and other
crates
2025-10-07 10:43:44 -03:00
Nikita Sivukhin
8dae601fac make rollback non-failing method 2025-10-06 13:21:45 +04:00
Pekka Enberg
dc1463c70d Merge 'Improve error handling for cyclic views' from Duy Dang
The cycle is detected by marking a seen view, if a seen view is process
again, that's a cycle and we throw an error.
Close #3404

Closes #3467
2025-10-02 19:33:12 +03:00
Duy Dang
5ceab1b3f4 Circle detection for views 2025-10-01 02:12:21 +07:00
Jussi Saurio
7c897d382f Implement MvTableId newtype for better type safety of table ids 2025-09-30 16:54:22 +03:00
Jussi Saurio
35b584f050 Merge 'core: change root_page to i64' from Pere Diaz Bou
Closes #3454
2025-09-30 12:50:23 +03:00
Pere Diaz Bou
0f631101df core: change page idx type from usize to i64
MVCC is like the annoying younger cousin (I know because I was him) that
needs to be treated differently. MVCC requires us to use root_pages that
might not be allocated yet, and the plan is to use negative root_pages
for that case. Therefore, we need i64 in order to fit this change.
2025-09-29 18:38:43 +02:00
Nikita Sivukhin
7a1da051d0 remove usage of expr.to_string() and fix normalize util test 2025-09-29 11:36:12 +04:00
Nikita Sivukhin
86a95e813d Merge branch 'main' into quoting-fix-attempt-2 2025-09-29 10:58:51 +04:00
Jussi Saurio
618cf2a4e7 Merge 'core/translate: rewrite default column value from identifier to string literal' from Preston Thorpe
closes #3390
closes #3389

Closes #3403
2025-09-28 08:07:11 +03:00
PThorpe92
03046faccb Translate default value Expr::Id to Literal to allow for identifier in col def 2025-09-27 13:56:04 -04:00
PThorpe92
b7fc9fef40 Persist NOT NULL column constraint to schema table 2025-09-27 13:12:19 -04:00
PThorpe92
5d8a735aaf fix clippy error 2025-09-26 18:06:09 -04:00
PThorpe92
e52aa1538e Remove unused BTreeTable method for checking single field on Column in schema 2025-09-26 17:32:51 -04:00
Nikita Sivukhin
2f4d76ec6d remove pattern matching over Name::Quoted 2025-09-26 13:01:49 +04:00