Debug + Mysql JSON Query

This commit is contained in:
İsmail
2024-11-05 23:01:09 +03:00
parent faff029843
commit 094dc2e69b
3 changed files with 16 additions and 3 deletions

View File

@@ -1383,6 +1383,8 @@ func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStren
query := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(lockStrength)}).Preload(clause.Associations)
if s.storeEngine == PostgresStoreEngine {
query = query.Order("json_array_length(peers::json) DESC")
} else if s.storeEngine == MysqlStoreEngine {
query = query.Order("JSON_LENGTH(JSON_EXTRACT(peers, \"$\")) DESC")
} else {
query = query.Order("json_array_length(peers) DESC")
}

View File

@@ -280,6 +280,8 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (
}
}
log.Printf("[DEBUG] Kind Type: %s", kind)
store, err := NewSqlStore(ctx, db, SqliteStoreEngine, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to create test store: %v", err)
@@ -290,6 +292,8 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (
func getSqlStoreEngine(ctx context.Context, store *SqlStore, kind StoreEngine) (Store, func(), error) {
log.Printf("[DEBUG] Kind Type - 2: %s", kind)
if kind == PostgresStoreEngine {
cleanUp, err := testutil.CreatePGDB()
if err != nil {
@@ -301,6 +305,8 @@ func getSqlStoreEngine(ctx context.Context, store *SqlStore, kind StoreEngine) (
return nil, nil, fmt.Errorf("%s is not set", postgresDsnEnv)
}
log.Printf("[DEBUG] PostgresStoreEngine DSN: %s", dsn)
store, err = NewPostgresqlStoreFromSqlStore(ctx, store, dsn, nil)
if err != nil {
return nil, nil, err
@@ -316,13 +322,12 @@ func getSqlStoreEngine(ctx context.Context, store *SqlStore, kind StoreEngine) (
}
dsn, ok := os.LookupEnv(mysqlDsnEnv)
log.WithContext(ctx).Warnf("MysqlStoreEngine DSN: %s", mysqlDsnEnv)
if !ok {
return nil, nil, fmt.Errorf("%s is not set", mysqlDsnEnv)
}
log.Printf("[DEBUG] MysqlStoreEngine DSN: %s", dsn)
store, err = NewMysqlStoreFromSqlStore(ctx, store, dsn, nil)
if err != nil {
return nil, nil, err

View File

@@ -16,6 +16,9 @@ import (
)
func CreatePGDB() (func(), error) {
log.Printf("[DEBUG] CreatePGDB")
ctx := context.Background()
c, err := postgres.Run(ctx, "postgres:16-alpine", testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
@@ -31,6 +34,9 @@ func CreatePGDB() (func(), error) {
}
func CreateMyDB() (func(), error) {
log.Printf("[DEBUG] CreateMyDB")
ctx := context.Background()
c, err := mysql.Run(ctx, "mysql:5.7.34", testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").