From f2ee2575a1ebfa98f9fdda216180120de90834e7 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 17 May 2021 01:21:44 +0300 Subject: [PATCH] feat: init migra lib --- Cargo.toml | 1 + migra-cli/Cargo.toml | 1 + migra/Cargo.toml | 9 +++++ migra/src/lib.rs | 5 +++ migra/src/migration.rs | 87 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 migra/Cargo.toml create mode 100644 migra/src/lib.rs create mode 100644 migra/src/migration.rs diff --git a/Cargo.toml b/Cargo.toml index dfca278..1f4513f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ + "migra", "migra-cli" ] \ No newline at end of file diff --git a/migra-cli/Cargo.toml b/migra-cli/Cargo.toml index d2a0791..42fa61b 100644 --- a/migra-cli/Cargo.toml +++ b/migra-cli/Cargo.toml @@ -14,6 +14,7 @@ readme = "../README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +migra = { version = "0", path = "../migra" } cfg-if = "1.0" structopt = "0.3" serde = { version = "1.0", features = ["derive"] } diff --git a/migra/Cargo.toml b/migra/Cargo.toml new file mode 100644 index 0000000..ccb49a8 --- /dev/null +++ b/migra/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "migra" +version = "0.1.0" +authors = ["Dmitriy Pleshevskiy "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/migra/src/lib.rs b/migra/src/lib.rs new file mode 100644 index 0000000..60a2864 --- /dev/null +++ b/migra/src/lib.rs @@ -0,0 +1,5 @@ +#![deny(missing_debug_implementations)] +#![deny(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] + +pub mod migration; diff --git a/migra/src/migration.rs b/migra/src/migration.rs new file mode 100644 index 0000000..6f66cb9 --- /dev/null +++ b/migra/src/migration.rs @@ -0,0 +1,87 @@ +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Migration { + name: String, + upgrade_sql_content: Option, + downgrade_sql_content: Option, +} + +impl Migration { + #[must_use] + pub fn new(name: &str) -> Self { + Migration { + name: name.to_owned(), + ..Migration::default() + } + } + + #[must_use] + pub fn with_upgrade(name: &str, up_content: &str) -> Self { + Migration { + name: name.to_owned(), + upgrade_sql_content: Some(up_content.to_owned()), + ..Migration::default() + } + } + + #[must_use] + pub fn with_upgrade_and_downgrade(name: &str, up_content: &str, down_content: &str) -> Self { + Migration { + name: name.to_owned(), + upgrade_sql_content: Some(up_content.to_owned()), + downgrade_sql_content: Some(down_content.to_owned()), + } + } + + #[must_use] + pub fn name(&self) -> &String { + &self.name + } + + #[must_use] + pub fn upgrade_sql_content(&self) -> Option<&String> { + self.upgrade_sql_content.as_ref() + } + + #[must_use] + pub fn downgrade_sql_content(&self) -> Option<&String> { + self.downgrade_sql_content.as_ref() + } +} + +#[derive(Debug, Clone, Default)] +pub struct List { + inner: Vec, +} + +impl> From> for List { + fn from(list: Vec) -> Self { + List { + inner: list.iter().map(AsRef::as_ref).map(Migration::new).collect(), + } + } +} + +impl List { + #[must_use] + pub fn new() -> Self { + List { inner: Vec::new() } + } + + pub fn push>(&mut self, name: &T) { + self.inner.push(Migration::new(name.as_ref())) + } + + #[must_use] + pub fn contains(&self, name: &str) -> bool { + self.inner.iter().any(|migration| migration.name() == name) + } + + #[must_use] + pub fn maybe_next<'a>(&self, name: &'a str) -> Option<&'a str> { + if self.contains(name) { + None + } else { + Some(name) + } + } +}