chore: add transactional ddl
This commit is contained in:
parent
c20f3c3411
commit
1602069eb5
7 changed files with 79 additions and 1 deletions
|
@ -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 {
|
impl DatabaseConnection for SqliteConnection {
|
||||||
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
||||||
|
|
|
@ -527,6 +527,20 @@ mod upgrade {
|
||||||
Ok(())
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -563,6 +577,20 @@ mod upgrade {
|
||||||
Ok(())
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
migra-cli/tests/data/Migra_sqlite_invalid.toml
Normal file
4
migra-cli/tests/data/Migra_sqlite_invalid.toml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
root = "./sqlite_invalid"
|
||||||
|
|
||||||
|
[database]
|
||||||
|
connection = "local.db"
|
|
@ -0,0 +1,3 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
|
||||||
|
DROP TABLE articles;
|
|
@ -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
|
||||||
|
);
|
|
@ -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;
|
|
@ -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;
|
Reference in a new issue