-
Notifications
You must be signed in to change notification settings - Fork 86
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
feat: optimize sqlserver test #652
Changes from 7 commits
db46df2
ce94162
183f762
c660a9e
08c3da6
fd804e7
8d7df98
ff1e323
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,11 @@ | |
username: username, | ||
password: password, | ||
image: &testing.Image{ | ||
Repository: "mcmoe/mssqldocker", | ||
Repository: "mcr.microsoft.com/mssql/server", | ||
Tag: "latest", | ||
Env: []string{ | ||
"ACCEPT_EULA=Y", | ||
"MSSQL_DB=" + database, | ||
"MSSQL_USER=" + username, | ||
"MSSQL_PASSWORD=" + password, | ||
"SA_PASSWORD=" + password, | ||
"MSSQL_SA_PASSWORD=" + password, | ||
}, | ||
ExposedPorts: []string{"1433"}, | ||
}, | ||
|
@@ -122,13 +119,47 @@ | |
) | ||
|
||
// docker compose need time to start | ||
for i := 0; i < 60; i++ { | ||
for i := 0; i < 100; i++ { | ||
instance, err = gormio.Open(sqlserver.New(sqlserver.Config{ | ||
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", | ||
receiver.username, receiver.password, receiver.host, receiver.port, receiver.database), | ||
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=master", | ||
"sa", receiver.password, receiver.host, receiver.port), | ||
})) | ||
|
||
if err == nil { | ||
// Check if database exists | ||
var exists bool | ||
query := fmt.Sprintf("SELECT CASE WHEN EXISTS (SELECT * FROM sys.databases WHERE name = '%s') THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END", receiver.database) | ||
if err := instance.Raw(query).Scan(&exists).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
if !exists { | ||
// Create User database | ||
if err := instance.Exec(fmt.Sprintf("CREATE DATABASE %s", receiver.database)).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create User account | ||
if err := instance.Exec(fmt.Sprintf("CREATE LOGIN %s WITH PASSWORD = '%s'", receiver.username, receiver.password)).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create DB account for User | ||
if err := instance.Exec(fmt.Sprintf("USE %s; CREATE USER %s FOR LOGIN %s", receiver.database, receiver.username, receiver.username)).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
// Add permission | ||
if err := instance.Exec(fmt.Sprintf("USE %s; ALTER ROLE db_owner ADD MEMBER %s", receiver.database, receiver.username)).Error; err != nil { | ||
return nil, err | ||
} | ||
} | ||
Comment on lines
+132
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize database and user creation queries. Currently, the code executes multiple SQL statements separately to set up the database and user. This can be optimized by combining statements and using transactions to ensure atomicity. You can refactor the SQL commands into a single script: script := fmt.Sprintf(`
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'%s')
BEGIN
CREATE DATABASE [%s];
END;
USE [%s];
IF NOT EXISTS (SELECT * FROM sys.sql_logins WHERE name = N'%s')
BEGIN
CREATE LOGIN [%s] WITH PASSWORD = N'%s';
END;
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'%s')
BEGIN
CREATE USER [%s] FOR LOGIN [%s];
ALTER ROLE db_owner ADD MEMBER [%s];
END;
`, receiver.database, receiver.database, receiver.database, receiver.username, receiver.username, receiver.password, receiver.username, receiver.username, receiver.username, receiver.username)
if err := instance.Exec(script).Error; err != nil {
return nil, fmt.Errorf("failed to set up database and user: %v", err)
} This approach reduces the number of round trips to the database and ensures that the setup is only performed if necessary. |
||
|
||
instance, err = gormio.Open(sqlserver.New(sqlserver.Config{ | ||
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", | ||
receiver.username, receiver.password, receiver.host, receiver.port, receiver.database), | ||
})) | ||
|
||
Comment on lines
+122
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance error handling in the connection logic. The connection logic has been extended to include database and user creation. However, if any of these steps fail, the error handling could be improved to provide more detailed information and ensure resources are cleaned up appropriately. Consider adding more descriptive error messages and handling partial failures. For example: // After each operation, check for errors and provide context
if err := instance.Exec(fmt.Sprintf("CREATE DATABASE %s", receiver.database)).Error; err != nil {
return nil, fmt.Errorf("failed to create database '%s': %v", receiver.database, err)
}
// Similarly for other operations |
||
break | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
Update approved, but action required for test files
The password change in
support/docker/docker.go
has been successfully implemented. However, there are a few important points to address:The old password "Goravel123" is still present in
testing/docker/database_test.go
. Please update these test cases to use either the new password or a different test password to avoid confusion and potential test failures.The
password
constant is used in various parts of the application, particularly in database-related code. Ensure that all components using this constant are still functioning correctly after the change.If there's any documentation referencing the old password, make sure to update it accordingly.
Action items:
testing/docker/database_test.go
password
constantAnalysis chain
Verify the password change across the codebase.
The password change looks good. However, please ensure that this new password is updated in all relevant places, such as:
Also, communicate this change to the team to avoid any disruptions.
Run the following script to verify the usage of the
password
constant:Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 370
Script:
Length of output: 5396