pgroll icon indicating copy to clipboard operation
pgroll copied to clipboard

No previous version exists for the first migration

Open agedemenli opened this issue 1 year ago • 0 comments

Since there is no "previous" version/schema for the first migration that is being run right after pgroll init, segv happens for column related changes such as adding unique constaint or a column; similar to #623

Having no previous version/schema might cause segv during Rollback, because it first tries to fetch the schema before the migration, in order to replay that migration on that schema. A similar issue that is happening when a sql command is run between migrations is fixed in #631 by taking inferred migrations into account. However, if the sql command is not added into migrations table, presumably because it is executed before pgroll init, pgroll wouldn't be able to find it.

To reproduce, first create a table:

create table products (id serial primary key, name varchar(255) unique, price decimal(10,2));

Then:

pgroll init
pgroll start ./examples/03_add_column.json
pgroll rollback ./examples/03_add_column.json
▀  Rolling back migration... (0s)panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x10 pc=0x10446ffe4]

goroutine 1 [running]:
github.com/xataio/pgroll/pkg/migrations.addColumn({0x104a6f4c0, 0x104eea2e0}, {0x104a6faa8, 0x104eea2e0}, {{0x0, 0x0, 0x0, 0x0, {0x14000292150, 0x17}, ...}, ...}, ...)
        /Users/ahmet/pgroll/pkg/migrations/op_add_column.go:242 +0x114
github.com/xataio/pgroll/pkg/migrations.(*OpAddColumn).Start(0x140002a2180, {0x104a6f4c0, 0x104eea2e0}, {0x104a6faa8, 0x104eea2e0}, {0x0, 0x0}, {0x104a67220, 0x104a62c10}, 0x1400028e3f0, ...)
        /Users/ahmet/pgroll/pkg/migrations/op_add_column.go:23 +0x134
github.com/xataio/pgroll/pkg/migrations.(*Migration).UpdateVirtualSchema(0x1400028e198?, {0x104a6f4c0, 0x104eea2e0}, 0x1400028e3f0)
        /Users/ahmet/pgroll/pkg/migrations/migrations.go:103 +0x84
github.com/xataio/pgroll/pkg/roll.(*Roll).Rollback(0x140000ed280, {0x104a6f4c0, 0x104eea2e0})
        /Users/ahmet/pgroll/pkg/roll/execute.go:259 +0x250
github.com/xataio/pgroll/cmd.init.func4(0x104e56440, {0x1046ca38b?, 0x4?, 0x1046ca363?})
        /Users/ahmet/pgroll/cmd/rollback.go:23 +0xf4
github.com/spf13/cobra.(*Command).execute(0x104e56440, {0x14000048980, 0x1, 0x1})
        /Users/ahmet/go/pkg/mod/github.com/spf13/[email protected]/command.go:985 +0x834
github.com/spf13/cobra.(*Command).ExecuteC(0x140001be608)
        /Users/ahmet/go/pkg/mod/github.com/spf13/[email protected]/command.go:1117 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/ahmet/go/pkg/mod/github.com/spf13/[email protected]/command.go:1041
github.com/xataio/pgroll/cmd.Execute()
        /Users/ahmet/pgroll/cmd/root.go:87 +0x20
main.main()
        /Users/ahmet/pgroll/main.go:12 +0x1c

We should consider doing a read_schema during the init phase and save it to somewhere, probably into migrations table.

agedemenli avatar Jan 30 '25 00:01 agedemenli