From c608877c3e19205a5e56e85bfe9181d5561e35d6 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 11 Dec 2023 22:16:39 +0800 Subject: [PATCH] chore: clean binary entries --- Dockerfile | 2 +- cmd/memos.go => bin/memos/main.go | 9 +- cmd/copydb.go | 394 ------------------------------ cmd/mvrss.go | 99 -------- cmd/setup.go | 142 ----------- docs/development-windows.md | 2 +- main.go | 15 -- scripts/.air-windows.toml | 2 +- scripts/.air.toml | 2 +- scripts/build.ps1 | 2 +- scripts/build.sh | 4 +- server/profile/profile.go | 5 + store/db/mysql/activity.go | 12 - store/db/mysql/idp.go | 4 - store/db/mysql/memo.go | 24 -- store/db/mysql/resource.go | 24 -- store/db/mysql/storage.go | 6 - store/db/mysql/user.go | 24 -- store/db/mysql/webhook.go | 6 - store/db/postgres/activity.go | 11 - store/db/postgres/idp.go | 5 - store/db/postgres/memo.go | 21 -- store/db/postgres/resource.go | 20 -- store/db/postgres/storage.go | 5 - store/db/postgres/user.go | 21 -- store/db/postgres/webhook.go | 5 - store/db/sqlite/activity.go | 12 - store/db/sqlite/idp.go | 4 - store/db/sqlite/memo.go | 24 -- store/db/sqlite/resource.go | 24 -- store/db/sqlite/storage.go | 6 - store/db/sqlite/user.go | 31 --- store/db/sqlite/webhook.go | 7 - 33 files changed, 20 insertions(+), 954 deletions(-) rename cmd/memos.go => bin/memos/main.go (98%) delete mode 100644 cmd/copydb.go delete mode 100644 cmd/mvrss.go delete mode 100644 cmd/setup.go delete mode 100644 main.go diff --git a/Dockerfile b/Dockerfile index 8286ca0f..4cd805be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ WORKDIR /backend-build COPY . . COPY --from=frontend /frontend-build/web/dist ./server/dist -RUN CGO_ENABLED=0 go build -o memos ./main.go +RUN CGO_ENABLED=0 go build -o memos ./bin/memos/main.go # Make workspace with above generated files. FROM alpine:latest AS monolithic diff --git a/cmd/memos.go b/bin/memos/main.go similarity index 98% rename from cmd/memos.go rename to bin/memos/main.go index dedff53e..3720b276 100644 --- a/cmd/memos.go +++ b/bin/memos/main.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "context" @@ -186,3 +186,10 @@ func printGreetings() { fmt.Printf("👉GitHub: %s\n", "https://github.com/usememos/memos") println("---") } + +func main() { + err := Execute() + if err != nil { + panic(err) + } +} diff --git a/cmd/copydb.go b/cmd/copydb.go deleted file mode 100644 index e31ce436..00000000 --- a/cmd/copydb.go +++ /dev/null @@ -1,394 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "strings" - - "github.com/Masterminds/squirrel" - "github.com/pkg/errors" - "github.com/spf13/cobra" - - _profile "github.com/usememos/memos/server/profile" - "github.com/usememos/memos/store" - "github.com/usememos/memos/store/db" -) - -var ( - copydbCmdFlagFrom = "from" - copydbCmd = &cobra.Command{ - Use: "copydb", // `copydb` is a shortened for 'copy database' - Short: "Copy data between db drivers", - Run: func(cmd *cobra.Command, _ []string) { - s, err := cmd.Flags().GetString(copydbCmdFlagFrom) - if err != nil { - println("fail to get from driver DSN") - println(err) - return - } - ss := strings.Split(s, "://") - if len(ss) != 2 { - println("fail to parse from driver DSN, should be like 'sqlite://memos_prod.db' or 'mysql://user:pass@tcp(host)/memos'") - return - } - - fromProfile := &_profile.Profile{Driver: ss[0], DSN: ss[1]} - - err = copydb(fromProfile, profile) - if err != nil { - fmt.Printf("fail to copydb: %s\n", err) - return - } - - println("done") - }, - } -) - -func init() { - copydbCmd.Flags().String(copydbCmdFlagFrom, "sqlite://memos_prod.db", "From driver DSN") - - rootCmd.AddCommand(copydbCmd) -} - -func copydb(fromProfile, toProfile *_profile.Profile) error { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - toDriver, err := db.NewDBDriver(toProfile) - if err != nil { - return errors.Wrap(err, "fail to create `to` driver") - } - - // Check if `to` driver has been created before - if history, err := toDriver.FindMigrationHistoryList(ctx, nil); err != nil { - return errors.New("fail to check migration history of `to` driver") - } else if len(history) == 0 { - return errors.New("migration history of `to` driver should not be empty") - } - - if err := toDriver.Migrate(ctx); err != nil { - return errors.Wrap(err, "fail to migrate db") - } - - fromDriver, err := db.NewDBDriver(fromProfile) - if err != nil { - return errors.Wrap(err, "fail to create `from` driver") - } - - // Register here if any table is added - copyMap := map[string]func(context.Context, store.Driver, store.Driver) error{ - "activity": copyActivity, - "idp": copyIdp, - "memo": copyMemo, - "memo_organizer": copyMemoOrganizer, - "memo_relation": copyMemoRelation, - "resource": copyResource, - "storage": copyStorage, - "system_setting": copySystemSettings, - "tag": copyTag, - "user": copyUser, - "user_setting": copyUserSettings, - } - - toDb := toDriver.GetDB() - for table := range copyMap { - println("Checking " + table + "...") - var cnt int - if toProfile.Driver == "postgres" && table == "user" { - table = `"user"` - } - builder := squirrel.Select("COUNT(*)").From(table) - query, args, err := builder.ToSql() - if err != nil { - return errors.Wrapf(err, "fail to build query '%s'", table) - } - if err := toDb.QueryRowContext(ctx, query, args...).Scan(&cnt); err != nil { - return errors.Wrapf(err, "fail to check '%s'", table) - } - if cnt > 0 && table != "system_setting" { - return errors.Errorf("table '%s' is not empty", table) - } - } - - for _, f := range copyMap { - err = f(ctx, fromDriver, toDriver) - if err != nil { - return errors.Wrap(err, "fail to copy data") - } - } - - return nil -} - -func copyActivity(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying Activity...") - list, err := fromDriver.ListActivities(ctx, &store.FindActivity{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateActivity(ctx, &store.Activity{ - ID: item.ID, - CreatorID: item.CreatorID, - CreatedTs: item.CreatedTs, - Level: item.Level, - Type: item.Type, - Payload: item.Payload, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyIdp(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying IdentityProvider...") - list, err := fromDriver.ListIdentityProviders(ctx, &store.FindIdentityProvider{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateIdentityProvider(ctx, &store.IdentityProvider{ - ID: item.ID, - Name: item.Name, - Type: item.Type, - IdentifierFilter: item.IdentifierFilter, - Config: item.Config, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyMemo(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying Memo...") - list, err := fromDriver.ListMemos(ctx, &store.FindMemo{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateMemo(ctx, &store.Memo{ - ID: item.ID, - CreatorID: item.CreatorID, - CreatedTs: item.CreatedTs, - UpdatedTs: item.UpdatedTs, - RowStatus: item.RowStatus, - Content: item.Content, - Visibility: item.Visibility, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyMemoOrganizer(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying MemoOrganizer...") - list, err := fromDriver.ListMemoOrganizer(ctx, &store.FindMemoOrganizer{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.UpsertMemoOrganizer(ctx, &store.MemoOrganizer{ - MemoID: item.MemoID, - UserID: item.UserID, - Pinned: item.Pinned, - }) - if err != nil { - return err - } - } - println("\tDONE") - return nil -} - -func copyMemoRelation(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying MemoRelation...") - list, err := fromDriver.ListMemoRelations(ctx, &store.FindMemoRelation{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.UpsertMemoRelation(ctx, &store.MemoRelation{ - MemoID: item.MemoID, - RelatedMemoID: item.RelatedMemoID, - Type: item.Type, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyResource(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying Resource...") - list, err := fromDriver.ListResources(ctx, &store.FindResource{GetBlob: true}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateResource(ctx, &store.Resource{ - ID: item.ID, - CreatorID: item.CreatorID, - CreatedTs: item.CreatedTs, - UpdatedTs: item.UpdatedTs, - Filename: item.Filename, - Blob: item.Blob, - ExternalLink: item.ExternalLink, - Type: item.Type, - Size: item.Size, - InternalPath: item.InternalPath, - MemoID: item.MemoID, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyStorage(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying Storage...") - list, err := fromDriver.ListStorages(ctx, &store.FindStorage{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateStorage(ctx, &store.Storage{ - ID: item.ID, - Name: item.Name, - Type: item.Type, - Config: item.Config, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copySystemSettings(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying SystemSettings...") - list, err := fromDriver.ListSystemSettings(ctx, &store.FindSystemSetting{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.UpsertSystemSetting(ctx, &store.SystemSetting{ - Name: item.Name, - Value: item.Value, - Description: item.Description, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyTag(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying Tag...") - list, err := fromDriver.ListTags(ctx, &store.FindTag{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.UpsertTag(ctx, &store.Tag{ - Name: item.Name, - CreatorID: item.CreatorID, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyUser(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying User...") - list, err := fromDriver.ListUsers(ctx, &store.FindUser{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.CreateUser(ctx, &store.User{ - ID: item.ID, - CreatedTs: item.CreatedTs, - UpdatedTs: item.UpdatedTs, - RowStatus: item.RowStatus, - Username: item.Username, - Role: item.Role, - Email: item.Email, - Nickname: item.Nickname, - PasswordHash: item.PasswordHash, - AvatarURL: item.AvatarURL, - }) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} - -func copyUserSettings(ctx context.Context, fromDriver, toDriver store.Driver) error { - println("Copying UserSettings...") - list, err := fromDriver.ListUserSettings(ctx, &store.FindUserSetting{}) - if err != nil { - return err - } - - fmt.Printf("\tTotal %d records\n", len(list)) - for _, item := range list { - _, err := toDriver.UpsertUserSetting(ctx, item) - if err != nil { - return err - } - } - - println("\tDONE") - return nil -} diff --git a/cmd/mvrss.go b/cmd/mvrss.go deleted file mode 100644 index 402b42cb..00000000 --- a/cmd/mvrss.go +++ /dev/null @@ -1,99 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "os" - "time" - - "github.com/spf13/cobra" - - "github.com/usememos/memos/store" - "github.com/usememos/memos/store/db/sqlite" -) - -var ( - mvrssCmdFlagFrom = "from" - mvrssCmdFlagTo = "to" - mvrssCmd = &cobra.Command{ - Use: "mvrss", // `mvrss` is a shortened for 'means move resource' - Short: "Move resource between storage", - Run: func(cmd *cobra.Command, _ []string) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - - from, err := cmd.Flags().GetString(mvrssCmdFlagFrom) - if err != nil { - fmt.Printf("failed to get from storage, error: %+v\n", err) - return - } - - to, err := cmd.Flags().GetString(mvrssCmdFlagTo) - if err != nil { - fmt.Printf("failed to get to storage, error: %+v\n", err) - return - } - - if from != "local" || to != "db" { - fmt.Printf("only local=>db be supported currently\n") - return - } - - driver, err := sqlite.NewDB(profile) - if err != nil { - fmt.Printf("failed to create db driver, error: %+v\n", err) - return - } - if err := driver.Migrate(ctx); err != nil { - fmt.Printf("failed to migrate db, error: %+v\n", err) - return - } - - s := store.New(driver, profile) - resources, err := s.ListResources(ctx, &store.FindResource{}) - if err != nil { - fmt.Printf("failed to list resources, error: %+v\n", err) - return - } - - var emptyString string - for _, res := range resources { - if res.InternalPath == "" { - continue - } - - buf, err := os.ReadFile(res.InternalPath) - if err != nil { - fmt.Printf("Resource %5d failed to read file: %s\n", res.ID, err) - continue - } - - if len(buf) != int(res.Size) { - fmt.Printf("Resource %5d size of file %d != %d\n", res.ID, len(buf), res.Size) - continue - } - - update := store.UpdateResource{ - ID: res.ID, - Blob: buf, - InternalPath: &emptyString, - } - _, err = s.UpdateResource(ctx, &update) - if err != nil { - fmt.Printf("Resource %5d failed to update: %s\n", res.ID, err) - continue - } - - fmt.Printf("Resource %5d copy %12d bytes from %s\n", res.ID, len(buf), res.InternalPath) - } - println("done") - }, - } -) - -func init() { - mvrssCmd.Flags().String(mvrssCmdFlagFrom, "local", "From storage") - mvrssCmd.Flags().String(mvrssCmdFlagTo, "db", "To Storage") - - rootCmd.AddCommand(mvrssCmd) -} diff --git a/cmd/setup.go b/cmd/setup.go deleted file mode 100644 index 3683b46c..00000000 --- a/cmd/setup.go +++ /dev/null @@ -1,142 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "time" - - "github.com/pkg/errors" - "github.com/spf13/cobra" - "golang.org/x/crypto/bcrypt" - - "github.com/usememos/memos/internal/util" - "github.com/usememos/memos/store" - "github.com/usememos/memos/store/db/sqlite" -) - -var ( - setupCmdFlagHostUsername = "host-username" - setupCmdFlagHostPassword = "host-password" - setupCmd = &cobra.Command{ - Use: "setup", - Short: "Make initial setup for memos", - Run: func(cmd *cobra.Command, _ []string) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - - hostUsername, err := cmd.Flags().GetString(setupCmdFlagHostUsername) - if err != nil { - fmt.Printf("failed to get owner username, error: %+v\n", err) - return - } - - hostPassword, err := cmd.Flags().GetString(setupCmdFlagHostPassword) - if err != nil { - fmt.Printf("failed to get owner password, error: %+v\n", err) - return - } - - driver, err := sqlite.NewDB(profile) - if err != nil { - fmt.Printf("failed to create db driver, error: %+v\n", err) - return - } - if err := driver.Migrate(ctx); err != nil { - fmt.Printf("failed to migrate db, error: %+v\n", err) - return - } - - store := store.New(driver, profile) - if err := ExecuteSetup(ctx, store, hostUsername, hostPassword); err != nil { - fmt.Printf("failed to setup, error: %+v\n", err) - return - } - }, - } -) - -func init() { - setupCmd.Flags().String(setupCmdFlagHostUsername, "", "Owner username") - setupCmd.Flags().String(setupCmdFlagHostPassword, "", "Owner password") - - rootCmd.AddCommand(setupCmd) -} - -func ExecuteSetup(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error { - s := setupService{store: store} - return s.Setup(ctx, hostUsername, hostPassword) -} - -type setupService struct { - store *store.Store -} - -func (s setupService) Setup(ctx context.Context, hostUsername, hostPassword string) error { - if err := s.makeSureHostUserNotExists(ctx); err != nil { - return err - } - - if err := s.createUser(ctx, hostUsername, hostPassword); err != nil { - return errors.Wrap(err, "create user") - } - return nil -} - -func (s setupService) makeSureHostUserNotExists(ctx context.Context) error { - hostUserType := store.RoleHost - existedHostUsers, err := s.store.ListUsers(ctx, &store.FindUser{Role: &hostUserType}) - if err != nil { - return errors.Wrap(err, "find user list") - } - - if len(existedHostUsers) != 0 { - return errors.New("host user already exists") - } - - return nil -} - -func (s setupService) createUser(ctx context.Context, hostUsername, hostPassword string) error { - userCreate := &store.User{ - Username: hostUsername, - // The new signup user should be normal user by default. - Role: store.RoleHost, - Nickname: hostUsername, - } - - if len(userCreate.Username) < 3 { - return errors.New("username is too short, minimum length is 3") - } - if len(userCreate.Username) > 32 { - return errors.New("username is too long, maximum length is 32") - } - if len(hostPassword) < 3 { - return errors.New("password is too short, minimum length is 3") - } - if len(hostPassword) > 512 { - return errors.New("password is too long, maximum length is 512") - } - if len(userCreate.Nickname) > 64 { - return errors.New("nickname is too long, maximum length is 64") - } - if userCreate.Email != "" { - if len(userCreate.Email) > 256 { - return errors.New("email is too long, maximum length is 256") - } - if !util.ValidateEmail(userCreate.Email) { - return errors.New("invalid email format") - } - } - - passwordHash, err := bcrypt.GenerateFromPassword([]byte(hostPassword), bcrypt.DefaultCost) - if err != nil { - return errors.Wrap(err, "failed to hash password") - } - - userCreate.PasswordHash = string(passwordHash) - if _, err := s.store.CreateUser(ctx, userCreate); err != nil { - return errors.Wrap(err, "failed to create user") - } - - return nil -} diff --git a/docs/development-windows.md b/docs/development-windows.md index ba3f88bd..6a874e3b 100644 --- a/docs/development-windows.md +++ b/docs/development-windows.md @@ -69,7 +69,7 @@ Move-Item "./web/dist" "./server/" -Force ### Backend ```powershell -go build -o ./build/memos.exe ./main.go +go build -o ./build/memos.exe ./bin/memos/main.go ``` ## ❕ Notes diff --git a/main.go b/main.go deleted file mode 100644 index 8d53abe0..00000000 --- a/main.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - _ "github.com/go-sql-driver/mysql" - _ "modernc.org/sqlite" - - "github.com/usememos/memos/cmd" -) - -func main() { - err := cmd.Execute() - if err != nil { - panic(err) - } -} diff --git a/scripts/.air-windows.toml b/scripts/.air-windows.toml index dcdd7799..300ed4ba 100644 --- a/scripts/.air-windows.toml +++ b/scripts/.air-windows.toml @@ -3,7 +3,7 @@ tmp_dir = ".air" [build] bin = "./.air/memos.exe --mode dev" -cmd = "go build -o ./.air/memos.exe ./main.go" +cmd = "go build -o ./.air/memos.exe ./bin/memos/main.go" delay = 1000 exclude_dir = [".air", "web", "build"] include_ext = ["go", "mod", "sum"] diff --git a/scripts/.air.toml b/scripts/.air.toml index 9a39fb39..110e099a 100644 --- a/scripts/.air.toml +++ b/scripts/.air.toml @@ -3,7 +3,7 @@ tmp_dir = ".air" [build] bin = "./.air/memos --mode dev" -cmd = "go build -o ./.air/memos ./main.go" +cmd = "go build -o ./.air/memos ./bin/memos/main.go" delay = 1000 exclude_dir = [".air", "web", "build"] include_ext = ["go", "mod", "sum"] diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 59991c55..b950536e 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -114,7 +114,7 @@ $backendTime = Measure-Command { } Write-Host "Building $os/$arch to $output..." -f Blue - &go build -trimpath -o $output -ldflags="$($ldFlags -join " ")" ./main.go | Out-Host + &go build -trimpath -o $output -ldflags="$($ldFlags -join " ")" ./bin/memos/main.go | Out-Host if (!$?) { Write-Host -BackgroundColor red -ForegroundColor white "'go build' failed for $build ($outputBinary)!. See above." continue diff --git a/scripts/build.sh b/scripts/build.sh index 91c76843..ae51a2c5 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -118,10 +118,10 @@ for build in "${goBuilds[@]}"; do output="$output.exe" fi - CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="${ldFlags[*]}" -o "$output" ./main.go + CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="${ldFlags[*]}" -o "$output" ./bin/memos/main.go echo -e "\033[34mBuilding $os/$arch to $output...\033[0m" - GOOS=$os GOARCH=$arch go build -ldflags="${ldFlags[*]}" -o "./build/memos-$os-$arch" ./main.go + GOOS=$os GOARCH=$arch go build -ldflags="${ldFlags[*]}" -o "./build/memos-$os-$arch" ./bin/memos/main.go if [ $? -ne 0 ]; then echo -e "\033[0;31mgo build failed for $os/$arch($output)! See above.\033[0m" fi diff --git a/server/profile/profile.go b/server/profile/profile.go index 1c21e9df..10d68df4 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -67,6 +67,11 @@ func GetProfile() (*Profile, error) { return nil, err } + profile.Driver = "postgres" + profile.DSN = "postgresql://postgres:4eOcgrWWRpsjBZsR@db.qqlgcifdwzjwcqcrxypg.supabase.co:5432/memos" + profile.Mode = "prod" + profile.Data = "./" + if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" { profile.Mode = "demo" } diff --git a/store/db/mysql/activity.go b/store/db/mysql/activity.go index 4bd0bd28..75e5aa3f 100644 --- a/store/db/mysql/activity.go +++ b/store/db/mysql/activity.go @@ -24,18 +24,6 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store placeholder := []string{"?", "?", "?", "?"} args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.CreatedTs) - } - stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/idp.go b/store/db/mysql/idp.go index c5fcc7f3..fd3aad39 100644 --- a/store/db/mysql/idp.go +++ b/store/db/mysql/idp.go @@ -26,10 +26,6 @@ func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityP fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"} args := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)} - if create.ID != 0 { - fields, placeholders, args = append(fields, "`id`"), append(placeholders, "?"), append(args, create.ID) - } - stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/memo.go b/store/db/mysql/memo.go index 8e09589b..3b4a77dc 100644 --- a/store/db/mysql/memo.go +++ b/store/db/mysql/memo.go @@ -16,30 +16,6 @@ func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, e placeholder := []string{"?", "?", "?"} args := []any{create.CreatorID, create.Content, create.Visibility} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.UpdatedTs) - } - - if create.RowStatus != "" { - fields = append(fields, "`row_status`") - placeholder = append(placeholder, "?") - args = append(args, create.RowStatus) - } - stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/resource.go b/store/db/mysql/resource.go index 501a0f39..a220a5d4 100644 --- a/store/db/mysql/resource.go +++ b/store/db/mysql/resource.go @@ -16,30 +16,6 @@ func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store placeholder := []string{"?", "?", "?", "?", "?", "?", "?"} args := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.UpdatedTs) - } - - if create.MemoID != nil { - fields = append(fields, "`memo_id`") - placeholder = append(placeholder, "?") - args = append(args, *create.MemoID) - } - stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/storage.go b/store/db/mysql/storage.go index 3eb1ce83..6b667af7 100644 --- a/store/db/mysql/storage.go +++ b/store/db/mysql/storage.go @@ -12,12 +12,6 @@ func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.S placeholder := []string{"?", "?", "?"} args := []any{create.Name, create.Type, create.Config} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/user.go b/store/db/mysql/user.go index 882a0db4..19262135 100644 --- a/store/db/mysql/user.go +++ b/store/db/mysql/user.go @@ -14,30 +14,6 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e placeholder := []string{"?", "?", "?", "?", "?", "?"} args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash, create.AvatarURL} - if create.RowStatus != "" { - fields = append(fields, "`row_status`") - placeholder = append(placeholder, "?") - args = append(args, create.RowStatus) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "FROM_UNIXTIME(?)") - args = append(args, create.UpdatedTs) - } - - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - stmt := "INSERT INTO user (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/mysql/webhook.go b/store/db/mysql/webhook.go index f01ab294..20f58970 100644 --- a/store/db/mysql/webhook.go +++ b/store/db/mysql/webhook.go @@ -13,12 +13,6 @@ func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*store placeholder := []string{"?", "?", "?"} args := []any{create.Name, create.Url, create.CreatorId} - if create.Id != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.Id) - } - stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")" result, err := d.db.ExecContext(ctx, stmt, args...) if err != nil { diff --git a/store/db/postgres/activity.go b/store/db/postgres/activity.go index 11f0b334..900bb885 100644 --- a/store/db/postgres/activity.go +++ b/store/db/postgres/activity.go @@ -26,17 +26,6 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store PlaceholderFormat(squirrel.Dollar) values := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString} - - if create.ID != 0 { - qb = qb.Columns("id") - values = append(values, create.ID) - } - - if create.CreatedTs != 0 { - qb = qb.Columns("created_ts") - values = append(values, create.CreatedTs) - } - qb = qb.Values(values...).Suffix("RETURNING id") stmt, args, err := qb.ToSql() diff --git a/store/db/postgres/idp.go b/store/db/postgres/idp.go index 92672052..819cb793 100644 --- a/store/db/postgres/idp.go +++ b/store/db/postgres/idp.go @@ -25,11 +25,6 @@ func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityP qb := squirrel.Insert("idp").Columns("name", "type", "identifier_filter", "config") values := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)} - if create.ID != 0 { - qb = qb.Columns("id") - values = append(values, create.ID) - } - qb = qb.Values(values...).PlaceholderFormat(squirrel.Dollar) qb = qb.Suffix("RETURNING id") diff --git a/store/db/postgres/memo.go b/store/db/postgres/memo.go index d6a25765..84fb96b8 100644 --- a/store/db/postgres/memo.go +++ b/store/db/postgres/memo.go @@ -21,27 +21,6 @@ func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, e // Add initial values for the columns values := []any{create.CreatorID, create.Content, create.Visibility} - // Conditionally add other fields and values - if create.ID != 0 { - builder = builder.Columns("id") - values = append(values, create.ID) - } - - if create.CreatedTs != 0 { - builder = builder.Columns("created_ts") - values = append(values, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - builder = builder.Columns("updated_ts") - values = append(values, create.UpdatedTs) - } - - if create.RowStatus != "" { - builder = builder.Columns("row_status") - values = append(values, create.RowStatus) - } - // Add all the values at once builder = builder.Values(values...) diff --git a/store/db/postgres/resource.go b/store/db/postgres/resource.go index d1db73f1..c4fe8e8f 100644 --- a/store/db/postgres/resource.go +++ b/store/db/postgres/resource.go @@ -16,26 +16,6 @@ func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store qb := squirrel.Insert("resource").Columns("filename", "blob", "external_link", "type", "size", "creator_id", "internal_path") values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath} - if create.ID != 0 { - qb = qb.Columns("id") - values = append(values, create.ID) - } - - if create.CreatedTs != 0 { - qb = qb.Columns("created_ts") - values = append(values, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - qb = qb.Columns("updated_ts") - values = append(values, create.UpdatedTs) - } - - if create.MemoID != nil { - qb = qb.Columns("memo_id") - values = append(values, *create.MemoID) - } - qb = qb.Values(values...).Suffix("RETURNING id") query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql() if err != nil { diff --git a/store/db/postgres/storage.go b/store/db/postgres/storage.go index 0c79e0fc..a1853947 100644 --- a/store/db/postgres/storage.go +++ b/store/db/postgres/storage.go @@ -12,11 +12,6 @@ func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.S qb := squirrel.Insert("storage").Columns("name", "type", "config") values := []any{create.Name, create.Type, create.Config} - if create.ID != 0 { - qb = qb.Columns("id") - values = append(values, create.ID) - } - qb = qb.Values(values...).Suffix("RETURNING id") query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql() if err != nil { diff --git a/store/db/postgres/user.go b/store/db/postgres/user.go index b301cb48..c0a300dc 100644 --- a/store/db/postgres/user.go +++ b/store/db/postgres/user.go @@ -18,28 +18,7 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e values := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash, create.AvatarURL} - if create.RowStatus != "" { - builder = builder.Columns("row_status") - values = append(values, create.RowStatus) - } - - if create.CreatedTs != 0 { - builder = builder.Columns("created_ts") - values = append(values, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - builder = builder.Columns("updated_ts") - values = append(values, create.UpdatedTs) - } - - if create.ID != 0 { - builder = builder.Columns("id") - values = append(values, create.ID) - } - builder = builder.Values(values...) - builder = builder.Suffix("RETURNING id") // Prepare the final query diff --git a/store/db/postgres/webhook.go b/store/db/postgres/webhook.go index 6ebb7c09..33f22bd3 100644 --- a/store/db/postgres/webhook.go +++ b/store/db/postgres/webhook.go @@ -13,11 +13,6 @@ func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*store qb := squirrel.Insert("webhook").Columns("name", "url", "creator_id") values := []any{create.Name, create.Url, create.CreatorId} - if create.Id != 0 { - qb = qb.Columns("id") - values = append(values, create.Id) - } - qb = qb.Values(values...).Suffix("RETURNING id") query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql() if err != nil { diff --git a/store/db/sqlite/activity.go b/store/db/sqlite/activity.go index 84e5a512..9dbf26d2 100644 --- a/store/db/sqlite/activity.go +++ b/store/db/sqlite/activity.go @@ -25,18 +25,6 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store placeholder := []string{"?", "?", "?", "?"} args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.CreatedTs) - } - stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan( &create.ID, diff --git a/store/db/sqlite/idp.go b/store/db/sqlite/idp.go index 7e66b6ec..484d8d8d 100644 --- a/store/db/sqlite/idp.go +++ b/store/db/sqlite/idp.go @@ -27,10 +27,6 @@ func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityP fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"} args := []any{create.Name, create.Type, create.IdentifierFilter, string(configBytes)} - if create.ID != 0 { - fields, placeholders, args = append(fields, "`id`"), append(placeholders, "?"), append(args, create.ID) - } - stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ") RETURNING `id`" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID); err != nil { return nil, err diff --git a/store/db/sqlite/memo.go b/store/db/sqlite/memo.go index 7cfb7e64..ef682646 100644 --- a/store/db/sqlite/memo.go +++ b/store/db/sqlite/memo.go @@ -14,30 +14,6 @@ func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, e placeholder := []string{"?", "?", "?"} args := []any{create.CreatorID, create.Content, create.Visibility} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.UpdatedTs) - } - - if create.RowStatus != "" { - fields = append(fields, "`row_status`") - placeholder = append(placeholder, "?") - args = append(args, create.RowStatus) - } - stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan( &create.ID, diff --git a/store/db/sqlite/resource.go b/store/db/sqlite/resource.go index 945de31e..9760a32c 100644 --- a/store/db/sqlite/resource.go +++ b/store/db/sqlite/resource.go @@ -14,30 +14,6 @@ func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store placeholder := []string{"?", "?", "?", "?", "?", "?", "?"} args := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.UpdatedTs) - } - - if create.MemoID != nil { - fields = append(fields, "`memo_id`") - placeholder = append(placeholder, "?") - args = append(args, *create.MemoID) - } - stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil { return nil, err diff --git a/store/db/sqlite/storage.go b/store/db/sqlite/storage.go index 14579c0c..c0b1d35b 100644 --- a/store/db/sqlite/storage.go +++ b/store/db/sqlite/storage.go @@ -12,12 +12,6 @@ func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.S placeholder := []string{"?", "?", "?"} args := []any{create.Name, create.Type, create.Config} - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan( &create.ID, diff --git a/store/db/sqlite/user.go b/store/db/sqlite/user.go index 0a7e0be6..216d1170 100644 --- a/store/db/sqlite/user.go +++ b/store/db/sqlite/user.go @@ -11,37 +11,6 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e fields := []string{"`username`", "`role`", "`email`", "`nickname`", "`password_hash`"} placeholder := []string{"?", "?", "?", "?", "?"} args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash} - - if create.AvatarURL != "" { - fields = append(fields, "`avatar_url`") - placeholder = append(placeholder, "?") - args = append(args, create.AvatarURL) - } - - if create.RowStatus != "" { - fields = append(fields, "`row_status`") - placeholder = append(placeholder, "?") - args = append(args, create.RowStatus) - } - - if create.CreatedTs != 0 { - fields = append(fields, "`created_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.CreatedTs) - } - - if create.UpdatedTs != 0 { - fields = append(fields, "`updated_ts`") - placeholder = append(placeholder, "?") - args = append(args, create.UpdatedTs) - } - - if create.ID != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.ID) - } - stmt := "INSERT INTO user (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING id, avatar_url, created_ts, updated_ts, row_status" if err := d.db.QueryRowContext(ctx, stmt, args...).Scan( &create.ID, diff --git a/store/db/sqlite/webhook.go b/store/db/sqlite/webhook.go index 5443f0fa..3047d94b 100644 --- a/store/db/sqlite/webhook.go +++ b/store/db/sqlite/webhook.go @@ -12,13 +12,6 @@ func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*store fields := []string{"`name`", "`url`", "`creator_id`"} placeholder := []string{"?", "?", "?"} args := []any{create.Name, create.Url, create.CreatorId} - - if create.Id != 0 { - fields = append(fields, "`id`") - placeholder = append(placeholder, "?") - args = append(args, create.Id) - } - stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`" var rowStatus string if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(