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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::fmt;
use crate::SchemaVersion;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, PartialEq)]
#[allow(clippy::enum_variant_names)]
#[non_exhaustive]
pub enum Error {
RusqliteError {
query: String,
err: rusqlite::Error,
},
SpecifiedSchemaVersion(SchemaVersionError),
MigrationDefinition(MigrationDefinitionError),
}
impl Error {
pub fn with_sql(e: rusqlite::Error, sql: &str) -> Error {
Error::RusqliteError {
query: String::from(sql),
err: e,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "rusqlite_migrate error: {:?}", self)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::RusqliteError { query: _, err } => Some(err),
Error::SpecifiedSchemaVersion(e) => Some(e),
Error::MigrationDefinition(e) => Some(e),
}
}
}
impl From<rusqlite::Error> for Error {
fn from(e: rusqlite::Error) -> Error {
Error::RusqliteError {
query: String::new(),
err: e,
}
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(clippy::enum_variant_names)]
#[non_exhaustive]
pub enum SchemaVersionError {
TargetVersionOutOfRange {
specified: SchemaVersion,
highest: SchemaVersion,
},
}
impl fmt::Display for SchemaVersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SchemaVersionError::TargetVersionOutOfRange { specified, highest } => {
write!(f, "Attempt to migrate to version {}, which is higher than the highest version currently supported, {}.", specified, highest)
}
}
}
}
impl std::error::Error for SchemaVersionError {}
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(clippy::enum_variant_names)]
#[non_exhaustive]
pub enum MigrationDefinitionError {
DownNotDefined {
migration_index: usize,
},
NoMigrationsDefined,
}
impl fmt::Display for MigrationDefinitionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MigrationDefinitionError::DownNotDefined { migration_index } => {
write!(
f,
"Migration {} (version {} -> {}) cannot be reverted.",
migration_index,
migration_index,
migration_index + 1
)
}
MigrationDefinitionError::NoMigrationsDefined => {
write!(f, "Attempt to migrate with no migrations defined")
}
}
}
}
impl std::error::Error for MigrationDefinitionError {}