From c20f3c3411cf4496a7fa0a64251c6e5adba8570b Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 17 May 2021 10:51:53 +0300 Subject: [PATCH] fix: supports old sqlite version in downgrade --- .gitignore | 3 +++ migra-cli/src/commands/apply.rs | 3 --- migra-cli/tests/commands.rs | 9 +++------ .../210218233414_create_persons/down.sql | 14 ++++++++++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 2c96eb1..6b1d017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ target/ Cargo.lock + +# sqlite databases +*.db diff --git a/migra-cli/src/commands/apply.rs b/migra-cli/src/commands/apply.rs index 3c53981..0a78448 100644 --- a/migra-cli/src/commands/apply.rs +++ b/migra-cli/src/commands/apply.rs @@ -12,8 +12,6 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: ApplyCommandOpt) -> StdResult<()> { let migration_manager = MigrationManager::from(&config); - println!("here"); - let file_contents = cmd_opts .file_paths .clone() @@ -35,7 +33,6 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: ApplyCommandOpt) -> StdResult<()> { file_contents .iter() .try_for_each(|content| { - println!("{}", &content); maybe_with_transaction( !cmd_opts.transaction_opts.single_transaction, conn, diff --git a/migra-cli/tests/commands.rs b/migra-cli/tests/commands.rs index 20742f0..7134500 100644 --- a/migra-cli/tests/commands.rs +++ b/migra-cli/tests/commands.rs @@ -476,7 +476,7 @@ mod upgrade { inner("sqlite", || { use rusqlite::Connection; - let conn = Connection::open_in_memory()?; + let conn = Connection::open(SQLITE_URL)?; let res = conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?; assert_eq!(res, ()); @@ -682,7 +682,6 @@ mod apply { #[cfg(any(feature = "sqlite", feature = "rusqlite"))] remove_sqlite_db().and_then(|_| { - println!("upgrade"); inner( "sqlite", vec![ @@ -692,8 +691,7 @@ mod apply { || { use rusqlite::Connection; - let conn = Connection::open_in_memory()?; - println!("upgraded?"); + let conn = Connection::open(SQLITE_URL)?; let res = conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?; assert_eq!(res, ()); @@ -702,7 +700,6 @@ mod apply { }, )?; - println!("downgrade"); inner( "sqlite", vec![ @@ -712,7 +709,7 @@ mod apply { || { use rusqlite::Connection; - let conn = Connection::open_in_memory()?; + let conn = Connection::open(SQLITE_URL)?; let res = conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a"); assert!(res.is_err()); diff --git a/migra-cli/tests/data/sqlite/migrations/210218233414_create_persons/down.sql b/migra-cli/tests/data/sqlite/migrations/210218233414_create_persons/down.sql index c03d71a..ea50e2f 100644 --- a/migra-cli/tests/data/sqlite/migrations/210218233414_create_persons/down.sql +++ b/migra-cli/tests/data/sqlite/migrations/210218233414_create_persons/down.sql @@ -1,6 +1,16 @@ -- This file should undo anything in `up.sql` -ALTER TABLE articles - DROP COLUMN author_person_id; +CREATE TABLE tmp_articles ( + id int AUTO_INCREMENT PRIMARY KEY, + title text NOT NULL CHECK (length(title) > 0), + content text NOT NULL, + created_at timestamp NOT NULL DEFAULT current_timestamp +); + +INSERT INTO tmp_articles (id, title, content, created_at) +SELECT id, title, content, created_at FROM articles; + +DROP TABLE articles; +ALTER TABLE tmp_articles RENAME TO articles; DROP TABLE persons;