Skip to content

Commit

Permalink
Merge branch 'master' into feat/ctx-key-to-any
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanialr authored Sep 12, 2024
2 parents ab90881 + 9996f02 commit a6b6dbf
Show file tree
Hide file tree
Showing 53 changed files with 985 additions and 429 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Benchmark
on:
push:
branches:
- master
paths-ignore:
- "**/*.md"
pull_request:
paths-ignore:
- "**/*.md"
permissions:
contents: write
pull-requests: write
jobs:
benchmark:
uses: goravel/.github/.github/workflows/benchmark.yml@master
secrets: inherit
8 changes: 8 additions & 0 deletions .github/workflows/govulncheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Govulncheck
on:
schedule:
- cron: "0 */6 * * *" # Run every 6 hours
jobs:
govulncheck:
uses: goravel/.github/.github/workflows/govulncheck.yml@master
secrets: inherit
3 changes: 3 additions & 0 deletions .github/workflows/mockery.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Mockery
on:
pull_request:
permissions:
contents: write
pull-requests: write
jobs:
mockery:
runs-on: ubuntu-latest
Expand Down
10 changes: 4 additions & 6 deletions contracts/database/orm/constants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package orm

const (
DriverMysql Driver = "mysql"
// DriverPostgresql DEPRECATED, use DriverPostgres instead.
DriverPostgresql Driver = "postgresql"
DriverPostgres Driver = "postgres"
DriverSqlite Driver = "sqlite"
DriverSqlserver Driver = "sqlserver"
DriverMysql Driver = "mysql"
DriverPostgres Driver = "postgres"
DriverSqlite Driver = "sqlite"
DriverSqlserver Driver = "sqlserver"
)

type Driver string
Expand Down
4 changes: 3 additions & 1 deletion contracts/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ type Manager interface {
// Driver retrieves the session driver by name.
Driver(name ...string) (Driver, error)
// Extend extends the session manager with a custom driver.
Extend(driver string, handler func() Driver) Manager
Extend(driver string, handler func() Driver) error
// ReleaseSession releases the session back to the pool.
ReleaseSession(session Session)
}
32 changes: 32 additions & 0 deletions crypt/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,35 @@ func (s *AesTestSuite) TestDecryptString() {
_, err = s.aes.DecryptString("eyJpdiI6IjEyMzQ1IiwidmFsdWUiOiIxMjM0NSJ9")
s.Error(err)
}

func Benchmark_EncryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aes.EncryptString("Goravel")
if err != nil {
b.Error(err)
}
}
}

func Benchmark_DecryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
payload, err := aes.EncryptString("Goravel")
if err != nil {
b.Error(err)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := aes.DecryptString(payload)
if err != nil {
b.Error(err)
}
}
}
10 changes: 5 additions & 5 deletions database/console/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
}

