Skip to content

Commit 11604da

Browse files
committed
Refactoring functions rust datatype to database datatype to be standalone
1 parent 1bf7ba1 commit 11604da

4 files changed

Lines changed: 197 additions & 197 deletions

File tree

Lines changed: 0 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,5 @@
1-
#[cfg(feature = "postgres")] use crate::constants::postgresql_type;
2-
#[cfg(feature = "mssql")] use crate::constants::sqlserver_type;
3-
use crate::constants::{regex_patterns, rust_type};
4-
51
pub mod handler;
62
pub mod information_schema;
73
pub mod memory;
84
pub mod processor;
95
pub mod transforms;
10-
11-
use canyon_entities::register_types::CanyonRegisterEntityField;
12-
use regex::Regex;
13-
14-
/// Return the postgres datatype and parameters to create a column for a given rust type
15-
#[cfg(feature = "postgres")]
16-
pub fn to_postgres_syntax(field: &CanyonRegisterEntityField) -> String {
17-
let rust_type_clean = field.field_type.replace(' ', "");
18-
19-
match rust_type_clean.as_str() {
20-
rust_type::I8 | rust_type::U8 => {
21-
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
22-
}
23-
rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(postgresql_type::INTEGER),
24-
25-
rust_type::I16 | rust_type::U16 => {
26-
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
27-
}
28-
rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(postgresql_type::INTEGER),
29-
30-
rust_type::I32 | rust_type::U32 => {
31-
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
32-
}
33-
rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(postgresql_type::INTEGER),
34-
35-
rust_type::I64 | rust_type::U64 => {
36-
String::from(&format!("{} NOT NULL", postgresql_type::BIGINT))
37-
}
38-
rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(postgresql_type::BIGINT),
39-
40-
rust_type::STRING => String::from(&format!("{} NOT NULL", postgresql_type::TEXT)),
41-
rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
42-
43-
rust_type::BOOL => String::from(&format!("{} NOT NULL", postgresql_type::BOOLEAN)),
44-
rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
45-
46-
rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", postgresql_type::DATE)),
47-
rust_type::OPT_NAIVE_DATE => String::from(postgresql_type::DATE),
48-
49-
rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", postgresql_type::TIME)),
50-
rust_type::OPT_NAIVE_TIME => String::from(postgresql_type::TIME),
51-
52-
rust_type::NAIVE_DATE_TIME => {
53-
String::from(&format!("{} NOT NULL", postgresql_type::DATETIME))
54-
}
55-
rust_type::OPT_NAIVE_DATE_TIME => String::from(postgresql_type::DATETIME),
56-
&_ => todo!("Not supported datatype for this migrations version"),
57-
}
58-
}
59-
60-
/// Return the postgres datatype and parameters to create a column for a given rust type
61-
/// for Microsoft SQL Server
62-
#[cfg(feature = "mssql")]
63-
pub fn to_sqlserver_syntax(field: &CanyonRegisterEntityField) -> String {
64-
let rust_type_clean = field.field_type.replace(' ', "");
65-
66-
match rust_type_clean.as_str() {
67-
rust_type::I8 | rust_type::U8 => {
68-
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
69-
}
70-
rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(sqlserver_type::INT),
71-
72-
rust_type::I16 | rust_type::U16 => {
73-
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
74-
}
75-
rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(sqlserver_type::INT),
76-
77-
rust_type::I32 | rust_type::U32 => {
78-
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
79-
}
80-
rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(sqlserver_type::INT),
81-
82-
rust_type::I64 | rust_type::U64 => {
83-
String::from(&format!("{} NOT NULL", sqlserver_type::BIGINT))
84-
}
85-
rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(sqlserver_type::BIGINT),
86-
87-
rust_type::STRING => {
88-
String::from(&format!("{} NOT NULL DEFAULT ''", sqlserver_type::NVARCHAR))
89-
}
90-
rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
91-
92-
rust_type::BOOL => String::from(&format!("{} NOT NULL", sqlserver_type::BIT)),
93-
rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
94-
95-
rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", sqlserver_type::DATE)),
96-
rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
97-
98-
rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", sqlserver_type::TIME)),
99-
rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
100-
101-
rust_type::NAIVE_DATE_TIME => {
102-
String::from(&format!("{} NOT NULL", sqlserver_type::DATETIME))
103-
}
104-
rust_type::OPT_NAIVE_DATE_TIME => String::from(sqlserver_type::DATETIME),
105-
&_ => todo!("Not supported datatype for this migrations version"),
106-
}
107-
}
108-
109-
#[cfg(feature = "postgres")]
110-
pub fn to_postgres_alter_syntax(field: &CanyonRegisterEntityField) -> String {
111-
let mut rust_type_clean = field.field_type.replace(' ', "");
112-
let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
113-
114-
if rs_type_is_optional {
115-
let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
116-
let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
117-
rust_type_clean = capture_rust_type
118-
.name("rust_type")
119-
.unwrap()
120-
.as_str()
121-
.to_string();
122-
}
123-
124-
match rust_type_clean.as_str() {
125-
rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
126-
String::from(postgresql_type::INT_8)
127-
}
128-
rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
129-
String::from(postgresql_type::SMALL_INT)
130-
}
131-
rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
132-
String::from(postgresql_type::INTEGER)
133-
}
134-
rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
135-
String::from(postgresql_type::BIGINT)
136-
}
137-
rust_type::STRING | rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
138-
rust_type::BOOL | rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
139-
rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => {
140-
String::from(postgresql_type::DATE)
141-
}
142-
rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => {
143-
String::from(postgresql_type::TIME)
144-
}
145-
rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
146-
String::from(postgresql_type::DATETIME)
147-
}
148-
&_ => todo!("Not supported datatype for this migrations version"),
149-
}
150-
}
151-
152-
#[cfg(feature = "mssql")]
153-
pub fn to_sqlserver_alter_syntax(field: &CanyonRegisterEntityField) -> String {
154-
let mut rust_type_clean = field.field_type.replace(' ', "");
155-
let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
156-
157-
if rs_type_is_optional {
158-
let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
159-
let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
160-
rust_type_clean = capture_rust_type
161-
.name("rust_type")
162-
.unwrap()
163-
.as_str()
164-
.to_string();
165-
}
166-
167-
match rust_type_clean.as_str() {
168-
rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
169-
String::from(sqlserver_type::TINY_INT)
170-
}
171-
rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
172-
String::from(sqlserver_type::SMALL_INT)
173-
}
174-
rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
175-
String::from(sqlserver_type::INT)
176-
}
177-
rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
178-
String::from(sqlserver_type::BIGINT)
179-
}
180-
rust_type::STRING | rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
181-
rust_type::BOOL | rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
182-
rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
183-
rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
184-
rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
185-
String::from(sqlserver_type::DATETIME)
186-
}
187-
&_ => todo!("Not supported datatype for this migrations version"),
188-
}
189-
}

