refactor: shorten model and table prefixes

This commit is contained in:
2026-07-23 14:30:17 +08:00
parent 6e7be2f3a4
commit 0ca2898ac0
71 changed files with 707 additions and 638 deletions

View File

@@ -102,7 +102,7 @@ func (h *Handler) upload(c *gin.Context) {
c.JSON(http.StatusCreated, sourceDTO(*source))
}
func sourceDTO(source models.SenlinAgentSource) SourceDTO {
func sourceDTO(source models.SaSource) SourceDTO {
return SourceDTO{
ID: source.Identity, ProjectID: source.ProjectIdentity, Kind: source.Kind,
Title: source.Title, StorageKey: filepath.Base(filepath.FromSlash(source.FilePath)),

View File

@@ -41,7 +41,7 @@ func TestFileRegistrarSavesBeforeCreatingIdentitySourceDTO(t *testing.T) {
router.ServeHTTP(rec, req)
require.Equal(t, http.StatusCreated, rec.Code, rec.Body.String())
var source models.SenlinAgentSource
var source models.SaSource
require.NoError(t, database.Where("project_id = ?", project.ID).First(&source).Error)
require.FileExists(t, filepath.Join(storageRoot, filepath.FromSlash(source.FilePath)))
require.Contains(t, filepath.ToSlash(source.FilePath), "projects/"+project.Identity+"/")
@@ -75,7 +75,7 @@ func TestFileRegistrarChecksOwnershipBeforeWritingFile(t *testing.T) {
require.Equal(t, http.StatusNotFound, rec.Code, rec.Body.String())
var count int64
require.NoError(t, database.Model(&models.SenlinAgentSource{}).Count(&count).Error)
require.NoError(t, database.Model(&models.SaSource{}).Count(&count).Error)
require.Zero(t, count)
entries, err := os.ReadDir(storageRoot)
require.NoError(t, err)
@@ -85,7 +85,7 @@ func TestFileRegistrarChecksOwnershipBeforeWritingFile(t *testing.T) {
func TestFileRegistrarRemovesStoredFileWhenSourceCreateFails(t *testing.T) {
router, database, project, storageRoot := newFileHandlerTestRouter(t, 1, 1)
require.NoError(t, database.Callback().Create().Before("gorm:create").Register("test:fail_source_create", func(tx *gorm.DB) {
if tx.Statement.Schema != nil && tx.Statement.Schema.Table == (models.SenlinAgentSource{}).TableName() {
if tx.Statement.Schema != nil && tx.Statement.Schema.Table == (models.SaSource{}).TableName() {
tx.AddError(errors.New("forced source create failure"))
}
}))
@@ -139,7 +139,7 @@ func TestFileRegistrarRejectsRequestOverConfiguredUploadLimit(t *testing.T) {
func TestFileRegistrarLogsCleanupFailureWithoutLeakingStorageRoot(t *testing.T) {
router, database, project, storageRoot, service := newFileHandlerTestRouterWithService(t, 1, 1, DefaultMaxUploadBytes)
require.NoError(t, database.Callback().Create().Before("gorm:create").Register("test:fail_source_create_for_cleanup_log", func(tx *gorm.DB) {
if tx.Statement.Schema != nil && tx.Statement.Schema.Table == (models.SenlinAgentSource{}).TableName() {
if tx.Statement.Schema != nil && tx.Statement.Schema.Table == (models.SaSource{}).TableName() {
tx.AddError(errors.New("forced source create failure"))
}
}))
@@ -173,24 +173,24 @@ func TestFileRegistrarLogsCleanupFailureWithoutLeakingStorageRoot(t *testing.T)
require.Contains(t, serverLog.String(), storageRoot)
}
func newFileHandlerTestRouter(t *testing.T, currentUserID, ownerID uint) (*gin.Engine, *gorm.DB, models.SenlinAgentProject, string) {
func newFileHandlerTestRouter(t *testing.T, currentUserID, ownerID uint) (*gin.Engine, *gorm.DB, models.SaProject, string) {
return newFileHandlerTestRouterWithLimit(t, currentUserID, ownerID, DefaultMaxUploadBytes)
}
func newFileHandlerTestRouterWithLimit(t *testing.T, currentUserID, ownerID uint, maxUploadBytes int64) (*gin.Engine, *gorm.DB, models.SenlinAgentProject, string) {
func newFileHandlerTestRouterWithLimit(t *testing.T, currentUserID, ownerID uint, maxUploadBytes int64) (*gin.Engine, *gorm.DB, models.SaProject, string) {
router, database, project, storageRoot, _ := newFileHandlerTestRouterWithService(t, currentUserID, ownerID, maxUploadBytes)
return router, database, project, storageRoot
}
func newFileHandlerTestRouterWithService(t *testing.T, currentUserID, ownerID uint, maxUploadBytes int64) (*gin.Engine, *gorm.DB, models.SenlinAgentProject, string, *Service) {
func newFileHandlerTestRouterWithService(t *testing.T, currentUserID, ownerID uint, maxUploadBytes int64) (*gin.Engine, *gorm.DB, models.SaProject, string, *Service) {
t.Helper()
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{TranslateError: true})
require.NoError(t, err)
require.NoError(t, models.AutoMigrate(database))
models.DBService = database
require.NoError(t, database.Create(&models.SenlinAgentUser{Email: "owner@example.com", DisplayName: "Owner", PasswordHash: "hash"}).Error)
require.NoError(t, database.Create(&models.SenlinAgentUser{Email: "other@example.com", DisplayName: "Other", PasswordHash: "hash"}).Error)
project := models.SenlinAgentProject{OwnerID: ownerID, Name: "Files", Identifier: fmt.Sprintf("FILES-%d", ownerID)}
require.NoError(t, database.Create(&models.SaUser{Email: "owner@example.com", DisplayName: "Owner", PasswordHash: "hash"}).Error)
require.NoError(t, database.Create(&models.SaUser{Email: "other@example.com", DisplayName: "Other", PasswordHash: "hash"}).Error)
project := models.SaProject{OwnerID: ownerID, Name: "Files", Identifier: fmt.Sprintf("FILES-%d", ownerID)}
require.NoError(t, database.Create(&project).Error)
storageRoot := t.TempDir()
service := NewService(storageRoot, database)

View File

@@ -193,7 +193,7 @@ func randomStorageKey() (string, error) {
}
// CreateSource 只持久化 Save 产生的相对路径,不接受 handler 自行拼接本地路径。
func (s *Service) CreateSource(ownerID uint, project *models.SenlinAgentProject, title string, stored StoredFile) (*models.SenlinAgentSource, error) {
func (s *Service) CreateSource(ownerID uint, project *models.SaProject, title string, stored StoredFile) (*models.SaSource, error) {
title = strings.TrimSpace(title)
if title == "" {
title = stored.OriginalName
@@ -205,7 +205,7 @@ func (s *Service) CreateSource(ownerID uint, project *models.SenlinAgentProject,
if relativePath == "" || filepath.IsAbs(relativePath) {
return nil, ErrSourcePathRequired
}
source := &models.SenlinAgentSource{
source := &models.SaSource{
ProjectID: project.ID, ProjectIdentity: project.Identity, CreatedBy: ownerID,
Kind: "file", Title: title, FilePath: relativePath,
}