shellql 0.1.6-beta

A Vim- and tmux-inspired terminal database manager for developers
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::env;

use shellql::connection::connect_db;
use shellql::connection::models::ConnectionSource;
use shellql::connection::models::DatabaseString;
use shellql::connection::{
    count_rows, delete_rows, filter_rows, insert_row, list_tables, query_rows, table_rows,
    table_schema, update_cell,
};

// ── Helpers ───────────────────────────────────────────────────────────────────

fn pg_url() -> String {
    env::var("TEST_POSTGRES_URL")
        .unwrap_or_else(|_| "postgres://shellql:shellql@localhost:5433/shellql_test".to_string())
}

fn mysql_url() -> String {
    env::var("TEST_MYSQL_URL")
        .unwrap_or_else(|_| "mysql://shellql:shellql@localhost:3307/shellql_test".to_string())
}

fn sqlite_url() -> String {
    env::var("TEST_SQLITE_URL")
        .unwrap_or_else(|_| "sqlite://./tests/docker/sqlite/seed.db".to_string())
}

async fn pg_pool() -> shellql::connection::models::DbPool {
    connect_db(ConnectionSource::Url(DatabaseString::Postgres(pg_url())))
        .await
        .expect("Failed to connect to Postgres test DB — is `docker compose -f tests/docker-compose.yml up` running?")
}

async fn mysql_pool() -> shellql::connection::models::DbPool {
    connect_db(ConnectionSource::Url(DatabaseString::Mysql(mysql_url())))
        .await
        .expect("Failed to connect to MySQL test DB — is `docker compose -f tests/docker-compose.yml up` running?")
}

async fn sqlite_pool() -> shellql::connection::models::DbPool {
    connect_db(ConnectionSource::Url(DatabaseString::Sqlite(sqlite_url())))
        .await
        .expect("Failed to connect to SQLite test DB")
}

// ── list_tables ───────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_list_tables_postgres() {
    let pool = pg_pool().await;
    let tables = list_tables(&pool).await.expect("list_tables should work");
    assert!(
        tables.contains(&"employees".to_string()),
        "expected employees table in Postgres"
    );
}

#[tokio::test]
#[serial_test::serial]
async fn test_list_tables_mysql() {
    let pool = mysql_pool().await;
    let tables = list_tables(&pool).await.expect("list_tables should work");
    assert!(
        tables.contains(&"employees".to_string()),
        "expected employees table in MySQL"
    );
}

#[tokio::test]
#[serial_test::serial]
async fn test_list_tables_sqlite() {
    let pool = sqlite_pool().await;
    let tables = list_tables(&pool).await.expect("list_tables should work");
    assert!(
        tables.contains(&"employees".to_string()),
        "expected employees table in SQLite"
    );
}

// ── table_schema ──────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_table_schema_postgres() {
    let pool = pg_pool().await;
    let schema = table_schema(&pool, "employees")
        .await
        .expect("schema should work");
    let cols: Vec<String> = schema.iter().map(|c| c.name.clone()).collect();
    assert!(cols.contains(&"emp_no".to_string()));
    assert!(cols.contains(&"first_name".to_string()));
    assert!(cols.contains(&"email".to_string()));

    let pk = schema.iter().find(|c| c.name == "emp_no").unwrap();
    assert!(pk.is_primary_key, "emp_no should be PK");
}

#[tokio::test]
#[serial_test::serial]
async fn test_table_schema_mysql() {
    let pool = mysql_pool().await;
    let schema = table_schema(&pool, "employees")
        .await
        .expect("schema should work");
    let cols: Vec<String> = schema.iter().map(|c| c.name.clone()).collect();
    assert!(cols.contains(&"emp_no".to_string()));

    let pk = schema.iter().find(|c| c.name == "emp_no").unwrap();
    assert!(pk.is_primary_key, "emp_no should be PK");
}

#[tokio::test]
#[serial_test::serial]
async fn test_table_schema_sqlite() {
    let pool = sqlite_pool().await;
    let schema = table_schema(&pool, "employees")
        .await
        .expect("schema should work");
    let cols: Vec<String> = schema.iter().map(|c| c.name.clone()).collect();
    assert!(cols.contains(&"emp_no".to_string()));

    let pk = schema.iter().find(|c| c.name == "emp_no").unwrap();
    assert!(pk.is_primary_key, "emp_no should be PK");
}

