xataio / pgroll

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

Panic in `op_rename_constraint` when the table doesn't exist #302

Closed andrew-farries closed 6 months ago

andrew-farries commented 6 months ago

The rename constraint operation panics if the table referenced in the operation doesn't exist:

{
  "name": "rename",
  "operations": [
    {
      "rename_constraint": {
        "table": "doesntexist",
        "from": "foo",
        "to": "bar"
      }
    }
  ]
}

Actual behavior:

▀  Starting migration... (0s)panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x60 pc=0x100c21790]

goroutine 1 [running]:
github.com/xataio/pgroll/pkg/schema.(*Table).ConstraintExists(...)
        /Users/andrew/git/pgroll/pkg/schema/schema.go:168
github.com/xataio/pgroll/pkg/migrations.(*OpRenameConstraint).Validate(0x1400020c330, {0x100df3838?, 0x1400020c330?}, 0x14000212390?)
        /Users/andrew/git/pgroll/pkg/migrations/op_rename_constraint.go:38 +0xd0
github.com/xataio/pgroll/pkg/migrations.(*Migration).Validate(0x1400020c120, {0x100df3560, 0x1010eb220}, 0x1400002a0b6?)
        /Users/andrew/git/pgroll/pkg/migrations/migrations.go:70 +0x188
github.com/xataio/pgroll/pkg/roll.(*Roll).StartDDLOperations(0x140002140f0, {0x100df3560, 0x1010eb220}, 0x1400020c120, {0x14000210060, 0x1, 0x1})
        /Users/andrew/git/pgroll/pkg/roll/execute.go:52 +0x114
github.com/xataio/pgroll/pkg/roll.(*Roll).Start(0x140002140f0, {0x100df3560, 0x1010eb220}, 0x4?, {0x14000210060?, 0x1010b14d8?, 0xbebc200?})
        /Users/andrew/git/pgroll/pkg/roll/execute.go:18 +0x2c
github.com/xataio/pgroll/cmd.startCmd.func1(0x14000124300, {0x1400011a910, 0x4?, 0x100c668b7?})
        /Users/andrew/git/pgroll/cmd/start.go:51 +0x2e0
github.com/spf13/cobra.(*Command).execute(0x14000124300, {0x1400011a8e0, 0x1, 0x1})
        /Users/andrew/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:940 +0x66c
github.com/spf13/cobra.(*Command).ExecuteC(0x1010b06c0)
        /Users/andrew/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x320
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/andrew/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992
github.com/xataio/pgroll/cmd.Execute()
        /Users/andrew/git/pgroll/cmd/root.go:69 +0x110
main.main()
        /Users/andrew/git/pgroll/main.go:12 +0x1c

Expected behaviour:

An error message stating that the table doesn't exists, as happens for other operations.

Cause:

The rename constraint operation needs an extra validation check to ensure that the table being referenced exists:

https://github.com/xataio/pgroll/blob/main/pkg/migrations/op_rename_constraint.go#L35-L47