Skip to content

Commit a42e66d

Browse files
committed
Replaced all for_each with normal for loops
1 parent 0826033 commit a42e66d

File tree

2 files changed

+126
-134
lines changed

2 files changed

+126
-134
lines changed

src/make_migrations/mod.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -135,63 +135,63 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
135135
let mut altered_fields: HashMap<String, Vec<(&Field, &Field)>> = HashMap::new();
136136

137137
// Check if any new models exist
138-
internal_models.models.iter().for_each(|x| {
138+
for x in &internal_models.models {
139139
if !old_lookup.iter().any(|(a, _)| x.name == *a) {
140140
new_models.push(x);
141141
}
142-
});
142+
}
143143

144144
// Check if any old model got deleted
145-
constructed.models.iter().for_each(|x| {
145+
for x in &constructed.models {
146146
if !new_lookup.iter().any(|(a, _)| x.name == *a) {
147147
deleted_models.push(x);
148148
}
149-
});
149+
}
150150

151151
// Iterate over all models, that are in the constructed
152152
// as well as in the new internal models
153-
internal_models
154-
.models
155-
.iter()
156-
.filter(|x| old_lookup.contains_key(x.name.as_str()))
157-
.for_each(|x| {
158-
// Check if a new field has been added
159-
x.fields.iter().for_each(|y| {
160-
if !old_lookup[x.name.as_str()]
161-
.fields
162-
.iter()
163-
.any(|z| z.name == y.name)
164-
{
165-
if !new_fields.contains_key(x.name.as_str()) {
166-
new_fields.insert(x.name.clone(), vec![]);
167-
}
168-
new_fields.get_mut(x.name.as_str()).unwrap().push(y);
153+
for x in &internal_models.models {
154+
if !old_lookup.contains_key(x.name.as_str()) {
155+
continue;
156+
}
157+
158+
// Check if a new field has been added
159+
for y in &x.fields {
160+
if !old_lookup[x.name.as_str()]
161+
.fields
162+
.iter()
163+
.any(|z| z.name == y.name)
164+
{
165+
if !new_fields.contains_key(x.name.as_str()) {
166+
new_fields.insert(x.name.clone(), vec![]);
169167
}
170-
});
168+
new_fields.get_mut(x.name.as_str()).unwrap().push(y);
169+
}
170+
}
171171

172-
// Check if a existing field got deleted
173-
old_lookup[x.name.as_str()].fields.iter().for_each(|y| {
174-
if !x.fields.iter().any(|z| z.name == y.name) {
175-
if !deleted_fields.contains_key(x.name.as_str()) {
176-
deleted_fields.insert(x.name.clone(), vec![]);
177-
}
178-
deleted_fields.get_mut(x.name.as_str()).unwrap().push(y);
172+
// Check if a existing field got deleted
173+
for y in &old_lookup[x.name.as_str()].fields {
174+
if !x.fields.iter().any(|z| z.name == y.name) {
175+
if !deleted_fields.contains_key(x.name.as_str()) {
176+
deleted_fields.insert(x.name.clone(), vec![]);
179177
}
180-
});
178+
deleted_fields.get_mut(x.name.as_str()).unwrap().push(y);
179+
}
180+
}
181181

182-
// Check if a existing field got altered
183-
old_lookup[x.name.as_str()].fields.iter().for_each(|y| {
184-
x.fields.iter().filter(|z| y.name == z.name).for_each(|z| {
185-
// Check for differences
186-
if y.db_type != z.db_type || y.annotations != z.annotations {
187-
if !altered_fields.contains_key(x.name.as_str()) {
188-
altered_fields.insert(x.name.clone(), vec![]);
189-
}
190-
altered_fields.get_mut(&x.name).unwrap().push((y, z));
182+
// Check if a existing field got altered
183+
for y in &old_lookup[x.name.as_str()].fields {
184+
for z in &x.fields {
185+
// Check for differences
186+
if y.db_type != z.db_type || y.annotations != z.annotations {
187+
if !altered_fields.contains_key(x.name.as_str()) {
188+
altered_fields.insert(x.name.clone(), vec![]);
191189
}
192-
});
193-
});
194-
});
190+
altered_fields.get_mut(&x.name).unwrap().push((y, z));
191+
}
192+
}
193+
}
194+
}
195195

196196
// Check if a model was renamed
197197
if !new_models.is_empty() && !deleted_models.is_empty() {
@@ -227,7 +227,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
227227
let mut references: HashMap<String, Vec<Field>> = HashMap::new();
228228

229229
// Create migration operations for new models
230-
new_models.iter().for_each(|x| {
230+
for x in &new_models {
231231
let mut normal_fields = vec![];
232232

233233
for y in &x.fields {
@@ -249,7 +249,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
249249
fields: normal_fields,
250250
});
251251
info!("Created model {}", x.name);
252-
});
252+
}
253253

254254
// Create referencing fields for new models
255255
for (model, fields) in references {
@@ -262,12 +262,12 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
262262
}
263263

264264
// Create migration operations for deleted models
265-
deleted_models.iter().for_each(|x| {
265+
for x in &deleted_models {
266266
op.push(Operation::DeleteModel {
267267
name: x.name.clone(),
268268
});
269269
info!("Deleted model {}", x.name);
270-
});
270+
}
271271

272272
for (model_name, new_fields) in &new_fields {
273273
if let Some(old_fields) = deleted_fields.get(model_name) {
@@ -298,7 +298,7 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
298298
}
299299
}
300300
// Remove renamed fields in existing models from new and deleted lists
301-
renamed_fields.iter().for_each(|(model_name, fields)| {
301+
for (model_name, fields) in &renamed_fields {
302302
for (old_field, new_field) in fields {
303303
new_fields
304304
.get_mut(model_name)
@@ -316,33 +316,33 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
316316
new: new_field.name.clone(),
317317
})
318318
}
319-
});
319+
}
320320

321321
// Create migration operations for new fields in existing models
322-
new_fields.iter().for_each(|(x, y)| {
323-
y.iter().for_each(|z| {
322+
for (x, y) in &new_fields {
323+
for z in y {
324324
op.push(Operation::CreateField {
325325
model: x.clone(),
326326
field: (*z).clone(),
327327
});
328328
info!("Added field {} to model {}", z.name, x);
329-
})
330-
});
329+
}
330+
}
331331

332332
// Create migration operations for deleted fields in existing models
333-
deleted_fields.iter().for_each(|(x, y)| {
334-
y.iter().for_each(|z| {
333+
for (x, y) in &deleted_fields {
334+
for z in y {
335335
op.push(Operation::DeleteField {
336336
model: x.clone(),
337337
name: z.name.clone(),
338338
});
339339
info!("Deleted field {} from model {}", z.name, x);
340-
})
341-
});
340+
}
341+
}
342342

343343
// Create migration operations for altered fields in existing models
344-
altered_fields.iter().for_each(|(model, af)| {
345-
af.iter().for_each(|(old, new)| {
344+
for (model, af) in &altered_fields {
345+
for (old, new) in af {
346346
// Check datatype
347347
if old.db_type != new.db_type {
348348
#[expect(clippy::match_single_binding, reason = "It will be extended™")]
@@ -376,8 +376,8 @@ pub fn run_make_migrations(options: MakeMigrationsOptions) -> anyhow::Result<()>
376376
});
377377
info!("Recreated field {} on model {}", &new.name, &model);
378378
}
379-
});
380-
});
379+
}
380+
}
381381

