-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add OracleDB support to golang-migrate #1230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JailtonJunior94
wants to merge
1
commit into
golang-migrate:master
Choose a base branch
from
JailtonJunior94:oracle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+934
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# oracle | ||
|
||
The supported oracle specific options can be configured in the query section of the oracle | ||
URL `oracle://user:password@host:port/ServiceName?query` | ||
|
||
| URL Query | WithInstance Config | Description | | ||
|--------------------------|----------------------|-------------------------------------------------------------------------------------------------------------------------| | ||
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table in UPPER case | | ||
| `x-multi-stmt-enabled` | `MultiStmtEnabled` | If the migration files are in multi-statements style | | ||
| `x-multi-stmt-separator` | `MultiStmtSeparator` | a single line which use as the token to spilt multiple statements in single migration file, triple-dash separator `---` | | ||
|
||
## Write migration files | ||
|
||
There are two ways to write the migration files, | ||
|
||
1. Single statement file in which it contains only one SQL statement or one PL/SQL statement(Default) | ||
2. Multi statements file in which it can have multi statements(can be SQL or PL/SQL or mixed) | ||
|
||
### Single statement file | ||
|
||
Oracle godor driver support process one statement at a time, so it is natural to support single statement per file as | ||
the default. | ||
Check the [single statement migration files](examples/migrations) as an example. | ||
|
||
### Multi statements file | ||
|
||
Although the golang oracle driver [godror](https://github.com/godror/godror) does not natively support executing | ||
multiple | ||
statements in a single query, it's more friendly and handy to support multi statements in a single migration file in | ||
some case, | ||
so the multi statements can be separated with a line separator(default to triple-dash separator ---), for example: | ||
|
||
``` | ||
statement 1 | ||
--- | ||
statement 2 | ||
``` | ||
|
||
Check the [multi statements' migration files](examples/migrations-multistmt) as an example. | ||
|
||
## Supported & tested version | ||
|
||
- 18-xe | ||
|
||
## Build cli | ||
|
||
```bash | ||
$ cd /path/to/repo/dir | ||
$ go build -tags 'oracle' -o bin/migrate github.com/golang-migrate/migrate/v4/cli | ||
``` | ||
|
||
## Run test code | ||
|
||
There are two ways to run the test code: | ||
|
||
- Run the test code locally with an existing Oracle Instance(Recommended) | ||
- Run the test code inside a container just like CI, It will require to start an Oracle container every time, and it's | ||
very time expense. | ||
|
||
### Run the test code locally with an existing Oracle Instance | ||
|
||
1. Start the `Oracle Database Instance` via docker first, so that you can reuse whenever you want to run the test code. | ||
|
||
```bash | ||
$ cat docker-compose.yaml | ||
--- | ||
services: | ||
oracle-db: | ||
container_name: oracle-db | ||
image: gvenzl/oracle-free:23.5-slim | ||
environment: | ||
ORACLE_PASSWORD: SuperPassword@2025 | ||
ports: | ||
- 1521:1521 | ||
healthcheck: | ||
test: ["CMD", "healthcheck.sh"] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 10 | ||
start_period: 5s | ||
start_interval: 5s | ||
volumes: | ||
- ${HOME}/database/oracle/testdata/init.sql:/docker-entrypoint-initdb.d/init.sql | ||
``` | ||
|
||
2. Go into the sqlplus console | ||
|
||
```bash | ||
$ docker exec -it orclxe bash | ||
# su oracle | ||
$ sqlplus / as sysdba | ||
``` | ||
|
||
3. Create a test DB | ||
|
||
```sql | ||
alter session set container=FREEPDB1; | ||
create user orcl identified by orcl; | ||
grant dba to orcl; | ||
grant create session to orcl; | ||
grant connect, resource to orcl; | ||
grant all privileges to orcl; | ||
``` | ||
|
||
4. Run the test code | ||
|
||
```bash | ||
$ cd /path/to/repo/database/oracle/dir | ||
$ ORACLE_DSN=oracle://orcl:orcl@localhost:1521/FREEPDB1 go test -tags "oracle" -race -v -covermode atomic ./... -coverprofile .coverage -timeout 20m | ||
``` |
2 changes: 2 additions & 0 deletions
2
database/oracle/examples/migrations-multistmt/1085649617_create_users_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TABLE IF EXISTS USERS_MS; | ||
--- |
17 changes: 17 additions & 0 deletions
17
database/oracle/examples/migrations-multistmt/1085649617_create_users_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE TABLE USERS_MS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
); | ||
|
||
--- | ||
|
||
DROP TABLE IF EXISTS USERS_MS; | ||
|
||
--- | ||
|
||
CREATE TABLE USERS_MS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
); |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1185749658_add_city_to_users.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE USERS_MS DROP COLUMN CITY; |
3 changes: 3 additions & 0 deletions
3
database/oracle/examples/migrations-multistmt/1185749658_add_city_to_users.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ALTER TABLE USERS_MS ADD CITY varchar(100); | ||
--- | ||
ALTER TABLE USERS_MS ADD ALIAS varchar(100); |
2 changes: 2 additions & 0 deletions
2
database/oracle/examples/migrations-multistmt/1285849751_add_index_on_user_emails.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP INDEX users_ms_email_index; | ||
--- |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1285849751_add_index_on_user_emails.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE UNIQUE INDEX users_ms_email_index ON users_ms (email); |
2 changes: 2 additions & 0 deletions
2
database/oracle/examples/migrations-multistmt/1385949617_create_books_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TABLE IF EXISTS BOOKS_MS; | ||
--- |
5 changes: 5 additions & 0 deletions
5
database/oracle/examples/migrations-multistmt/1385949617_create_books_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE BOOKS_MS ( | ||
USER_ID integer, | ||
NAME varchar(40), | ||
AUTHOR varchar(40) | ||
); |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1485949617_create_movies_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS MOVIES_MS |
5 changes: 5 additions & 0 deletions
5
database/oracle/examples/migrations-multistmt/1485949617_create_movies_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE MOVIES_MS ( | ||
USER_ID integer, | ||
NAME varchar(40), | ||
DIRECTOR varchar(40) | ||
); |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1585849751_just_a_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1685849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1785849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations-multistmt/1885849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1085649617_create_users_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS USERS |
5 changes: 5 additions & 0 deletions
5
database/oracle/examples/migrations/1085649617_create_users_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
) |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1185749658_add_city_to_users.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE USERS DROP COLUMN CITY |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1185749658_add_city_to_users.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE USERS ADD CITY varchar(100) |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1285849751_add_index_on_user_emails.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP INDEX IF EXISTS users_email_index |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1285849751_add_index_on_user_emails.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE UNIQUE INDEX users_email_index ON users (email) |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1385949617_create_books_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS BOOKS |
5 changes: 5 additions & 0 deletions
5
database/oracle/examples/migrations/1385949617_create_books_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE BOOKS ( | ||
USER_ID integer, | ||
NAME varchar(40), | ||
AUTHOR varchar(40) | ||
) |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1485949617_create_movies_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS MOVIES |
5 changes: 5 additions & 0 deletions
5
database/oracle/examples/migrations/1485949617_create_movies_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE MOVIES ( | ||
USER_ID integer, | ||
NAME varchar(40), | ||
DIRECTOR varchar(40) | ||
) |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1585849751_just_a_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1685849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1785849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
1 change: 1 addition & 0 deletions
1
database/oracle/examples/migrations/1885849751_another_comment.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
package oracle | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
sqldriver "database/sql/driver" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/dhui/dktest" | ||
"github.com/docker/docker/api/types/mount" | ||
"github.com/docker/go-connections/nat" | ||
"github.com/golang-migrate/migrate/v4" | ||
dt "github.com/golang-migrate/migrate/v4/database/testing" | ||
"github.com/golang-migrate/migrate/v4/dktesting" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
const ( | ||
defaultPort = 1521 | ||
userdba = "orcl" | ||
userdbaPass = "orcl" | ||
defaultPass = "orcl" | ||
) | ||
|
||
var ( | ||
specs = []dktesting.ContainerSpec{ | ||
{ | ||
ImageName: "gvenzl/oracle-free:23.5-slim", Options: oracleOptions(), | ||
}, | ||
} | ||
) | ||
|
||
func oracleOptions() dktest.Options { | ||
cwd, _ := os.Getwd() | ||
mounts := []mount.Mount{ | ||
{ | ||
Type: mount.TypeBind, | ||
Source: filepath.Join(cwd, "testdata/init.sql"), | ||
Target: "/docker-entrypoint-initdb.d/init.sql", | ||
}, | ||
} | ||
|
||
return dktest.Options{ | ||
PortRequired: true, | ||
Mounts: mounts, | ||
ReadyFunc: isReady, | ||
ExposedPorts: nat.PortSet{ | ||
nat.Port(fmt.Sprintf("%d/tcp", defaultPort)): {}, | ||
}, | ||
PortBindings: map[nat.Port][]nat.PortBinding{ | ||
nat.Port(fmt.Sprintf("%d/tcp", defaultPort)): { | ||
nat.PortBinding{ | ||
HostIP: "0.0.0.0", | ||
HostPort: "0/tcp", | ||
}, | ||
}, | ||
}, | ||
Env: map[string]string{ | ||
"ORACLE_PASSWORD": defaultPass, | ||
}, | ||
} | ||
} | ||
|
||
func oracleConnectionString(host, port string) string { | ||
return fmt.Sprintf("oracle://%s:%s@%s:%s/FREEPDB1", userdba, userdbaPass, host, port) | ||
} | ||
|
||
func isReady(ctx context.Context, c dktest.ContainerInfo) bool { | ||
ip, port, err := c.Port(defaultPort) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
db, err := sql.Open("oracle", oracleConnectionString(ip, port)) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
defer func() { | ||
if err := db.Close(); err != nil { | ||
log.Println("close error:", err) | ||
} | ||
}() | ||
|
||
if err = db.PingContext(ctx); err != nil { | ||
switch err { | ||
case sqldriver.ErrBadConn, io.EOF: | ||
return false | ||
default: | ||
fmt.Println(err) | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
type oracleSuite struct { | ||
dsn string | ||
suite.Suite | ||
} | ||
|
||
// In order for 'go test' to run this suite, we need to create | ||
// a normal test function and pass our suite to suite.Run | ||
func TestOracleTestSuite(t *testing.T) { | ||
if dsn := os.Getenv("ORACLE_DSN"); dsn != "" { | ||
s := oracleSuite{dsn: dsn} | ||
suite.Run(t, &s) | ||
return | ||
} | ||
|
||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.Port(defaultPort) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
dsn := oracleConnectionString(ip, port) | ||
s := oracleSuite{dsn: dsn} | ||
|
||
suite.Run(t, &s) | ||
}) | ||
} | ||
|
||
func (s *oracleSuite) TestMigrate() { | ||
ora := &Oracle{} | ||
d, err := ora.Open(s.dsn) | ||
s.Require().Nil(err) | ||
s.Require().NotNil(d) | ||
|
||
defer func() { | ||
if err := d.Close(); err != nil { | ||
s.Error(err) | ||
} | ||
}() | ||
|
||
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "", d) | ||
s.Require().Nil(err) | ||
dt.TestMigrate(s.T(), m) | ||
} | ||
|
||
func (s *oracleSuite) TestMultiStmtMigrate() { | ||
ora := &Oracle{} | ||
dsn := fmt.Sprintf("%s?%s=%s&&%s=%s", s.dsn, multiStmtEnableQueryKey, "true", multiStmtSeparatorQueryKey, "---") | ||
d, err := ora.Open(dsn) | ||
s.Require().Nil(err) | ||
s.Require().NotNil(d) | ||
defer func() { | ||
if err := d.Close(); err != nil { | ||
s.Error(err) | ||
} | ||
}() | ||
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations-multistmt", "", d) | ||
s.Require().Nil(err) | ||
dt.TestMigrate(s.T(), m) | ||
} | ||
|
||
func (s *oracleSuite) TestLockWorks() { | ||
ora := &Oracle{} | ||
d, err := ora.Open(s.dsn) | ||
s.Require().Nil(err) | ||
s.Require().NotNil(d) | ||
defer func() { | ||
if err := d.Close(); err != nil { | ||
s.Error(err) | ||
} | ||
}() | ||
|
||
dt.Test(s.T(), d, []byte(`BEGIN DBMS_OUTPUT.PUT_LINE('hello'); END;`)) | ||
|
||
ora = d.(*Oracle) | ||
err = ora.Lock() | ||
s.Require().Nil(err) | ||
|
||
err = ora.Unlock() | ||
s.Require().Nil(err) | ||
|
||
err = ora.Lock() | ||
s.Require().Nil(err) | ||
|
||
err = ora.Unlock() | ||
s.Require().Nil(err) | ||
} | ||
|
||
func TestParseStatements(t *testing.T) { | ||
cases := []struct { | ||
migration string | ||
expectedQueries []string | ||
}{ | ||
{migration: ` | ||
CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
); | ||
--- | ||
-- | ||
BEGIN | ||
EXECUTE IMMEDIATE 'DROP TABLE USERS'; | ||
EXCEPTION | ||
WHEN OTHERS THEN | ||
IF SQLCODE != -942 THEN | ||
RAISE; | ||
END IF; | ||
END; | ||
--- | ||
-- comment | ||
-- | ||
CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
); | ||
--- | ||
--`, | ||
expectedQueries: []string{ | ||
`CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
)`, | ||
`BEGIN | ||
EXECUTE IMMEDIATE 'DROP TABLE USERS'; | ||
EXCEPTION | ||
WHEN OTHERS THEN | ||
IF SQLCODE != -942 THEN | ||
RAISE; | ||
END IF; | ||
END;`, | ||
`CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
)`, | ||
}}, | ||
{migration: ` | ||
-- comment | ||
CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
); | ||
-- this is comment | ||
--- | ||
ALTER TABLE USERS ADD CITY varchar(100); | ||
`, | ||
expectedQueries: []string{ | ||
`CREATE TABLE USERS ( | ||
USER_ID integer unique, | ||
NAME varchar(40), | ||
EMAIL varchar(40) | ||
)`, | ||
`ALTER TABLE USERS ADD CITY varchar(100)`, | ||
}}, | ||
} | ||
for _, c := range cases { | ||
queries, err := parseMultiStatements(bytes.NewBufferString(c.migration), DefaultMultiStmtSeparator) | ||
require.Nil(t, err) | ||
require.Equal(t, c.expectedQueries, queries) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
alter session set container=FREEPDB1; | ||
create user orcl identified by orcl; | ||
grant dba to orcl; | ||
grant create session to orcl; | ||
grant connect, resource to orcl; | ||
grant all privileges to orcl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build oracle | ||
// +build oracle | ||
|
||
package cli | ||
|
||
import ( | ||
_ "github.com/golang-migrate/migrate/v4/database/oracle" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with OracleDB, but is it possible to use an official Docker image? Most of the other database tests use official images.