1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Regression tests for Database::reset() catalog and index metadata clearing
//!
//! This module tests that Database::reset() properly clears:
//! - Catalog metadata (tables, schemas, indexes)
//! - User-defined B-tree indexes
//! - Spatial indexes
//!
//! This prevents cross-file contamination when reusing Database instances
//! (e.g., in SQLLogicTest pooled DB adapter).
use vibesql_ast::{IndexColumn, OrderDirection};
use vibesql_catalog::{ColumnSchema, TableSchema};
use vibesql_types::{DataType, SqlValue};
use crate::{Database, Row};
#[test]
fn test_reset_clears_catalog_and_indexes() {
let mut db = Database::new();
// ============================================================================
// FIRST USAGE: Create table with index, insert data
// ============================================================================
// Create a table
let schema = TableSchema::new(
"users".to_string(),
vec![
ColumnSchema::new("id".to_string(), DataType::Integer, false),
ColumnSchema::new(
"name".to_string(),
DataType::Varchar { max_length: Some(255) },
false,
),
],
);
db.create_table(schema.clone()).unwrap();
// Create an index on the table
db.create_index(
"idx_users_id".to_string(),
"users".to_string(),
false,
vec![IndexColumn::Column {
column_name: "id".to_string(),
direction: OrderDirection::Asc,
prefix_length: None,
}],
)
.unwrap();
// Insert some data
db.insert_row(
"users",
Row::new(vec![SqlValue::Integer(1), SqlValue::Varchar(arcstr::ArcStr::from("Alice"))]),
)
.unwrap();
db.insert_row(
"users",
Row::new(vec![SqlValue::Integer(2), SqlValue::Varchar(arcstr::ArcStr::from("Bob"))]),
)
.unwrap();
// Verify table and index exist
assert!(db.get_table("users").is_some());
assert!(db.index_exists("idx_users_id"));
assert_eq!(db.list_tables().len(), 1);
assert_eq!(db.list_indexes().len(), 1);
// ============================================================================
// RESET: Clear everything
// ============================================================================
db.reset();
// Verify catalog is cleared
assert_eq!(db.list_tables().len(), 0, "Catalog should be empty after reset");
assert_eq!(db.list_indexes().len(), 0, "All indexes should be cleared after reset");
assert!(db.get_table("users").is_none(), "Table should not exist after reset");
assert!(!db.index_exists("idx_users_id"), "Index should not exist after reset");
// ============================================================================
// SECOND USAGE: Recreate same table with index (different data)
// ============================================================================
// Recreate the same table (should not conflict with stale catalog metadata)
db.create_table(schema.clone()).unwrap();
// Recreate the same index (should not conflict with stale index metadata)
db.create_index(
"idx_users_id".to_string(),
"users".to_string(),
false,
vec![IndexColumn::Column {
column_name: "id".to_string(),
direction: OrderDirection::Asc,
prefix_length: None,
}],
)
.unwrap();
// Insert different data
db.insert_row(
"users",
Row::new(vec![SqlValue::Integer(10), SqlValue::Varchar(arcstr::ArcStr::from("Charlie"))]),
)
.unwrap();
db.insert_row(
"users",
Row::new(vec![SqlValue::Integer(20), SqlValue::Varchar(arcstr::ArcStr::from("Diana"))]),
)
.unwrap();
// Verify new table and index work correctly
assert!(db.get_table("users").is_some());
assert!(db.index_exists("idx_users_id"));
let table = db.get_table("users").unwrap();
assert_eq!(table.row_count(), 2, "Table should have only new rows after reset");
// Verify index was recreated successfully (old index metadata was cleared)
// If stale index metadata existed, creating the index would have failed or returned wrong
// results
let index = db.get_index("idx_users_id").unwrap();
// Just verify that index was successfully recreated - exact table name format may vary
assert_eq!(index.columns.len(), 1, "Index should have exactly one column");
}
#[test]
fn test_reset_clears_spatial_indexes() {
let mut db = Database::new();
// ============================================================================
// FIRST USAGE: Create table with spatial index
// ============================================================================
let schema = TableSchema::new(
"locations".to_string(),
vec![
ColumnSchema::new("id".to_string(), DataType::Integer, false),
ColumnSchema::new("point".to_string(), DataType::CharacterLargeObject, false), /* Using CLOB as placeholder for geometry */
],
);
db.create_table(schema.clone()).unwrap();
// Create a spatial index
use crate::{database::operations::SpatialIndexMetadata, index::SpatialIndex};
let spatial_metadata = SpatialIndexMetadata {
index_name: "idx_locations_point".to_string(),
table_name: "locations".to_string(),
column_name: "point".to_string(),
schema: "main".to_string(),
created_at: None,
};
let spatial_index = SpatialIndex::new("point".to_string());
db.create_spatial_index(spatial_metadata, spatial_index).unwrap();
// Verify spatial index exists
assert!(db.spatial_index_exists("idx_locations_point"));
assert_eq!(db.list_spatial_indexes().len(), 1);
// ============================================================================
// RESET: Clear everything
// ============================================================================
db.reset();
// Verify spatial index is cleared
assert!(
!db.spatial_index_exists("idx_locations_point"),
"Spatial index should not exist after reset"
);
assert_eq!(
db.list_spatial_indexes().len(),
0,
"All spatial indexes should be cleared after reset"
);
// ============================================================================
// SECOND USAGE: Recreate same spatial index
// ============================================================================
db.create_table(schema.clone()).unwrap();
let spatial_metadata2 = SpatialIndexMetadata {
index_name: "idx_locations_point".to_string(),
table_name: "locations".to_string(),
column_name: "point".to_string(),
schema: "main".to_string(),
created_at: None,
};
let spatial_index2 = SpatialIndex::new("point".to_string());
db.create_spatial_index(spatial_metadata2, spatial_index2).unwrap();
// Verify new spatial index works
assert!(db.spatial_index_exists("idx_locations_point"));
assert_eq!(db.list_spatial_indexes().len(), 1);
}
#[test]
fn test_reset_preserves_database_config() {
use std::path::PathBuf;
use crate::DatabaseConfig;
// Create database with custom config and path
let config = DatabaseConfig::server_default();
let path = PathBuf::from("/tmp/test_db");
let mut db = Database::with_path_and_config(path.clone(), config.clone());
// Create some data
let schema = TableSchema::new(
"test".to_string(),
vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
);
db.create_table(schema).unwrap();
// Reset should clear data but preserve config
db.reset();
// Verify config is preserved by creating a new index and checking it works
// (If config was lost, disk-backed indexes wouldn't work correctly)
let schema2 = TableSchema::new(
"test2".to_string(),
vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
);
db.create_table(schema2).unwrap();
db.create_index(
"idx_test2".to_string(),
"test2".to_string(),
false,
vec![IndexColumn::Column {
column_name: "id".to_string(),
direction: OrderDirection::Asc,
prefix_length: None,
}],
)
.unwrap();
// If config was lost, this would fail
assert!(db.index_exists("idx_test2"));
}
#[test]
fn test_reset_multiple_tables_and_indexes() {
let mut db = Database::new();
// ============================================================================
// FIRST USAGE: Create multiple tables with multiple indexes
// ============================================================================
for i in 0..3 {
let table_name = format!("table{}", i);
let schema = TableSchema::new(
table_name.clone(),
vec![
ColumnSchema::new("id".to_string(), DataType::Integer, false),
ColumnSchema::new(
"value".to_string(),
DataType::Varchar { max_length: Some(255) },
false,
),
],
);
db.create_table(schema).unwrap();
// Create 2 indexes per table
for j in 0..2 {
let index_name = format!("idx_{}_{}", table_name, j);
let col_name = if j == 0 { "id" } else { "value" };
db.create_index(
index_name,
table_name.clone(),
false,
vec![IndexColumn::Column {
column_name: col_name.to_string(),
direction: OrderDirection::Asc,
prefix_length: None,
}],
)
.unwrap();
}
// Insert data
db.insert_row(
&table_name,
Row::new(vec![
SqlValue::Integer(i as i64),
SqlValue::Varchar(arcstr::ArcStr::from(format!("value{}", i))),
]),
)
.unwrap();
}
// Verify all tables and indexes exist
assert_eq!(db.list_tables().len(), 3);
assert_eq!(db.list_indexes().len(), 6); // 3 tables * 2 indexes each
// ============================================================================
// RESET: Clear everything
// ============================================================================
db.reset();
// Verify everything is cleared
assert_eq!(db.list_tables().len(), 0);
assert_eq!(db.list_indexes().len(), 0);
// ============================================================================
// SECOND USAGE: Create different tables/indexes
// ============================================================================
let new_schema = TableSchema::new(
"new_table".to_string(),
vec![ColumnSchema::new("col".to_string(), DataType::Integer, false)],
);
db.create_table(new_schema).unwrap();
assert_eq!(db.list_tables().len(), 1);
assert_eq!(db.list_tables()[0], "new_table");
}