Archived
1
0
Fork 0

fix: supports old sqlite version in downgrade

This commit is contained in:
Dmitriy Pleshevskiy 2021-05-17 10:51:53 +03:00
parent 97178fcb02
commit c20f3c3411
4 changed files with 18 additions and 11 deletions

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
target/ target/
Cargo.lock Cargo.lock
# sqlite databases
*.db

View file

@ -12,8 +12,6 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: ApplyCommandOpt) -> StdResult<()> {
let migration_manager = MigrationManager::from(&config); let migration_manager = MigrationManager::from(&config);
println!("here");
let file_contents = cmd_opts let file_contents = cmd_opts
.file_paths .file_paths
.clone() .clone()
@ -35,7 +33,6 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: ApplyCommandOpt) -> StdResult<()> {
file_contents file_contents
.iter() .iter()
.try_for_each(|content| { .try_for_each(|content| {
println!("{}", &content);
maybe_with_transaction( maybe_with_transaction(
!cmd_opts.transaction_opts.single_transaction, !cmd_opts.transaction_opts.single_transaction,
conn, conn,

View file

@ -476,7 +476,7 @@ mod upgrade {
inner("sqlite", || { inner("sqlite", || {
use rusqlite::Connection; use rusqlite::Connection;
let conn = Connection::open_in_memory()?; let conn = Connection::open(SQLITE_URL)?;
let res = let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?; conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ()); assert_eq!(res, ());
@ -682,7 +682,6 @@ mod apply {
#[cfg(any(feature = "sqlite", feature = "rusqlite"))] #[cfg(any(feature = "sqlite", feature = "rusqlite"))]
remove_sqlite_db().and_then(|_| { remove_sqlite_db().and_then(|_| {
println!("upgrade");
inner( inner(
"sqlite", "sqlite",
vec![ vec![
@ -692,8 +691,7 @@ mod apply {
|| { || {
use rusqlite::Connection; use rusqlite::Connection;
let conn = Connection::open_in_memory()?; let conn = Connection::open(SQLITE_URL)?;
println!("upgraded?");
let res = let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?; conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ()); assert_eq!(res, ());
@ -702,7 +700,6 @@ mod apply {
}, },
)?; )?;
println!("downgrade");
inner( inner(
"sqlite", "sqlite",
vec![ vec![
@ -712,7 +709,7 @@ mod apply {
|| { || {
use rusqlite::Connection; use rusqlite::Connection;
let conn = Connection::open_in_memory()?; let conn = Connection::open(SQLITE_URL)?;
let res = let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a"); conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a");
assert!(res.is_err()); assert!(res.is_err());

View file

@ -1,6 +1,16 @@
-- This file should undo anything in `up.sql` -- This file should undo anything in `up.sql`
ALTER TABLE articles CREATE TABLE tmp_articles (
DROP COLUMN author_person_id; 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; DROP TABLE persons;