sz-rust-cli 1.4.0

SZ-Rust 命令行工具:项目脚手架、数据库迁移、调度器管理
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-2026 SZ-Rust Team
//
//! CLI DB 集成测试 — 需要 PostgreSQL
//! 运行: cargo test -p sz-rust-cli --test cli_db_integration -- --ignored --test-threads=1

use clap::Parser;
use sz_rust_cli::cmd::admin::AdminCommand;
use sz_rust_cli::cmd::migrate::{execute_migrate, execute_status_full, MigrateArgs};
use sz_rust_cli::cmd::seed::{execute_seed, SeedArgs};

const PG_URL: &str = "postgres://lewuli:JkbC2jsaWAYDe2Gz@127.0.0.1:5433/marketplace_test";

async fn cleanup() {
    let url = std::env::var("MARKETPLACE_PG_URL").unwrap_or_else(|_| PG_URL.to_string());
    let pool = sqlx::PgPool::connect(&url).await.expect("PG 连接失败");
    let _ = sqlx::query("DROP TABLE IF EXISTS install_records CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS review_records CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS plugin_versions CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS plugins CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS developers CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS __migrations CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS role_permissions CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS user_roles CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS permissions CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS menus CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS roles CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS users CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS configs CASCADE")
        .execute(&pool)
        .await;
    let _ = sqlx::query("DROP TABLE IF EXISTS operation_logs CASCADE")
        .execute(&pool)
        .await;
}

