Skip to content

Commit 2e48471

Browse files
committed
#update Refactored and upgraded the Canyon Memory process. Also, queries are now full parameterized.
1 parent 6d779e1 commit 2e48471

9 files changed

Lines changed: 191 additions & 189 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ Year format is defined as: `YYYY-m-d`
99

1010
## [Unreleased]
1111

12-
## [0.1.1] - 2023 - 03 - 20
12+
## [0.1.2] - 2023 - 03 - 23
13+
14+
### Update
15+
16+
- Removed from `postgresql` migrations the auto conversion to lowercase of the table and column names
1317

1418
### Fix
1519

20+
- Solved a bug in the canyon_entity proc macro that was wiring the incorrect user table name in the migrations
21+
22+
## [0.1.1] - 2023 - 03 - 20
23+
24+
### Update
25+
1626
- Adding more types to the supported ones for Tiberius in the row mapper
1727

1828
## [0.1.0] - 2022 - 12 - 25

canyon_macros/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,14 @@ pub fn canyon_entity(
196196
.expect("Something went wrong parsing the `table_name` argument")
197197
.to_string();
198198

199-
if attr_arg_ident == "table_name" || attr_arg_ident == "schema" {
200-
table_name = Some(Box::leak(attr_arg_ident.into_boxed_str()));
199+
if &attr_arg_ident == "table_name" || &attr_arg_ident == "schema" {
201200
match nv.lit {
202201
syn::Lit::Str(ref l) => {
203-
schema_name = Some(Box::leak(l.value().into_boxed_str()))
202+
if &attr_arg_ident == "table_name" {
203+
table_name = Some(Box::leak(l.value().into_boxed_str()))
204+
} else {
205+
schema_name = Some(Box::leak(l.value().into_boxed_str()))
206+
}
204207
}
205208
_ => {
206209
parsing_attribute_error = Some(syn::Error::new(
@@ -257,7 +260,9 @@ pub fn canyon_entity(
257260
let mut new_entity = CanyonRegisterEntity::default();
258261
let e = Box::leak(entity.struct_name.to_string().into_boxed_str());
259262
new_entity.entity_name = e;
260-
new_entity.user_table_name = table_name;
263+
new_entity.entity_db_table_name = table_name.unwrap_or(
264+
Box::leak(helpers::default_database_table_name_from_entity_name(e).into_boxed_str())
265+
);
261266
new_entity.user_schema_name = schema_name;
262267

263268
// The entity fields

canyon_macros/src/utils/helpers.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub fn table_schema_parser(macro_data: &MacroTokens<'_>) -> Result<String, Token
8181
}
8282

8383
/// Parses a syn::Identifier to get a snake case database name from the type identifier
84-
/// TODO: #[macro(table_name = 'user_defined_db_table_name)]'
8584
pub fn _database_table_name_from_struct(ty: &Ident) -> String {
8685
let struct_name: String = ty.to_string();
8786
let mut table_name: String = String::new();
@@ -105,9 +104,8 @@ pub fn _database_table_name_from_struct(ty: &Ident) -> String {
105104
table_name
106105
}
107106

108-
/// Parses a syn::Identifier to get a snake case database name from the type identifier
109-
/// TODO: #[macro(table_name = 'user_defined_db_table_name)]'
110-
pub fn _database_table_name_from_entity_name(ty: &str) -> String {
107+
/// Parses a syn::Identifier to create a defaulted snake case database table name
108+
pub fn default_database_table_name_from_entity_name(ty: &str) -> String {
111109
let struct_name: String = ty.to_string();
112110
let mut table_name: String = String::new();
113111

canyon_observer/src/constants.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
pub mod queries {}
1+
pub const NUMERIC_PK_DATATYPE: [&str; 6] = ["i16", "u16", "i32", "u32", "i64", "u64"];
2+
3+
pub mod queries {
4+
pub const INSERT_INTO_CANYON_MEMORY: &str =
5+
"INSERT INTO canyon_memory (filepath, struct_name, declared_table_name) \
6+
VALUES ($1, $2, $3)";
7+
pub const UPDATE_CANYON_MEMORY: &str =
8+
"UPDATE canyon_memory SET filepath = $1, struct_name = $2, \
9+
declared_table_name = $3 WHERE id = $4";
10+
pub const DELETE_FROM_CANYON_MEMORY: &str =
11+
"DELETE FROM canyon_memory WHERE struct_name = $1";
12+
}
213

314
pub mod postgresql_queries {
415
pub static CANYON_MEMORY_TABLE: &str = "CREATE TABLE IF NOT EXISTS canyon_memory (
516
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
617
filepath VARCHAR NOT NULL,
7-
struct_name VARCHAR NOT NULL
18+
struct_name VARCHAR NOT NULL,
19+
declared_table_name VARCHAR NOT NULL
820
)";
921

1022
pub static FETCH_PUBLIC_SCHEMA: &str =
@@ -38,9 +50,10 @@ pub mod mssql_queries {
3850
pub static CANYON_MEMORY_TABLE: &str = "IF OBJECT_ID(N'[dbo].[canyon_memory]', N'U') IS NULL
3951
BEGIN
4052
CREATE TABLE dbo.canyon_memory (
41-
id INT PRIMARY KEY IDENTITY,
42-
filepath NVARCHAR(250) NOT NULL,
43-
struct_name NVARCHAR(100) NOT NULL
53+
id INT PRIMARY KEY IDENTITY,
54+
filepath NVARCHAR(250) NOT NULL,
55+
struct_name NVARCHAR(100) NOT NULL,
56+
declared_table_name NVARCHAR(100) NOT NULL
4457
);
4558
END";
4659

@@ -166,17 +179,8 @@ pub mod sqlserver_type {
166179
pub const DATETIME: &str = "DATETIME2";
167180
}
168181

169-
/// Contains fragments queries to be invoked as const items and to be concatenated
170-
/// with dynamic data
171-
///
172-
/// Ex: ` format!("{} PRIMARY KEY GENERATED ALWAYS AS IDENTITY", postgres_datatype_syntax)`
173-
pub mod query_chunk {
174-
// TODO @gbm25
175-
}
176-
177182
pub mod mocked_data {
178183
use canyon_connection::lazy_static::lazy_static;
179-
180184
use crate::migrations::information_schema::{ColumnMetadata, TableMetadata};
181185

182186
lazy_static! {

canyon_observer/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub mod migrations;
1313

1414
extern crate canyon_crud;
1515

16-
// The migrator tool
1716
mod constants;
1817
pub mod manager;
1918

@@ -26,4 +25,27 @@ pub static CANYON_REGISTER_ENTITIES: Mutex<Vec<CanyonRegisterEntity<'static>>> =
2625
lazy_static! {
2726
pub static ref QUERIES_TO_EXECUTE: Mutex<HashMap<&'static str, Vec<String>>> =
2827
Mutex::new(HashMap::new());
28+
pub static ref CM_QUERIES_TO_EXECUTE: Mutex<HashMap<&'static str, Vec<(&'static str, Vec<String>)>>> = // TODO Provisional until we parameterize as well the migration queries
29+
Mutex::new(HashMap::new());
2930
}
31+
32+
// TODO replace the unwraps for the operator ? when the appropiated crate will be added
33+
pub fn add_cm_query_to_execute(stmt: &'static str, datasource_name: &'static str, params: Vec<String>) {
34+
if CM_QUERIES_TO_EXECUTE
35+
.lock()
36+
.unwrap()
37+
.contains_key(datasource_name)
38+
{
39+
CM_QUERIES_TO_EXECUTE
40+
.lock()
41+
.unwrap()
42+
.get_mut(datasource_name)
43+
.unwrap()
44+
.push((stmt, params));
45+
} else {
46+
CM_QUERIES_TO_EXECUTE
47+
.lock()
48+
.unwrap()
49+
.insert(datasource_name, vec![(stmt, params)]);
50+
}
51+
}

canyon_observer/src/migrations/handler.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,41 @@ impl Migrations {
4747

4848
let mut migrations_processor = MigrationsProcessor::default();
4949

50-
let canyon_memory = CanyonMemory::remember(datasource).await;
51-
let canyon_tables = CANYON_REGISTER_ENTITIES.lock().unwrap().to_vec();
50+
let canyon_entities = CANYON_REGISTER_ENTITIES.lock().unwrap().to_vec();
51+
let canyon_memory = CanyonMemory::remember(datasource, &canyon_entities).await;
52+
// println!("Canyon memory: {:?}", &canyon_memory);
53+
// println!("Canyon tables: {:?}", &canyon_entities);
5254

5355
// Tracked entities that must be migrated whenever Canyon starts
5456
let schema_status =
5557
Self::fetch_database(datasource.name, datasource.properties.db_type).await;
5658
let database_tables_schema_info = Self::map_rows(schema_status);
59+
// println!("DB tables: {:?}", &database_tables_schema_info);
60+
5761

5862
// We filter the tables from the schema that aren't Canyon entities
5963
let mut user_database_tables = vec![];
6064
for parsed_table in database_tables_schema_info.iter() {
6165
if canyon_memory
6266
.memory
63-
.values()
64-
.any(|f| f.to_lowercase() == parsed_table.table_name)
65-
|| canyon_memory
66-
.renamed_entities
67-
.values()
68-
.any(|f| *f == parsed_table.table_name.to_lowercase())
67+
.iter()
68+
.any(|f|
69+
f.declared_table_name.eq(&parsed_table.table_name)
70+
) || canyon_memory
71+
.renamed_entities
72+
.values()
73+
.any(|f| *f == parsed_table.table_name)
6974
{
7075
user_database_tables.append(&mut vec![parsed_table]);
7176
}
7277
}
7378

79+
println!("Tables to process: {:?}", user_database_tables.iter().map(|t| &t.table_name).collect::<Vec<_>>());
80+
7481
migrations_processor
7582
.process(
7683
canyon_memory,
77-
canyon_tables,
84+
canyon_entities,
7885
user_database_tables,
7986
datasource,
8087
)

0 commit comments

Comments
 (0)