xataio / pgroll

PostgreSQL zero-downtime migrations made easy
https://www.xata.io
Apache License 2.0
2.97k stars 54 forks source link

Add `Upper` and `Downer` interfaces to identify operations that create `up` and `down` triggers #326

Closed andrew-farries closed 5 months ago

andrew-farries commented 5 months ago

Add two new interfaces Upper and Downer, implemented by operations that create up and down triggers.

Consumers of pgroll may wish to identify operations that run arbitrary SQL via up and down triggers and pre-process them in such a way to ensure that the SQL is safe, or otherwise restricted.

With the new interfaces clients can do something like:

for _, op := range migration.Operations {
  if up, ok := op.(migrations.Upper); ok {
    processedSQL := processSQL(up.UpSQL())
    up.SetUpSQL(processedSQL)
  }
  if down, ok := op.(migrations.Downer); ok {
    processedSQL := processSQL(down.DownSQL())
    down.SetDownSQL(processedSQL)
  }
}

to iterate over all operations in a migration and process the up or down SQL of each operation.

Update the createTrigger function to check that the operation creating the trigger implements either Upper or Downer according to the direction of the trigger. This guards against future operations creating triggers without implementing Upper or Downer.

andrew-farries commented 5 months ago

Closing this after some discussion in favour of an option-based approach.