package types import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) type UUID_ID struct { ID uuid.UUID `gorm:"type:uuid;primaryKey"` } func (u *UUID_ID) BeforeCreate(tx *gorm.DB) (err error) { if u.ID == uuid.Nil { u.ID, _ = uuid.NewV7() } return } type User struct { UUID_ID Handle string `gorm:"uniqueIndex;not null"` Name string Email string `gorm:"uniqueIndex;not null"` PasswordHash string `gorm:"not null"` HackatimeToken string SSHPubKeys []SSHPubKey Config UserConfig `gorm:"foreignKey:UserID"` Sessions []Session `gorm:"foreignKey:UserID"` Repos []Repo `gorm:"foreignKey:UserID"` } type UserConfig struct { UUID_ID UserID uuid.UUID `gorm:"index;not null"` GitDefaultBranch string `gorm:"default:master;not null"` } type SSHPubKey struct { UUID_ID UserID uuid.UUID `gorm:"not null;index;uniqueIndex:idx_user_ssh_name"` Name string `gorm:"not null;uniqueIndex:idx_user_ssh_name"` PublicKey string `gorm:"not null"` Fingerprint string `gorm:"uniqueIndex;not null"` Algorithm string `gorm:"not null"` } type Session struct { UUID_ID UserID *uuid.UUID User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` CreatedAt time.Time } type Repo struct { UUID_ID CreatedAt time.Time UserID *uuid.UUID `gorm:"not null;uniqueIndex:idx_user_repo_name"` User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Name string `gorm:"uniqueIndex:idx_user_repo_name"` // idx_user_repo_name prevents two user's repos from having the same name. HackatimeName string Description string BaseBranch string `gorm:"default:master"` IsPrivate bool `gorm:"not null"` Config RepoConfig `gorm:"foreignKey:RepoID"` Issues []Issue `gorm:"foreignKey:RepoID"` NextIssueID uint `gorm:"not null;default:1"` } type RepoConfig struct { UUID_ID RepoID uuid.UUID `gorm:"index;not null"` AllowAnonymousIssues bool `gorm:"default:true"` AllowNewIssues bool `gorm:"default:true"` } type Issue struct { UUID_ID IssueID uint CreatedAt time.Time RepoID *uuid.UUID Repo *Repo // todo: figure out how to prevent user deletion cascading UserID *uuid.UUID // anonymous issues require UserID to be nullable. `gorm:"not null;"` User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Title string `gorm:"not null;"` Description string `gorm:"not null;"` } type IssueComment struct { UUID_ID IssueID *uuid.UUID Issue *Issue UserID *uuid.UUID User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Comment string `gorm:"not null;"` CreatedAt time.Time } type OAuthState struct { UUID_ID UserID *uuid.UUID `gorm:"not null;"` State string `gorm:"uniqueIndex;size:64;not null"` Verifier string `gorm:"size:128;not null"` ExpiresAt time.Time `gorm:"index;not null"` CreatedAt time.Time }