sessions/internal/bundb/migrations/20221119072648_create_api_keys_table.go
Dawid Wysokiński ffcb965590
All checks were successful
continuous-integration/drone/push Build is passing
feat: add a new command - db apikey create (#6)
Reviewed-on: #6
2022-11-20 08:14:41 +00:00

32 lines
769 B
Go

package migrations
import (
"context"
"fmt"
"gitea.dwysokinski.me/twhelp/sessions/internal/bundb/internal/model"
"github.com/uptrace/bun"
)
func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
if _, err := db.NewCreateTable().
Model(&model.APIKey{}).
Varchar(defaultVarcharLength).
ForeignKey(`(user_id) REFERENCES users (id) ON DELETE CASCADE`).
Exec(ctx); err != nil {
return fmt.Errorf("couldn't create the 'api_keys' table: %w", err)
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
if _, err := db.NewDropTable().
Model(&model.APIKey{}).
IfExists().
Cascade().
Exec(ctx); err != nil {
return fmt.Errorf("couldn't drop the 'api_keys' table: %w", err)
}
return nil
})
}