Skip to content

Commit

Permalink
change primary key from 'created_at' to 'id'
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gauravgahlot committed Dec 18, 2020
1 parent 617d56a commit f3a4074
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions db/migration/2020121691335-update-events-primary-key.go
Original file line number Diff line number Diff line change
@@ -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);
`},
}
}
1 change: 1 addition & 0 deletions db/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func GetMigrations() *migrate.MemoryMigrationSource {
Get202010221010(),
Get202012041103(),
Get202012091055(),
Get2020121691335(),
},
}
}

0 comments on commit f3a4074

Please sign in to comment.