117 lines
4.3 KiB
Go
117 lines
4.3 KiB
Go
package database
|
|
|
|
import "time"
|
|
|
|
// Repository 仓库模型
|
|
type Repository struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
Name string `gorm:"uniqueIndex;not null" json:"name"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
// 关联关系
|
|
Manifests []Manifest `gorm:"foreignKey:RepositoryID;constraint:OnDelete:CASCADE" json:"manifests,omitempty"`
|
|
Tags []Tag `gorm:"foreignKey:RepositoryID;constraint:OnDelete:CASCADE" json:"tags,omitempty"`
|
|
BlobUploads []BlobUpload `gorm:"foreignKey:RepositoryID;constraint:OnDelete:CASCADE" json:"blob_uploads,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Repository) TableName() string {
|
|
return "repositories"
|
|
}
|
|
|
|
// Manifest Manifest 模型
|
|
type Manifest struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
RepositoryID uint `gorm:"index;not null" json:"repository_id"`
|
|
Digest string `gorm:"index;not null" json:"digest"`
|
|
MediaType string `gorm:"not null" json:"media_type"`
|
|
Size int64 `gorm:"not null" json:"size"`
|
|
DataPath string `gorm:"not null" json:"data_path"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
// 唯一约束:同一仓库中 digest 唯一
|
|
_ struct{} `gorm:"uniqueIndex:idx_repo_digest"`
|
|
|
|
// 关联关系
|
|
Repository Repository `gorm:"foreignKey:RepositoryID" json:"repository,omitempty"`
|
|
Tags []Tag `gorm:"foreignKey:ManifestID;constraint:OnDelete:CASCADE" json:"tags,omitempty"`
|
|
ManifestBlobs []ManifestBlob `gorm:"foreignKey:ManifestID;constraint:OnDelete:CASCADE" json:"manifest_blobs,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Manifest) TableName() string {
|
|
return "manifests"
|
|
}
|
|
|
|
// Blob Blob 模型
|
|
type Blob struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
Digest string `gorm:"uniqueIndex;not null" json:"digest"`
|
|
Size int64 `gorm:"not null" json:"size"`
|
|
DataPath string `gorm:"not null" json:"data_path"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
// 关联关系
|
|
ManifestBlobs []ManifestBlob `gorm:"foreignKey:BlobID;constraint:OnDelete:CASCADE" json:"manifest_blobs,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Blob) TableName() string {
|
|
return "blobs"
|
|
}
|
|
|
|
// Tag 标签模型
|
|
type Tag struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
RepositoryID uint `gorm:"uniqueIndex:idx_repo_name;not null" json:"repository_id"`
|
|
Name string `gorm:"uniqueIndex:idx_repo_name;not null" json:"name"`
|
|
ManifestID uint `gorm:"index;not null" json:"manifest_id"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
// 关联关系
|
|
Repository Repository `gorm:"foreignKey:RepositoryID" json:"repository,omitempty"`
|
|
Manifest Manifest `gorm:"foreignKey:ManifestID" json:"manifest,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Tag) TableName() string {
|
|
return "tags"
|
|
}
|
|
|
|
// BlobUpload Blob 上传会话模型
|
|
type BlobUpload struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
UUID string `gorm:"uniqueIndex;not null" json:"uuid"`
|
|
RepositoryID uint `gorm:"index;not null" json:"repository_id"`
|
|
Size int64 `gorm:"default:0" json:"size"`
|
|
DataPath string `gorm:"not null" json:"data_path"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
// 关联关系
|
|
Repository Repository `gorm:"foreignKey:RepositoryID" json:"repository,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (BlobUpload) TableName() string {
|
|
return "blob_uploads"
|
|
}
|
|
|
|
// ManifestBlob Manifest 和 Blob 关联模型
|
|
type ManifestBlob struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
ManifestID uint `gorm:"uniqueIndex:idx_manifest_blob;index;not null" json:"manifest_id"`
|
|
BlobID uint `gorm:"uniqueIndex:idx_manifest_blob;index;not null" json:"blob_id"`
|
|
|
|
// 关联关系
|
|
Manifest Manifest `gorm:"foreignKey:ManifestID" json:"manifest,omitempty"`
|
|
Blob Blob `gorm:"foreignKey:BlobID" json:"blob,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (ManifestBlob) TableName() string {
|
|
return "manifest_blobs"
|
|
}
|