Fix migrate! and add migration test

This commit is contained in:
Raphaël Thériault
2020-07-23 17:46:27 -04:00
parent 39b3e4a966
commit 92646e00b8
9 changed files with 61 additions and 32 deletions

View File

@@ -48,14 +48,14 @@ jobs:
args: >
--manifest-path sqlx-core/Cargo.toml
--no-default-features
--features offline,all-databases,all-types,runtime-${{ matrix.runtime }}
--features offline,all-databases,all-types,migrate,runtime-${{ matrix.runtime }}
- uses: actions-rs/cargo@v1
with:
command: check
args: >
--no-default-features
--features offline,all-databases,all-types,runtime-${{ matrix.runtime }},macros
--features offline,all-databases,all-types,migrate,runtime-${{ matrix.runtime }},macros
test:
name: Unit Test
@@ -97,7 +97,7 @@ jobs:
command: test
args: >
--no-default-features
--features any,macros,sqlite,all-types,runtime-${{ matrix.runtime }}
--features any,macros,migrate,sqlite,all-types,runtime-${{ matrix.runtime }}
--
--test-threads=1
env:
@@ -143,7 +143,7 @@ jobs:
command: test
args: >
--no-default-features
--features any,postgres,macros,all-types,runtime-${{ matrix.runtime }}
--features any,postgres,macros,migrate,all-types,runtime-${{ matrix.runtime }}
env:
DATABASE_URL: postgres://postgres:password@localhost:5432/sqlx?sslmode=verify-ca&sslrootcert=.%2Ftests%2Fcerts%2Fca.crt
@@ -178,7 +178,7 @@ jobs:
command: test
args: >
--no-default-features
--features any,mysql,macros,all-types,runtime-${{ matrix.runtime }}
--features any,mysql,macros,migrate,all-types,runtime-${{ matrix.runtime }}
env:
DATABASE_URL: mysql://root:password@localhost:3306/sqlx
@@ -213,7 +213,7 @@ jobs:
command: test
args: >
--no-default-features
--features any,mysql,macros,all-types,runtime-${{ matrix.runtime }}
--features any,mysql,macros,migrate,all-types,runtime-${{ matrix.runtime }}
env:
DATABASE_URL: mysql://root:password@localhost:3306/sqlx
@@ -248,6 +248,6 @@ jobs:
command: test
args: >
--no-default-features
--features any,mssql,macros,all-types,runtime-${{ matrix.runtime }}
--features any,mssql,macros,migrate,all-types,runtime-${{ matrix.runtime }}
env:
DATABASE_URL: mssql://sa:Password123!@localhost/sqlx

View File

@@ -107,6 +107,15 @@ name = "any-pool"
path = "tests/any/pool.rs"
required-features = [ "any" ]
#
# Migrations
#
[[test]]
name = "migrate-macro"
path = "tests/migrate/macro.rs"
required-features = [ "macros", "migrate" ]
#
# SQLite
#

View File

@@ -44,8 +44,8 @@ pub async fn info(uri: &str) -> anyhow::Result<()> {
for migration in migrator.iter() {
println!(
"{}/{} {}",
style(migration.version()).cyan(),
if version >= migration.version() {
style(migration.version).cyan(),
if version >= migration.version {
style("installed").green()
} else {
style("pending").yellow()
@@ -70,12 +70,12 @@ pub async fn run(uri: &str) -> anyhow::Result<()> {
}
for migration in migrator.iter() {
if migration.version() > version {
if migration.version > version {
let elapsed = conn.apply(migration).await?;
println!(
"{}/{} {} {}",
style(migration.version()).cyan(),
style(migration.version).cyan(),
style("migrate").green(),
migration.description,
style(format!("({:?})", elapsed)).dim()

View File

@@ -2,7 +2,6 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens, TokenStreamExt};
use sha2::{Digest, Sha384};
use std::fs;
use std::path::Path;
use syn::LitStr;
struct QuotedMigration {
@@ -26,7 +25,7 @@ impl ToTokens for QuotedMigration {
version: #version,
description: std::borrow::Cow::Borrowed(#description),
sql: std::borrow::Cow::Borrowed(#sql),
checksum: std::borow::Cow::Borrowed(&[
checksum: std::borrow::Cow::Borrowed(&[
#(#checksum),*
]),
}

View File

@@ -1,4 +1,3 @@
use std::env;
use std::fs;
use proc_macro2::{Ident, Span};

View File

@@ -594,25 +594,19 @@ macro_rules! query_file_as_unchecked (
#[cfg(feature = "migrate")]
#[macro_export]
macro_rules! migrate {
($dir:literal) => {
#[allow(dead_code)]
{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::migrate!($dir);
}
macro_result!()
($dir:literal) => {{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::migrate!($dir);
}
};
macro_result!()
}};
() => {
#[allow(dead_code)]
{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::migrate!("migrations");
}
macro_result!()
() => {{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::migrate!("migrations");
}
};
macro_result!()
}};
}

17
tests/migrate/macro.rs Normal file
View File

@@ -0,0 +1,17 @@
use sqlx::migrate::Migrator;
use std::path::Path;
#[sqlx_macros::test]
async fn same_output() -> anyhow::Result<()> {
let embedded = sqlx::migrate!("tests/migrate/migrations");
let runtime = Migrator::new(Path::new("tests/migrate/migrations")).await?;
for (e, r) in embedded.iter().zip(runtime.iter()) {
assert_eq!(e.version, r.version);
assert_eq!(e.description, r.description);
assert_eq!(e.sql, r.sql);
assert_eq!(e.checksum, r.checksum);
}
Ok(())
}

View File

@@ -0,0 +1,6 @@
CREATE TABLE tweet (
id BIGINT NOT NULL PRIMARY KEY,
text TEXT NOT NULL,
is_sent BOOLEAN NOT NULL DEFAULT TRUE,
owner_id BIGINT
);

View File

@@ -0,0 +1,5 @@
CREATE TABLE accounts (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
is_active BOOLEAN
);