Skip to content

Commit f8bd873

Browse files
TheRustifyergbm25
andauthored
Added "migrations" feature (#41)
* Creating feature "migrations". Changed "canyon_observer" to "canyon_migrations" * WIP - Bringing back canyon_manager as canyon_entities * Refactoring functions rust datatype to database datatype to be standalone * Cargo fmt * Added lib and Cargo.toml for canyon_entities --------- Co-authored-by: Gonzalo Busto Musi <gonzalo.busto@gmail.com>
1 parent f470c2a commit f8bd873

27 files changed

Lines changed: 342 additions & 430 deletions

File tree

.github/workflows/code-quality.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
strategy:
5656
fail-fast: false
5757
matrix:
58-
crate: [canyon_connection, canyon_crud, canyon_macros, canyon_observer]
58+
crate: [canyon_connection, canyon_crud, canyon_macros, canyon_migrations]
5959
steps:
6060
- uses: actions/checkout@v3
6161

Cargo.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ description.workspace = true
1313
members = [
1414
"canyon_connection",
1515
"canyon_crud",
16-
"canyon_observer",
16+
"canyon_entities",
17+
"canyon_migrations",
1718
"canyon_macros",
1819

1920
"tests"
@@ -23,7 +24,8 @@ members = [
2324
# Project crates
2425
canyon_connection = { workspace = true, path = "canyon_connection" }
2526
canyon_crud = { workspace = true, path = "canyon_crud" }
26-
canyon_observer = { workspace = true, path = "canyon_observer" }
27+
canyon_entities = { workspace = true, path = "canyon_entities" }
28+
canyon_migrations = { workspace = true, path = "canyon_migrations", optional = true }
2729
canyon_macros = { workspace = true, path = "canyon_macros" }
2830

2931
# To be marked as opt deps
@@ -33,7 +35,8 @@ tiberius = { workspace = true, optional = true }
3335
[workspace.dependencies]
3436
canyon_crud = { version = "0.3.1", path = "canyon_crud" }
3537
canyon_connection = { version = "0.3.1", path = "canyon_connection" }
36-
canyon_observer = { version = "0.3.1", path = "canyon_observer" }
38+
canyon_entities = { version = "0.3.1", path = "canyon_entities" }
39+
canyon_migrations = { version = "0.3.1", path = "canyon_migrations"}
3740
canyon_macros = { version = "0.3.1", path = "canyon_macros" }
3841

3942
tokio = { version = "1.27.0", features = ["full"] }
@@ -52,20 +55,22 @@ toml = "0.7.3"
5255
async-trait = "0.1.68"
5356
walkdir = "2.3.3"
5457
regex = "1.5"
58+
partialdebug = "0.2.0"
5559

5660
quote = "1.0.9"
5761
proc-macro2 = "1.0.27"
5862

5963
[workspace.package]
6064
version = "0.3.1"
6165
edition = "2021"
62-
authors = ["Alex Vergara<pyzyryab@tutanota.com>, Gonzalo Busto<gbm25@gmail.com>"]
66+
authors = ["Alex Vergara<pyzyryab@tutanota.com>, Gonzalo Busto Musi<gonzalo.busto@gmail.com>"]
6367
documentation = "https://zerodaycode.github.io/canyon-book/"
6468
homepage = "https://github.com/zerodaycode/Canyon-SQL"
6569
readme = "README.md"
6670
license = "MIT"
6771
description = "A Rust ORM and QueryBuilder"
6872

6973
[features]
70-
postgres = ["tokio-postgres", "canyon_connection/postgres", "canyon_crud/postgres", "canyon_observer/postgres", "canyon_macros/postgres"]
71-
mssql = ["tiberius", "canyon_connection/mssql", "canyon_crud/mssql", "canyon_observer/mssql", "canyon_macros/mssql"]
74+
postgres = ["tokio-postgres", "canyon_connection/postgres", "canyon_crud/postgres", "canyon_migrations/postgres", "canyon_macros/postgres"]
75+
mssql = ["tiberius", "canyon_connection/mssql", "canyon_crud/mssql", "canyon_migrations/mssql", "canyon_macros/mssql"]
76+
migrations = ["canyon_migrations", "canyon_macros/migrations"]

bash_aliases.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ alias SqlServerInitializationLinux='cargo test initialize_sql_server_docker_inst
3939

4040

4141
# Publish Canyon-SQL to the registry with its dependencies
42-
alias PublishCanyon='cargo publish -p canyon_connection && cargo publish -p canyon_crud && cargo publish -p canyon_observer && cargo publish -p canyon_macros && cargo publish -p canyon_sql_root'
42+
alias PublishCanyon='cargo publish -p canyon_connection && cargo publish -p canyon_crud && cargo publish -p canyon_migrations && cargo publish -p canyon_macros && cargo publish -p canyon_sql_root'
4343

4444
# Collects the code coverage for the project (tests must run before this)
4545
alias CcEnvVars='export CARGO_INCREMENTAL=0

canyon_entities/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "canyon_entities"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
documentation.workspace = true
7+
homepage.workspace = true
8+
readme.workspace = true
9+
license.workspace = true
10+
description.workspace = true
11+
12+
[dependencies]
13+
regex = { workspace = true }
14+
partialdebug = { workspace = true }
15+
quote = { workspace = true }
16+
proc-macro2 = { workspace = true }
17+
syn = { version = "1.0.86", features = ["full", "parsing"] } # TODO Pending to refactor and upgrade
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syn::{
1010
use super::entity_fields::EntityField;
1111

1212
/// Provides a convenient way of handling the data on any
13-
/// `CanyonEntity` struct anntotaded with the macro `#[canyon_entity]`
13+
/// `CanyonEntity` struct annotated with the macro `#[canyon_entity]`
1414
#[derive(PartialDebug, Clone)]
1515
pub struct CanyonEntity {
1616
pub struct_name: Ident,
File renamed without changes.
File renamed without changes.

canyon_entities/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::register_types::CanyonRegisterEntity;
2+
use std::sync::Mutex;
3+
4+
pub mod entity;
5+
pub mod entity_fields;
6+
pub mod field_annotation;
7+
pub mod manager_builder;
8+
pub mod register_types;
9+
10+
pub static CANYON_REGISTER_ENTITIES: Mutex<Vec<CanyonRegisterEntity<'static>>> =
11+
Mutex::new(Vec::new());
File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// This file contains `Rust` types that represents an entry on the `CanyonRegister`
2+
/// where `Canyon` tracks the user types that has to manage
3+
4+
pub const NUMERIC_PK_DATATYPE: [&str; 6] = ["i16", "u16", "i32", "u32", "i64", "u64"];
5+
6+
/// Gets the necessary identifiers of a CanyonEntity to make it the comparative
7+
/// against the database schemas
8+
#[derive(Debug, Clone, Default)]
9+
pub struct CanyonRegisterEntity<'a> {
10+
pub entity_name: &'a str,
11+
pub entity_db_table_name: &'a str,
12+
pub user_schema_name: Option<&'a str>,
13+
pub entity_fields: Vec<CanyonRegisterEntityField>,
14+
}
15+
16+
/// Complementary type for a field that represents a struct field that maps
17+
/// some real database column data
18+
#[derive(Debug, Clone, Default)]
19+
pub struct CanyonRegisterEntityField {
20+
pub field_name: String,
21+
pub field_type: String,
22+
pub annotations: Vec<String>,
23+
}
24+
25+
impl CanyonRegisterEntityField {
26+
/// Return if the field is autoincremental
27+
pub fn is_autoincremental(&self) -> bool {
28+
let has_pk_annotation = self
29+
.annotations
30+
.iter()
31+
.find(|a| a.starts_with("Annotation: PrimaryKey"));
32+
33+
let pk_is_autoincremental = match has_pk_annotation {
34+
Some(annotation) => annotation.contains("true"),
35+
None => false,
36+
};
37+
38+
NUMERIC_PK_DATATYPE.contains(&self.field_type.as_str()) && pk_is_autoincremental
39+
}
40+
41+
/// Return the nullability of a the field
42+
pub fn is_nullable(&self) -> bool {
43+
self.field_type.to_uppercase().starts_with("OPTION")
44+
}
45+
}

0 commit comments

Comments
 (0)