Browse Source

fix docker-compose up sql run error

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

2
common/utils_test.go

@ -42,7 +42,7 @@ func TestFuncs(t *testing.T) {
wd := MustGetwd()
assert.NotEqual(t, "", wd)
dir1 := GetCallerCodeDir()
dir1 := GetSqlDir()
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 {
dbr := dbGet().Exec("select 1")
return dbr.Error
db, err := dtmimp.StandaloneDB(config.Store.GetDBConf())
dtmimp.E2P(err)
_, err = db.Exec("select 1")
return err
}
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)
}

4
examples/data.go

@ -34,9 +34,9 @@ func resetXaData() {
// PopulateDB populate example mysql data
func PopulateDB(skipDrop bool) {
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)
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)
}

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
COPY --from=builder /app/dtm/main /app/dtm/
COPY --from=builder /app/dtm/sqls/*.sql /app/dtm/sqls/
ENV IS_DOCKER=1
WORKDIR /app/dtm
CMD ["/app/dtm/main", "dtmsvr"]

11
helper/compose.store.yml

@ -14,6 +14,17 @@ services:
]
ports:
- '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:
image: 'redis'
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