fn create_migration_dir(dir: &std::path::Path) {
    std::fs::create_dir_all(dir).unwrap();
    std::fs::write(
        dir.join("001_create_users_up.sql"),
        "CREATE TABLE users (id BIGSERIAL PRIMARY KEY, name VARCHAR(255));",
    )
    .unwrap();
    std::fs::write(
        dir.join("001_create_users_down.sql"),
        "DROP TABLE IF EXISTS users;",
    )
    .unwrap();
    std::fs::write(
        dir.join("002_add_index_up.sql"),
        "CREATE INDEX idx_users_name ON users (name);",
    )
    .unwrap();
    std::fs::write(
        dir.join("002_add_index_down.sql"),
        "DROP INDEX IF EXISTS idx_users_name;",
    )
    .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_migrate_online_applies_migrations() {
    cleanup().await;
    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    let result = execute_migrate(&args).await;
    // execute_with_params 在 AnyPool adapter 未实现,迁移 SQL 已执行但记录插入失败
    assert!(result.is_err(), "应因 execute_with_params 未实现而失败");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("execute_with_params"),
        "错误应含 execute_with_params: {err}"
    );

    // 迁移 SQL 本身已执行(users 表已创建)
    let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
    let exists: (bool,) = sqlx::query_as(
        "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'users')",
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(exists.0, "users 表应已创建(SQL 已执行)");
}

#[tokio::test]
#[ignore]
async fn test_migrate_online_no_pending() {
    cleanup().await;
    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    // 第一次 migrate 会因 execute_with_params 未实现而失败
    let _ = execute_migrate(&args).await;
    // 第二次调用也应到达相同路径
    let result = execute_migrate(&args).await;
    assert!(result.is_err(), "应因 execute_with_params 未实现而失败");
}

#[tokio::test]
#[ignore]
async fn test_migrate_online_rollback() {
    cleanup().await;
    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    // migrate 会因 execute_with_params 失败,但 SQL 已执行
    let _ = execute_migrate(&args).await;

    let rollback_args = MigrateArgs {
        rollback: true,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    let result = execute_migrate(&rollback_args).await;
    // rollback 也使用 delete_migration_record(execute_with_params),预期失败
    assert!(
        result.is_err(),
        "rollback 应因 execute_with_params 未实现而失败"
    );
}

#[tokio::test]
#[ignore]
async fn test_migrate_status_online() {
    cleanup().await;
    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let result = execute_status_full(
        temp.path().to_str().unwrap(),
        "postgres",
        false,
        Some(PG_URL),
    )
    .await;
    assert!(result.is_ok(), "status 在线模式应成功: {:?}", result.err());
}

#[tokio::test]
#[ignore]
async fn test_migrate_status_online_after_apply() {
    cleanup().await;
    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    // migrate 会因 execute_with_params 失败,但 SQL 已执行
    let _ = execute_migrate(&args).await;

    // status 在线模式应成功(不依赖 execute_with_params)
    let result = execute_status_full(
        temp.path().to_str().unwrap(),
        "postgres",
        true,
        Some(PG_URL),
    )
    .await;
    assert!(result.is_ok(), "status 在线模式应成功: {:?}", result.err());
}

#[derive(Parser)]
struct AdminCli {
    #[command(subcommand)]
    cmd: AdminCommand,
}

#[tokio::test]
#[ignore]
async fn test_admin_migrate_online() {
    cleanup().await;
    let cli = AdminCli::parse_from([
        "sz-rust",
        "migrate",
        "--url",
        PG_URL,
        "--db-type",
        "postgres",
    ]);
    let result = sz_rust_cli::cmd::admin::execute(&cli.cmd).await;
    assert!(result.is_ok(), "admin migrate 应成功: {:?}", result.err());

    let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
    let exists: (bool,) = sqlx::query_as(
        "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'roles')",
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(exists.0, "roles 表应已创建");

    let exists: (bool,) = sqlx::query_as(
        "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'users')",
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(exists.0, "users 表应已创建");
}

#[tokio::test]
#[ignore]
async fn test_admin_migrate_show_sql_offline() {
    let cli = AdminCli::parse_from(["sz-rust", "migrate", "--show-sql"]);
    let result = sz_rust_cli::cmd::admin::execute(&cli.cmd).await;
    assert!(result.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_admin_init_offline() {
    let cli = AdminCli::parse_from([
        "sz-rust",
        "init",
        "--username",
        "testadmin",
        "--password",
        "testpass",
    ]);
    let result = sz_rust_cli::cmd::admin::execute(&cli.cmd).await;
    assert!(result.is_ok());
}
fn create_seed_dir(dir: &std::path::Path) {
    std::fs::create_dir_all(dir).unwrap();
    std::fs::write(
        dir.join("001_create_test_table.sql"),
        "CREATE TABLE IF NOT EXISTS seed_test (id INT, name VARCHAR(50));",
    )
    .unwrap();
    std::fs::write(
        dir.join("002_insert_data.sql"),
        "INSERT INTO seed_test VALUES (1, 'hello');",
    )
    .unwrap();
}

#[test]
#[ignore]
fn test_seed_online_executes_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_seed_dir(temp.path());

    let args = SeedArgs {
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
        class: None,
    };
    let result = execute_seed(&args);
    assert!(result.is_ok(), "seed 在线模式应成功: {:?}", result.err());

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM seed_test")
            .fetch_one(&pool)
            .await
            .unwrap();
        assert_eq!(row.0, 1, "应插入 1 条数据");
        let _ = sqlx::query("DROP TABLE IF EXISTS seed_test CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_seed_online_with_show_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_seed_dir(temp.path());

    let args = SeedArgs {
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: true,
        url: Some(PG_URL.to_string()),
        class: None,
    };
    let result = execute_seed(&args);
    assert!(result.is_ok(), "seed 在线模式 show_sql 应成功");

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS seed_test CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_seed_online_with_class_filter() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_seed_dir(temp.path());

    let args = SeedArgs {
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
        class: Some("001_create_test_table".to_string()),
    };
    let result = execute_seed(&args);
    assert!(result.is_ok(), "seed 在线模式 class filter 应成功");

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let exists: (bool,) = sqlx::query_as(
            "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'seed_test')",
        )
        .fetch_one(&pool)
        .await
        .unwrap();
        assert!(exists.0, "seed_test 表应已创建");
        let _ = sqlx::query("DROP TABLE IF EXISTS seed_test CASCADE")
            .execute(&pool)
            .await;
    });
}
#[test]
#[ignore]
fn test_migrate_online_with_show_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: true,
        url: Some(PG_URL.to_string()),
    };
    let result = rt.block_on(async { execute_migrate(&args).await });
    assert!(
        result.is_err(),
        "insert_migration_record 应因 execute_with_params 未实现而失败"
    );
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("execute_with_params"));

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS users CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS __migrations CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_migrate_status_online_with_show_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let result = rt.block_on(async {
        execute_status_full(
            temp.path().to_str().unwrap(),
            "postgres",
            true,
            Some(PG_URL),
        )
        .await
    });
    assert!(result.is_ok(), "migrate:status 在线模式 show_sql 应成功");

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS __migrations CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_migrate_online_rollback_with_show_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    create_migration_dir(temp.path());

    let args = MigrateArgs {
        rollback: false,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: false,
        url: Some(PG_URL.to_string()),
    };
    let result = rt.block_on(async { execute_migrate(&args).await });
    assert!(result.is_err(), "insert_migration_record 应失败");

    let args = MigrateArgs {
        rollback: true,
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "postgres".to_string(),
        show_sql: true,
        url: Some(PG_URL.to_string()),
    };
    let result = rt.block_on(async { execute_migrate(&args).await });
    assert!(result.is_err(), "rollback 也应因 execute_with_params 失败");

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS users CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS __migrations CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_admin_migrate_online_with_show_sql() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    let original = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp.path()).unwrap();

    let cli = sz_rust_cli::Cli::parse_from([
        "sz-rust",
        "admin",
        "migrate",
        "--url",
        PG_URL,
        "--show-sql",
    ]);
    let result = rt.block_on(async { cli.execute().await });
    std::env::set_current_dir(&original).unwrap();
    assert!(
        result.is_ok(),
        "admin migrate --show-sql 在线应成功: {:?}",
        result.err()
    );

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS roles CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS permissions CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS menus CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS users CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS configs CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS operation_logs CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS role_permissions CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS user_roles CASCADE")
            .execute(&pool)
            .await;
    });
}
#[test]
#[ignore]
fn test_admin_init_online() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { cleanup().await });

    let temp = tempfile::tempdir().unwrap();
    let original = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp.path()).unwrap();

    let cli = sz_rust_cli::Cli::parse_from([
        "sz-rust",
        "admin",
        "init",
        "--url",
        PG_URL,
        "--username",
        "admin",
        "--password",
        "admin123",
    ]);
    let result = rt.block_on(async { cli.execute().await });
    std::env::set_current_dir(&original).unwrap();
    assert!(result.is_err(), "execute_with_params 未实现应失败");

    rt.block_on(async {
        let pool = sqlx::PgPool::connect(PG_URL).await.unwrap();
        let _ = sqlx::query("DROP TABLE IF EXISTS roles CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS permissions CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS users CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS role_permissions CASCADE")
            .execute(&pool)
            .await;
        let _ = sqlx::query("DROP TABLE IF EXISTS user_roles CASCADE")
            .execute(&pool)
            .await;
    });
}

#[test]
#[ignore]
fn test_seed_online_oracle_unsupported() {
    let temp = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(temp.path()).unwrap();
    std::fs::write(
        temp.path().join("001_test.sql"),
        "INSERT INTO t VALUES (1);",
    )
    .unwrap();

    let args = SeedArgs {
        path: temp.path().to_str().unwrap().to_string(),
        db_type: "oracle".to_string(),
        show_sql: false,
        url: Some("oracle://user:pass@localhost:1521/db".to_string()),
        class: None,
    };
    let result = execute_seed(&args);
    assert!(result.is_err(), "Oracle seed 在线模式应不支持");
    assert!(result.unwrap_err().to_string().contains("not supported"));
}