From 1602069eb510960175097031ad249a05cde11eda Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 23 May 2021 00:30:43 +0300 Subject: [PATCH] chore: add transactional ddl --- migra-cli/src/database/clients/sqlite.rs | 7 ++++- migra-cli/tests/commands.rs | 28 +++++++++++++++++++ .../tests/data/Migra_sqlite_invalid.toml | 4 +++ .../210218232851_create_articles/down.sql | 3 ++ .../210218232851_create_articles/up.sql | 8 ++++++ .../210218233414_create_persons/down.sql | 16 +++++++++++ .../210218233414_create_persons/up.sql | 14 ++++++++++ 7 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 migra-cli/tests/data/Migra_sqlite_invalid.toml create mode 100644 migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/down.sql create mode 100644 migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/up.sql create mode 100644 migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/down.sql create mode 100644 migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/up.sql diff --git a/migra-cli/src/database/clients/sqlite.rs b/migra-cli/src/database/clients/sqlite.rs index 6b67b3f..8a0eafb 100644 --- a/migra-cli/src/database/clients/sqlite.rs +++ b/migra-cli/src/database/clients/sqlite.rs @@ -26,7 +26,12 @@ impl DatabaseStatements for SqliteConnection { } } -impl SupportsTransactionalDdl for SqliteConnection {} +impl SupportsTransactionalDdl for SqliteConnection { + #[inline] + fn supports_transactional_ddl(&self) -> bool { + true + } +} impl DatabaseConnection for SqliteConnection { fn batch_execute(&mut self, query: &str) -> StdResult<()> { diff --git a/migra-cli/tests/commands.rs b/migra-cli/tests/commands.rs index 7134500..c7ca53a 100644 --- a/migra-cli/tests/commands.rs +++ b/migra-cli/tests/commands.rs @@ -527,6 +527,20 @@ mod upgrade { Ok(()) })?; + #[cfg(any(feature = "sqlite", feature = "rusqlite"))] + inner("sqlite_invalid", || { + use rusqlite::Connection; + + let conn = Connection::open(SQLITE_URL)?; + let articles_res = conn.execute_batch("SELECT a.id FROM articles AS a"); + let persons_res = conn.execute_batch("SELECT p.id FROM persons AS p"); + + assert!(articles_res.is_ok()); + assert!(persons_res.is_err()); + + Ok(()) + })?; + Ok(()) } @@ -563,6 +577,20 @@ mod upgrade { Ok(()) })?; + #[cfg(any(feature = "sqlite", feature = "rusqlite"))] + inner("sqlite_invalid", || { + use rusqlite::Connection; + + let conn = Connection::open(SQLITE_URL)?; + let articles_res = conn.execute_batch("SELECT a.id FROM articles AS a"); + let persons_res = conn.execute_batch("SELECT p.id FROM persons AS p"); + + assert!(articles_res.is_err()); + assert!(persons_res.is_err()); + + Ok(()) + })?; + Ok(()) } } diff --git a/migra-cli/tests/data/Migra_sqlite_invalid.toml b/migra-cli/tests/data/Migra_sqlite_invalid.toml new file mode 100644 index 0000000..9b9c973 --- /dev/null +++ b/migra-cli/tests/data/Migra_sqlite_invalid.toml @@ -0,0 +1,4 @@ +root = "./sqlite_invalid" + +[database] +connection = "local.db" diff --git a/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/down.sql b/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/down.sql new file mode 100644 index 0000000..b625086 --- /dev/null +++ b/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` + +DROP TABLE articles; diff --git a/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/up.sql b/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/up.sql new file mode 100644 index 0000000..2742046 --- /dev/null +++ b/migra-cli/tests/data/sqlite_invalid/migrations/210218232851_create_articles/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here + +CREATE TABLE 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 +); diff --git a/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/down.sql b/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/down.sql new file mode 100644 index 0000000..ea50e2f --- /dev/null +++ b/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/down.sql @@ -0,0 +1,16 @@ +-- This file should undo anything in `up.sql` + +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; diff --git a/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/up.sql b/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/up.sql new file mode 100644 index 0000000..c01a919 --- /dev/null +++ b/migra-cli/tests/data/sqlite_invalid/migrations/210218233414_create_persons/up.sql @@ -0,0 +1,14 @@ +-- Your SQL goes here + +CREATE TABLE persons ( + id int AUTO_INCREMENT PRIMARY KEY, + email varchar(256) NOT NULL UNIQUE, + display_name text NOT NULL, + created_at timestamp NOT NULL DEFAULT current_timestamp +); + +/* This table doesn't exist + ↓↓↓↓↓↓↓ */ +ALTER TABLE recipes + ADD COLUMN author_person_id int NULL + REFERENCES persons (id) ON UPDATE CASCADE ON DELETE CASCADE;