canyon_migrations/src/migrations/processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::save_migrations_query_to_execute;
1414
use super::information_schema::{ColumnMetadata, TableMetadata};
1515
use super::memory::CanyonMemory;
1616
use canyon_entities::register_types::{CanyonRegisterEntity, CanyonRegisterEntityField};
17-
#[cfg(feature = "postgres")] use crate::migrations::{to_postgres_alter_syntax, to_postgres_syntax};
18-
#[cfg(feature = "mssql")] use crate::migrations::{to_sqlserver_alter_syntax, to_sqlserver_syntax};
17+
#[cfg(feature = "postgres")] use crate::migrations::transforms::{to_postgres_alter_syntax, to_postgres_syntax};
18+
#[cfg(feature = "mssql")] use crate::migrations::transforms::{to_sqlserver_alter_syntax, to_sqlserver_syntax};
1919

2020
/// Responsible of generating the queries to sync the database status with the
2121
/// Rust source code managed by Canyon, for successfully make the migrations
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#[cfg(feature = "postgres")] use crate::constants::postgresql_type;
2+
#[cfg(feature = "mssql")] use crate::constants::sqlserver_type;
3+
use crate::constants::{regex_patterns, rust_type};
4+
5+
6+
use canyon_entities::register_types::CanyonRegisterEntityField;
7+
use regex::Regex;
8+
9+
/// Return the postgres datatype and parameters to create a column for a given rust type
10+
#[cfg(feature = "postgres")]
11+
pub fn to_postgres_syntax(field: &CanyonRegisterEntityField) -> String {
12+
let rust_type_clean = field.field_type.replace(' ', "");
13+
14+
match rust_type_clean.as_str() {
15+
rust_type::I8 | rust_type::U8 => {
16+
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
17+
}
18+
rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(postgresql_type::INTEGER),
19+
20+
rust_type::I16 | rust_type::U16 => {
21+
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
22+
}
23+
rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(postgresql_type::INTEGER),
24+
25+
rust_type::I32 | rust_type::U32 => {
26+
String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
27+
}
28+
rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(postgresql_type::INTEGER),
29+
30+
rust_type::I64 | rust_type::U64 => {
31+
String::from(&format!("{} NOT NULL", postgresql_type::BIGINT))
32+
}
33+
rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(postgresql_type::BIGINT),
34+
35+
rust_type::STRING => String::from(&format!("{} NOT NULL", postgresql_type::TEXT)),
36+
rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
37+
38+
rust_type::BOOL => String::from(&format!("{} NOT NULL", postgresql_type::BOOLEAN)),
39+
rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
40+
41+
rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", postgresql_type::DATE)),
42+
rust_type::OPT_NAIVE_DATE => String::from(postgresql_type::DATE),
43+
44+
rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", postgresql_type::TIME)),
45+
rust_type::OPT_NAIVE_TIME => String::from(postgresql_type::TIME),
46+
47+
rust_type::NAIVE_DATE_TIME => {
48+
String::from(&format!("{} NOT NULL", postgresql_type::DATETIME))
49+
}
50+
rust_type::OPT_NAIVE_DATE_TIME => String::from(postgresql_type::DATETIME),
51+
&_ => todo!("Not supported datatype for this migrations version"),
52+
}
53+
}
54+
55+
/// Return the postgres datatype and parameters to create a column for a given rust type
56+
/// for Microsoft SQL Server
57+
#[cfg(feature = "mssql")]
58+
pub fn to_sqlserver_syntax(field: &CanyonRegisterEntityField) -> String {
59+
let rust_type_clean = field.field_type.replace(' ', "");
60+
61+
match rust_type_clean.as_str() {
62+
rust_type::I8 | rust_type::U8 => {
63+
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
64+
}
65+
rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(sqlserver_type::INT),
66+
67+
rust_type::I16 | rust_type::U16 => {
68+
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
69+
}
70+
rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(sqlserver_type::INT),
71+
72+
rust_type::I32 | rust_type::U32 => {
73+
String::from(&format!("{} NOT NULL", sqlserver_type::INT))
74+
}
75+
rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(sqlserver_type::INT),
76+
77+
rust_type::I64 | rust_type::U64 => {
78+
String::from(&format!("{} NOT NULL", sqlserver_type::BIGINT))
79+
}
80+
rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(sqlserver_type::BIGINT),
81+
82+
rust_type::STRING => {
83+
String::from(&format!("{} NOT NULL DEFAULT ''", sqlserver_type::NVARCHAR))
84+
}
85+
rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
86+
87+
rust_type::BOOL => String::from(&format!("{} NOT NULL", sqlserver_type::BIT)),
88+
rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
89+
90+
rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", sqlserver_type::DATE)),
91+
rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
92+
93+
rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", sqlserver_type::TIME)),
94+
rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
95+
96+
rust_type::NAIVE_DATE_TIME => {
97+
String::from(&format!("{} NOT NULL", sqlserver_type::DATETIME))
98+
}
99+
rust_type::OPT_NAIVE_DATE_TIME => String::from(sqlserver_type::DATETIME),
100+
&_ => todo!("Not supported datatype for this migrations version"),
101+
}
102+
}
103+
104+
#[cfg(feature = "postgres")]
105+
pub fn to_postgres_alter_syntax(field: &CanyonRegisterEntityField) -> String {
106+
let mut rust_type_clean = field.field_type.replace(' ', "");
107+
let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
108+
109+
if rs_type_is_optional {
110+
let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
111+
let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
112+
rust_type_clean = capture_rust_type
113+
.name("rust_type")
114+
.unwrap()
115+
.as_str()
116+
.to_string();
117+
}
118+
119+
match rust_type_clean.as_str() {
120+
rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
121+
String::from(postgresql_type::INT_8)
122+
}
123+
rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
124+
String::from(postgresql_type::SMALL_INT)
125+
}
126+
rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
127+
String::from(postgresql_type::INTEGER)
128+
}
129+
rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
130+
String::from(postgresql_type::BIGINT)
131+
}
132+
rust_type::STRING | rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
133+
rust_type::BOOL | rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
134+
rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => {
135+
String::from(postgresql_type::DATE)
136+
}
137+
rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => {
138+
String::from(postgresql_type::TIME)
139+
}
140+
rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
141+
String::from(postgresql_type::DATETIME)
142+
}
143+
&_ => todo!("Not supported datatype for this migrations version"),
144+
}
145+
}
146+
147+
#[cfg(feature = "mssql")]
148+
pub fn to_sqlserver_alter_syntax(field: &CanyonRegisterEntityField) -> String {
149+
let mut rust_type_clean = field.field_type.replace(' ', "");
150+
let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
151+
152+
if rs_type_is_optional {
153+
let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
154+
let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
155+
rust_type_clean = capture_rust_type
156+
.name("rust_type")
157+
.unwrap()
158+
.as_str()
159+
.to_string();
160+
}
161+
162+
match rust_type_clean.as_str() {
163+
rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
164+
String::from(sqlserver_type::TINY_INT)
165+
}
166+
rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
167+
String::from(sqlserver_type::SMALL_INT)
168+
}
169+
rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
170+
String::from(sqlserver_type::INT)
171+
}
172+
rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
173+
String::from(sqlserver_type::BIGINT)
174+
}
175+
rust_type::STRING | rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
176+
rust_type::BOOL | rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
177+
rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
178+
rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
179+
rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
180+
String::from(sqlserver_type::DATETIME)
181+
}
182+
&_ => todo!("Not supported datatype for this migrations version"),
183+
}
184+
}

0 commit comments

Comments
 (0)