Rename Context to MigrationContext

Context collides with the anyhow::Context trait.
This commit is contained in:
fabianlindfors 2022-01-02 23:40:34 +01:00
parent ef82acd480
commit 6e005144f4
9 changed files with 130 additions and 54 deletions

View File

@ -1,5 +1,5 @@
use crate::{
migrations::{Context, Migration},
migrations::{Migration, MigrationContext},
schema::Schema,
};
@ -109,7 +109,7 @@ impl Reshape {
for (action_index, action) in migration.actions.iter().enumerate() {
print!(" + {} ", action.describe());
let ctx = Context::new(migration_index, action_index);
let ctx = MigrationContext::new(migration_index, action_index);
result = action.run(&ctx, &mut self.db, &new_schema);
if result.is_ok() {
@ -197,7 +197,7 @@ impl Reshape {
for (action_index, action) in migration.actions.iter().enumerate() {
print!(" + {} ", action.describe());
let ctx = Context::new(migration_index, action_index);
let ctx = MigrationContext::new(migration_index, action_index);
action.complete(&ctx, &mut transaction, &temp_schema)?;
action.update_schema(&ctx, &mut temp_schema);
@ -330,7 +330,7 @@ fn abort_migrations(db: &mut dyn Conn, migrations: &[Migration]) -> anyhow::Resu
for (migration_index, migration) in migrations.iter().rev().enumerate() {
print!("Aborting '{}' ", migration.name);
for (action_index, action) in migration.actions.iter().rev().enumerate() {
let ctx = Context::new(migration_index, action_index);
let ctx = MigrationContext::new(migration_index, action_index);
action.abort(&ctx, db)?;
}
println!("{}", "done".green());

View File

@ -1,4 +1,4 @@
use super::{common, Action, Column, Context};
use super::{common, Action, Column, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -10,7 +10,7 @@ pub struct AddColumn {
}
impl AddColumn {
fn temp_column_name(&self, ctx: &Context) -> String {
fn temp_column_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_temp_column_{}_{}",
ctx.prefix(),
@ -19,7 +19,7 @@ impl AddColumn {
)
}
fn trigger_name(&self, ctx: &Context) -> String {
fn trigger_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_add_column_{}_{}",
ctx.prefix(),
@ -28,7 +28,7 @@ impl AddColumn {
)
}
fn not_null_constraint_name(&self, ctx: &Context) -> String {
fn not_null_constraint_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_add_column_not_null_{}_{}",
ctx.prefix(),
@ -47,7 +47,12 @@ impl Action for AddColumn {
)
}
fn run(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
let table = schema.get_table(db, &self.table)?;
let temp_column_name = self.temp_column_name(ctx);
@ -148,7 +153,12 @@ impl Action for AddColumn {
Ok(())
}
fn complete(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
let table = schema.get_table(db, &self.table)?;
// Remove triggers and procedures
@ -216,7 +226,7 @@ impl Action for AddColumn {
Ok(())
}
fn update_schema(&self, ctx: &Context, schema: &mut Schema) {
fn update_schema(&self, ctx: &MigrationContext, schema: &mut Schema) {
schema.change_table(&self.table, |table_changes| {
table_changes.change_column(&self.column.name, |column_changes| {
column_changes.set_column(&self.temp_column_name(ctx));
@ -224,7 +234,7 @@ impl Action for AddColumn {
});
}
fn abort(&self, ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
// Remove triggers and procedures
let query = format!(
"

View File

@ -1,4 +1,4 @@
use super::{Action, Context};
use super::{Action, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -15,7 +15,12 @@ impl Action for AddIndex {
format!("Adding index \"{}\" to table \"{}\"", self.name, self.table)
}
fn run(&self, _ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
_ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
let table = schema.get_table(db, &self.table)?;
let column_real_names: Vec<String> = table
@ -36,13 +41,18 @@ impl Action for AddIndex {
Ok(())
}
fn complete(&self, _ctx: &Context, _db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
_ctx: &MigrationContext,
_db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
Ok(())
}
fn update_schema(&self, _ctx: &Context, _schema: &mut Schema) {}
fn update_schema(&self, _ctx: &MigrationContext, _schema: &mut Schema) {}
fn abort(&self, _ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, _ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
db.run(&format!(
"
DROP INDEX {name} ON {table}

View File

@ -1,4 +1,4 @@
use super::{Action, Context};
use super::{Action, MigrationContext};
use crate::{db::Conn, migrations::common, schema::Schema};
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
@ -21,11 +21,11 @@ pub struct ColumnChanges {
}
impl AlterColumn {
fn temporary_column_name(&self, ctx: &Context) -> String {
fn temporary_column_name(&self, ctx: &MigrationContext) -> String {
format!("{}_new_{}", ctx.prefix(), self.column)
}
fn up_trigger_name(&self, ctx: &Context) -> String {
fn up_trigger_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_alter_column_up_trigger_{}_{}",
ctx.prefix(),
@ -34,7 +34,7 @@ impl AlterColumn {
)
}
fn down_trigger_name(&self, ctx: &Context) -> String {
fn down_trigger_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_alter_column_down_trigger_{}_{}",
ctx.prefix_inverse(),
@ -43,7 +43,7 @@ impl AlterColumn {
)
}
fn not_null_constraint_name(&self, ctx: &Context) -> String {
fn not_null_constraint_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_alter_column_temporary_not_null_{}_{}",
ctx.prefix(),
@ -65,7 +65,12 @@ impl Action for AlterColumn {
format!("Altering column \"{}\" on \"{}\"", self.column, self.table)
}
fn run(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
// If we are only changing the name of a column, we don't have to do anything at this stage
// We'll set the new schema to point to the old column. When the migration is completed,
// we rename the actual column.
@ -194,7 +199,12 @@ impl Action for AlterColumn {
Ok(())
}
fn complete(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
if self.can_short_circuit() {
if let Some(new_name) = &self.changes.name {
let query = format!(
@ -298,7 +308,7 @@ impl Action for AlterColumn {
Ok(())
}
fn update_schema(&self, ctx: &Context, schema: &mut Schema) {
fn update_schema(&self, ctx: &MigrationContext, schema: &mut Schema) {
// If we are only changing the name of a column, we haven't created a temporary column
// Instead, we rename the schema column but point it to the old column
if self.can_short_circuit() {
@ -320,7 +330,7 @@ impl Action for AlterColumn {
});
}
fn abort(&self, ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
// Remove triggers and procedures
let query = format!(
"

View File

@ -1,4 +1,4 @@
use super::{Action, Column, Context};
use super::{Action, Column, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -23,7 +23,12 @@ impl Action for CreateTable {
format!("Creating table \"{}\"", self.name)
}
fn run(&self, _ctx: &Context, db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
_ctx: &MigrationContext,
db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
let mut definition_rows: Vec<String> = self
.columns
.iter()
@ -70,14 +75,19 @@ impl Action for CreateTable {
Ok(())
}
fn complete(&self, _ctx: &Context, _db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
_ctx: &MigrationContext,
_db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
// Do nothing
Ok(())
}
fn update_schema(&self, _ctx: &Context, _schema: &mut Schema) {}
fn update_schema(&self, _ctx: &MigrationContext, _schema: &mut Schema) {}
fn abort(&self, _ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, _ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
let query = format!("DROP TABLE IF EXISTS {table}", table = self.name,);
db.run(&query)?;

View File

@ -64,14 +64,14 @@ impl Clone for Migration {
}
}
pub struct Context {
pub struct MigrationContext {
migration_index: usize,
action_index: usize,
}
impl Context {
impl MigrationContext {
pub fn new(migration_index: usize, action_index: usize) -> Self {
Context {
MigrationContext {
migration_index,
action_index,
}
@ -96,8 +96,14 @@ impl Context {
#[typetag::serde(tag = "type")]
pub trait Action: Debug {
fn describe(&self) -> String;
fn run(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()>;
fn complete(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()>;
fn update_schema(&self, ctx: &Context, schema: &mut Schema);
fn abort(&self, ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()>;
fn run(&self, ctx: &MigrationContext, db: &mut dyn Conn, schema: &Schema)
-> anyhow::Result<()>;
fn complete(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()>;
fn update_schema(&self, ctx: &MigrationContext, schema: &mut Schema);
fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()>;
}

View File

@ -1,4 +1,4 @@
use super::{Action, Context};
use super::{Action, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -10,7 +10,7 @@ pub struct RemoveColumn {
}
impl RemoveColumn {
fn trigger_name(&self, ctx: &Context) -> String {
fn trigger_name(&self, ctx: &MigrationContext) -> String {
format!(
"{}_remove_column_{}_{}",
ctx.prefix(),
@ -29,7 +29,12 @@ impl Action for RemoveColumn {
)
}
fn run(&self, ctx: &Context, db: &mut dyn Conn, schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
schema: &Schema,
) -> anyhow::Result<()> {
// Add down trigger
if let Some(down) = &self.down {
let table = schema.get_table(db, &self.table)?;
@ -78,7 +83,12 @@ impl Action for RemoveColumn {
Ok(())
}
fn complete(&self, ctx: &Context, db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
ctx: &MigrationContext,
db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
// Remove column, function and trigger
let query = format!(
"
@ -97,7 +107,7 @@ impl Action for RemoveColumn {
Ok(())
}
fn update_schema(&self, _ctx: &Context, schema: &mut Schema) {
fn update_schema(&self, _ctx: &MigrationContext, schema: &mut Schema) {
schema.change_table(&self.table, |table_changes| {
table_changes.change_column(&self.column, |column_changes| {
column_changes.set_removed();
@ -105,7 +115,7 @@ impl Action for RemoveColumn {
});
}
fn abort(&self, ctx: &Context, db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
// Remove function and trigger
db.query(&format!(
"

View File

@ -1,4 +1,4 @@
use super::{Action, Context};
use super::{Action, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -13,11 +13,21 @@ impl Action for RemoveTable {
format!("Removing table \"{}\"", self.table)
}
fn run(&self, _ctx: &Context, _db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
_ctx: &MigrationContext,
_db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
Ok(())
}
fn complete(&self, _ctx: &Context, db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
_ctx: &MigrationContext,
db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
// Remove table
let query = format!(
"
@ -30,13 +40,13 @@ impl Action for RemoveTable {
Ok(())
}
fn update_schema(&self, _ctx: &Context, schema: &mut Schema) {
fn update_schema(&self, _ctx: &MigrationContext, schema: &mut Schema) {
schema.change_table(&self.table, |table_changes| {
table_changes.set_removed();
});
}
fn abort(&self, _ctx: &Context, _db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, _ctx: &MigrationContext, _db: &mut dyn Conn) -> anyhow::Result<()> {
Ok(())
}
}

View File

@ -1,4 +1,4 @@
use super::{Action, Context};
use super::{Action, MigrationContext};
use crate::{db::Conn, schema::Schema};
use serde::{Deserialize, Serialize};
@ -14,11 +14,21 @@ impl Action for RenameTable {
format!("Renaming table \"{}\" to \"{}\"", self.table, self.new_name)
}
fn run(&self, _ctx: &Context, _db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn run(
&self,
_ctx: &MigrationContext,
_db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
Ok(())
}
fn complete(&self, _ctx: &Context, db: &mut dyn Conn, _schema: &Schema) -> anyhow::Result<()> {
fn complete(
&self,
_ctx: &MigrationContext,
db: &mut dyn Conn,
_schema: &Schema,
) -> anyhow::Result<()> {
// Rename table
let query = format!(
"
@ -33,13 +43,13 @@ impl Action for RenameTable {
Ok(())
}
fn update_schema(&self, _ctx: &Context, schema: &mut Schema) {
fn update_schema(&self, _ctx: &MigrationContext, schema: &mut Schema) {
schema.change_table(&self.table, |table_changes| {
table_changes.set_name(&self.new_name);
});
}
fn abort(&self, _ctx: &Context, _db: &mut dyn Conn) -> anyhow::Result<()> {
fn abort(&self, _ctx: &MigrationContext, _db: &mut dyn Conn) -> anyhow::Result<()> {
Ok(())
}
}