@@ -43,7 +43,7 @@ impl RemoveColumn {
4343 )
4444 }
4545
46− fn not_null_constraint_trigger_name(&self, ctx: &MigrationContext) -> String {
46+ fn null_constraint_trigger_name(&self, ctx: &MigrationContext) -> String {
4747 format!(
4848 "{}_remove_column_{}_{}_nn",
4949 ctx.prefix(),
@@ -51,15 +51,6 @@ impl RemoveColumn {
5151 self.column
5252 )
5353 }
54−
55− fn not_null_constraint_name(&self, ctx: &MigrationContext) -> String {
56− format!(
57− "{}_add_column_not_null_{}_{}",
58− ctx.prefix(),
59− self.table,
60− self.column
61− )
62− }
6354}
6455
6556#[typetag::serde(name = "remove_column")]
@@ -141,7 +132,6 @@ impl Action for RemoveColumn {
141132
142133 let maybe_null_check = if !column.nullable {
143134 // Replace NOT NULL constraint with a constraint trigger that only triggers on the old schema.
144− // We will add a null check to the down function on the new schema below as well to cover both cases.
145135 // As we are using a complex down function, we must remove the NOT NULL check for the new schema.
146136 // NOT NULL is not checked at the end of a transaction, but immediately upon update.
147137 let query = format!(
@@ -167,7 +157,7 @@ impl Action for RemoveColumn {
167157 EXECUTE PROCEDURE {trigger_name}();
168158 "#,
169159 table = self.table,
170− trigger_name = self.not_null_constraint_trigger_name(ctx),
160+ trigger_name = self.null_constraint_trigger_name(ctx),
171161 column = self.column,
172162 );
173163 db.run(&query)
@@ -246,6 +236,7 @@ impl Action for RemoveColumn {
246236 from_table_real = from_table.real_name,
247237 column_name = self.column,
248238 trigger_name = self.trigger_name(ctx),
239+ // declarations = declarations.join("\n"),
249240 );
250241 db.run(&query).context("failed to create down trigger")?;
251242
@@ -319,6 +310,8 @@ impl Action for RemoveColumn {
319310 }
320311 }
321312
313+ if !column.nullable {}
314+
322315 Ok(())
323316 }
324317
@@ -354,7 +347,7 @@ impl Action for RemoveColumn {
354347 column = self.column,
355348 trigger_name = self.trigger_name(ctx),
356349 reverse_trigger_name = self.reverse_trigger_name(ctx),
357− null_trigger_name = self.not_null_constraint_trigger_name(ctx),
350+ null_trigger_name = self.null_constraint_trigger_name(ctx),
358351 );
359352 db.run(&query)
360353 .context("failed to drop column and down trigger")?;
@@ -371,70 +364,17 @@ impl Action for RemoveColumn {
371364 }
372365
373366 fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
374− // We might have temporaily removed the NOT NULL check and have to reinstate it
375− let has_not_null_function = !db
376− .query_with_params(
377− "
378− SELECT routine_name
379− FROM information_schema.routines
380− WHERE routine_schema = 'public'
381− AND routine_name = $1
382− ",
383− &[&self.not_null_constraint_trigger_name(ctx)],
384− )
385− .context("failed to get any NOT NULL function")?
386− .is_empty();
387−
388− if has_not_null_function {
389− // Make column NOT NULL again without taking any long lived locks with a temporary constraint
390− let query = format!(
391− r#"
392− ALTER TABLE "{table}"
393− ADD CONSTRAINT "{constraint_name}"
394− CHECK ("{column}" IS NOT NULL) NOT VALID
395− "#,
396− table = self.table,
397− constraint_name = self.not_null_constraint_name(ctx),
398− column = self.column,
399− );
400− db.run(&query)
401− .context("failed to add NOT NULL constraint")?;
402−
403− let query = format!(
404− r#"
405− ALTER TABLE "{table}"
406− VALIDATE CONSTRAINT "{constraint_name}"
407− "#,
408− table = self.table,
409− constraint_name = self.not_null_constraint_name(ctx),
410− );
411− db.run(&query)
412− .context("failed to validate NOT NULL constraint")?;
413−
414− // This ALTER TABLE call will not require any exclusive locks as it can use the validated constraint from above
415− db.run(&format!(
416− r#"
417− ALTER TABLE {table}
418− ALTER COLUMN {column}
419− SET NOT NULL
420− "#,
421− table = self.table,
422− column = self.column
423− ))
424− .context("failed to reinstate column NOT NULL")?;
425−
426− // Drop the temporary constraint
427− let query = format!(
428− r#"
429− ALTER TABLE "{table}"
430− DROP CONSTRAINT "{constraint_name}"
431− "#,
432− table = self.table,
433− constraint_name = self.not_null_constraint_name(ctx),
434− );
435− db.run(&query)
436− .context("failed to drop NOT NULL constraint")?;
437− }
367+ // We might have removed the NOT NULL check so we reinstate it
368+ db.run(&format!(
369+ r#"
370+ ALTER TABLE {table}
371+ ALTER COLUMN {column}
372+ SET NOT NULL
373+ "#,
374+ table = self.table,
375+ column = self.column
376+ ))
377+ .context("failed to reinstate column not null constraint")?;
438378
439379 // Remove function and trigger
440380 db.run(&format!(
@@ -445,7 +385,7 @@ impl Action for RemoveColumn {
445385 "#,
446386 trigger_name = self.trigger_name(ctx),
447387 reverse_trigger_name = self.reverse_trigger_name(ctx),
448− null_trigger_name = self.not_null_constraint_trigger_name(ctx),
388+ null_trigger_name = self.null_constraint_trigger_name(ctx),
449389 ))
450390 .context("failed to drop down trigger")?;
451391