38 lines
717 B
Go
38 lines
717 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"sonarqube-badge/config"
|
|
)
|
|
|
|
type SQProject struct {
|
|
gorm.Model
|
|
ProjectName string `gorm:"unique"`
|
|
Token string `gorm:"unique"`
|
|
}
|
|
|
|
type User struct {
|
|
gorm.Model
|
|
Username string
|
|
Password string
|
|
Email string
|
|
Salt string
|
|
}
|
|
|
|
func CreateDatabase(ctx context.Context) context.Context {
|
|
cfg := ctx.Value("config").(config.Config)
|
|
db, err := gorm.Open(sqlite.Open(fmt.Sprintf("%s/sqbadge.db", cfg.DataPath)), &gorm.Config{})
|
|
if err != nil {
|
|
panic("failed to open database")
|
|
}
|
|
|
|
err = db.AutoMigrate(&SQProject{})
|
|
if err != nil {
|
|
panic("failed to migrate database")
|
|
}
|
|
|
|
return context.WithValue(ctx, "db", db)
|
|
}
|