// ── table_rows (with offset + limit) ──────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_table_rows_paginated_postgres() {
    let pool = pg_pool().await;
    let (cols, rows) = table_rows(&pool, "employees", 10, 0)
        .await
        .expect("table_rows should work");
    assert_eq!(cols.len(), 7);
    assert_eq!(rows.len(), 10);

    let (_, rows2) = table_rows(&pool, "employees", 10, 5)
        .await
        .expect("offset should work");
    assert_eq!(rows2.len(), 10);
    // Row 5 from the first query should match row 0 from the second
    assert_eq!(rows[5], rows2[0]);
}

#[tokio::test]
#[serial_test::serial]
async fn test_table_rows_paginated_mysql() {
    let pool = mysql_pool().await;
    let (cols, rows) = table_rows(&pool, "employees", 10, 0)
        .await
        .expect("table_rows should work");
    assert_eq!(cols.len(), 7);
    assert_eq!(rows.len(), 10);
}

#[tokio::test]
#[serial_test::serial]
async fn test_table_rows_paginated_sqlite() {
    let pool = sqlite_pool().await;
    let (cols, rows) = table_rows(&pool, "employees", 10, 0)
        .await
        .expect("table_rows should work");
    assert_eq!(cols.len(), 7);
    assert_eq!(rows.len(), 10);
}

// ── count_rows ────────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_count_rows_postgres() {
    let pool = pg_pool().await;
    let count = count_rows(&pool, "employees")
        .await
        .expect("count_rows should work");
    assert_eq!(count, 100);
}

#[tokio::test]
#[serial_test::serial]
async fn test_count_rows_mysql() {
    let pool = mysql_pool().await;
    let count = count_rows(&pool, "employees")
        .await
        .expect("count_rows should work");
    assert_eq!(count, 100);
}

#[tokio::test]
#[serial_test::serial]
async fn test_count_rows_sqlite() {
    let pool = sqlite_pool().await;
    let count = count_rows(&pool, "employees")
        .await
        .expect("count_rows should work");
    assert_eq!(count, 50); // SQLite seed has 50 rows
}

// ── filter_rows ───────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_filter_rows_postgres() {
    let pool = pg_pool().await;
    let (cols, rows) = filter_rows(&pool, "employees", "first_name", "Georgi", 10, 0)
        .await
        .expect("filter_rows should work");
    assert!(!rows.is_empty());
    let first_name_idx = cols.iter().position(|c| c == "first_name").unwrap();
    assert!(rows[0][first_name_idx].contains("Georgi"));
}

#[tokio::test]
#[serial_test::serial]
async fn test_filter_rows_mysql() {
    let pool = mysql_pool().await;
    let (_cols, rows) = filter_rows(&pool, "employees", "first_name", "Georgi", 10, 0)
        .await
        .expect("filter_rows should work");
    assert!(!rows.is_empty());
}

#[tokio::test]
#[serial_test::serial]
async fn test_filter_rows_sqlite() {
    let pool = sqlite_pool().await;
    let (_cols, rows) = filter_rows(&pool, "employees", "first_name", "Georgi", 10, 0)
        .await
        .expect("filter_rows should work");
    assert!(!rows.is_empty());
}

// ── update_cell ───────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_update_cell_postgres() {
    let pool = pg_pool().await;
    let affected = update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "updated@example.com",
    )
    .await
    .expect("update_cell should work");
    assert_eq!(affected, 1);

    // Verify updated row by PK (table_rows order is not deterministic).
    let (headers, rows) = query_rows(
        &pool,
        "employees",
        Some("emp_no = 10001"),
        None,
        false,
        Some(&["email".to_string()]),
        10,
        0,
    )
    .await
    .unwrap();
    assert_eq!(headers, vec!["email".to_string()]);
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0][0], "updated@example.com");

    // Restore
    update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "georgi.facello@example.com",
    )
    .await
    .unwrap();
}

#[tokio::test]
#[serial_test::serial]
async fn test_update_cell_mysql() {
    let pool = mysql_pool().await;
    let affected = update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "updated@example.com",
    )
    .await
    .expect("update_cell should work");
    assert_eq!(affected, 1);

    // Restore
    update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "georgi.facello@example.com",
    )
    .await
    .unwrap();
}

#[tokio::test]
#[serial_test::serial]
async fn test_update_cell_sqlite() {
    let pool = sqlite_pool().await;
    let affected = update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "updated@example.com",
    )
    .await
    .expect("update_cell should work");
    assert_eq!(affected, 1);

    // Restore
    update_cell(
        &pool,
        "employees",
        "emp_no",
        "10001",
        "email",
        "georgi.facello@example.com",
    )
    .await
    .unwrap();
}

