diesel/migration/
errors.rs

1//! Error types that represent migration errors.
2//! These are split into multiple segments, depending on
3//! where in the migration process an error occurs.
4
5use std::convert::From;
6use std::error::Error;
7use std::path::PathBuf;
8use std::{fmt, io};
9
10use result;
11
12/// Errors that occur while preparing to run migrations
13#[derive(Debug)]
14pub enum MigrationError {
15    /// The migration directory wasn't found
16    MigrationDirectoryNotFound,
17    /// Provided migration was in an unknown format
18    UnknownMigrationFormat(PathBuf),
19    /// General system IO error
20    IoError(io::Error),
21    /// Provided migration had an incompatible version number
22    UnknownMigrationVersion(String),
23    /// No migrations had to be/ could be run
24    NoMigrationRun,
25    ///
26    #[doc(hidden)]
27    __NonExhaustive,
28}
29
30impl Error for MigrationError {
31    fn description(&self) -> &str {
32        match *self {
33            MigrationError::MigrationDirectoryNotFound => {
34                "Unable to find migrations directory in this directory or any parent directories."
35            }
36            MigrationError::UnknownMigrationFormat(_) => {
37                "Invalid migration directory, the directory's name should be \
38                 <timestamp>_<name_of_migration>, and it should only contain up.sql and down.sql."
39            }
40            MigrationError::IoError(ref error) => error.description(),
41            MigrationError::UnknownMigrationVersion(_) => {
42                "Unable to find migration version to revert in the migrations directory."
43            }
44            MigrationError::NoMigrationRun => {
45                "No migrations have been run. Did you forget `diesel migration run`?"
46            }
47            MigrationError::__NonExhaustive => unreachable!(),
48        }
49    }
50}
51
52impl fmt::Display for MigrationError {
53    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
54        self.description().fmt(f)
55    }
56}
57
58impl PartialEq for MigrationError {
59    fn eq(&self, other: &Self) -> bool {
60        match (self, other) {
61            (
62                &MigrationError::MigrationDirectoryNotFound,
63                &MigrationError::MigrationDirectoryNotFound,
64            ) => true,
65            (
66                &MigrationError::UnknownMigrationFormat(ref p1),
67                &MigrationError::UnknownMigrationFormat(ref p2),
68            ) => p1 == p2,
69            _ => false,
70        }
71    }
72}
73
74impl From<io::Error> for MigrationError {
75    fn from(e: io::Error) -> Self {
76        MigrationError::IoError(e)
77    }
78}
79
80/// Errors that occur while running migrations
81#[derive(Debug, PartialEq)]
82#[allow(clippy::enum_variant_names)]
83pub enum RunMigrationsError {
84    /// A general migration error occured
85    MigrationError(MigrationError),
86    /// The provided migration included an invalid query
87    QueryError(result::Error),
88    /// The provided migration was empty
89    EmptyMigration,
90    ///
91    #[doc(hidden)]
92    __NonExhaustive,
93}
94
95impl Error for RunMigrationsError {
96    fn description(&self) -> &str {
97        match *self {
98            RunMigrationsError::MigrationError(ref error) => error.description(),
99            RunMigrationsError::QueryError(ref error) => error.description(),
100            RunMigrationsError::EmptyMigration => "Attempted to run an empty migration.",
101            RunMigrationsError::__NonExhaustive => unreachable!(),
102        }
103    }
104}
105
106impl fmt::Display for RunMigrationsError {
107    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
108        write!(f, "Failed with: {}", self.description())
109    }
110}
111
112impl From<MigrationError> for RunMigrationsError {
113    fn from(e: MigrationError) -> Self {
114        RunMigrationsError::MigrationError(e)
115    }
116}
117
118impl From<result::Error> for RunMigrationsError {
119    fn from(e: result::Error) -> Self {
120        RunMigrationsError::QueryError(e)
121    }
122}
123
124impl From<io::Error> for RunMigrationsError {
125    fn from(e: io::Error) -> Self {
126        RunMigrationsError::MigrationError(e.into())
127    }
128}