Skip to content

Commit 508d45b

Browse files
committed
Modern map api and other minor refactors
1 parent 5882824 commit 508d45b

File tree

1 file changed

+26
-47
lines changed

1 file changed

+26
-47
lines changed

src/make_migrations/mod.rs

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ pub struct MakeMigrationsOptions {
3131
pub warnings_disabled: bool,
3232
}
3333

34-
/**
35-
Checks the options
36-
*/
34+
/// Checks the options
3735
pub fn check_options(options: &MakeMigrationsOptions) -> anyhow::Result<()> {
3836
let models_file = Path::new(options.models_file.as_str());
3937
if !models_file.exists() || !models_file.is_file() {
@@ -59,11 +57,9 @@ pub fn check_options(options: &MakeMigrationsOptions) -> anyhow::Result<()> {
5957
Ok(())
6058
}
6159

62-
/**
63-
A helper function to retrieve the internal models from a given location.
64-
65-
`models_file`: [&str]: The path to the models file.
66-
*/
60+
/// A helper function to retrieve the internal models from a given location.
61+
///
62+
/// `models_file`: [&str]: The path to the models file.
6763
pub fn get_internal_models(models_file: &str) -> anyhow::Result<InternalModelFormat> {
6864
let internal_str = read_to_string(Path::new(&models_file))
6965
.with_context(|| "Couldn't read internal models file")?;
@@ -73,9 +69,7 @@ pub fn get_internal_models(models_file: &str) -> anyhow::Result<InternalModelFor
7369
Ok(internal)
7470
}
7571

76-
/**
77-
Runs the make-migrations tool
78-
*/
72+
/// Runs the make-migrations tool
7973
pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()> {
8074
check_options(&options).with_context(|| "Error while checking options")?;
8175

@@ -106,7 +100,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
106100
.with_context(|| "Error while parsing existing migration files")?;
107101

108102
let last_id: u16 = last_migration.id + 1;
109-
let name = options.name.as_ref().map_or("placeholder", |x| x.as_str());
103+
let name = options.name.as_deref().unwrap_or("placeholder");
110104

111105
let mut op: Vec<Operation> = vec![];
112106

@@ -151,53 +145,40 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
151145
// Iterate over all models, that are in the constructed
152146
// as well as in the new internal models
153147
for new_model in &internal_models.models {
154-
if !old_lookup.contains_key(new_model.name.as_str()) {
148+
let Some(old_model) = old_lookup.get(&new_model.name) else {
155149
continue;
156-
}
150+
};
157151

158152
// Check if a new field has been added
159153
for new_field in &new_model.fields {
160-
if !old_lookup[new_model.name.as_str()]
161-
.fields
162-
.iter()
163-
.any(|z| z.name == new_field.name)
164-
{
165-
if !new_fields.contains_key(new_model.name.as_str()) {
166-
new_fields.insert(new_model.name.clone(), vec![]);
167-
}
154+
if !old_model.fields.iter().any(|z| z.name == new_field.name) {
168155
new_fields
169-
.get_mut(new_model.name.as_str())
170-
.unwrap()
156+
.entry(new_model.name.clone())
157+
.or_default()
171158
.push(new_field);
172159
}
173160
}
174161

175162
// Check if a existing field got deleted
176-
for old_field in &old_lookup[new_model.name.as_str()].fields {
163+
for old_field in &old_model.fields {
177164
if !new_model.fields.iter().any(|z| z.name == old_field.name) {
178-
if !deleted_fields.contains_key(new_model.name.as_str()) {
179-
deleted_fields.insert(new_model.name.clone(), vec![]);
180-
}
181165
deleted_fields
182-
.get_mut(new_model.name.as_str())
183-
.unwrap()
166+
.entry(new_model.name.clone())
167+
.or_default()
184168
.push(old_field);
185169
}
186170
}
187171

188172
// Check if a existing field got altered
189-
for old_field in &old_lookup[new_model.name.as_str()].fields {
173+
for old_field in &old_model.fields {
190174
for new_field in &new_model.fields {
191175
// Check for differences
192176
if old_field.db_type != new_field.db_type
193177
|| old_field.annotations != new_field.annotations
194178
{
195-
if !altered_fields.contains_key(new_model.name.as_str()) {
196-
altered_fields.insert(new_model.name.clone(), vec![]);
197-
}
198179
altered_fields
199-
.get_mut(&new_model.name)
200-
.unwrap()
180+
.entry(new_model.name.clone())
181+
.or_default()
201182
.push((old_field, new_field));
202183
}
203184
}
@@ -245,7 +226,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
245226
if new_field
246227
.annotations
247228
.iter()
248-
.any(|z| z.eq_shallow(&Annotation::ForeignKey(Default::default())))
229+
.any(|x| matches!(x, Annotation::ForeignKey(_)))
249230
{
250231
references
251232
.entry(new_model.name.clone())
@@ -295,11 +276,10 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
295276
.as_str(),
296277
)
297278
{
298-
if !renamed_fields.contains_key(model_name) {
299-
renamed_fields.insert(model_name.clone(), vec![]);
300-
}
301-
let f = renamed_fields.get_mut(model_name).unwrap();
302-
f.push((old_field, new_field));
279+
renamed_fields
280+
.entry(model_name.clone())
281+
.or_default()
282+
.push((old_field, new_field));
303283
info!(
304284
"Renamed field {} of model {model_name} to {}.",
305285
old_field.name, new_field.name
@@ -416,7 +396,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
416396
if field
417397
.annotations
418398
.iter()
419-
.any(|z| z.eq_shallow(&Annotation::ForeignKey(Default::default())))
399+
.any(|x| matches!(x, Annotation::ForeignKey(_)))
420400
{
421401
references
422402
.entry(model.name.clone())
@@ -427,12 +407,11 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
427407
}
428408
}
429409

430-
let o = Operation::CreateModel {
410+
info!("Created model {}", model.name);
411+
Operation::CreateModel {
431412
name: model.name.clone(),
432413
fields: normal_fields,
433-
};
434-
info!("Created model {}", model.name);
435-
o
414+
}
436415
}));
437416

438417
operations.extend(references.into_iter().flat_map(|(model, fields)| {

0 commit comments

Comments
 (0)