Archived
1
0
Fork 0

feat: init migra lib

This commit is contained in:
Dmitriy Pleshevskiy 2021-05-17 01:21:44 +03:00
parent c144086cb1
commit f2ee2575a1
5 changed files with 103 additions and 0 deletions

View file

@ -1,4 +1,5 @@
[workspace]
members = [
"migra",
"migra-cli"
]

View file

@ -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"] }

9
migra/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "migra"
version = "0.1.0"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
migra/src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
#![deny(missing_debug_implementations)]
#![deny(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
pub mod migration;

87
migra/src/migration.rs Normal file
View file

@ -0,0 +1,87 @@
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Migration {
name: String,
upgrade_sql_content: Option<String>,
downgrade_sql_content: Option<String>,
}
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<Migration>,
}
impl<T: AsRef<str>> From<Vec<T>> for List {
fn from(list: Vec<T>) -> 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<T: AsRef<str>>(&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)
}
}
}