Skip to content

Commit

Permalink
update sql-migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
pyar6329 committed Apr 19, 2024
1 parent dfc7413 commit f7834d7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
45 changes: 29 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
FROM golang:1.13.4-alpine3.10 AS sql-migrate-build
RUN set -ex && \
apk add --no-cache --virtual .installer git build-base && \
go get -v github.com/rubenv/sql-migrate/... && \
mkdir -p /build && \
cp -rf /go/bin/sql-migrate /build/sql-migrate

FROM alpine:3.10

WORKDIR /workspace
COPY --from=sql-migrate-build /build/sql-migrate /usr/local/bin/sql-migrate
COPY entrypoint.sh /usr/local/bin/entrypoint.sh

RUN set -ex && \
chmod +x /usr/local/bin/* && \
rm -rf /var/cache/apk/*
FROM golang:1.22-alpine3.19 AS build-base

ARG SQL_MIGRATE_VERSION="1.6.1"

RUN set -x && \
apk add --no-cache \
git \
build-base && \
go install github.com/rubenv/sql-migrate/...@v${SQL_MIGRATE_VERSION} && \
cp -rf /go/bin/sql-migrate /usr/local/bin/sql-migrate

FROM alpine:3.19

ARG USER_ID=1000
ARG USER_NAME=migration
ARG HOME_DIR="/home/migration"
ARG PROJECT_ROOT="/app"

RUN set -x && \
addgroup -g ${USER_ID} -S ${USER_NAME} && \
adduser -u ${USER_ID} -S -D -G ${USER_NAME} -H -h ${HOME_DIR} -s /bin/ash ${USER_NAME} && \
mkdir -p ${PROJECT_ROOT} && \
chown -R ${USER_NAME}:${USER_NAME} ${PROJECT_ROOT}

WORKDIR ${PROJECT_ROOT}
USER ${USER_NAME}

COPY --chmod=755 --from=build-base /usr/local/bin/sql-migrate /usr/local/bin/sql-migrate
COPY --chmod=755 entrypoint.sh /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

Expand Down
41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# db-migrate for docker

[Docker Hubh](https://hub.docker.com/r/pyar6329/sql-migrate)

This is wrap [rubenv/sql-migrate](https://github.com/rubenv/sql-migrate) using Docker

## migrate up
Expand Down Expand Up @@ -56,13 +54,33 @@ drop table users;
Besides, You run PostgreSQL.

```bash
$ docker run -d --rm --name "example-postgres" -e "POSTGRES_PASSWORD=postgres" -e "POSTGRES_DB=example" -p "5432:5432" -e "POSTGRES_INITDB_ARGS=--encoding=UTF-8 --locale=C.UTF-8" postgres:12.0
$ docker run -d \
--rm \
--name "example-postgres" \
-e "POSTGRES_PASSWORD=postgres" \
-e "POSTGRES_DB=example" \
-p "5432:5432" \
-e "POSTGRES_INITDB_ARGS=--encoding=UTF-8 --locale=C.UTF-8" \
postgres:14.11
```

You can run below, and apply migration

```bash
$ docker run --rm -v "$(pwd)/dbconfig.yml:/workspace/dbconfig.yml" -v "$(pwd)/migrations:/workspace/migrations" -e "DEV_DATABASE_HOST=host.docker.internal" -e "DEV_DATABASE_USER=postgres" -e "DEV_DATABASE_PASSWORD=postgres" -e "DEV_DATABASE_NAME=example" -e "DEV_DATABASE_PORT=5432" pyar6329/sql-migrate:latest up
$ [ "$(uname -s)" = "Linux" ] && export LINUX_ARGS="--add-host=host.docker.internal:host-gateway" || export LINUX_ARGS=""

$ docker run \
--rm \
-v "$(pwd)/dbconfig.yml:/app/dbconfig.yml" \
-v "$(pwd)/migrations:/app/migrations" \
-w "/app" \
${LINUX_ARGS} \
-e "DEV_DATABASE_HOST=host.docker.internal" \
-e "DEV_DATABASE_USER=postgres" \
-e "DEV_DATABASE_PASSWORD=postgres" \
-e "DEV_DATABASE_NAME=example" \
-e "DEV_DATABASE_PORT=5432" \
ghcr.io/pyar6329/sql-migrate:1.6.1 up
```

And, check schema;
Expand All @@ -77,6 +95,19 @@ Run below command, tables are dropped.


```bash
$ docker run --rm -v "$(pwd)/dbconfig.yml:/workspace/dbconfig.yml" -v "$(pwd)/migrations:/workspace/migrations" -e "DEV_DATABASE_HOST=host.docker.internal" -e "DEV_DATABASE_USER=postgres" -e "DEV_DATABASE_PASSWORD=postgres" -e "DEV_DATABASE_NAME=example" -e "DEV_DATABASE_PORT=5432" pyar6329/sql-migrate:latest down
$ [ "$(uname -s)" = "Linux" ] && export LINUX_ARGS="--add-host=host.docker.internal:host-gateway" || export LINUX_ARGS=""

$ docker run \
--rm \
-v "$(pwd)/dbconfig.yml:/app/dbconfig.yml" \
-v "$(pwd)/migrations:/app/migrations" \
-w "/app" \
--add-host=host.docker.internal:host-gateway \
-e "DEV_DATABASE_HOST=host.docker.internal" \
-e "DEV_DATABASE_USER=postgres" \
-e "DEV_DATABASE_PASSWORD=postgres" \
-e "DEV_DATABASE_NAME=example" \
-e "DEV_DATABASE_PORT=5432" \
ghcr.io/pyar6329/sql-migrate:1.6.1 down
```

File renamed without changes.
File renamed without changes.

0 comments on commit f7834d7

Please sign in to comment.