return migrate.NewWithDatabaseInstance(dir, "mysql", instance)
case orm.DriverPostgresql:
case orm.DriverPostgres:
dsn := db.NewDsnImpl(config, connection)
postgresqlDsn := dsn.Postgresql(writeConfigs[0])
if postgresqlDsn == "" {
postgresDsn := dsn.Postgres(writeConfigs[0])
if postgresDsn == "" {
return nil, nil
}

db, err := sql.Open("postgres", postgresqlDsn)
db, err := sql.Open("postgres", postgresDsn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,6 +114,6 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {

return migrate.NewWithDatabaseInstance(dir, "sqlserver", instance)
default:
return nil, errors.New("database driver only support mysql, postgresql, sqlite and sqlserver")
return nil, errors.New("database driver only support mysql, postgres, sqlite and sqlserver")
}
}
6 changes: 3 additions & 3 deletions database/console/migrate_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func TestMigrateCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down Expand Up @@ -120,7 +120,7 @@ INSERT INTO agents (name, created_at, updated_at) VALUES ('goravel', '2023-03-11
`)
}

func createPostgresqlMigrations() {
func createPostgresMigrations() {
_ = file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
`CREATE TABLE agents (
id SERIAL PRIMARY KEY NOT NULL,
Expand Down
6 changes: 3 additions & 3 deletions database/console/migrate_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (receiver *MigrateCreator) getStub(table string, create bool) (string, stri

driver := receiver.config.GetString("database.connections." + receiver.config.GetString("database.default") + ".driver")
switch orm.Driver(driver) {
case orm.DriverPostgresql:
case orm.DriverPostgres:
if create {
return migration.PostgresqlStubs{}.CreateUp(), migration.PostgresqlStubs{}.CreateDown()
return migration.PostgresStubs{}.CreateUp(), migration.PostgresStubs{}.CreateDown()
}

return migration.PostgresqlStubs{}.UpdateUp(), migration.PostgresqlStubs{}.UpdateDown()
return migration.PostgresStubs{}.UpdateUp(), migration.PostgresStubs{}.UpdateDown()
case orm.DriverSqlite:
if create {
return migration.SqliteStubs{}.CreateUp(), migration.SqliteStubs{}.CreateDown()
Expand Down
4 changes: 2 additions & 2 deletions database/console/migrate_fresh_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func TestMigrateFreshCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions database/console/migrate_refresh_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func TestMigrateRefreshCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions database/console/migrate_reset_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestMigrateResetCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions database/console/migrate_rollback_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestMigrateRollbackCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions database/console/migrate_status_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestMigrateStatusCommand(t *testing.T) {
},
},
{
name: "postgresql",
name: "postgres",
setup: func() {
var err error
docker := gorm.NewPostgresDocker(docker.Postgres())
query, err = docker.New()
assert.Nil(t, err)
mockConfig = docker.MockConfig
createPostgresqlMigrations()
createPostgresMigrations()
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions database/db/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Dsn interface {
Mysql(config databasecontract.Config) string
Postgresql(config databasecontract.Config) string
Postgres(config databasecontract.Config) string
Sqlite(config databasecontract.Config) string
Sqlserver(config databasecontract.Config) string
}
Expand Down Expand Up @@ -39,7 +39,7 @@ func (d *DsnImpl) Mysql(config databasecontract.Config) string {
config.Username, config.Password, host, config.Port, config.Database, charset, true, loc)
}

func (d *DsnImpl) Postgresql(config databasecontract.Config) string {
func (d *DsnImpl) Postgres(config databasecontract.Config) string {
host := config.Host
if host == "" {
return ""
Expand Down
6 changes: 3 additions & 3 deletions database/db/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ func (s *DsnTestSuite) TestMysql() {
testUsername, testPassword, testHost, testPort, testDatabase, charset, true, loc), dsn.Mysql(testConfig))
}

func (s *DsnTestSuite) TestPostgresql() {
connection := orm.DriverPostgresql.String()
func (s *DsnTestSuite) TestPostgres() {
connection := orm.DriverPostgres.String()
dsn := NewDsnImpl(s.mockConfig, connection)
sslmode := "disable"
timezone := "UTC"
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.sslmode", connection)).Return(sslmode).Once()
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.timezone", connection)).Return(timezone).Once()

s.Equal(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
testUsername, testPassword, testHost, testPort, testDatabase, sslmode, timezone), dsn.Postgresql(testConfig))
testUsername, testPassword, testHost, testPort, testDatabase, sslmode, timezone), dsn.Postgres(testConfig))
}

func (s *DsnTestSuite) TestSqlite() {
Expand Down
10 changes: 5 additions & 5 deletions database/gorm/dialector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (d *DialectorImpl) Make(configs []databasecontract.Config) ([]gorm.Dialecto
switch orm.Driver(driver) {
case orm.DriverMysql:
dialector = d.mysql(item)
case orm.DriverPostgresql:
dialector = d.postgresql(item)
case orm.DriverPostgres:
dialector = d.postgres(item)
case orm.DriverSqlite:
dialector = d.sqlite(item)
case orm.DriverSqlserver:
dialector = d.sqlserver(item)
default:
err = fmt.Errorf("err database driver: %s, only support mysql, postgresql, sqlite and sqlserver", driver)
err = fmt.Errorf("err database driver: %s, only support mysql, postgres, sqlite and sqlserver", driver)
}

if err != nil {
Expand All @@ -78,8 +78,8 @@ func (d *DialectorImpl) mysql(config databasecontract.Config) gorm.Dialector {
})
}

func (d *DialectorImpl) postgresql(config databasecontract.Config) gorm.Dialector {
dsn := d.dsn.Postgresql(config)
func (d *DialectorImpl) postgres(config databasecontract.Config) gorm.Dialector {
dsn := d.dsn.Postgres(config)
if dsn == "" {
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func (s *DialectorTestSuite) TestMysql() {
s.Nil(err)
}

func (s *DialectorTestSuite) TestPostgresql() {
dialector := NewDialectorImpl(s.mockConfig, orm.DriverPostgresql.String())
s.mockConfig.On("GetString", "database.connections.postgresql.driver").
Return(orm.DriverPostgresql.String()).Once()
s.mockConfig.On("GetString", "database.connections.postgresql.sslmode").
func (s *DialectorTestSuite) TestPostgres() {
dialector := NewDialectorImpl(s.mockConfig, orm.DriverPostgres.String())
s.mockConfig.On("GetString", "database.connections.postgres.driver").
Return(orm.DriverPostgres.String()).Once()
s.mockConfig.On("GetString", "database.connections.postgres.sslmode").
Return("disable").Once()
s.mockConfig.On("GetString", "database.connections.postgresql.timezone").
s.mockConfig.On("GetString", "database.connections.postgres.timezone").
Return("UTC").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Equal(postgres.New(postgres.Config{
Expand Down
8 changes: 4 additions & 4 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,10 @@ func (r *QueryImpl) buildLockForUpdate(db *gormio.DB) *gormio.DB {

driver := r.instance.Name()
mysqlDialector := mysql.Dialector{}
postgresqlDialector := postgres.Dialector{}
postgresDialector := postgres.Dialector{}
sqlserverDialector := sqlserver.Dialector{}

if driver == mysqlDialector.Name() || driver == postgresqlDialector.Name() {
if driver == mysqlDialector.Name() || driver == postgresDialector.Name() {
return db.Clauses(clause.Locking{Strength: "UPDATE"})
} else if driver == sqlserverDialector.Name() {
return db.Clauses(hints.With("rowlock", "updlock", "holdlock"))
Expand Down Expand Up @@ -1041,10 +1041,10 @@ func (r *QueryImpl) buildSharedLock(db *gormio.DB) *gormio.DB {

driver := r.instance.Name()
mysqlDialector := mysql.Dialector{}
postgresqlDialector := postgres.Dialector{}
postgresDialector := postgres.Dialector{}
sqlserverDialector := sqlserver.Dialector{}

if driver == mysqlDialector.Name() || driver == postgresqlDialector.Name() {
if driver == mysqlDialector.Name() || driver == postgresDialector.Name() {
return db.Clauses(clause.Locking{Strength: "SHARE"})
} else if driver == sqlserverDialector.Name() {
return db.Clauses(hints.With("rowlock", "holdlock"))
Expand Down
Loading

0 comments on commit a6b6dbf

Please sign in to comment.