From 409a2e830421515a830dc9fcbe1b8d7ddc60f055 Mon Sep 17 00:00:00 2001 From: Rogee Date: Fri, 23 May 2025 21:43:49 +0800 Subject: [PATCH] feat: update --- backend/app/http/admin/uploads.go | 2 +- backend/app/jobs/video_extract_head_image.go | 13 ++- backend/app/jobs/video_store_short.go | 13 ++- backend/app/model/medias.go | 8 +- backend/app/model/medias_test.go | 6 +- backend/app/model/migrations.gen.go | 19 ----- backend/database/table/migrations.go | 84 -------------------- backend/database/table/table_use_schema.go | 1 - backend/database/transform.yaml | 1 + 9 files changed, 21 insertions(+), 126 deletions(-) delete mode 100644 backend/app/model/migrations.gen.go delete mode 100644 backend/database/table/migrations.go diff --git a/backend/app/http/admin/uploads.go b/backend/app/http/admin/uploads.go index 012cc11..58bcaba 100644 --- a/backend/app/http/admin/uploads.go +++ b/backend/app/http/admin/uploads.go @@ -75,7 +75,7 @@ func (up *uploads) PostUploadedAction(ctx fiber.Ctx, body *PostUploadedForm) err Hash: body.Md5, Path: filepath.Join(UPLOAD_PATH, body.Md5+filepath.Ext(body.OriginalName)), } - if err := model.MediasModel().Create(ctx.Context(), m); err != nil { + if err := m.Create(ctx.Context()); err != nil { return err } diff --git a/backend/app/jobs/video_extract_head_image.go b/backend/app/jobs/video_extract_head_image.go index f0898cc..5de3cca 100644 --- a/backend/app/jobs/video_extract_head_image.go +++ b/backend/app/jobs/video_extract_head_image.go @@ -89,12 +89,11 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx // create a new media record for the image imageMedia := &model.Medias{ - CreatedAt: time.Now(), - Name: name, - MimeType: "image/jpeg", - Size: fileSize, - Path: w.oss.GetSavePath(filename), - Hash: fileMd5, + Name: name, + MimeType: "image/jpeg", + Size: fileSize, + Path: w.oss.GetSavePath(filename), + Hash: fileMd5, Metas: fields.ToJson(fields.MediaMetas{ ParentHash: media.Hash, }), @@ -110,7 +109,7 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx log.Errorf("Error removing original file: %v", err) } - if err := model.MediasModel().Create(ctx, imageMedia); err != nil { + if err := imageMedia.Create(ctx); err != nil { log.Errorf("Error creating media record: %v", err) return errors.Wrap(err, "failed to create media record") } diff --git a/backend/app/jobs/video_store_short.go b/backend/app/jobs/video_store_short.go index fc72bb5..b25d328 100644 --- a/backend/app/jobs/video_store_short.go +++ b/backend/app/jobs/video_store_short.go @@ -91,12 +91,11 @@ func (w *VideoStoreShortWorker) Work(ctx context.Context, job *Job[VideoStoreSho // save to db and relate to master mediaModel := &model.Medias{ - CreatedAt: time.Now(), - Name: "[试听]" + media.Name, - MimeType: media.MimeType, - Size: fileSize, - Path: filePath, - Hash: fileMd5, + Name: "[试听] " + media.Name, + MimeType: media.MimeType, + Size: fileSize, + Path: filePath, + Hash: fileMd5, Metas: fields.ToJson(fields.MediaMetas{ ParentHash: media.Hash, Short: true, @@ -112,7 +111,7 @@ func (w *VideoStoreShortWorker) Work(ctx context.Context, job *Job[VideoStoreSho } log.Infof("pending create media record %s", job.Args.FilePath) - if err := model.MediasModel().Create(ctx, mediaModel); err != nil { + if err := mediaModel.Create(ctx); err != nil { log.Errorf("Error saving media record: %v data: %+v", err, mediaModel) return err } diff --git a/backend/app/model/medias.go b/backend/app/model/medias.go index 858d784..a95b1a4 100644 --- a/backend/app/model/medias.go +++ b/backend/app/model/medias.go @@ -97,12 +97,12 @@ func (m *Medias) BatchCreate(ctx context.Context, models []*Medias) error { return nil } -func (m *Medias) Create(ctx context.Context, medias *Medias) error { - medias.CreatedAt = time.Now() - stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(medias).RETURNING(table.Medias.AllColumns) +func (m *Medias) Create(ctx context.Context) error { + m.CreatedAt = time.Now() + stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns) m.log().Infof("sql: %s", stmt.DebugSql()) - if err := stmt.QueryContext(ctx, db, medias); err != nil { + if err := stmt.QueryContext(ctx, db, m); err != nil { m.log().Errorf("error creating media item: %v", err) return err } diff --git a/backend/app/model/medias_test.go b/backend/app/model/medias_test.go index 0548985..1e02994 100644 --- a/backend/app/model/medias_test.go +++ b/backend/app/model/medias_test.go @@ -132,7 +132,7 @@ func (s *MediasTestSuite) Test_Create() { Path: "path/to/media.pdf", } - err := MediasModel().Create(context.Background(), model) + err := model.Create(context.Background()) Convey("Create should not return an error", func() { So(err, ShouldBeNil) }) @@ -165,7 +165,7 @@ func (s *MediasTestSuite) Test_Page() { Path: "path/to/media.pdf", } - err := MediasModel().Create(context.Background(), model) + err := model.Create(context.Background()) So(err, ShouldBeNil) } @@ -208,7 +208,7 @@ func (s *MediasTestSuite) Test_CreateGetID() { Path: "path/to/media.pdf", } - err := MediasModel().Create(context.Background(), model) + err := model.Create(context.Background()) So(err, ShouldBeNil) So(model.ID, ShouldNotBeEmpty) diff --git a/backend/app/model/migrations.gen.go b/backend/app/model/migrations.gen.go deleted file mode 100644 index ba622c1..0000000 --- a/backend/app/model/migrations.gen.go +++ /dev/null @@ -1,19 +0,0 @@ -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package model - -import ( - "time" -) - -type Migrations struct { - ID int32 `sql:"primary_key" json:"id"` - VersionID int64 `json:"version_id"` - IsApplied bool `json:"is_applied"` - Tstamp time.Time `json:"tstamp"` -} diff --git a/backend/database/table/migrations.go b/backend/database/table/migrations.go deleted file mode 100644 index c4a6b2d..0000000 --- a/backend/database/table/migrations.go +++ /dev/null @@ -1,84 +0,0 @@ -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package table - -import ( - "github.com/go-jet/jet/v2/postgres" -) - -var Migrations = newMigrationsTable("public", "migrations", "") - -type migrationsTable struct { - postgres.Table - - // Columns - ID postgres.ColumnInteger - VersionID postgres.ColumnInteger - IsApplied postgres.ColumnBool - Tstamp postgres.ColumnTimestamp - - AllColumns postgres.ColumnList - MutableColumns postgres.ColumnList -} - -type MigrationsTable struct { - migrationsTable - - EXCLUDED migrationsTable -} - -// AS creates new MigrationsTable with assigned alias -func (a MigrationsTable) AS(alias string) *MigrationsTable { - return newMigrationsTable(a.SchemaName(), a.TableName(), alias) -} - -// Schema creates new MigrationsTable with assigned schema name -func (a MigrationsTable) FromSchema(schemaName string) *MigrationsTable { - return newMigrationsTable(schemaName, a.TableName(), a.Alias()) -} - -// WithPrefix creates new MigrationsTable with assigned table prefix -func (a MigrationsTable) WithPrefix(prefix string) *MigrationsTable { - return newMigrationsTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) -} - -// WithSuffix creates new MigrationsTable with assigned table suffix -func (a MigrationsTable) WithSuffix(suffix string) *MigrationsTable { - return newMigrationsTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) -} - -func newMigrationsTable(schemaName, tableName, alias string) *MigrationsTable { - return &MigrationsTable{ - migrationsTable: newMigrationsTableImpl(schemaName, tableName, alias), - EXCLUDED: newMigrationsTableImpl("", "excluded", ""), - } -} - -func newMigrationsTableImpl(schemaName, tableName, alias string) migrationsTable { - var ( - IDColumn = postgres.IntegerColumn("id") - VersionIDColumn = postgres.IntegerColumn("version_id") - IsAppliedColumn = postgres.BoolColumn("is_applied") - TstampColumn = postgres.TimestampColumn("tstamp") - allColumns = postgres.ColumnList{IDColumn, VersionIDColumn, IsAppliedColumn, TstampColumn} - mutableColumns = postgres.ColumnList{VersionIDColumn, IsAppliedColumn, TstampColumn} - ) - - return migrationsTable{ - Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), - - //Columns - ID: IDColumn, - VersionID: VersionIDColumn, - IsApplied: IsAppliedColumn, - Tstamp: TstampColumn, - - AllColumns: allColumns, - MutableColumns: mutableColumns, - } -} diff --git a/backend/database/table/table_use_schema.go b/backend/database/table/table_use_schema.go index e8f6717..d919caf 100644 --- a/backend/database/table/table_use_schema.go +++ b/backend/database/table/table_use_schema.go @@ -11,7 +11,6 @@ package table // this method only once at the beginning of the program. func UseSchema(schema string) { Medias = Medias.FromSchema(schema) - Migrations = Migrations.FromSchema(schema) Orders = Orders.FromSchema(schema) Posts = Posts.FromSchema(schema) UserPosts = UserPosts.FromSchema(schema) diff --git a/backend/database/transform.yaml b/backend/database/transform.yaml index 9df97e6..9513916 100644 --- a/backend/database/transform.yaml +++ b/backend/database/transform.yaml @@ -6,6 +6,7 @@ ignores: - river_client_queue - river_job_state - river_queue + - migrations model: - migrations - user_posts