Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] Refactor group to use store methods #2867

Merged
merged 27 commits into from
Nov 15, 2024

Conversation

bcmmbaga
Copy link
Contributor

@bcmmbaga bcmmbaga commented Nov 8, 2024

Describe your changes

Remove calls to get and save whole account in group operations.

Issue ticket number and link

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

@ismail0234
Copy link
Contributor

@bcmmbaga When you use ExecuteInTransaction, storeEngine always returns empty value.

@bcmmbaga
Copy link
Contributor Author

bcmmbaga commented Nov 8, 2024

@bcmmbaga When you use ExecuteInTransaction, storeEngine always returns empty value.

I didn’t quite understand this. Are you getting an empty value when calling transaction.(methodName), or is it happening in a different case?

@ismail0234
Copy link
Contributor

@bcmmbaga When you use ExecuteInTransaction, storeEngine always returns empty value.

I didn’t quite understand this. Are you getting an empty value when calling transaction.(methodName), or is it happening in a different case?

For example, if you call “s.storeEngine” in this method, it returns an empty value. Because this method is called from a transaction.

func (s *SqlStore) GetSetupKeyBySecret(ctx context.Context, lockStrength LockingStrength, key string) (*SetupKey, error) {
var setupKey SetupKey
result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).
First(&setupKey, keyQueryCondition, key)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, status.Errorf(status.NotFound, "setup key not found")
}
return nil, status.NewSetupKeyNotFoundError(result.Error)
}
return &setupKey, nil

…nt-refactoring

# Conflicts:
#	management/server/sql_store.go
@ismail0234
Copy link
Contributor

This can cause many problems in the future. When you try to access this value in the methods called by the transaction, it will return empty.

@bcmmbaga
Copy link
Contributor Author

bcmmbaga commented Nov 8, 2024

This can cause many problems in the future. When you try to access this value in the methods called by the transaction, it will return empty.

You're right. However, the Store methods should be generic across all databases and shouldn't include unique implementations for each supported database. Therefore, we shouldn't reference the specific engine in the Store implementation. There's currently a TODO in GetGroupByName that will be removed in this PR

@ismail0234
Copy link
Contributor

ismail0234 commented Nov 8, 2024

This can cause many problems in the future. When you try to access this value in the methods called by the transaction, it will return empty.

You're right. However, the Store methods should be generic across all databases and shouldn't include unique implementations for each supported database. Therefore, we shouldn't reference the specific engine in the Store implementation. There's currently a TODO in GetGroupByName that will be removed in this PR

This is needed for MySQL Integration. Because key is a reserved word for MySQL. You cannot use it in queries without enclosing it in double quotes. But when you use it with double quotes, an error occurs because Sqlite and Postgres queries do not support it. In this case, we should check for Mysql and Postgres and use the correct one.

https://doc.ispirer.com/sqlways/Output/SQLWays-1-205.html

The “queryCondition” value must take the correct value according to the current engine.


 func (s *SqlStore) GetSetupKeyBySecret(ctx context.Context, lockStrength LockingStrength, key string) (*SetupKey, error) { 

 	var queryCondition := "key"

	if s.storeEngine == MysqlStoreEngine {
		queryCondition = "`key`";
	}

 	var setupKey SetupKey 
 	result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). 
 		First(&setupKey, queryCondition, key) 
 	if result.Error != nil { 
 		if errors.Is(result.Error, gorm.ErrRecordNotFound) { 
 			return nil, status.Errorf(status.NotFound, "setup key not found") 
 		} 
 		return nil, status.NewSetupKeyNotFoundError(result.Error) 
 	} 
 	return &setupKey, nil 
}

@bcmmbaga
Copy link
Contributor Author

bcmmbaga commented Nov 8, 2024

@ismail0234 In that case, you can pass the store engine to this initialization, allowing the transaction to be aware of the current engine in use

func (s *SqlStore) withTx(tx *gorm.DB) Store {
return &SqlStore{
db: tx,
}
}

func (s *SqlStore) withTx(tx *gorm.DB) Store {
	return &SqlStore{
		db:          tx,
		storeEngine: s.storeEngine,
	}
}

@ismail0234
Copy link
Contributor

@ismail0234 In that case, you can pass the store engine to this initialization, allowing the transaction to be aware of the current engine in use

func (s *SqlStore) withTx(tx *gorm.DB) Store {
return &SqlStore{
db: tx,
}
}

func (s *SqlStore) withTx(tx *gorm.DB) Store {
	return &SqlStore{
		db:          tx,
		storeEngine: s.storeEngine,
	}
}

Thanks. Will you add this?

I am currently querying the database name to overcome this problem.

func GetKeyQueryCondition(s *SqlStore) string {

	if s.storeEngine == MysqlStoreEngine {
		return mysqlKeyQueryCondition
	}

	if s.storeEngine == "" && s.db.Name() == "mysql" {
		return mysqlKeyQueryCondition
	}

	return keyQueryCondition
}

@bcmmbaga
Copy link
Contributor Author

bcmmbaga commented Nov 8, 2024

Thanks. Will you add this?

Yes. I’ll add that.

@bcmmbaga bcmmbaga changed the base branch from feature/get-account-refactoring to main November 8, 2024 22:56
# Conflicts:
#	management/server/group.go
#	management/server/group/group.go
#	management/server/setupkey.go
#	management/server/sql_store.go
#	management/server/status/error.go
#	management/server/store.go
Signed-off-by: bcmmbaga <[email protected]>
Signed-off-by: bcmmbaga <[email protected]>
Signed-off-by: bcmmbaga <[email protected]>
# Conflicts:
#	management/server/account.go
#	management/server/status/error.go
management/server/group.go Show resolved Hide resolved
management/server/group.go Outdated Show resolved Hide resolved
management/server/group.go Show resolved Hide resolved
management/server/group.go Outdated Show resolved Hide resolved
management/server/group.go Show resolved Hide resolved
management/server/sql_store.go Show resolved Hide resolved
management/server/sql_store.go Show resolved Hide resolved
management/server/sql_store.go Show resolved Hide resolved
management/server/sql_store.go Show resolved Hide resolved
management/server/sql_store.go Outdated Show resolved Hide resolved
pascal-fischer
pascal-fischer previously approved these changes Nov 13, 2024
# Conflicts:
#	management/server/group.go
Copy link

sonarcloud bot commented Nov 15, 2024

@bcmmbaga bcmmbaga merged commit 12f4424 into main Nov 15, 2024
21 checks passed
@bcmmbaga bcmmbaga deleted the groups-get-account-refactoring branch November 15, 2024 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants