revert when query failed
This commit is contained in:
parent
9ec9746233
commit
6025b0ef30
2 changed files with 15 additions and 7 deletions
|
@ -9,8 +9,7 @@ pub struct Config {
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct ScyllaConfig {
|
pub struct ScyllaConfig {
|
||||||
pub host: String,
|
pub nodes: Vec<String>,
|
||||||
pub port: u32,
|
|
||||||
pub keyspace: String,
|
pub keyspace: String,
|
||||||
pub replication_factor: i32,
|
pub replication_factor: i32,
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ impl Migrator {
|
||||||
|
|
||||||
pub(crate) async fn new(dir: PathBuf, config: &ScyllaConfig) -> Result<Self, Error> {
|
pub(crate) async fn new(dir: PathBuf, config: &ScyllaConfig) -> Result<Self, Error> {
|
||||||
let session = SessionBuilder::new()
|
let session = SessionBuilder::new()
|
||||||
.known_node(format!("{}:{}", config.host, config.port))
|
.known_nodes(&config.nodes)
|
||||||
.build()
|
.build()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -167,10 +167,6 @@ impl Migrator {
|
||||||
let now = Utc::now().timestamp_millis();
|
let now = Utc::now().timestamp_millis();
|
||||||
let cql = self.cql_fs(version, "up.cql")?;
|
let cql = self.cql_fs(version, "up.cql")?;
|
||||||
|
|
||||||
for query in cql {
|
|
||||||
self.session.query(query, &[]).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save migration history
|
// Save migration history
|
||||||
self.session
|
self.session
|
||||||
.execute(
|
.execute(
|
||||||
|
@ -178,6 +174,19 @@ impl Migrator {
|
||||||
(version, Timestamp(Duration::milliseconds(now))),
|
(version, Timestamp(Duration::milliseconds(now))),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
for query in cql {
|
||||||
|
match self.session.query(query.to_owned(), &[]).await {
|
||||||
|
Err(e) => {
|
||||||
|
println!("{query}");
|
||||||
|
println!("Query failed. Rolling back...");
|
||||||
|
self.down(1).await?;
|
||||||
|
|
||||||
|
return Err(e.into());
|
||||||
|
},
|
||||||
|
Ok(_) => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue