Browse Source

fix docker-compose up sql run error

topic
yedf2 5 years ago
parent
commit
1a339fc894
  1. 12
      common/utils.go
  2. 2
      common/utils_test.go
  3. 8
      dtmsvr/storage/sql.go
  4. 4
      examples/data.go
  5. 1
      helper/Dockerfile-release
  6. 11
      helper/compose.store.yml
  7. 0
      sqls/dtmcli.barrier.mysql.sql
  8. 0
      sqls/dtmcli.barrier.postgres.sql
  9. 0
      sqls/dtmsvr.storage.mysql.sql
  10. 0
      sqls/dtmsvr.storage.postgres.sql
  11. 0
      sqls/examples.mysql.sql
  12. 0
      sqls/examples.postgres.sql

12
common/utils.go

@ -12,7 +12,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"time" "time"
@ -80,10 +79,13 @@ func MustGetwd() string {
return wd return wd
} }
// GetCallerCodeDir 获取调用该函数的caller源代码的目录,主要用于测试时,查找相关文件 // GetSqlDir 获取调用该函数的caller源代码的目录,主要用于测试时,查找相关文件
func GetCallerCodeDir() string { func GetSqlDir() string {
_, file, _, _ := runtime.Caller(1) wd := MustGetwd()
return filepath.Dir(file) if filepath.Base(wd) == "test" {
wd = filepath.Dir(wd) + "/sqls"
}
return wd
} }
func RecoverPanic(err *error) { func RecoverPanic(err *error) {

2
common/utils_test.go

@ -42,7 +42,7 @@ func TestFuncs(t *testing.T) {
wd := MustGetwd() wd := MustGetwd()
assert.NotEqual(t, "", wd) assert.NotEqual(t, "", wd)
dir1 := GetCallerCodeDir() dir1 := GetSqlDir()
assert.Equal(t, true, strings.HasSuffix(dir1, "common")) assert.Equal(t, true, strings.HasSuffix(dir1, "common"))
} }

8
dtmsvr/storage/sql.go

@ -16,12 +16,14 @@ type SqlStore struct {
} }
func (s *SqlStore) Ping() error { func (s *SqlStore) Ping() error {
dbr := dbGet().Exec("select 1") db, err := dtmimp.StandaloneDB(config.Store.GetDBConf())
return dbr.Error dtmimp.E2P(err)
_, err = db.Exec("select 1")
return err
} }
func (s *SqlStore) PopulateData(skipDrop bool) { func (s *SqlStore) PopulateData(skipDrop bool) {
file := fmt.Sprintf("%s/storage.%s.sql", common.GetCallerCodeDir(), config.Store.Driver) file := fmt.Sprintf("%s/dtmsvr.storage.%s.sql", common.GetSqlDir(), config.Store.Driver)
common.RunSQLScript(config.Store.GetDBConf(), file, skipDrop) common.RunSQLScript(config.Store.GetDBConf(), file, skipDrop)
} }

4
examples/data.go

@ -34,9 +34,9 @@ func resetXaData() {
// PopulateDB populate example mysql data // PopulateDB populate example mysql data
func PopulateDB(skipDrop bool) { func PopulateDB(skipDrop bool) {
resetXaData() resetXaData()
file := fmt.Sprintf("%s/examples.%s.sql", common.GetCallerCodeDir(), config.ExamplesDB.Driver) file := fmt.Sprintf("%s/examples.%s.sql", common.GetSqlDir(), config.ExamplesDB.Driver)
common.RunSQLScript(config.ExamplesDB, file, skipDrop) common.RunSQLScript(config.ExamplesDB, file, skipDrop)
file = fmt.Sprintf("%s/../dtmcli/barrier.%s.sql", common.GetCallerCodeDir(), config.ExamplesDB.Driver) file = fmt.Sprintf("%s/dtmcli.barrier.%s.sql", common.GetSqlDir(), config.ExamplesDB.Driver)
common.RunSQLScript(config.ExamplesDB, file, skipDrop) common.RunSQLScript(config.ExamplesDB, file, skipDrop)
} }

1
helper/Dockerfile-release

@ -10,6 +10,7 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w" ap
FROM --platform=$TARGETPLATFORM alpine:3.14 FROM --platform=$TARGETPLATFORM alpine:3.14
COPY --from=builder /app/dtm/main /app/dtm/ COPY --from=builder /app/dtm/main /app/dtm/
COPY --from=builder /app/dtm/sqls/*.sql /app/dtm/sqls/
ENV IS_DOCKER=1 ENV IS_DOCKER=1
WORKDIR /app/dtm WORKDIR /app/dtm
CMD ["/app/dtm/main", "dtmsvr"] CMD ["/app/dtm/main", "dtmsvr"]

11
helper/compose.store.yml

@ -14,6 +14,17 @@ services:
] ]
ports: ports:
- '3306:3306' - '3306:3306'
postgres:
image: 'postgres:13'
command: postgres --max_prepared_transactions=1000
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
environment:
POSTGRES_PASSWORD: mysecretpassword
ports:
- '5432:5432'
redis: redis:
image: 'redis' image: 'redis'
volumes: volumes:

0
dtmcli/barrier.mysql.sql → sqls/dtmcli.barrier.mysql.sql

0
dtmcli/barrier.postgres.sql → sqls/dtmcli.barrier.postgres.sql

0
dtmsvr/storage/storage.mysql.sql → sqls/dtmsvr.storage.mysql.sql

0
dtmsvr/storage/storage.postgres.sql → sqls/dtmsvr.storage.postgres.sql

0
examples/examples.mysql.sql → sqls/examples.mysql.sql

0
examples/examples.postgres.sql → sqls/examples.postgres.sql

Loading…
Cancel
Save