Skip to main content

diesel_migrations/
migration_harness.rs

1use diesel::backend::Backend;
2use diesel::migration::{
3    Migration, MigrationConnection, MigrationSource, MigrationVersion, Result,
4};
5use diesel::prelude::*;
6use diesel::query_builder::QueryFragment;
7use diesel::query_dsl::methods;
8use diesel::serialize::ToSql;
9use diesel::sql_types::{Text, VarChar};
10use std::cell::RefCell;
11use std::collections::HashMap;
12use std::io::Write;
13
14use crate::errors::MigrationError;
15
16#[allow(unused_imports, dead_code, unreachable_pub, unused_qualifications)]
pub mod __diesel_schema_migrations {
    use ::diesel;
    pub use self::columns::*;
    use diesel::sql_types::*;
    #[doc = r" Re-exports all of the columns of this table, as well as the"]
    #[doc = r" table struct renamed to the module name. This is meant to be"]
    #[doc = r" glob imported for functions which only deal with one table."]
    pub mod dsl {
        pub use super::columns::version;
        pub use super::columns::run_on;
        pub use super::table as __diesel_schema_migrations;
    }
    #[allow(non_upper_case_globals, dead_code)]
    #[doc = r" A tuple of all of the columns on this table"]
    pub const all_columns: (version, run_on) = (version, run_on);
    #[allow(non_camel_case_types)]
    #[doc = r" The actual table struct"]
    #[doc = r""]
    #[doc = r" This is the type which provides the base methods of the query"]
    #[doc = r" builder, such as `.select` and `.filter`."]
    pub struct table;
    #[automatically_derived]
    #[allow(non_camel_case_types)]
    impl ::core::fmt::Debug for table {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "table")
        }
    }
    #[automatically_derived]
    #[doc(hidden)]
    #[allow(non_camel_case_types)]
    unsafe impl ::core::clone::TrivialClone for table { }
    #[automatically_derived]
    #[allow(non_camel_case_types)]
    impl ::core::clone::Clone for table {
        #[inline]
        fn clone(&self) -> table { *self }
    }
    #[automatically_derived]
    #[allow(non_camel_case_types)]
    impl ::core::marker::Copy for table { }
    const _: () =
        {
            use diesel;
            #[allow(non_camel_case_types)]
            impl diesel::query_builder::QueryId for table {
                type QueryId = table<>;
                const HAS_STATIC_QUERY_ID: bool = true;
                const IS_WINDOW_FUNCTION: bool = false;
            }
        };
    #[automatically_derived]
    #[allow(non_camel_case_types)]
    impl ::core::default::Default for table {
        #[inline]
        fn default() -> table { table {} }
    }
    impl table {
        #[allow(dead_code)]
        #[doc = r" Represents `table_name.*`, which is sometimes necessary"]
        #[doc =
        r" for efficient count queries. It cannot be used in place of"]
        #[doc = r" `all_columns`"]
        pub fn star(&self) -> star { star }
    }
    #[doc = r" The SQL type of all of the columns on this table"]
    pub type SqlType = (VarChar, Timestamp);
    #[doc = r" Helper type for representing a boxed query from this table"]
    pub type BoxedQuery<'a, DB, ST = SqlType> =
        diesel::internal::table_macro::BoxedSelectStatement<'a, ST,
        diesel::internal::table_macro::FromClause<table>, DB>;
    impl diesel::QuerySource for table {
        type FromClause =
            diesel::internal::table_macro::StaticQueryFragmentInstance<table>;
        type DefaultSelection = <Self as diesel::Table>::AllColumns;
        fn from_clause(&self) -> Self::FromClause {
            diesel::internal::table_macro::StaticQueryFragmentInstance::new()
        }
        fn default_selection(&self) -> Self::DefaultSelection {
            use diesel::Table;
            Self::all_columns()
        }
    }
    impl<DB> diesel::query_builder::QueryFragment<DB> for table where
        DB: diesel::backend::Backend,
        <table as
        diesel::internal::table_macro::StaticQueryFragment>::Component: diesel::query_builder::QueryFragment<DB>
        {
        fn walk_ast<'b>(&'b self,
            __diesel_internal_pass:
                diesel::query_builder::AstPass<'_, 'b, DB>)
            -> diesel::result::QueryResult<()> {
            <table as
                    diesel::internal::table_macro::StaticQueryFragment>::STATIC_COMPONENT.walk_ast(__diesel_internal_pass)
        }
    }
    impl diesel::internal::table_macro::StaticQueryFragment for table {
        type Component = diesel::internal::table_macro::Identifier<'static>;
        const STATIC_COMPONENT: &'static Self::Component =
            &diesel::internal::table_macro::Identifier("__diesel_schema_migrations");
    }
    impl diesel::query_builder::AsQuery for table {
        type SqlType = SqlType;
        type Query =
            diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<Self>>;
        fn as_query(self) -> Self::Query {
            diesel::internal::table_macro::SelectStatement::simple(self)
        }
    }
    impl diesel::Table for table {
        type PrimaryKey = version;
        type AllColumns = (version, run_on);
        fn primary_key(&self) -> Self::PrimaryKey { version }
        fn all_columns() -> Self::AllColumns { (version, run_on) }
    }
    impl diesel::associations::HasTable for table {
        type Table = Self;
        fn table() -> Self::Table { table }
    }
    impl diesel::query_builder::IntoUpdateTarget for table {
        type WhereClause =
            <<Self as diesel::query_builder::AsQuery>::Query as
            diesel::query_builder::IntoUpdateTarget>::WhereClause;
        fn into_update_target(self)
            ->
                diesel::query_builder::UpdateTarget<Self::Table,
                Self::WhereClause> {
            use diesel::query_builder::AsQuery;
            let q:
                    diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<table>> =
                self.as_query();
            q.into_update_target()
        }
    }
    impl diesel::query_source::AppearsInFromClause<table> for table {
        type Count = diesel::query_source::Once;
    }
    impl<S> diesel::internal::table_macro::AliasAppearsInFromClause<S, table>
        for table where S: diesel::query_source::AliasSource<Target = table> {
        type Count = diesel::query_source::Never;
    }
    impl<S1, S2>
        diesel::internal::table_macro::AliasAliasAppearsInFromClause<table,
        S2, S1> for table where
        S1: diesel::query_source::AliasSource<Target = table>,
        S2: diesel::query_source::AliasSource<Target = table>,
        S1: diesel::internal::table_macro::AliasAliasAppearsInFromClauseSameTable<S2,
        table> {
        type Count =
            <S1 as
            diesel::internal::table_macro::AliasAliasAppearsInFromClauseSameTable<S2,
            table>>::Count;
    }
    impl<S>
        diesel::query_source::AppearsInFromClause<diesel::query_source::Alias<S>>
        for table where S: diesel::query_source::AliasSource {
        type Count = diesel::query_source::Never;
    }
    impl<S, C>
        diesel::internal::table_macro::FieldAliasMapperAssociatedTypesDisjointnessTrick<table,
        S, C> for table where
        S: diesel::query_source::AliasSource<Target = table> +
        ::std::clone::Clone, C: diesel::query_source::Column<Table = table> {
        type Out = diesel::query_source::AliasedField<S, C>;
        fn map(__diesel_internal_column: C,
            __diesel_internal_alias: &diesel::query_source::Alias<S>)
            -> Self::Out {
            __diesel_internal_alias.field(__diesel_internal_column)
        }
    }
    impl diesel::query_source::AppearsInFromClause<table> for
        diesel::internal::table_macro::NoFromClause {
        type Count = diesel::query_source::Never;
    }
    impl<Left, Right, Kind>
        diesel::JoinTo<diesel::internal::table_macro::Join<Left, Right, Kind>>
        for table where
        diesel::internal::table_macro::Join<Left, Right,
        Kind>: diesel::JoinTo<table>, Left: diesel::query_source::QuerySource,
        Right: diesel::query_source::QuerySource {
        type FromClause =
            diesel::internal::table_macro::Join<Left, Right, Kind>;
        type OnClause =
            <diesel::internal::table_macro::Join<Left, Right, Kind> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs:
                diesel::internal::table_macro::Join<Left, Right, Kind>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::internal::table_macro::Join::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<Join, On>
        diesel::JoinTo<diesel::internal::table_macro::JoinOn<Join, On>> for
        table where
        diesel::internal::table_macro::JoinOn<Join, On>: diesel::JoinTo<table>
        {
        type FromClause = diesel::internal::table_macro::JoinOn<Join, On>;
        type OnClause =
            <diesel::internal::table_macro::JoinOn<Join, On> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs:
                diesel::internal::table_macro::JoinOn<Join, On>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::internal::table_macro::JoinOn::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<F, S, D, W, O, L, Of, G>
        diesel::JoinTo<diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<F>,
        S, D, W, O, L, Of, G>> for table where
        diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<F>,
        S, D, W, O, L, Of, G>: diesel::JoinTo<table>,
        F: diesel::query_source::QuerySource {
        type FromClause =
            diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<F>,
            S, D, W, O, L, Of, G>;
        type OnClause =
            <diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<F>,
            S, D, W, O, L, Of, G> as diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs:
                diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<F>,
                S, D, W, O, L, Of, G>) -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::internal::table_macro::SelectStatement::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<'a, QS, ST, DB>
        diesel::JoinTo<diesel::internal::table_macro::BoxedSelectStatement<'a,
        diesel::internal::table_macro::FromClause<QS>, ST, DB>> for table
        where
        diesel::internal::table_macro::BoxedSelectStatement<'a,
        diesel::internal::table_macro::FromClause<QS>, ST,
        DB>: diesel::JoinTo<table>, QS: diesel::query_source::QuerySource {
        type FromClause =
            diesel::internal::table_macro::BoxedSelectStatement<'a,
            diesel::internal::table_macro::FromClause<QS>, ST, DB>;
        type OnClause =
            <diesel::internal::table_macro::BoxedSelectStatement<'a,
            diesel::internal::table_macro::FromClause<QS>, ST, DB> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs:
                diesel::internal::table_macro::BoxedSelectStatement<'a,
                diesel::internal::table_macro::FromClause<QS>, ST, DB>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::internal::table_macro::BoxedSelectStatement::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<S> diesel::JoinTo<diesel::query_source::Alias<S>> for table where
        diesel::query_source::Alias<S>: diesel::JoinTo<table> {
        type FromClause = diesel::query_source::Alias<S>;
        type OnClause =
            <diesel::query_source::Alias<S> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs: diesel::query_source::Alias<S>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::query_source::Alias::<S>::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<T> diesel::insertable::Insertable<T> for table where
        <table as
        diesel::query_builder::AsQuery>::Query: diesel::insertable::Insertable<T>
        {
        type Values =
            <<table as diesel::query_builder::AsQuery>::Query as
            diesel::insertable::Insertable<T>>::Values;
        fn values(self) -> Self::Values {
            use diesel::query_builder::AsQuery;
            self.as_query().values()
        }
    }
    impl<'a, T> diesel::insertable::Insertable<T> for &'a table where
        table: diesel::insertable::Insertable<T> {
        type Values = <table as diesel::insertable::Insertable<T>>::Values;
        fn values(self) -> Self::Values { (*self).values() }
    }
    impl<S> diesel::JoinTo<diesel::query_builder::Only<S>> for table where
        diesel::query_builder::Only<S>: diesel::JoinTo<table> {
        type FromClause = diesel::query_builder::Only<S>;
        type OnClause =
            <diesel::query_builder::Only<S> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs: diesel::query_builder::Only<S>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::query_builder::Only::<S>::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl diesel::query_source::AppearsInFromClause<diesel::query_builder::Only<table>>
        for table {
        type Count = diesel::query_source::Once;
    }
    impl diesel::query_source::AppearsInFromClause<table> for
        diesel::query_builder::Only<table> {
        type Count = diesel::query_source::Once;
    }
    impl<S, TSM> diesel::JoinTo<diesel::query_builder::Tablesample<S, TSM>>
        for table where
        diesel::query_builder::Tablesample<S, TSM>: diesel::JoinTo<table>,
        TSM: diesel::internal::table_macro::TablesampleMethod {
        type FromClause = diesel::query_builder::Tablesample<S, TSM>;
        type OnClause =
            <diesel::query_builder::Tablesample<S, TSM> as
            diesel::JoinTo<table>>::OnClause;
        fn join_target(__diesel_internal_rhs:
                diesel::query_builder::Tablesample<S, TSM>)
            -> (Self::FromClause, Self::OnClause) {
            let (_, __diesel_internal_on_clause) =
                diesel::query_builder::Tablesample::<S,
                        TSM>::join_target(table);
            (__diesel_internal_rhs, __diesel_internal_on_clause)
        }
    }
    impl<TSM>
        diesel::query_source::AppearsInFromClause<diesel::query_builder::Tablesample<table,
        TSM>> for table where
        TSM: diesel::internal::table_macro::TablesampleMethod {
        type Count = diesel::query_source::Once;
    }
    impl<TSM> diesel::query_source::AppearsInFromClause<table> for
        diesel::query_builder::Tablesample<table, TSM> where
        TSM: diesel::internal::table_macro::TablesampleMethod {
        type Count = diesel::query_source::Once;
    }
    #[doc = r" Contains all of the columns of this table"]
    pub mod columns {
        use ::diesel;
        use super::table;
        use diesel::sql_types::*;
        #[allow(non_camel_case_types, dead_code)]
        #[doc = r" Represents `table_name.*`, which is sometimes needed for"]
        #[doc = r" efficient count queries. It cannot be used in place of"]
        #[doc = r" `all_columns`, and has a `SqlType` of `()` to prevent it"]
        #[doc = r" being used that way"]
        pub struct star;
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::fmt::Debug for star {
            #[inline]
            fn fmt(&self, f: &mut ::core::fmt::Formatter)
                -> ::core::fmt::Result {
                ::core::fmt::Formatter::write_str(f, "star")
            }
        }
        #[automatically_derived]
        #[doc(hidden)]
        #[allow(non_camel_case_types, dead_code)]
        unsafe impl ::core::clone::TrivialClone for star { }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::clone::Clone for star {
            #[inline]
            fn clone(&self) -> star { *self }
        }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::marker::Copy for star { }
        const _: () =
            {
                use diesel;
                #[allow(non_camel_case_types)]
                impl diesel::query_builder::QueryId for star {
                    type QueryId = star<>;
                    const HAS_STATIC_QUERY_ID: bool = true;
                    const IS_WINDOW_FUNCTION: bool = false;
                }
            };
        impl<__GB> diesel::expression::ValidGrouping<__GB> for star where
            (version, run_on): diesel::expression::ValidGrouping<__GB> {
            type IsAggregate =
                <(version, run_on) as
                diesel::expression::ValidGrouping<__GB>>::IsAggregate;
        }
        impl diesel::Expression for star {
            type SqlType =
                diesel::expression::expression_types::NotSelectable;
        }
        impl<DB: diesel::backend::Backend>
            diesel::query_builder::QueryFragment<DB> for star where
            <table as
            diesel::QuerySource>::FromClause: diesel::query_builder::QueryFragment<DB>
            {
            #[allow(non_snake_case)]
            fn walk_ast<'b>(&'b self,
                mut __diesel_internal_out:
                    diesel::query_builder::AstPass<'_, 'b, DB>)
                -> diesel::result::QueryResult<()> {
                use diesel::QuerySource;
                if !__diesel_internal_out.should_skip_from() {
                    const FROM_CLAUSE:
                        diesel::internal::table_macro::StaticQueryFragmentInstance<table>
                        =
                        diesel::internal::table_macro::StaticQueryFragmentInstance::new();
                    FROM_CLAUSE.walk_ast(__diesel_internal_out.reborrow())?;
                    __diesel_internal_out.push_sql(".");
                }
                __diesel_internal_out.push_sql("*");
                Ok(())
            }
        }
        impl diesel::SelectableExpression<table> for star {}
        impl diesel::AppearsOnTable<table> for star {}
        #[allow(non_camel_case_types, dead_code)]
        pub struct version;
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::fmt::Debug for version {
            #[inline]
            fn fmt(&self, f: &mut ::core::fmt::Formatter)
                -> ::core::fmt::Result {
                ::core::fmt::Formatter::write_str(f, "version")
            }
        }
        #[automatically_derived]
        #[doc(hidden)]
        #[allow(non_camel_case_types, dead_code)]
        unsafe impl ::core::clone::TrivialClone for version { }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::clone::Clone for version {
            #[inline]
            fn clone(&self) -> version { *self }
        }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::marker::Copy for version { }
        const _: () =
            {
                use diesel;
                #[allow(non_camel_case_types)]
                impl diesel::query_builder::QueryId for version {
                    type QueryId = version<>;
                    const HAS_STATIC_QUERY_ID: bool = true;
                    const IS_WINDOW_FUNCTION: bool = false;
                }
            };
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::default::Default for version {
            #[inline]
            fn default() -> version { version {} }
        }
        impl diesel::expression::Expression for version {
            type SqlType = VarChar;
        }
        impl<DB> diesel::query_builder::QueryFragment<DB> for version where
            DB: diesel::backend::Backend,
            diesel::internal::table_macro::StaticQueryFragmentInstance<table>: diesel::query_builder::QueryFragment<DB>
            {
            #[allow(non_snake_case)]
            fn walk_ast<'b>(&'b self,
                mut __diesel_internal_out:
                    diesel::query_builder::AstPass<'_, 'b, DB>)
                -> diesel::result::QueryResult<()> {
                if !__diesel_internal_out.should_skip_from() {
                    const FROM_CLAUSE:
                        diesel::internal::table_macro::StaticQueryFragmentInstance<table>
                        =
                        diesel::internal::table_macro::StaticQueryFragmentInstance::new();
                    FROM_CLAUSE.walk_ast(__diesel_internal_out.reborrow())?;
                    __diesel_internal_out.push_sql(".");
                }
                __diesel_internal_out.push_identifier("version")
            }
        }
        impl diesel::SelectableExpression<super::table> for version {}
        impl<QS> diesel::AppearsOnTable<QS> for version where
            QS: diesel::query_source::AppearsInFromClause<super::table, Count
            = diesel::query_source::Once> {}
        impl<Left, Right>
            diesel::SelectableExpression<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::LeftOuter>> for version
            where
            version: diesel::AppearsOnTable<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::LeftOuter>>,
            Self: diesel::SelectableExpression<Left>,
            Right: diesel::query_source::AppearsInFromClause<super::table,
            Count = diesel::query_source::Never> +
            diesel::query_source::QuerySource,
            Left: diesel::query_source::QuerySource {}
        impl<Left, Right>
            diesel::SelectableExpression<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::Inner>> for version where
            version: diesel::AppearsOnTable<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::Inner>>,
            Left: diesel::query_source::AppearsInFromClause<super::table> +
            diesel::query_source::QuerySource,
            Right: diesel::query_source::AppearsInFromClause<super::table> +
            diesel::query_source::QuerySource,
            (Left::Count,
            Right::Count): diesel::internal::table_macro::Pick<Left, Right>,
            Self: diesel::SelectableExpression<<(Left::Count, Right::Count) as
            diesel::internal::table_macro::Pick<Left, Right>>::Selection> {}
        impl<Join, On>
            diesel::SelectableExpression<diesel::internal::table_macro::JoinOn<Join,
            On>> for version where
            version: diesel::SelectableExpression<Join> +
            diesel::AppearsOnTable<diesel::internal::table_macro::JoinOn<Join,
            On>> {}
        impl<From>
            diesel::SelectableExpression<diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<From>>>
            for version where From: diesel::query_source::QuerySource,
            version: diesel::SelectableExpression<From> +
            diesel::AppearsOnTable<diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<From>>>
            {}
        impl<__GB> diesel::expression::ValidGrouping<__GB> for version where
            __GB: diesel::expression::IsContainedInGroupBy<version, Output =
            diesel::expression::is_contained_in_group_by::Yes> {
            type IsAggregate = diesel::expression::is_aggregate::Yes;
        }
        impl diesel::expression::ValidGrouping<()> for version {
            type IsAggregate = diesel::expression::is_aggregate::No;
        }
        impl diesel::expression::IsContainedInGroupBy<version> for version {
            type Output = diesel::expression::is_contained_in_group_by::Yes;
        }
        impl diesel::query_source::Column for version {
            type Table = super::table;
            const NAME: &'static str = "version";
        }
        impl<T> diesel::EqAll<T> for version where
            T: diesel::expression::AsExpression<VarChar>,
            diesel::dsl::Eq<version,
            T::Expression>: diesel::Expression<SqlType =
            diesel::sql_types::Bool> {
            type Output = diesel::dsl::Eq<Self, T::Expression>;
            fn eq_all(self, __diesel_internal_rhs: T) -> Self::Output {
                use diesel::expression_methods::ExpressionMethods;
                self.eq(__diesel_internal_rhs)
            }
        }
        impl diesel::query_source::AppearsInFromClause<diesel::query_builder::Only<super::table>>
            for version {
            type Count = diesel::query_source::Once;
        }
        impl diesel::SelectableExpression<diesel::query_builder::Only<super::table>>
            for version {}
        impl<TSM>
            diesel::query_source::AppearsInFromClause<diesel::query_builder::Tablesample<super::table,
            TSM>> for version where
            TSM: diesel::internal::table_macro::TablesampleMethod {
            type Count = diesel::query_source::Once;
        }
        impl<TSM>
            diesel::SelectableExpression<diesel::query_builder::Tablesample<super::table,
            TSM>> for version where
            TSM: diesel::internal::table_macro::TablesampleMethod {}
        #[allow(non_camel_case_types, dead_code)]
        pub struct run_on;
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::fmt::Debug for run_on {
            #[inline]
            fn fmt(&self, f: &mut ::core::fmt::Formatter)
                -> ::core::fmt::Result {
                ::core::fmt::Formatter::write_str(f, "run_on")
            }
        }
        #[automatically_derived]
        #[doc(hidden)]
        #[allow(non_camel_case_types, dead_code)]
        unsafe impl ::core::clone::TrivialClone for run_on { }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::clone::Clone for run_on {
            #[inline]
            fn clone(&self) -> run_on { *self }
        }
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::marker::Copy for run_on { }
        const _: () =
            {
                use diesel;
                #[allow(non_camel_case_types)]
                impl diesel::query_builder::QueryId for run_on {
                    type QueryId = run_on<>;
                    const HAS_STATIC_QUERY_ID: bool = true;
                    const IS_WINDOW_FUNCTION: bool = false;
                }
            };
        #[automatically_derived]
        #[allow(non_camel_case_types, dead_code)]
        impl ::core::default::Default for run_on {
            #[inline]
            fn default() -> run_on { run_on {} }
        }
        impl diesel::expression::Expression for run_on {
            type SqlType = Timestamp;
        }
        impl<DB> diesel::query_builder::QueryFragment<DB> for run_on where
            DB: diesel::backend::Backend,
            diesel::internal::table_macro::StaticQueryFragmentInstance<table>: diesel::query_builder::QueryFragment<DB>
            {
            #[allow(non_snake_case)]
            fn walk_ast<'b>(&'b self,
                mut __diesel_internal_out:
                    diesel::query_builder::AstPass<'_, 'b, DB>)
                -> diesel::result::QueryResult<()> {
                if !__diesel_internal_out.should_skip_from() {
                    const FROM_CLAUSE:
                        diesel::internal::table_macro::StaticQueryFragmentInstance<table>
                        =
                        diesel::internal::table_macro::StaticQueryFragmentInstance::new();
                    FROM_CLAUSE.walk_ast(__diesel_internal_out.reborrow())?;
                    __diesel_internal_out.push_sql(".");
                }
                __diesel_internal_out.push_identifier("run_on")
            }
        }
        impl diesel::SelectableExpression<super::table> for run_on {}
        impl<QS> diesel::AppearsOnTable<QS> for run_on where
            QS: diesel::query_source::AppearsInFromClause<super::table, Count
            = diesel::query_source::Once> {}
        impl<Left, Right>
            diesel::SelectableExpression<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::LeftOuter>> for run_on where
            run_on: diesel::AppearsOnTable<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::LeftOuter>>,
            Self: diesel::SelectableExpression<Left>,
            Right: diesel::query_source::AppearsInFromClause<super::table,
            Count = diesel::query_source::Never> +
            diesel::query_source::QuerySource,
            Left: diesel::query_source::QuerySource {}
        impl<Left, Right>
            diesel::SelectableExpression<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::Inner>> for run_on where
            run_on: diesel::AppearsOnTable<diesel::internal::table_macro::Join<Left,
            Right, diesel::internal::table_macro::Inner>>,
            Left: diesel::query_source::AppearsInFromClause<super::table> +
            diesel::query_source::QuerySource,
            Right: diesel::query_source::AppearsInFromClause<super::table> +
            diesel::query_source::QuerySource,
            (Left::Count,
            Right::Count): diesel::internal::table_macro::Pick<Left, Right>,
            Self: diesel::SelectableExpression<<(Left::Count, Right::Count) as
            diesel::internal::table_macro::Pick<Left, Right>>::Selection> {}
        impl<Join, On>
            diesel::SelectableExpression<diesel::internal::table_macro::JoinOn<Join,
            On>> for run_on where run_on: diesel::SelectableExpression<Join> +
            diesel::AppearsOnTable<diesel::internal::table_macro::JoinOn<Join,
            On>> {}
        impl<From>
            diesel::SelectableExpression<diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<From>>>
            for run_on where From: diesel::query_source::QuerySource,
            run_on: diesel::SelectableExpression<From> +
            diesel::AppearsOnTable<diesel::internal::table_macro::SelectStatement<diesel::internal::table_macro::FromClause<From>>>
            {}
        impl<__GB> diesel::expression::ValidGrouping<__GB> for run_on where
            __GB: diesel::expression::IsContainedInGroupBy<run_on, Output =
            diesel::expression::is_contained_in_group_by::Yes> {
            type IsAggregate = diesel::expression::is_aggregate::Yes;
        }
        impl diesel::expression::ValidGrouping<()> for run_on {
            type IsAggregate = diesel::expression::is_aggregate::No;
        }
        impl diesel::expression::IsContainedInGroupBy<run_on> for run_on {
            type Output = diesel::expression::is_contained_in_group_by::Yes;
        }
        impl diesel::query_source::Column for run_on {
            type Table = super::table;
            const NAME: &'static str = "run_on";
        }
        impl<T> diesel::EqAll<T> for run_on where
            T: diesel::expression::AsExpression<Timestamp>,
            diesel::dsl::Eq<run_on,
            T::Expression>: diesel::Expression<SqlType =
            diesel::sql_types::Bool> {
            type Output = diesel::dsl::Eq<Self, T::Expression>;
            fn eq_all(self, __diesel_internal_rhs: T) -> Self::Output {
                use diesel::expression_methods::ExpressionMethods;
                self.eq(__diesel_internal_rhs)
            }
        }
        impl<Rhs> ::std::ops::Add<Rhs> for run_on where
            Rhs: diesel::expression::AsExpression<<<run_on as
            diesel::Expression>::SqlType as diesel::sql_types::ops::Add>::Rhs>
            {
            type Output =
                diesel::internal::table_macro::ops::Add<Self,
                Rhs::Expression>;
            fn add(self, __diesel_internal_rhs: Rhs) -> Self::Output {
                diesel::internal::table_macro::ops::Add::new(self,
                    __diesel_internal_rhs.as_expression())
            }
        }
        impl<Rhs> ::std::ops::Sub<Rhs> for run_on where
            Rhs: diesel::expression::AsExpression<<<run_on as
            diesel::Expression>::SqlType as diesel::sql_types::ops::Sub>::Rhs>
            {
            type Output =
                diesel::internal::table_macro::ops::Sub<Self,
                Rhs::Expression>;
            fn sub(self, __diesel_internal_rhs: Rhs) -> Self::Output {
                diesel::internal::table_macro::ops::Sub::new(self,
                    __diesel_internal_rhs.as_expression())
            }
        }
        impl diesel::query_source::AppearsInFromClause<diesel::query_builder::Only<super::table>>
            for run_on {
            type Count = diesel::query_source::Once;
        }
        impl diesel::SelectableExpression<diesel::query_builder::Only<super::table>>
            for run_on {}
        impl<TSM>
            diesel::query_source::AppearsInFromClause<diesel::query_builder::Tablesample<super::table,
            TSM>> for run_on where
            TSM: diesel::internal::table_macro::TablesampleMethod {
            type Count = diesel::query_source::Once;
        }
        impl<TSM>
            diesel::SelectableExpression<diesel::query_builder::Tablesample<super::table,
            TSM>> for run_on where
            TSM: diesel::internal::table_macro::TablesampleMethod {}
        impl diesel::expression::IsContainedInGroupBy<version> for run_on {
            type Output = diesel::expression::is_contained_in_group_by::No;
        }
        impl diesel::expression::IsContainedInGroupBy<run_on> for version {
            type Output = diesel::expression::is_contained_in_group_by::Yes;
        }
    }
}diesel::table! {
17    __diesel_schema_migrations (version) {
18        version -> VarChar,
19        run_on -> Timestamp,
20    }
21}
22
23/// A migration harness is an entity which applies migration to an existing database
24pub trait MigrationHarness<DB: Backend> {
25    /// Checks if the database represented by the current harness has unapplied migrations
26    fn has_pending_migration<S: MigrationSource<DB>>(&mut self, source: S) -> Result<bool> {
27        self.pending_migrations(source).map(|p| !p.is_empty())
28    }
29
30    /// Execute all unapplied migrations for a given migration source
31    fn run_pending_migrations<S: MigrationSource<DB>>(
32        &mut self,
33        source: S,
34    ) -> Result<Vec<MigrationVersion<'_>>> {
35        let pending = self.pending_migrations(source)?;
36        self.run_migrations(&pending)
37    }
38
39    /// Execute all migrations in the given list
40    ///
41    /// This method does not check if a certain migration was already applied or not
42    #[doc(hidden)]
43    fn run_migrations(
44        &mut self,
45        migrations: &[Box<dyn Migration<DB>>],
46    ) -> Result<Vec<MigrationVersion<'_>>> {
47        migrations.iter().map(|m| self.run_migration(m)).collect()
48    }
49
50    /// Execute the next migration from the given migration source
51    fn run_next_migration<S: MigrationSource<DB>>(
52        &mut self,
53        source: S,
54    ) -> Result<MigrationVersion<'_>> {
55        let pending_migrations = self.pending_migrations(source)?;
56        let next_migration = pending_migrations
57            .first()
58            .ok_or(MigrationError::NoMigrationRun)?;
59        self.run_migration(next_migration)
60    }
61
62    /// Revert all applied migrations from a given migration source
63    fn revert_all_migrations<S: MigrationSource<DB>>(
64        &mut self,
65        source: S,
66    ) -> Result<Vec<MigrationVersion<'_>>> {
67        let applied_versions = self.applied_migrations()?;
68        let mut migrations = source
69            .migrations()?
70            .into_iter()
71            .map(|m| (m.name().version().as_owned(), m))
72            .collect::<HashMap<_, _>>();
73
74        applied_versions
75            .into_iter()
76            .map(|version| {
77                let migration_to_revert = migrations
78                    .remove(&version)
79                    .ok_or(MigrationError::UnknownMigrationVersion(version))?;
80                self.revert_migration(&migration_to_revert)
81            })
82            .collect()
83    }
84
85    /// Revert the last migration from a given migration source
86    ///
87    /// This method returns a error if the given migration source does not
88    /// contain the last applied migration
89    fn revert_last_migration<S: MigrationSource<DB>>(
90        &mut self,
91        source: S,
92    ) -> Result<MigrationVersion<'static>> {
93        let applied_versions = self.applied_migrations()?;
94        let migrations = source.migrations()?;
95        let last_migration_version = applied_versions
96            .first()
97            .ok_or(MigrationError::NoMigrationRun)?;
98        let migration_to_revert = migrations
99            .iter()
100            .find(|m| m.name().version() == *last_migration_version)
101            .ok_or_else(|| {
102                MigrationError::UnknownMigrationVersion(last_migration_version.as_owned())
103            })?;
104        self.revert_migration(migration_to_revert)
105    }
106
107    /// Get a list of non applied migrations for a specific migration source
108    ///
109    /// The returned migration list is sorted in ascending order by the individual version
110    /// of each migration
111    fn pending_migrations<S: MigrationSource<DB>>(
112        &mut self,
113        source: S,
114    ) -> Result<Vec<Box<dyn Migration<DB>>>> {
115        let applied_versions = self.applied_migrations()?;
116        let mut migrations = source
117            .migrations()?
118            .into_iter()
119            .map(|m| (m.name().version().as_owned(), m))
120            .collect::<HashMap<_, _>>();
121
122        for applied_version in applied_versions {
123            migrations.remove(&applied_version);
124        }
125
126        let mut migrations = migrations.into_values().collect::<Vec<_>>();
127
128        migrations.sort_unstable_by(|a, b| a.name().version().cmp(&b.name().version()));
129
130        Ok(migrations)
131    }
132
133    /// Apply a single migration
134    ///
135    /// Types implementing this trait should call [`Migration::run`] internally and record
136    /// that a specific migration version was executed afterwards.
137    ///
138    /// The default implementation wraps the migration in a transaction when the migration
139    /// metadata reports `run_in_transaction = true` (the default). See
140    /// [`FileBasedMigrations`](crate::FileBasedMigrations) to learn how to configure
141    /// this for a specific migration.
142    fn run_migration(&mut self, migration: &dyn Migration<DB>)
143        -> Result<MigrationVersion<'static>>;
144
145    /// Revert a single migration
146    ///
147    /// Types implementing this trait should call [`Migration::revert`] internally
148    /// and record that a specific migration version was reverted afterwards.
149    ///
150    /// The default implementation wraps the revert in a transaction when the migration
151    /// metadata reports `run_in_transaction = true` (the default). See
152    /// [`FileBasedMigrations`](crate::FileBasedMigrations) to learn how to configure
153    /// this for a specific migration.
154    fn revert_migration(
155        &mut self,
156        migration: &dyn Migration<DB>,
157    ) -> Result<MigrationVersion<'static>>;
158
159    /// Get a list of already applied migration versions
160    fn applied_migrations(&mut self) -> Result<Vec<MigrationVersion<'static>>>;
161}
162
163impl<C, DB> MigrationHarness<DB> for C
164where
165    DB: Backend + diesel::internal::migrations::DieselReserveSpecialization,
166    C: Connection<Backend = DB> + MigrationConnection + 'static,
167    __diesel_schema_migrations::table: methods::BoxedDsl<
168        'static,
169        DB,
170        Output = __diesel_schema_migrations::BoxedQuery<'static, DB>,
171    >,
172    __diesel_schema_migrations::BoxedQuery<'static, DB, VarChar>:
173        methods::LoadQuery<'static, C, MigrationVersion<'static>>,
174    diesel::internal::migrations::DefaultValues: QueryFragment<DB>,
175    str: ToSql<Text, DB>,
176{
177    fn run_migration(
178        &mut self,
179        migration: &dyn Migration<DB>,
180    ) -> Result<MigrationVersion<'static>> {
181        let apply_migration = |conn: &mut C| -> Result<()> {
182            migration.run(conn)?;
183            diesel::insert_into(__diesel_schema_migrations::table)
184                .values(
185                    __diesel_schema_migrations::version.eq(migration.name().version().as_owned()),
186                )
187                .execute(conn)?;
188            Ok(())
189        };
190
191        if migration.metadata().run_in_transaction() {
192            self.transaction(apply_migration)?;
193        } else {
194            apply_migration(self)?;
195        }
196        Ok(migration.name().version().as_owned())
197    }
198
199    fn revert_migration(
200        &mut self,
201        migration: &dyn Migration<DB>,
202    ) -> Result<MigrationVersion<'static>> {
203        let revert_migration = |conn: &mut C| -> Result<()> {
204            migration.revert(conn)?;
205            diesel::delete(
206                __diesel_schema_migrations::table.find(migration.name().version().as_owned()),
207            )
208            .execute(conn)?;
209            Ok(())
210        };
211
212        if migration.metadata().run_in_transaction() {
213            self.transaction(revert_migration)?;
214        } else {
215            revert_migration(self)?;
216        }
217        Ok(migration.name().version().as_owned())
218    }
219
220    fn applied_migrations(&mut self) -> Result<Vec<MigrationVersion<'static>>> {
221        setup_database(self)?;
222        Ok(__diesel_schema_migrations::table
223            .into_boxed()
224            .select(__diesel_schema_migrations::version)
225            .order(__diesel_schema_migrations::version.desc())
226            .load(self)?)
227    }
228}
229
230/// A migration harness that writes messages
231/// into some output for each applied/reverted migration
232pub struct HarnessWithOutput<'a, C, W> {
233    connection: &'a mut C,
234    output: RefCell<W>,
235}
236
237impl<'a, C, W> HarnessWithOutput<'a, C, W> {
238    /// Create a new `HarnessWithOutput` that writes to a specific writer
239    pub fn new<DB>(harness: &'a mut C, output: W) -> Self
240    where
241        C: MigrationHarness<DB>,
242        DB: Backend,
243        W: Write,
244    {
245        Self {
246            connection: harness,
247            output: RefCell::new(output),
248        }
249    }
250}
251
252impl<'a, C> HarnessWithOutput<'a, C, std::io::Stdout> {
253    /// Create a new `HarnessWithOutput` that writes to stdout
254    pub fn write_to_stdout<DB>(harness: &'a mut C) -> Self
255    where
256        C: MigrationHarness<DB>,
257        DB: Backend,
258    {
259        Self {
260            connection: harness,
261            output: RefCell::new(std::io::stdout()),
262        }
263    }
264}
265
266impl<C, W, DB> MigrationHarness<DB> for HarnessWithOutput<'_, C, W>
267where
268    W: Write,
269    C: MigrationHarness<DB>,
270    DB: Backend,
271{
272    fn run_migration(
273        &mut self,
274        migration: &dyn Migration<DB>,
275    ) -> Result<MigrationVersion<'static>> {
276        if migration.name().version() != MigrationVersion::from("00000000000000") {
277            let mut output = self.output.try_borrow_mut()?;
278            output.write_fmt(format_args!("Running migration {0}\n", migration.name()))writeln!(output, "Running migration {}", migration.name())?;
279        }
280        self.connection.run_migration(migration)
281    }
282
283    fn revert_migration(
284        &mut self,
285        migration: &dyn Migration<DB>,
286    ) -> Result<MigrationVersion<'static>> {
287        if migration.name().version() != MigrationVersion::from("00000000000000") {
288            let mut output = self.output.try_borrow_mut()?;
289            output.write_fmt(format_args!("Rolling back migration {0}\n",
        migration.name()))writeln!(output, "Rolling back migration {}", migration.name())?;
290        }
291        self.connection.revert_migration(migration)
292    }
293
294    fn applied_migrations(&mut self) -> Result<Vec<MigrationVersion<'static>>> {
295        self.connection.applied_migrations()
296    }
297}
298
299fn setup_database<Conn: MigrationConnection>(conn: &mut Conn) -> QueryResult<usize> {
300    conn.setup()
301}