Skip to content

Commit ffc8870

Browse files
authored
fix: include APP_NAME in model (#228)
1 parent 30cf784 commit ffc8870

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

cot-macros/src/model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl ModelBuilder {
170170
const COLUMNS: &'static [#orm_ident::Column] = &[
171171
#(#fields_as_columns,)*
172172
];
173+
const APP_NAME: &'static str = env!("CARGO_PKG_NAME");
173174
const TABLE_NAME: #orm_ident::Identifier = #orm_ident::Identifier::new(#table_name);
174175
const PRIMARY_KEY_NAME: #orm_ident::Identifier = #orm_ident::Identifier::new(#pk_column_name);
175176

cot/src/db.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ pub trait Model: Sized + Send + 'static {
125125
/// The primary key type of the model.
126126
type PrimaryKey: PrimaryKey;
127127

128+
/// The name of the app this model is defined in.
129+
const APP_NAME: &'static str;
130+
128131
/// The name of the table in the database.
129132
const TABLE_NAME: Identifier;
130133

cot/src/db/migrations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ enum MigrationDependencyInner {
15031503
},
15041504
Model {
15051505
app: &'static str,
1506-
table_name: &'static str,
1506+
table_name: Identifier,
15071507
},
15081508
}
15091509

@@ -1528,7 +1528,7 @@ impl MigrationDependency {
15281528
/// creates the model with the given app and table name before the current
15291529
/// migration.
15301530
#[must_use]
1531-
pub const fn model(app: &'static str, table_name: &'static str) -> Self {
1531+
pub const fn model(app: &'static str, table_name: Identifier) -> Self {
15321532
Self::new(MigrationDependencyInner::Model { app, table_name })
15331533
}
15341534
}

cot/src/db/migrations/sorter.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use thiserror::Error;
55
use crate::db::migrations::{
66
DynMigration, MigrationDependency, MigrationDependencyInner, OperationInner,
77
};
8+
use crate::db::Identifier;
89
use crate::utils::graph::{apply_permutation, Graph};
910

1011
#[derive(Debug, Clone, PartialEq, Eq, Error)]
@@ -98,7 +99,7 @@ impl<'a, T: DynMigration> MigrationSorter<'a, T> {
9899
if let OperationInner::CreateModel { table_name, .. } = operation.inner {
99100
let app_and_model = MigrationLookup::ByAppAndModel {
100101
app: migration.app_name(),
101-
table_name: table_name.0,
102+
table_name,
102103
};
103104
if map.insert(app_and_model, index).is_some() {
104105
return Err(MigrationSorterError::DuplicateModel {
@@ -116,8 +117,14 @@ impl<'a, T: DynMigration> MigrationSorter<'a, T> {
116117

117118
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
118119
enum MigrationLookup<'a> {
119-
ByAppAndName { app: &'a str, name: &'a str },
120-
ByAppAndModel { app: &'a str, table_name: &'a str },
120+
ByAppAndName {
121+
app: &'a str,
122+
name: &'a str,
123+
},
124+
ByAppAndModel {
125+
app: &'a str,
126+
table_name: Identifier,
127+
},
121128
}
122129

123130
impl From<&MigrationDependency> for MigrationLookup<'_> {
@@ -179,11 +186,11 @@ mod tests {
179186
}));
180187
assert!(lookup.contains_key(&MigrationLookup::ByAppAndModel {
181188
app: "app1",
182-
table_name: "model1"
189+
table_name: Identifier::new("model1")
183190
}));
184191
assert!(lookup.contains_key(&MigrationLookup::ByAppAndModel {
185192
app: "app1",
186-
table_name: "model2"
193+
table_name: Identifier::new("model2")
187194
}));
188195
}
189196

0 commit comments

Comments
 (0)