sessions/internal/bundb/migrations/20221122050217_create_sessions_table.go

32 lines
771 B
Go
Raw Normal View History

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.Session{}).
Varchar(defaultVarcharLength).
ForeignKey(`(user_id) REFERENCES users (id) ON DELETE CASCADE`).
Exec(ctx); err != nil {
return fmt.Errorf("couldn't create the 'sessions' table: %w", err)
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
if _, err := db.NewDropTable().
Model(&model.Session{}).
IfExists().
Cascade().
Exec(ctx); err != nil {
return fmt.Errorf("couldn't drop the 'sessions' table: %w", err)
}
return nil
})
}