Skip to content

Commit

Permalink
add suport oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
JailtonJunior94 committed Feb 13, 2025
1 parent d477553 commit a601110
Show file tree
Hide file tree
Showing 37 changed files with 934 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher oracle
VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-)
TEST_FLAGS ?=
REPO_OWNER ?= $(shell cd .. && basename "$$(pwd)")
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Database drivers run migrations. [Add a new database?](database/driver.go)
* [Firebird](database/firebird)
* [MS SQL Server](database/sqlserver)
* [rqlite](database/rqlite)

* [Oracle](database/oracle)
*
### Database URLs

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`
Expand Down
110 changes: 110 additions & 0 deletions database/oracle/README.md
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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS USERS_MS;
---
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)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE USERS_MS DROP COLUMN CITY;
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX users_ms_email_index;
---
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX users_ms_email_index ON users_ms (email);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS BOOKS_MS;
---
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)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS MOVIES_MS
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)
);
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.
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.
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.
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS USERS
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)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE USERS DROP COLUMN CITY
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE USERS ADD CITY varchar(100)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS users_email_index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX users_email_index ON users (email)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS BOOKS
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)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS MOVIES
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)
)
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.
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.
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.
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.
Loading

0 comments on commit a601110

Please sign in to comment.