Skip to content
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 a way to provide a backend specific create table statement for #1676

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion diesel/src/migration/mod.rs
Expand Up @@ -3,7 +3,7 @@
mod errors;
pub use self::errors::{MigrationError, RunMigrationsError};

use connection::SimpleConnection;
use connection::{Connection, SimpleConnection};
use std::path::Path;

/// Represents a migration that interacts with diesel
Expand Down Expand Up @@ -53,3 +53,26 @@ impl<'a> Migration for &'a Migration {
(&**self).file_path()
}
}

/// A trait indicating that a connection could be used to run migrations
///
/// Normal users of diesel should not use/see this trait.
/// This trait is only relevant when you are implementing a new connection type
/// that could be used with diesel.
pub trait MigrationConnection: Connection {
/// The create table statement used to create the internal table used to
/// track which migrations were already run
///
/// This constant should contain a `CREATE TABLE` statement that
/// creates a new table called `__diesel_schema_migrations` containing
/// two columns named `version` and `run_on`. The `version` column must have a
/// datatype compatible with diesels `Text` sql type and must be the primary key
/// of the table. The `run_on` column must have a datatype compatible with diesels
/// `Timestamp` sql type, have a `NOT NULL` annotation and a default value
/// corresponding to the actual insert time (`CURRENT_TIMESTAMP` in ISO sql).
const CREATE_MIGRATIONS_TABLE: &'static str =
"CREATE TABLE IF NOT EXISTS __diesel_schema_migrations (\
version VARCHAR(50) PRIMARY KEY NOT NULL,\
run_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\
)";
}
3 changes: 3 additions & 0 deletions diesel/src/mysql/connection/mod.rs
Expand Up @@ -10,6 +10,7 @@ use super::backend::Mysql;
use super::bind_collector::MysqlBindCollector;
use connection::*;
use deserialize::{Queryable, QueryableByName};
use migration::MigrationConnection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflict occurred here

use query_builder::*;
use result::*;
use sql_types::HasSqlType;
Expand All @@ -32,6 +33,8 @@ impl SimpleConnection for MysqlConnection {
}
}

impl MigrationConnection for MysqlConnection {}

impl Connection for MysqlConnection {
type Backend = Mysql;
type TransactionManager = AnsiTransactionManager;
Expand Down
3 changes: 3 additions & 0 deletions diesel/src/pg/connection/mod.rs
Expand Up @@ -14,6 +14,7 @@ use self::result::PgResult;
use self::stmt::Statement;
use connection::*;
use deserialize::{Queryable, QueryableByName};
use migration::MigrationConnection;
use pg::{Pg, PgMetadataLookup, TransactionBuilder};
use query_builder::bind_collector::RawBytesBindCollector;
use query_builder::*;
Expand Down Expand Up @@ -107,6 +108,8 @@ impl Connection for PgConnection {
}
}

impl MigrationConnection for PgConnection {}

impl PgConnection {
/// Build a transaction, specifying additional details such as isolation level
///
Expand Down
3 changes: 3 additions & 0 deletions diesel/src/sqlite/connection/mod.rs
Expand Up @@ -17,6 +17,7 @@ use self::statement_iterator::*;
use self::stmt::{Statement, StatementUse};
use connection::*;
use deserialize::{Queryable, QueryableByName};
use migration::MigrationConnection;
use query_builder::bind_collector::RawBytesBindCollector;
use query_builder::*;
use result::*;
Expand Down Expand Up @@ -45,6 +46,8 @@ impl SimpleConnection for SqliteConnection {
}
}

impl MigrationConnection for SqliteConnection {}

impl Connection for SqliteConnection {
type Backend = Sqlite;
type TransactionManager = AnsiTransactionManager;
Expand Down
4 changes: 2 additions & 2 deletions diesel_migrations/migrations_internals/src/connection.rs
Expand Up @@ -14,15 +14,15 @@ use super::schema::__diesel_schema_migrations::dsl::*;
/// to wrap up some constraints which are meant to hold for *all* connections.
/// This trait will go away at some point in the future. Any Diesel connection
/// should be useable where this trait is required.
pub trait MigrationConnection: Connection {
pub trait MigrationConnection: ::diesel::migration::MigrationConnection {
fn previously_run_migration_versions(&self) -> QueryResult<HashSet<String>>;
fn latest_run_migration_version(&self) -> QueryResult<Option<String>>;
fn insert_new_migration(&self, version: &str) -> QueryResult<()>;
}

impl<T> MigrationConnection for T
where
T: Connection,
T: ::diesel::migration::MigrationConnection,
String: FromSql<VarChar, T::Backend>,
// FIXME: HRTB is preventing projecting on any associated types here
for<'a> InsertStatement<
Expand Down
13 changes: 5 additions & 8 deletions diesel_migrations/migrations_internals/src/lib.rs
Expand Up @@ -257,17 +257,14 @@ fn migration_with_version(
}

#[doc(hidden)]
pub fn setup_database<Conn: Connection>(conn: &Conn) -> QueryResult<usize> {
pub fn setup_database<Conn: MigrationConnection>(conn: &Conn) -> QueryResult<usize> {
create_schema_migrations_table_if_needed(conn)
}

fn create_schema_migrations_table_if_needed<Conn: Connection>(conn: &Conn) -> QueryResult<usize> {
conn.execute(
"CREATE TABLE IF NOT EXISTS __diesel_schema_migrations (\
version VARCHAR(50) PRIMARY KEY NOT NULL,\
run_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\
)",
)
fn create_schema_migrations_table_if_needed<Conn: MigrationConnection>(
conn: &Conn,
) -> QueryResult<usize> {
conn.execute(<Conn as ::diesel::migration::MigrationConnection>::CREATE_MIGRATIONS_TABLE)
}

#[doc(hidden)]
Expand Down