Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e6ef45c
Implement oracle migration
xianchaoyu Feb 14, 2020
a5e2691
Update readme
xianchaoyu Feb 14, 2020
78b1636
Merge branch 'master' into master
maxnilz Feb 14, 2020
594390c
Fix lint issue
xianchaoyu Feb 14, 2020
8a49f20
Merge branch 'master' of maxnilz:maxnilz/migrate
xianchaoyu Feb 14, 2020
bdc3b2d
Remove oracle from binary build
xianchaoyu Feb 14, 2020
f7c17f1
Update readme
xianchaoyu Feb 14, 2020
1d601cf
Update readme
xianchaoyu Feb 14, 2020
a088100
Update readme
xianchaoyu Feb 14, 2020
b6c304f
Update readme
xianchaoyu Feb 16, 2020
4b45014
1. Add oracle build tags to all oracle related files
xianchaoyu Feb 16, 2020
587a3c6
Merge branch 'master' of maxnilz:maxnilz/migrate
xianchaoyu Feb 16, 2020
d67775b
update readme
xianchaoyu Feb 16, 2020
1b56c06
update readme
xianchaoyu Feb 16, 2020
a02208b
use godror driver instead of oic8 to remove comple dependencies on th…
xianchaoyu Feb 16, 2020
09a7acf
update readme
xianchaoyu Feb 16, 2020
d360106
Merge branch 'master' of https://github.com/golang-migrate/migrate
xianchaoyu Feb 17, 2020
7c4a909
Remove assets & update readme & update multi-statements
xianchaoyu Feb 18, 2020
ef470f4
Merge branch 'master' of https://github.com/golang-migrate/migrate
xianchaoyu Feb 18, 2020
dceedfe
add dk test cases
xianchaoyu Feb 18, 2020
07107b5
update readme
xianchaoyu Feb 18, 2020
2b45d64
update readme
xianchaoyu Feb 19, 2020
ef208e1
update comments
xianchaoyu Feb 19, 2020
9076190
update go sum
xianchaoyu Feb 19, 2020
27ac402
Update test time limit
xianchaoyu Feb 19, 2020
bf3005b
Update container read timeout
xianchaoyu Feb 19, 2020
b541b0e
remove err info
xianchaoyu Feb 19, 2020
eb69618
update timeout limit
xianchaoyu Feb 19, 2020
8c2d613
reuse started oracle container to avoid travis ci job exceeded the ma…
xianchaoyu Feb 19, 2020
0f00e02
oracle err code
xianchaoyu Feb 19, 2020
41fddbd
fix port selection in test case
xianchaoyu Feb 19, 2020
0fafece
Merge branch 'master' of github.com:golang-migrate/migrate
maxnilz Mar 24, 2022
b446502
update & refactor
maxnilz Mar 24, 2022
8113788
update
maxnilz Mar 24, 2022
25ad010
update
maxnilz Mar 24, 2022
24545d4
fix ci
maxnilz Mar 25, 2022
5de0268
fix ci
maxnilz Mar 25, 2022
5ae0902
fix ci
maxnilz Mar 25, 2022
348db07
Merge branch 'master' of github.com:golang-migrate/migrate
maxnilz Jul 7, 2022
b65a340
merge upstream & use go-ora instead of gordor
maxnilz Mar 16, 2023
5382463
Merge branch 'master' of github.com:golang-migrate/migrate into go-ora
maxnilz Mar 16, 2023
f60731d
tidyup
maxnilz Mar 16, 2023
647bfec
merge upstream and remvoe testcontainers-go
maxnilz Aug 18, 2023
3808f30
merge upstream and remvoe testcontainers-go
maxnilz Aug 18, 2023
512d0e4
fix lint issue and update pull timeout
maxnilz Aug 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Database drivers run migrations. [Add a new database?](database/driver.go)
* [ClickHouse](database/clickhouse)
* [Firebird](database/firebird)
* [MS SQL Server](database/sqlserver)
* [Oracle](database/oracle)

### Database URLs

Expand Down
105 changes: 105 additions & 0 deletions database/oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 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
---
version: '2'
services:
orclxe:
image: container-registry.oracle.com/database/express:18.4.0-xe
ports:
- 1521:1521
container_name: orclxe
environment:
ORACLE_PWD: oracle
volumes:
- ${HOME}/data/orclxe:/opt/oracle/oradata # permission chown -R 54321:54321 ${HOME}/data/orclxe

```

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=XEPDB1;
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/XEPDB1 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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE USERS_MS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE TABLE USERS_MS (
USER_ID integer unique,
NAME varchar(40),
EMAIL varchar(40)
);

---

BEGIN
EXECUTE IMMEDIATE 'DROP TABLE USERS_MS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;

---

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,14 @@
DECLARE
v_column_exists number := 0;
BEGIN
SELECT COUNT(*)
INTO v_column_exists
FROM user_tab_cols
WHERE table_name = 'USERS_MS'
AND column_name = 'CITY';

IF( v_column_exists = 1 )
THEN
EXECUTE IMMEDIATE 'ALTER TABLE USERS_MS DROP COLUMN CITY';
END IF;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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,12 @@
DECLARE
COUNT_INDEXES INTEGER;
BEGIN
SELECT COUNT ( * )
INTO COUNT_INDEXES_MS
FROM USERS_MS_INDEXES
WHERE INDEX_NAME = 'users_ms_email_index';
IF COUNT_INDEXES > 0
THEN
EXECUTE IMMEDIATE 'DROP INDEX users_ms_email_index';
END IF;
END;
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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE BOOKS_MS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE MOVIES_MS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE USERS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
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,14 @@
DECLARE
v_column_exists number := 0;
BEGIN
SELECT COUNT(*)
INTO v_column_exists
FROM user_tab_cols
WHERE table_name = 'USERS'
AND column_name = 'CITY';

IF( v_column_exists = 1 )
THEN
EXECUTE IMMEDIATE 'ALTER TABLE users DROP COLUMN CITY';
END IF;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE USERS ADD CITY varchar(100)


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DECLARE
COUNT_INDEXES INTEGER;
BEGIN
SELECT COUNT ( * )
INTO COUNT_INDEXES
FROM USER_INDEXES
WHERE INDEX_NAME = 'users_email_index';
IF COUNT_INDEXES > 0
THEN
EXECUTE IMMEDIATE 'DROP INDEX users_email_index';
END IF;
END;
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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE BOOKS';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
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,8 @@
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE MOVIES';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
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