-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding barrel
feature
#1573
Adding barrel
feature
#1573
Changes from 1 commit
7f841e5
5739101
f258e56
439620e
4cd7b79
25267f9
9b95920
b4bacf4
67fd97e
4f64f21
e46570d
7f4b65b
487404d
a4cb6dd
f0b153f
1f74f90
c4cee3d
c62e6ea
1aa7232
5f4b402
7da5525
1887333
bac30c0
1aaaf5c
a3275f9
6f374e3
e0e97d6
ed2b8a1
b650a55
bc1607c
a27d1d4
4fdfb3d
8c025b6
feed83f
df26c2b
4e2668b
3c992c4
8468bc2
9a9b01b
56234c8
daa998d
a14287b
3101a99
d5330d2
a7cd615
9dd24cf
8960d05
cfc453d
2fc3637
6b902d4
051661f
b4a2b3b
5029b64
4eebacf
03e0248
b7bd404
3bb1233
a5d375d
e8bd6f5
6693e87
13e8caa
0dc23b8
8595def
f84f394
c1a217f
5168672
2086b99
ed20737
ef98ddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ pub use self::errors::*; | |
|
||
use connection::SimpleConnection; | ||
use std::path::Path; | ||
use std::fmt; | ||
|
||
/// Represents a migration that interacts with diesel | ||
pub trait Migration { | ||
|
@@ -19,3 +20,67 @@ pub trait Migration { | |
None | ||
} | ||
} | ||
|
||
/// A migration name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we document what this actually does rather than just repeating the name of the type? |
||
#[allow(missing_debug_implementations)] | ||
#[derive(Clone, Copy)] | ||
pub struct MigrationName<'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this need to move? |
||
/// Wraps around a migration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we provide more information than just repeating the type? |
||
pub migration: &'a Migration, | ||
} | ||
|
||
/// Get the name of a migration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we provide more information than just repeating the function/argument names? Maybe at least a usage example? |
||
pub fn name(migration: &Migration) -> MigrationName { | ||
MigrationName { | ||
migration: migration, | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for MigrationName<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let file_name = self.migration | ||
.file_path() | ||
.and_then(|file_path| file_path.file_name()) | ||
.and_then(|file| file.to_str()); | ||
if let Some(name) = file_name { | ||
f.write_str(name)?; | ||
} else { | ||
f.write_str(self.migration.version())?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Migration for Box<Migration> { | ||
fn version(&self) -> &str { | ||
(&**self).version() | ||
} | ||
|
||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).run(conn) | ||
} | ||
|
||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).revert(conn) | ||
} | ||
fn file_path(&self) -> Option<&Path> { | ||
(&**self).file_path() | ||
} | ||
} | ||
|
||
impl<'a> Migration for &'a Migration { | ||
fn version(&self) -> &str { | ||
(&**self).version() | ||
} | ||
|
||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).run(conn) | ||
} | ||
|
||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).revert(conn) | ||
} | ||
fn file_path(&self) -> Option<&Path> { | ||
(&**self).file_path() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ lint = ["clippy"] | |
postgres = ["diesel/postgres", "infer_schema_internals/postgres", "url"] | ||
sqlite = ["diesel/sqlite", "infer_schema_internals/sqlite"] | ||
mysql = ["diesel/mysql", "infer_schema_internals/mysql", "url"] | ||
barrels = ["migrations_internals/barrel", "barrel"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
|
||
[[test]] | ||
name = "tests" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,12 @@ | ||
use diesel::connection::SimpleConnection; | ||
use diesel::migration::errors::*; | ||
use diesel::migration::*; | ||
|
||
use std::path::{Path, PathBuf}; | ||
use std::fmt; | ||
|
||
pub trait Migration { | ||
fn version(&self) -> &str; | ||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError>; | ||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError>; | ||
fn file_path(&self) -> Option<&Path> { | ||
None | ||
} | ||
} | ||
|
||
#[allow(missing_debug_implementations)] | ||
#[derive(Clone, Copy)] | ||
pub struct MigrationName<'a> { | ||
pub migration: &'a Migration, | ||
} | ||
|
||
pub fn name(migration: &Migration) -> MigrationName { | ||
MigrationName { | ||
migration: migration, | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for MigrationName<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let file_name = self.migration | ||
.file_path() | ||
.and_then(|file_path| file_path.file_name()) | ||
.and_then(|file| file.to_str()); | ||
if let Some(name) = file_name { | ||
f.write_str(name)?; | ||
} else { | ||
f.write_str(self.migration.version())?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn migration_from(path: PathBuf) -> Result<Box<Migration>, MigrationError> { | ||
#[cfg(feature = "barrel")] | ||
match ::barrel::diesel::migration_from(&path) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about changing this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be more elegant although rustc tells me I'd suggest we change the syntax when the feature becomes stable, as it is more elegant. |
||
Some(sql) => { | ||
let version = try!(version_from_path(&path)); | ||
return barrel_to_migration(path, version, sql); | ||
} | ||
Some(migration) => return Ok(migration), | ||
None => {} | ||
} | ||
|
||
|
@@ -58,74 +18,6 @@ pub fn migration_from(path: PathBuf) -> Result<Box<Migration>, MigrationError> { | |
} | ||
} | ||
|
||
#[cfg(feature = "barrel")] | ||
struct BarrelMigration(PathBuf, String, String, String); | ||
|
||
#[cfg(feature = "barrel")] | ||
impl Migration for BarrelMigration { | ||
fn file_path(&self) -> Option<&Path> { | ||
Some(self.0.as_path()) | ||
} | ||
|
||
fn version(&self) -> &str { | ||
&self.1 | ||
} | ||
|
||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
try!(conn.batch_execute(&self.2)); | ||
Ok(()) | ||
} | ||
|
||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
try!(conn.batch_execute(&self.3)); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "barrel")] | ||
fn barrel_to_migration( | ||
path: PathBuf, | ||
version: String, | ||
sql: (String, String), | ||
) -> Result<Box<Migration>, MigrationError> { | ||
Ok(Box::new(BarrelMigration(path, version, sql.0, sql.1))) | ||
} | ||
|
||
|
||
impl Migration for Box<Migration> { | ||
fn version(&self) -> &str { | ||
(&**self).version() | ||
} | ||
|
||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).run(conn) | ||
} | ||
|
||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).revert(conn) | ||
} | ||
fn file_path(&self) -> Option<&Path> { | ||
(&**self).file_path() | ||
} | ||
} | ||
|
||
impl<'a> Migration for &'a Migration { | ||
fn version(&self) -> &str { | ||
(&**self).version() | ||
} | ||
|
||
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).run(conn) | ||
} | ||
|
||
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> { | ||
(&**self).revert(conn) | ||
} | ||
fn file_path(&self) -> Option<&Path> { | ||
(&**self).file_path() | ||
} | ||
} | ||
|
||
fn valid_sql_migration_directory(path: &Path) -> bool { | ||
file_names(path) | ||
.map(|files| files.contains(&"down.sql".into()) && files.contains(&"up.sql".into())) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,10 +78,6 @@ extern crate migrations_macros; | |
#[doc(hidden)] | ||
pub use migrations_macros::*; | ||
#[doc(inline)] | ||
pub use migrations_internals::migration::Migration; | ||
#[doc(inline)] | ||
pub use migrations_internals::MigrationName; | ||
#[doc(inline)] | ||
pub use migrations_internals::MigrationConnection; | ||
// #[doc(inline)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the commented lines? |
||
// pub use migrations_internals::MigrationError; | ||
|
@@ -117,8 +113,8 @@ pub use migrations_internals::find_migrations_directory; | |
pub use migrations_internals::search_for_migrations_directory; | ||
#[doc(inline)] | ||
pub use migrations_internals::version_from_path; | ||
#[doc(inline)] | ||
pub use migrations_internals::name; | ||
// #[doc(inline)] | ||
// pub use migrations_internals::name; | ||
|
||
pub mod connection { | ||
#[doc(inline)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed that's it's super hard to see what's up with SimpleConnection – it's not public and the only way aside from reading the code where you can find it is in the docs for Connection (which it's a supertrait of). Not something for this PR to fix but seemed worth commenting on.