382382
new_migration = Some(Migration {
383383
hash: h.to_string(),

src/utils/migrations.rs

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -169,95 +169,87 @@ pub fn convert_migrations_to_internal_models(
169169
) -> anyhow::Result<InternalModelFormat> {
170170
let mut m = vec![];
171171

172-
let res: anyhow::Result<_> = migrations.iter().try_for_each(|x| {
173-
x.operations.iter().try_for_each(|y| match y {
174-
Operation::CreateModel { name, fields } => {
175-
m.push(Model {
176-
name: name.clone(),
177-
fields: fields.clone(),
178-
source_defined_at: None,
179-
});
180-
Ok(())
181-
}
182-
Operation::RenameModel { old, new } => {
183-
m = m
184-
.iter()
185-
.map(|z| {
186-
let mut a = z.clone();
187-
if &a.name == old {
188-
a.name = new.to_string();
172+
for x in migrations {
173+
for y in &x.operations {
174+
match y {
175+
Operation::CreateModel { name, fields } => {
176+
m.push(Model {
177+
name: name.clone(),
178+
fields: fields.clone(),
179+
source_defined_at: None,
180+
});
181+
}
182+
Operation::RenameModel { old, new } => {
183+
m = m
184+
.iter()
185+
.map(|z| {
186+
let mut a = z.clone();
187+
if &a.name == old {
188+
a.name = new.to_string();
189+
}
190+
a
191+
})
192+
.collect();
193+
}
194+
Operation::DeleteModel { name } => {
195+
m.retain(|z| z.name != *name);
196+
}
197+
Operation::CreateField { model, field } => {
198+
for i in &mut m {
199+
if i.name == *model {
200+
i.fields.push(field.clone());
189201
}
190-
a
191-
})
192-
.collect();
193-
Ok(())
194-
}
195-
Operation::DeleteModel { name } => {
196-
m.retain(|z| z.name != *name);
197-
Ok(())
198-
}
199-
Operation::CreateField { model, field } => {
200-
for i in &mut m {
201-
if i.name == *model {
202-
i.fields.push(field.clone());
203202
}
204203
}
205-
Ok(())
206-
}
207-
Operation::RenameField {
208-
table_name,
209-
old,
210-
new,
211-
} => {
212-
m = m
213-
.iter()
214-
.map(|z| {
215-
let mut a = z.clone();
216-
if &a.name == table_name {
217-
a.fields = a
218-
.fields
219-
.iter()
220-
.map(|b| {
221-
let mut c = b.clone();
222-
if &c.name == old {
223-
c.name = new.to_string();
224-
}
225-
c
226-
})
227-
.collect();
204+
Operation::RenameField {
205+
table_name,
206+
old,
207+
new,
208+
} => {
209+
m = m
210+
.iter()
211+
.map(|z| {
212+
let mut a = z.clone();
213+
if &a.name == table_name {
214+
a.fields = a
215+
.fields
216+
.iter()
217+
.map(|b| {
218+
let mut c = b.clone();
219+
if &c.name == old {
220+
c.name = new.to_string();
221+
}
222+
c
223+
})
224+
.collect();
225+
}
226+
a
227+
})
228+
.collect();
229+
}
230+
Operation::DeleteField { model, name } => {
231+
for i in &mut m {
232+
if i.name == *model {
233+
i.fields.retain(|z| z.name != *name);
228234
}
229-
a
230-
})
231-
.collect();
232-
Ok(())
233-
}
234-
Operation::DeleteField { model, name } => {
235-
for i in &mut m {
236-
if i.name == *model {
237-
i.fields.retain(|z| z.name != *name);
238235
}
239236
}
240-
Ok(())
241-
}
242-
Operation::RawSQL { structure_safe, .. } => {
243-
if *structure_safe {
244-
Ok(())
245-
} else {
246-
Err(anyhow!(
247-
r#"RawSQL migration without StructureSafe flag found!
237+
Operation::RawSQL { structure_safe, .. } => {
238+
if !*structure_safe {
239+
return Err(anyhow!(
240+
r#"RawSQL migration without StructureSafe flag found!
248241
249242
Can not proceed to generate migrations as the current database state can not be determined anymore!
250243
You can still write migrations with all available operations yourself.
251244
252245
To use the make-migrations feature again, check that all RawSQL operations don't change any
253246
structure and mark them as StructureSafe or delete all RawSQL operations."#
254-
))
247+
));
248+
}
255249
}
256250
}
257-
})
258-
});
259-
260-
res?;
251+
}
252+
}
261253

262254
Ok(InternalModelFormat { models: m })
263255
}

0 commit comments

Comments
 (0)