Added unique constraint on onetimetoken

This commit is contained in:
Asaf Gartner 2021-08-17 08:31:48 +03:00
parent 34a0649dfc
commit 40ba0d5455
1 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package migrations
import (
"context"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"git.handmade.network/hmn/hmn/src/oops"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(OTTUniqueConstraint{})
}
type OTTUniqueConstraint struct{}
func (m OTTUniqueConstraint) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 8, 17, 5, 25, 8, 0, time.UTC))
}
func (m OTTUniqueConstraint) Name() string {
return "OTTUniqueConstraint"
}
func (m OTTUniqueConstraint) Description() string {
return "Add userid+type unique constraint to onetimetoken"
}
func (m OTTUniqueConstraint) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx,
`
DROP INDEX handmade_onetimetoken_ownerid_type;
ALTER TABLE handmade_onetimetoken
ADD CONSTRAINT handmade_onetimetoken_ownerid_type UNIQUE(owner_id, token_type);
`,
)
if err != nil {
return oops.New(err, "failed to replace index with unique constraint")
}
return nil
}
func (m OTTUniqueConstraint) Down(ctx context.Context, tx pgx.Tx) error {
panic("Implement me")
}