// ── insert_row ────────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_insert_row_postgres() {
    let pool = pg_pool().await;
    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
        "email".to_string(),
    ];
    let vals = vec![
        "99999".to_string(),
        "1990-01-01".to_string(),
        "Test".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
        "test@example.com".to_string(),
    ];
    let affected = insert_row(&pool, "employees", &cols, &vals)
        .await
        .expect("insert_row should work");
    assert_eq!(affected, 1);

    // Cleanup
    delete_rows(&pool, "employees", "emp_no", &["99999".to_string()])
        .await
        .unwrap();
}

#[tokio::test]
#[serial_test::serial]
async fn test_insert_row_mysql() {
    let pool = mysql_pool().await;
    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
        "email".to_string(),
    ];
    let vals = vec![
        "99999".to_string(),
        "1990-01-01".to_string(),
        "Test".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
        "test@example.com".to_string(),
    ];
    let affected = insert_row(&pool, "employees", &cols, &vals)
        .await
        .expect("insert_row should work");
    assert_eq!(affected, 1);

    // Cleanup
    delete_rows(&pool, "employees", "emp_no", &["99999".to_string()])
        .await
        .unwrap();
}

#[tokio::test]
#[serial_test::serial]
async fn test_insert_row_sqlite() {
    let pool = sqlite_pool().await;
    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
        "email".to_string(),
    ];
    let vals = vec![
        "99999".to_string(),
        "1990-01-01".to_string(),
        "Test".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
        "test@example.com".to_string(),
    ];
    let affected = insert_row(&pool, "employees", &cols, &vals)
        .await
        .expect("insert_row should work");
    assert_eq!(affected, 1);

    // Cleanup
    delete_rows(&pool, "employees", "emp_no", &["99999".to_string()])
        .await
        .unwrap();
}

// ── delete_rows ───────────────────────────────────────────────────────────────

#[tokio::test]
#[serial_test::serial]
async fn test_delete_rows_postgres() {
    let pool = pg_pool().await;

    // Insert a row to delete
    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
    ];
    let vals = vec![
        "88888".to_string(),
        "1990-01-01".to_string(),
        "ToDelete".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
    ];
    insert_row(&pool, "employees", &cols, &vals).await.unwrap();

    let affected = delete_rows(&pool, "employees", "emp_no", &["88888".to_string()])
        .await
        .expect("delete_rows should work");
    assert_eq!(affected, 1);

    // Verify target row is gone regardless of baseline table size.
    let (_headers, rows) = query_rows(
        &pool,
        "employees",
        Some("emp_no = 88888"),
        None,
        false,
        None,
        10,
        0,
    )
    .await
    .unwrap();
    assert!(rows.is_empty(), "deleted row should not exist");
}

#[tokio::test]
#[serial_test::serial]
async fn test_delete_rows_mysql() {
    let pool = mysql_pool().await;

    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
    ];
    let vals = vec![
        "88888".to_string(),
        "1990-01-01".to_string(),
        "ToDelete".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
    ];
    insert_row(&pool, "employees", &cols, &vals).await.unwrap();

    let affected = delete_rows(&pool, "employees", "emp_no", &["88888".to_string()])
        .await
        .expect("delete_rows should work");
    assert_eq!(affected, 1);

    // Verify target row is gone regardless of baseline table size.
    let (_headers, rows) = query_rows(
        &pool,
        "employees",
        Some("emp_no = 88888"),
        None,
        false,
        None,
        10,
        0,
    )
    .await
    .unwrap();
    assert!(rows.is_empty(), "deleted row should not exist");
}

#[tokio::test]
#[serial_test::serial]
async fn test_delete_rows_sqlite() {
    let pool = sqlite_pool().await;

    let cols = vec![
        "emp_no".to_string(),
        "birth_date".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "gender".to_string(),
        "hire_date".to_string(),
    ];
    let vals = vec![
        "88888".to_string(),
        "1990-01-01".to_string(),
        "ToDelete".to_string(),
        "User".to_string(),
        "M".to_string(),
        "2024-01-01".to_string(),
    ];
    insert_row(&pool, "employees", &cols, &vals).await.unwrap();

    let affected = delete_rows(&pool, "employees", "emp_no", &["88888".to_string()])
        .await
        .expect("delete_rows should work");
    assert_eq!(affected, 1);

    let count = count_rows(&pool, "employees").await.unwrap();
    assert_eq!(count, 50); // back to original count
}