diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml index 00653cf08..7200ac42e 100644 --- a/.woodpecker/test.yml +++ b/.woodpecker/test.yml @@ -39,7 +39,7 @@ pipeline: image: golang:1.16 group: test environment: - - DATABASE_DRIVER=sqlite3 + - WOODPECKER_DATABASE_DRIVER=sqlite3 commands: - make test-server-datastore @@ -47,8 +47,8 @@ pipeline: image: golang:1.16 group: test environment: - - DATABASE_DRIVER=postgres - - DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable + - WOODPECKER_DATABASE_DRIVER=postgres + - WOODPECKER_DATABASE_DATASOURCE=host=service-postgres user=postgres dbname=postgres sslmode=disable commands: - make test-server-datastore @@ -56,8 +56,8 @@ pipeline: image: golang:1.16 group: test environment: - - DATABASE_DRIVER=mysql - - DATABASE_CONFIG=root@tcp(mysql:3306)/test?parseTime=true + - WOODPECKER_DATABASE_DRIVER=mysql + - WOODPECKER_DATABASE_DATASOURCE=root@tcp(service-mysql:3306)/test?parseTime=true commands: - make test-server-datastore diff --git a/server/store/datastore/engine_test.go b/server/store/datastore/engine_test.go index 8de20e5a8..13735690e 100644 --- a/server/store/datastore/engine_test.go +++ b/server/store/datastore/engine_test.go @@ -29,7 +29,7 @@ func testDriverConfig() (driver, config string) { if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" { driver = os.Getenv("WOODPECKER_DATABASE_DRIVER") - config = os.Getenv("WOODPECKER_DATABASE_CONFIG") + config = os.Getenv("WOODPECKER_DATABASE_DATASOURCE") } return } diff --git a/server/store/datastore/migration/migration_test.go b/server/store/datastore/migration/migration_test.go new file mode 100644 index 000000000..8993c9e09 --- /dev/null +++ b/server/store/datastore/migration/migration_test.go @@ -0,0 +1,91 @@ +package migration + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "xorm.io/xorm" + + // blank imports to register the sql drivers + _ "github.com/go-sql-driver/mysql" + _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" +) + +const ( + sqliteDB = "./testfiles/sqlite.db" +) + +func testDriver() string { + driver := os.Getenv("WOODPECKER_DATABASE_DRIVER") + if len(driver) == 0 { + return "sqlite3" + } + return driver +} + +func createSQLiteDB(t *testing.T) string { + tmpF, err := ioutil.TempFile("./testfiles", "tmp_") + if !assert.NoError(t, err) { + t.FailNow() + } + dbF, err := ioutil.ReadFile(sqliteDB) + if !assert.NoError(t, err) { + t.FailNow() + } + + if !assert.NoError(t, ioutil.WriteFile(tmpF.Name(), dbF, 0644)) { + t.FailNow() + } + return tmpF.Name() +} + +func testDB(t *testing.T, new bool) (engine *xorm.Engine, close func()) { + driver := testDriver() + var err error + close = func() {} + switch driver { + case "sqlite3": + config := ":memory:" + if !new { + config = createSQLiteDB(t) + close = func() { + _ = os.Remove(config) + } + } + engine, err = xorm.NewEngine(driver, config) + if !assert.NoError(t, err) { + t.FailNow() + } + return + case "mysql", "postgres": + config := os.Getenv("WOODPECKER_DATABASE_DATASOURCE") + if !new { + t.Logf("do not have dump to test against") + t.SkipNow() + } + engine, err = xorm.NewEngine(driver, config) + if !assert.NoError(t, err) { + t.FailNow() + } + return + default: + t.Errorf("unsupported driver: %s", driver) + t.FailNow() + } + return +} + +func TestMigrate(t *testing.T) { + // init new db + engine, close := testDB(t, true) + assert.NoError(t, Migrate(engine)) + close() + + // migrate old db + engine, close = testDB(t, false) + assert.NoError(t, Migrate(engine)) + close() +} diff --git a/server/store/datastore/migration/testfiles/.gitignore b/server/store/datastore/migration/testfiles/.gitignore new file mode 100644 index 000000000..ab1a9904f --- /dev/null +++ b/server/store/datastore/migration/testfiles/.gitignore @@ -0,0 +1 @@ +tmp_* diff --git a/server/store/datastore/migration/testfiles/sqlite.db b/server/store/datastore/migration/testfiles/sqlite.db new file mode 100644 index 000000000..219e5184a Binary files /dev/null and b/server/store/datastore/migration/testfiles/sqlite.db differ