From 9f7263abd551823189df8db644a1d5d65a729566 Mon Sep 17 00:00:00 2001 From: Johan Siebens Date: Tue, 27 Dec 2022 11:14:53 +0100 Subject: [PATCH] fix: add machines indeces and use gorm take instead of first --- .../m202212270800_machine_indeces.go | 32 +++++++++++++++++++ internal/database/migration/migrations.go | 1 + internal/domain/api_key.go | 2 +- internal/domain/auth_key.go | 2 +- internal/domain/authentication_request.go | 2 +- internal/domain/machine.go | 8 ++--- internal/domain/registration_request.go | 4 +-- internal/domain/ssh_action_request.go | 2 +- internal/domain/system_api_key.go | 2 +- internal/domain/user.go | 2 +- 10 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 internal/database/migration/m202212270800_machine_indeces.go diff --git a/internal/database/migration/m202212270800_machine_indeces.go b/internal/database/migration/m202212270800_machine_indeces.go new file mode 100644 index 0000000..f02b627 --- /dev/null +++ b/internal/database/migration/m202212270800_machine_indeces.go @@ -0,0 +1,32 @@ +package migration + +import ( + "github.com/go-gormigrate/gormigrate/v2" + "github.com/jsiebens/ionscale/internal/domain" + "gorm.io/gorm" +) + +func m202212270800_machine_indeces() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "202212270800", + Migrate: func(db *gorm.DB) error { + type Machine struct { + ID uint64 `gorm:"primaryKey;autoIncrement:false;index:idx_tailnet_id_id,priority:2"` + MachineKey string `gorm:"index:idx_machine_keys"` + NodeKey string `gorm:"index:idx_machine_keys"` + + Name string `gorm:"index:idx_tailnet_id_name,priority:2"` + NameIdx uint64 `gorm:"index:idx_tailnet_id_name,sort:desc,priority:3"` + + TailnetID uint64 `gorm:"index:idx_tailnet_id_id,priority:1;index:idx_tailnet_id_name,priority:1"` + + IPv4 domain.IP `gorm:"index:idx_ipv4"` + } + + return db.AutoMigrate( + &Machine{}, + ) + }, + Rollback: nil, + } +} diff --git a/internal/database/migration/migrations.go b/internal/database/migration/migrations.go index 58fa42c..6f6f3dd 100644 --- a/internal/database/migration/migrations.go +++ b/internal/database/migration/migrations.go @@ -15,6 +15,7 @@ func Migrations() []*gormigrate.Migration { m202210080700_ssh_action_request(), m202211031100_add_authorized_column(), m202212201300_add_user_id_column(), + m202212270800_machine_indeces(), } return migrations } diff --git a/internal/domain/api_key.go b/internal/domain/api_key.go index 26e70b0..7c7ecb2 100644 --- a/internal/domain/api_key.go +++ b/internal/domain/api_key.go @@ -65,7 +65,7 @@ func (r *repository) LoadApiKey(ctx context.Context, key string) (*ApiKey, error } var m ApiKey - tx := r.withContext(ctx).Preload("User").Preload("Tailnet").First(&m, "key = ?", split[0]) + tx := r.withContext(ctx).Preload("User").Preload("Tailnet").Take(&m, "key = ?", split[0]) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/auth_key.go b/internal/domain/auth_key.go index 428bd9c..827527f 100644 --- a/internal/domain/auth_key.go +++ b/internal/domain/auth_key.go @@ -136,7 +136,7 @@ func (r *repository) LoadAuthKey(ctx context.Context, key string) (*AuthKey, err } var m AuthKey - tx := r.withContext(ctx).Preload("User").Preload("Tailnet").First(&m, "key = ?", split[0]) + tx := r.withContext(ctx).Preload("User").Preload("Tailnet").Take(&m, "key = ?", split[0]) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/authentication_request.go b/internal/domain/authentication_request.go index 41b3946..9c973f8 100644 --- a/internal/domain/authentication_request.go +++ b/internal/domain/authentication_request.go @@ -27,7 +27,7 @@ func (r *repository) SaveAuthenticationRequest(ctx context.Context, session *Aut func (r *repository) GetAuthenticationRequest(ctx context.Context, key string) (*AuthenticationRequest, error) { var m AuthenticationRequest - tx := r.withContext(ctx).First(&m, "key = ?", key) + tx := r.withContext(ctx).Take(&m, "key = ?", key) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/machine.go b/internal/domain/machine.go index 3557850..7b69e1f 100644 --- a/internal/domain/machine.go +++ b/internal/domain/machine.go @@ -357,7 +357,7 @@ func (r *repository) DeleteMachine(ctx context.Context, id uint64) (bool, error) func (r *repository) GetMachine(ctx context.Context, machineID uint64) (*Machine, error) { var m Machine - tx := r.withContext(ctx).Preload("Tailnet").Preload("User").First(&m, "id = ?", machineID) + tx := r.withContext(ctx).Preload("Tailnet").Preload("User").Take(&m, machineID) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil @@ -376,7 +376,7 @@ func (r *repository) GetNextMachineNameIndex(ctx context.Context, tailnetID uint tx := r.withContext(ctx). Where("name = ? AND tailnet_id = ?", name, tailnetID). Order("name_idx desc"). - First(&m) + Take(&m) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return 0, nil @@ -391,7 +391,7 @@ func (r *repository) GetNextMachineNameIndex(ctx context.Context, tailnetID uint func (r *repository) GetMachineByKey(ctx context.Context, tailnetID uint64, machineKey string) (*Machine, error) { var m Machine - tx := r.withContext(ctx).Preload("Tailnet").Preload("User").First(&m, "tailnet_id = ? AND machine_key = ?", tailnetID, machineKey) + tx := r.withContext(ctx).Preload("Tailnet").Preload("User").Take(&m, "tailnet_id = ? AND machine_key = ?", tailnetID, machineKey) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil @@ -406,7 +406,7 @@ func (r *repository) GetMachineByKey(ctx context.Context, tailnetID uint64, mach func (r *repository) GetMachineByKeys(ctx context.Context, machineKey string, nodeKey string) (*Machine, error) { var m Machine - tx := r.withContext(ctx).Preload("Tailnet").Preload("User").First(&m, "machine_key = ? AND node_key = ?", machineKey, nodeKey) + tx := r.withContext(ctx).Preload("Tailnet").Preload("User").Take(&m, "machine_key = ? AND node_key = ?", machineKey, nodeKey) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/registration_request.go b/internal/domain/registration_request.go index cd87396..56ec1af 100644 --- a/internal/domain/registration_request.go +++ b/internal/domain/registration_request.go @@ -68,7 +68,7 @@ func (r *repository) SaveRegistrationRequest(ctx context.Context, request *Regis func (r *repository) GetRegistrationRequestByKey(ctx context.Context, key string) (*RegistrationRequest, error) { var m RegistrationRequest - tx := r.withContext(ctx).First(&m, "key = ?", key) + tx := r.withContext(ctx).Take(&m, "key = ?", key) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil @@ -83,7 +83,7 @@ func (r *repository) GetRegistrationRequestByKey(ctx context.Context, key string func (r *repository) GetRegistrationRequestByMachineKey(ctx context.Context, key string) (*RegistrationRequest, error) { var m RegistrationRequest - tx := r.withContext(ctx).First(&m, "machine_key = ?", key) + tx := r.withContext(ctx).Take(&m, "machine_key = ?", key) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/ssh_action_request.go b/internal/domain/ssh_action_request.go index 7b2e0c5..737831a 100644 --- a/internal/domain/ssh_action_request.go +++ b/internal/domain/ssh_action_request.go @@ -27,7 +27,7 @@ func (r *repository) SaveSSHActionRequest(ctx context.Context, session *SSHActio func (r *repository) GetSSHActionRequest(ctx context.Context, key string) (*SSHActionRequest, error) { var m SSHActionRequest - tx := r.withContext(ctx).First(&m, "key = ?", key) + tx := r.withContext(ctx).Take(&m, "key = ?", key) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/system_api_key.go b/internal/domain/system_api_key.go index bd10caf..7e216df 100644 --- a/internal/domain/system_api_key.go +++ b/internal/domain/system_api_key.go @@ -69,7 +69,7 @@ func (r *repository) LoadSystemApiKey(ctx context.Context, token string) (*Syste } var m SystemApiKey - tx := r.withContext(ctx).Preload("Account").First(&m, "key = ?", key) + tx := r.withContext(ctx).Preload("Account").Take(&m, "key = ?", key) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil diff --git a/internal/domain/user.go b/internal/domain/user.go index e44af28..42d59e2 100644 --- a/internal/domain/user.go +++ b/internal/domain/user.go @@ -100,7 +100,7 @@ func (r *repository) GetOrCreateUserWithAccount(ctx context.Context, tailnet *Ta func (r *repository) GetUser(ctx context.Context, userID uint64) (*User, error) { var m User - tx := r.withContext(ctx).Preload("Tailnet").Preload("Account").First(&m, "id = ?", userID) + tx := r.withContext(ctx).Preload("Tailnet").Preload("Account").Take(&m, "id = ?", userID) if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, nil