From f3a4074b5e8ea24eaa30d9d0fa70baf82b32893b Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Fri, 18 Dec 2020 16:46:01 +0530 Subject: [PATCH] change primary key from 'created_at' to 'id' We can have multiple events generated at a given time, therefore the value of 'created_at' field will be same for each event. This violates the unique constraint on "events_pkey", when these events are add to the events table. The migration changes the primary key on events table from 'created_at' to `id`, which will always be unique for each event generated at any point in time. Signed-off-by: Gaurav Gahlot --- ...2020121691335-update-events-primary-key.go | 24 +++++++++++++++++++ db/migration/migration.go | 1 + 2 files changed, 25 insertions(+) create mode 100644 db/migration/2020121691335-update-events-primary-key.go diff --git a/db/migration/2020121691335-update-events-primary-key.go b/db/migration/2020121691335-update-events-primary-key.go new file mode 100644 index 000000000..47f348f2c --- /dev/null +++ b/db/migration/2020121691335-update-events-primary-key.go @@ -0,0 +1,24 @@ +package migration + +import migrate "github.com/rubenv/sql-migrate" + +// Get2020121691335 updates the primary key on events table. +// +// Fixes: https://github.com/tinkerbell/tink/issues/379 +// +// We can have multiple events generated at a given time, therefore the value of +// 'created_at' field will be same for each event. This violates the unique constraint +// on "events_pkey", when these events are add to the events table. +// +// The migration changes the primary key on events table from 'created_at' to `id`, +// which will always be unique for each event generated at any point in time. +func Get2020121691335() *migrate.Migration { + return &migrate.Migration{ + Id: "2020121691335-update-events-primary-key", + Up: []string{` + ALTER TABLE events DROP CONSTRAINT events_pkey; + ALTER TABLE events ADD PRIMARY KEY (id); + CREATE INDEX IF NOT EXISTS idx_events_created_at ON events (created_at); + `}, + } +} diff --git a/db/migration/migration.go b/db/migration/migration.go index 13a6cb89c..7243bfc56 100644 --- a/db/migration/migration.go +++ b/db/migration/migration.go @@ -10,6 +10,7 @@ func GetMigrations() *migrate.MemoryMigrationSource { Get202010221010(), Get202012041103(), Get202012091055(), + Get2020121691335(), }, } }