Skip to content
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

Impelement oracle database migration #341

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
33 changes: 33 additions & 0 deletions Dockerfile-oracle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:1.13-buster as builder

RUN apt-get update && apt-get install -y --no-install-recommends zip unzip build-essential libaio1 \
&& rm -rf /var/lib/apt/lists/*

ARG VERSION

COPY database/oracle/assets/instantclient-basiclite-linux.x64-18.5.0.0.0dbru.zip /opt/oracle/
RUN unzip -d /opt/oracle /opt/oracle/instantclient-basiclite-linux.x64-18.5.0.0.0dbru.zip
ENV LD_LIBRARY_PATH="/opt/oracle/instantclient_18_5"

WORKDIR /go/src/github.com/golang-migrate/migrate

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -a -o build/migrate.linux-386 -ldflags="-s -w -X main.Version=${VERSION}" -tags "oracle" ./cmd/migrate

FROM debian:buster-slim

RUN apt-get update && apt-get install -y --no-install-recommends libaio1 ca-certificates \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /opt/oracle/instantclient_18_5 /opt/oracle/instantclient_18_5
COPY --from=builder /go/src/github.com/golang-migrate/migrate/build/migrate.linux-386 /migrate

ENV LD_LIBRARY_PATH="/opt/oracle/instantclient_18_5:${LD_LIBRARY_PATH}"

ENTRYPOINT ["/migrate"]
CMD ["--help"]
80 changes: 80 additions & 0 deletions database/oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# oracle
The golang oracle driver [godror](https://github.com/godror/godror) does not natively support executing multiple statements in a single query.
Here are the strategies for splitting the migration text into separately-executed statements:
1. If there is no PL/SQL statement in a migration file, the `semicolons` will be the separator
1. If there is any PL/SQL statement in a migration file, the separator will be `---` in a single line or specified by `x-plsql-line-separator`,
And in this case the multiple statements cannot be used when a statement in the migration contains a string with the given `line separator`.

`oracle://user:password@host:port/sid?query`

| URL Query | WithInstance Config | Description |
|------------|---------------------|-------------|
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table in UPPER case |
| `x-plsql-line-separator` | `PLSQLStatementSeparator` | a single line which use as the token to spilt multiple statements in single migration file (See note above), default `---` |

## Runtime dependency

Although an Oracle client is NOT required for compiling, it is at run time. One can download it from https://www.oracle.com/database/technologies/instant-client/downloads.html

## Supported & tested version
- 12c-ee
maxnilz marked this conversation as resolved.
Show resolved Hide resolved
- 18c-xe

## How to use

In order to compile & run the migration against Oracle database, basically it will require:

## Build cli

```bash
$ cd /path/to/repo/dir
$ go build -tags 'oracle' -o bin/migrate github.com/golang-migrate/migrate/v4/cli
```

## Configure Oracle database
1. Example Oracle version: `Oracle Database Express Edition`, check [here](https://docs.oracle.com/cd/B28359_01/license.111/b28287/editions.htm#DBLIC119) from version details.
maxnilz marked this conversation as resolved.
Show resolved Hide resolved
1. Start a oracle docker container based on customized community oracle-xe image(include a PDB database & default user `oracle` in it): `docker run --name oracle -d -p 1521:1521 -p 5500:5500 --volume ~/data/oracle-xe:/opt/oracle/oradata maxnilz/oracle-xe:18c`
1. Wait a moment, first time will take a while to run for as the oracle-xe configure script needs to complete

## Download runtime dependency

Download [oracle client dynamic library](https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html) from their official site manually, because it requires to logon and honor a check box on download page manually.

## Play

### Run test code

```bash
$ cd /path/to/repo/database/oracle/dir
$ ORACLE_DSN=oracle://oracle:oracle@localhost:1521/XEPDB1 LD_LIBRARY_PATH=/path/to/oracle/lib/dir go test -tags "oracle" -race -v -covermode atomic ./... -coverprofile .coverage
```

### Write migration files

Check [example migration files](examples)

## FAQs

Maybe not "frequently asked", but hopefully these answers will be useful.

### Why the test code for oracle in CI are disabled

Setup test case via oracle container in CI is very expensive for these reasons:
1. There is no public official docker images available
1. The oracle image size in community is 8GB, which is huge
1. The volume size of one single oracle container is about 5GB, which is huge too
1. And more importantly, It will take a long time to start just a single oracle container & configure it(almost 30min on my 16GB memory, 8 cores machine). The test case will run in parallel and each case will require it's own container, which will increase the resource & time costs many times.
1. Although an Oracle client is NOT required for compiling, it is at run time. and it's tricky to download the dynamic lib directly/automatically because of the oracle download policies.

### Why there is a dockerfile for oracle only?

The dependent dynamic libs are missing in alpine system, the dockerfile for oracle is based on debian system.

### Why there is an assets dir for the oracle libs

1. It requires to login to the oracle official site & config the license manually for downloading these oracle lib, we can't use wget & curl to download directly.
1. In order to make `Dockerfile.oracle` works, I download them manually and put them in the `assets` dir.

### Why we need the dynamic library?

There is no static lib for the application to compile & link. check [here](https://community.oracle.com/thread/4177571) for more details.
Binary file not shown.
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,24 @@
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)
);
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,4 @@
ALTER TABLE USERS ADD CITY varchar(100);
ALTER TABLE USERS 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
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