Skip to content

Commit b87415a

Browse files
add suport oracle
1 parent 2bd822b commit b87415a

37 files changed

+940
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab
22
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite
3-
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher
3+
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher oracle
44
VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-)
55
TEST_FLAGS ?=
66
REPO_OWNER ?= $(shell cd .. && basename "$$(pwd)")

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Database drivers run migrations. [Add a new database?](database/driver.go)
4444
* [Firebird](database/firebird)
4545
* [MS SQL Server](database/sqlserver)
4646
* [rqlite](database/rqlite)
47-
47+
* [Oracle](database/oracle)
48+
*
4849
### Database URLs
4950

5051
Database connection strings are specified via URLs. The URL format is driver dependent but generally has the form: `dbdriver://username:password@host:port/dbname?param1=true&param2=false`

database/oracle/README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# oracle
2+
3+
The supported oracle specific options can be configured in the query section of the oracle
4+
URL `oracle://user:password@host:port/ServiceName?query`
5+
6+
| URL Query | WithInstance Config | Description |
7+
|--------------------------|----------------------|-------------------------------------------------------------------------------------------------------------------------|
8+
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table in UPPER case |
9+
| `x-multi-stmt-enabled` | `MultiStmtEnabled` | If the migration files are in multi-statements style |
10+
| `x-multi-stmt-separator` | `MultiStmtSeparator` | a single line which use as the token to spilt multiple statements in single migration file, triple-dash separator `---` |
11+
12+
## Write migration files
13+
14+
There are two ways to write the migration files,
15+
16+
1. Single statement file in which it contains only one SQL statement or one PL/SQL statement(Default)
17+
2. Multi statements file in which it can have multi statements(can be SQL or PL/SQL or mixed)
18+
19+
### Single statement file
20+
21+
Oracle godor driver support process one statement at a time, so it is natural to support single statement per file as
22+
the default.
23+
Check the [single statement migration files](examples/migrations) as an example.
24+
25+
### Multi statements file
26+
27+
Although the golang oracle driver [godror](https://github.com/godror/godror) does not natively support executing
28+
multiple
29+
statements in a single query, it's more friendly and handy to support multi statements in a single migration file in
30+
some case,
31+
so the multi statements can be separated with a line separator(default to triple-dash separator ---), for example:
32+
33+
```
34+
statement 1
35+
---
36+
statement 2
37+
```
38+
39+
Check the [multi statements' migration files](examples/migrations-multistmt) as an example.
40+
41+
## Supported & tested version
42+
43+
- 18-xe
44+
45+
## Build cli
46+
47+
```bash
48+
$ cd /path/to/repo/dir
49+
$ go build -tags 'oracle' -o bin/migrate github.com/golang-migrate/migrate/v4/cli
50+
```
51+
52+
## Run test code
53+
54+
There are two ways to run the test code:
55+
56+
- Run the test code locally with an existing Oracle Instance(Recommended)
57+
- Run the test code inside a container just like CI, It will require to start an Oracle container every time, and it's
58+
very time expense.
59+
60+
### Run the test code locally with an existing Oracle Instance
61+
62+
1. Start the `Oracle Database Instance` via docker first, so that you can reuse whenever you want to run the test code.
63+
64+
```bash
65+
$ cat docker-compose.yaml
66+
---
67+
services:
68+
oracle-db:
69+
container_name: oracle-db
70+
image: gvenzl/oracle-free:23.5-slim
71+
environment:
72+
ORACLE_PASSWORD: SuperPassword@2025
73+
ports:
74+
- 1521:1521
75+
healthcheck:
76+
test: ["CMD", "healthcheck.sh"]
77+
interval: 10s
78+
timeout: 5s
79+
retries: 10
80+
start_period: 5s
81+
start_interval: 5s
82+
volumes:
83+
- ${HOME}/database/oracle/testdata/init.sql:/docker-entrypoint-initdb.d/init.sql
84+
```
85+
86+
2. Go into the sqlplus console
87+
88+
```bash
89+
$ docker exec -it orclxe bash
90+
# su oracle
91+
$ sqlplus / as sysdba
92+
```
93+
94+
3. Create a test DB
95+
96+
```sql
97+
alter session set container=FREEPDB1;
98+
create user orcl identified by orcl;
99+
grant dba to orcl;
100+
grant create session to orcl;
101+
grant connect, resource to orcl;
102+
grant all privileges to orcl;
103+
```
104+
105+
4. Run the test code
106+
107+
```bash
108+
$ cd /path/to/repo/database/oracle/dir
109+
$ ORACLE_DSN=oracle://orcl:orcl@localhost:1521/FREEPDB1 go test -tags "oracle" -race -v -covermode atomic ./... -coverprofile .coverage -timeout 20m
110+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE IF EXISTS USERS_MS;
2+
---
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE USERS_MS (
2+
USER_ID integer unique,
3+
NAME varchar(40),
4+
EMAIL varchar(40)
5+
);
6+
7+
---
8+
9+
DROP TABLE IF EXISTS USERS_MS;
10+
11+
---
12+
13+
CREATE TABLE USERS_MS (
14+
USER_ID integer unique,
15+
NAME varchar(40),
16+
EMAIL varchar(40)
17+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE USERS_MS DROP COLUMN CITY;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE USERS_MS ADD CITY varchar(100);
2+
---
3+
ALTER TABLE USERS_MS ADD ALIAS varchar(100);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP INDEX users_ms_email_index;
2+
---
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE UNIQUE INDEX users_ms_email_index ON users_ms (email);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE IF EXISTS BOOKS_MS;
2+
---

0 commit comments

Comments
 (0)