chore: clean binary entries

This commit is contained in:
Steven 2023-12-11 22:16:39 +08:00
parent 52f399a154
commit c608877c3e
33 changed files with 20 additions and 954 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

15
main.go
View File

@ -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)
}
}

View File

@ -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"]

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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"
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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()

View File

@ -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")

View File

@ -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...)

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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 {

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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(