Skip to content

improve logging output for async and sync migrate functions #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,10 @@ mod mysql {
])
.unwrap()
.assert()
.stdout(contains("applying migration: V2__add_cars_and_motos_table"))
.stdout(contains("applying migration: V3__add_brand_to_cars_table"));
.stdout(contains(
"applying migration:\tV2__add_cars_and_motos_table",
))
.stdout(contains("applying migration:\tV3__add_brand_to_cars_table"));
})
}
}
6 changes: 4 additions & 2 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,10 @@ mod postgres {
])
.unwrap()
.assert()
.stdout(contains("applying migration: V2__add_cars_and_motos_table"))
.stdout(contains("applying migration: V3__add_brand_to_cars_table"));
.stdout(contains(
"applying migration:\tV2__add_cars_and_motos_table",
))
.stdout(contains("applying migration:\tV3__add_brand_to_cars_table"));
})
}
}
6 changes: 4 additions & 2 deletions refinery/tests/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,10 @@ mod rusqlite {
])
.unwrap()
.assert()
.stdout(contains("applying migration: V2__add_cars_and_motos_table"))
.stdout(contains("applying migration: V3__add_brand_to_cars_table"));
.stdout(contains(
"applying migration:\tV2__add_cars_and_motos_table",
))
.stdout(contains("applying migration:\tV3__add_brand_to_cars_table"));
})
}
}
6 changes: 4 additions & 2 deletions refinery/tests/tiberius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,10 @@ mod tiberius {
])
.unwrap()
.assert()
.stdout(contains("applying migration: V2__add_cars_and_motos_table"))
.stdout(contains("applying migration: V3__add_brand_to_cars_table"));
.stdout(contains(
"applying migration:\tV2__add_cars_and_motos_table",
))
.stdout(contains("applying migration:\tV3__add_brand_to_cars_table"));
})
.await;
}
Expand Down
10 changes: 7 additions & 3 deletions refinery_core/src/traits/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn migrate<T: AsyncTransaction>(
}
}

log::info!("applying migration: {}", migration);
log::info!("applying migration:\t{}", migration);
migration.set_applied();
let update_query = insert_migration_query(&migration, migration_table_name);
transaction
Expand Down Expand Up @@ -91,9 +91,13 @@ async fn migrate_grouped<T: AsyncTransaction>(
log::info!("not going to apply any migration as fake flag is enabled");
}
Target::Latest | Target::Version(_) => {
let migrations_display = applied_migrations
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
log::info!(
"going to apply batch migrations in single transaction: {:#?}",
applied_migrations.iter().map(ToString::to_string)
"going to apply batch migrations in single transaction:\n{migrations_display}"
);
}
};
Expand Down
25 changes: 19 additions & 6 deletions refinery_core/src/traits/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub fn migrate<T: Transaction>(
}
}

log::info!("applying migration: {}", migration);
migration.set_applied();
let insert_migration = insert_migration_query(&migration, migration_table_name);
let migration_sql = migration.sql().expect("sql must be Some!").to_string();
Expand All @@ -51,31 +50,45 @@ pub fn migrate<T: Transaction>(

match (target, batched) {
(Target::Fake | Target::FakeVersion(_), _) => {
log::info!("not going to apply any migration as fake flag is enabled");
log::info!("not going to apply any migration as fake flag is enabled.");
}
(Target::Latest | Target::Version(_), true) => {
log::info!(
"going to apply batch migrations in single transaction: {:#?}",
applied_migrations.iter().map(ToString::to_string)
"going to batch apply {} migrations in single transaction.",
applied_migrations.len()
);
}
(Target::Latest | Target::Version(_), false) => {
log::info!(
"preparing to apply {} migrations: {:#?}",
"going to apply {} migrations in multiple transactions.",
applied_migrations.len(),
applied_migrations.iter().map(ToString::to_string)
);
}
};

let refs: Vec<&str> = migration_batch.iter().map(AsRef::as_ref).collect();

if batched {
let migrations_display = applied_migrations
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
log::info!("going to apply batch migrations in single transaction:\n{migrations_display}");
transaction
.execute(refs.as_ref())
.migration_err("error applying migrations", None)?;
} else {
for (i, update) in refs.iter().enumerate() {
let applying_migration = i % 2 == 0;

let current_migration = &applied_migrations[i / 2];
if applying_migration {
log::info!("applying migration:\t{current_migration} ...");
} else {
//Writing the migration state to the db
log::info!("applied migration:\t{current_migration} writing state to db.");
}
transaction
.execute(&[update])
.migration_err("error applying update", Some(&applied_migrations[0..i / 2]))?;
Expand Down