Files
aho_corasick
bigdecimal
bitflags
byteorder
cfg_if
chrono
diesel
associations
connection
expression
expression_methods
macros
migration
mysql
pg
query_builder
query_dsl
query_source
sql_types
sqlite
type_impls
types
diesel_derives
diesel_migrations
env_logger
idna
instant
ipnetwork
itoa
kernel32
libc
libsqlite3_sys
lock_api
log
matches
memchr
migrations_internals
migrations_macros
mysqlclient_sys
num_bigint
num_integer
num_traits
parking_lot
parking_lot_core
percent_encoding
pq_sys
proc_macro2
quickcheck
quote
r2d2
regex
regex_syntax
ryu
scheduled_thread_pool
scopeguard
serde
serde_derive
serde_json
smallvec
syn
thread_id
thread_local
time
tinyvec
tinyvec_macros
unicode_bidi
unicode_normalization
unicode_xid
url
utf8_ranges
uuid
winapi
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use diesel::deserialize::FromSql;
use diesel::expression::bound::Bound;
use diesel::insertable::ColumnInsertValue;
use diesel::prelude::*;
use diesel::query_builder::{InsertStatement, ValuesClause};
use diesel::query_dsl::methods::ExecuteDsl;
use diesel::sql_types::VarChar;
use std::collections::HashSet;
use std::iter::FromIterator;

use super::schema::__diesel_schema_migrations::dsl::*;

/// A connection which can be passed to the migration methods. This exists only
/// 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 {
    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,
    String: FromSql<VarChar, T::Backend>,
    // FIXME: HRTB is preventing projecting on any associated types here
    for<'a> InsertStatement<
        __diesel_schema_migrations,
        ValuesClause<
            ColumnInsertValue<version, &'a Bound<VarChar, &'a str>>,
            __diesel_schema_migrations,
        >,
    >: ExecuteDsl<T>,
{
    fn previously_run_migration_versions(&self) -> QueryResult<HashSet<String>> {
        __diesel_schema_migrations
            .select(version)
            .load(self)
            .map(FromIterator::from_iter)
    }

    fn latest_run_migration_version(&self) -> QueryResult<Option<String>> {
        use diesel::dsl::max;
        __diesel_schema_migrations.select(max(version)).first(self)
    }

    fn insert_new_migration(&self, ver: &str) -> QueryResult<()> {
        ::diesel::insert_into(__diesel_schema_migrations)
            .values(&version.eq(ver))
            .execute(self)?;
        Ok(())
    }
}