add_pats.go
009_add_pats.go - Overview
This file defines a SQL migration to add tables org_domains
and personal_access_tokens
to the database. It includes the migration logic for creating these tables and a placeholder for the rollback operation.
Detailed Documentation
type addPats
type addPats struct{}
- Purpose: Represents the SQL migration for adding the
org_domains
andpersonal_access_tokens
tables.
func NewAddPatsFactory
func NewAddPatsFactory() factory.ProviderFactory[SQLMigration, Config] {
return factory.NewProviderFactory(factory.MustNewName("add_pats"), newAddPats)
}
- Purpose: Creates a new factory for the
addPats
migration. - Returns: A
factory.ProviderFactory
that createsSQLMigration
instances.
func newAddPats
func newAddPats(_ context.Context, _ factory.ProviderSettings, _ Config) (SQLMigration, error) {
return &addPats{}, nil
}
- Purpose: Instantiates a new
addPats
migration object. - Parameters:
_
: Acontext.Context
(unused)._
: Afactory.ProviderSettings
(unused)._
: AConfig
(unused).
- Returns: A new
addPats
instance and anil
error.
func (*addPats) Register
func (migration *addPats) Register(migrations *migrate.Migrations) error {
if err := migrations.Register(migration.Up, migration.Down); err != nil {
return err
}
return nil
}
- Purpose: Registers the
Up
andDown
functions of the migration with themigrate.Migrations
object. - Parameters:
migrations
: A pointer to amigrate.Migrations
object to register the migration functions.
- Returns: An
error
if registration fails,nil
otherwise.
func (*addPats) Up
func (migration *addPats) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.NewCreateTable().
Model(&struct {
bun.BaseModel `bun:"table:org_domains"`
ID string `bun:"id,pk,type:text"`
OrgID string `bun:"org_id,type:text,notnull"`
Name string `bun:"name,type:varchar(50),notnull,unique"`
CreatedAt int `bun:"created_at,notnull"`
UpdatedAt int `bun:"updated_at"`
Data string `bun:"data,type:text,notnull"`
}{}).
ForeignKey(`("org_id") REFERENCES "organizations" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err
}
if _, err := db.NewCreateTable().
Model(&struct {
bun.BaseModel `bun:"table:personal_access_tokens"`
ID int `bun:"id,pk,autoincrement"`
Role string `bun:"role,type:text,notnull,default:'ADMIN'"`
UserID string `bun:"user_id,type:text,notnull"`
Token string `bun:"token,type:text,notnull,unique"`
Name string `bun:"name,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull,default:0"`
ExpiresAt int `bun:"expires_at,notnull,default:0"`
UpdatedAt int `bun:"updated_at,notnull,default:0"`
LastUsed int `bun:"last_used,notnull,default:0"`
Revoked bool `bun:"revoked,notnull,default:false"`
UpdatedByUserID string `bun:"updated_by_user_id,type:text,notnull,default:''"`
}{}).
ForeignKey(`("user_id") REFERENCES "users" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err
}
return nil
}
- Purpose: Creates the
org_domains
andpersonal_access_tokens
tables in the database. - Parameters:
ctx
: Acontext.Context
for executing database operations.db
: A pointer to abun.DB
object representing the database connection.
- Returns: An
error
if table creation fails,nil
otherwise.
func (*addPats) Down
func (migration *addPats) Down(ctx context.Context, db *bun.DB) error {
return nil
}
- Purpose: Placeholder for the migration rollback operation. Currently, it does nothing.
- Parameters:
ctx
: Acontext.Context
(unused).db
: A pointer to abun.DB
object (unused).
- Returns:
nil
.
Include in Getting Started: NO