@@ -3,7 +3,7 @@ use crate::{
3 db::{Conn, Transaction},
3 db::{Conn, Transaction},
4 schema::Schema,
4 schema::Schema,
6−use anyhow::{bail, Context};
6+use anyhow::{anyhow, bail, Context};
7use serde::{Deserialize, Serialize};
7use serde::{Deserialize, Serialize};
9#[derive(Serialize, Deserialize, Debug)]
9#[derive(Serialize, Deserialize, Debug)]
@@ -33,6 +33,33 @@ impl RemoveColumn {
33 self.column
33 self.column
37+ fn reverse_trigger_name(&self, ctx: &MigrationContext) -> String {
39+ "{}_remove_column_{}_{}_rev",
46+ fn not_null_constraint_trigger_name(&self, ctx: &MigrationContext) -> String {
48+ "{}_remove_column_{}_{}_nn",
55+ fn not_null_constraint_name(&self, ctx: &MigrationContext) -> String {
57+ "{}_add_column_not_null_{}_{}",
38#[typetag::serde(name = "remove_column")]
65#[typetag::serde(name = "remove_column")]
@@ -51,6 +78,9 @@ impl Action for RemoveColumn {
51 schema: &Schema,
78 schema: &Schema,
52 ) -> anyhow::Result<()> {
79 ) -> anyhow::Result<()> {
53 let table = schema.get_table(db, &self.table)?;
80 let table = schema.get_table(db, &self.table)?;
82+ .get_column(&self.column)
83+ .ok_or_else(|| anyhow!("no such column {} exists", self.column))?;
55 // Add down trigger
85 // Add down trigger
56 if let Some(down) = &self.down {
86 if let Some(down) = &self.down {
@@ -109,18 +139,75 @@ impl Action for RemoveColumn {
110 let from_table = schema.get_table(db, &from_table)?;
140 let from_table = schema.get_table(db, &from_table)?;
112− let declarations: Vec<String> = from_table
142+ let maybe_null_check = if !column.nullable {
143+ // 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.
145+ // As we are using a complex down function, we must remove the NOT NULL check for the new schema.
146+ // NOT NULL is not checked at the end of a transaction, but immediately upon update.
147+ let query = format!(
149+ CREATE OR REPLACE FUNCTION {trigger_name}()
150+ RETURNS TRIGGER AS $$
152+ IF NOT reshape.is_new_schema() THEN
153+ IF NEW.{column} IS NULL THEN
154+ RAISE EXCEPTION '{column} can not be null';
159+ $$ language 'plpgsql';
161+ DROP TRIGGER IF EXISTS "{trigger_name}" ON "{table}";
163+ CREATE CONSTRAINT TRIGGER "{trigger_name}"
164+ AFTER INSERT OR UPDATE
167+ EXECUTE PROCEDURE {trigger_name}();
170+ trigger_name = self.not_null_constraint_trigger_name(ctx),
171+ column = self.column,
174+ .context("failed to create null constraint trigger")?;
179+ ALTER COLUMN {column}
183+ column = self.column
185+ .context("failed to remove column not null constraint")?;
189+ IF {value} IS NULL THEN
190+ RAISE EXCEPTION '{column_name} can not be null';
193+ column_name = self.column,
199+ let into_variables = from_table
115 .map(|column| {
202 .map(|column| {
117− "{alias} public.{table}.{real_name}%TYPE := NEW.{real_name};",
204+ "NEW.{real_name} AS {alias}",
118− table = from_table.real_name,
119 alias = column.name,
205 alias = column.name,
120 real_name = column.real_name,
206 real_name = column.real_name,
123− .collect();
209+ .collect::<Vec<String>>()
125 let query = format!(
212 let query = format!(
@@ -130,11 +217,21 @@ impl Action for RemoveColumn {
131 IF reshape.is_new_schema() THEN
218 IF reshape.is_new_schema() THEN
133− {declarations}
220+ {from_table} record;
222+ SELECT {into_variables}
227+ -- Don't trigger reverse trigger when making this update
228+ perform set_config('reshape.disable_triggers', 'TRUE', TRUE);
135 UPDATE "migration_{existing_schema_name}"."{changed_table}" "{changed_table}"
230 UPDATE "migration_{existing_schema_name}"."{changed_table}" "{changed_table}"
136 SET "{column_name}" = {value}
231 SET "{column_name}" = {value}
137 WHERE {where};
232 WHERE {where};
234+ perform set_config('reshape.disable_triggers', '', TRUE);
140 RETURN NEW;
237 RETURN NEW;
@@ -145,12 +242,80 @@ impl Action for RemoveColumn {
145 CREATE TRIGGER "{trigger_name}" BEFORE UPDATE OR INSERT ON "{from_table_real}" FOR EACH ROW EXECUTE PROCEDURE {trigger_name}();
242 CREATE TRIGGER "{trigger_name}" BEFORE UPDATE OR INSERT ON "{from_table_real}" FOR EACH ROW EXECUTE PROCEDURE {trigger_name}();
147 changed_table = self.table,
244 changed_table = self.table,
245+ from_table = from_table.name,
148 from_table_real = from_table.real_name,
246 from_table_real = from_table.real_name,
149 column_name = self.column,
247 column_name = self.column,
150 trigger_name = self.trigger_name(ctx),
248 trigger_name = self.trigger_name(ctx),
151− declarations = declarations.join("\n"),
153 db.run(&query).context("failed to create down trigger")?;
250 db.run(&query).context("failed to create down trigger")?;
252+ let changed_into_variables = table
257+ "NEW.{real_name} AS {alias}",
258+ alias = column.name,
259+ real_name = column.real_name,
262+ .collect::<Vec<String>>()
265+ let from_table_columns = from_table
268+ .map(|column| format!("{} as {}", column.real_name, column.name))
269+ .collect::<Vec<String>>()
272+ let query = format!(
274+ CREATE OR REPLACE FUNCTION {trigger_name}()
275+ RETURNS TRIGGER AS $$
276+ #variable_conflict use_variable
278+ IF reshape.is_new_schema() AND NOT current_setting('reshape.disable_triggers', TRUE) = 'TRUE' THEN
280+ {changed_table} record;
283+ SELECT {changed_into_variables}
284+ INTO {changed_table};
289+ SELECT {from_table_columns}
290+ FROM public.{from_table_real}
295+ {from_table} record;
297+ {from_table} := __temp_row;
298+ NEW.{column_name_real} = {value};
304+ $$ language 'plpgsql';
306+ DROP TRIGGER IF EXISTS "{trigger_name}" ON "{changed_table_real}";
307+ CREATE TRIGGER "{trigger_name}" BEFORE UPDATE OR INSERT ON "{changed_table_real}" FOR EACH ROW EXECUTE PROCEDURE {trigger_name}();
309+ changed_table = table.name,
310+ changed_table_real = table.real_name,
311+ from_table = from_table.name,
312+ from_table_real = from_table.real_name,
313+ column_name_real = column.real_name,
314+ trigger_name = self.reverse_trigger_name(ctx),
315+ // declarations = declarations.join("\n"),
318+ .context("failed to create reverse down trigger")?;
@@ -176,25 +341,20 @@ impl Action for RemoveColumn {
178 // Remove column, function and trigger
343 // Remove column, function and trigger
179− let trigger_table = match &self.down {
180− Some(Transformation::Update {
187 let query = format!(
344 let query = format!(
189 ALTER TABLE "{table}"
346 ALTER TABLE "{table}"
190 DROP COLUMN IF EXISTS "{column}";
347 DROP COLUMN IF EXISTS "{column}";
192− DROP TRIGGER IF EXISTS "{trigger_name}" ON "{trigger_table}";
349+ DROP FUNCTION IF EXISTS "{trigger_name}" CASCADE;
350+ DROP FUNCTION IF EXISTS "{reverse_trigger_name}" CASCADE;
193− DROP FUNCTION IF EXISTS "{trigger_name}";
351+ DROP FUNCTION IF EXISTS "{null_trigger_name}" CASCADE;
195 table = self.table,
353 table = self.table,
196 column = self.column,
354 column = self.column,
197 trigger_name = self.trigger_name(ctx),
355 trigger_name = self.trigger_name(ctx),
356+ reverse_trigger_name = self.reverse_trigger_name(ctx),
357+ null_trigger_name = self.not_null_constraint_trigger_name(ctx),
199 db.run(&query)
359 db.run(&query)
200 .context("failed to drop column and down trigger")?;
360 .context("failed to drop column and down trigger")?;
@@ -211,21 +371,81 @@ impl Action for RemoveColumn {
213 fn abort(&self, ctx: &MigrationContext, db: &mut dyn Conn) -> anyhow::Result<()> {
373 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
379+ FROM information_schema.routines
380+ WHERE routine_schema = 'public'
381+ AND routine_name = $1
383+ &[&self.not_null_constraint_trigger_name(ctx)],
385+ .context("failed to get any NOT NULL function")?
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!(
392+ ALTER TABLE "{table}"
393+ ADD CONSTRAINT "{constraint_name}"
394+ CHECK ("{column}" IS NOT NULL) NOT VALID
397+ constraint_name = self.not_null_constraint_name(ctx),
398+ column = self.column,
401+ .context("failed to add NOT NULL constraint")?;
403+ let query = format!(
405+ ALTER TABLE "{table}"
406+ VALIDATE CONSTRAINT "{constraint_name}"
409+ constraint_name = self.not_null_constraint_name(ctx),
412+ .context("failed to validate NOT NULL constraint")?;
414+ // This ALTER TABLE call will not require any exclusive locks as it can use the validated constraint from above
418+ ALTER COLUMN {column}
422+ column = self.column
424+ .context("failed to reinstate column NOT NULL")?;
426+ // Drop the temporary constraint
427+ let query = format!(
429+ ALTER TABLE "{table}"
430+ DROP CONSTRAINT "{constraint_name}"
433+ constraint_name = self.not_null_constraint_name(ctx),
436+ .context("failed to drop NOT NULL constraint")?;
214 // Remove function and trigger
439 // Remove function and trigger
215− let trigger_table = match &self.down {
216− Some(Transformation::Update {
223 db.run(&format!(
440 db.run(&format!(
225− DROP TRIGGER IF EXISTS "{trigger_name}" ON "{trigger_table}";
442+ DROP FUNCTION IF EXISTS "{trigger_name}" CASCADE;
443+ DROP FUNCTION IF EXISTS "{reverse_trigger_name}" CASCADE;
226− DROP FUNCTION IF EXISTS "{trigger_name}";
444+ DROP FUNCTION IF EXISTS "{null_trigger_name}" CASCADE;
228 trigger_name = self.trigger_name(ctx),
446 trigger_name = self.trigger_name(ctx),
447+ reverse_trigger_name = self.reverse_trigger_name(ctx),
448+ null_trigger_name = self.not_null_constraint_trigger_name(ctx),
230 .context("failed to drop down trigger")?;
450 .context("failed to drop down trigger")?;