Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .app-image-tag
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.9.0.4
v0.9.0.5
18 changes: 15 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
.ash_history
.env
.irb_history
.irb_history
.irbrc
.rspec_status
.yardoc
**/.git
**/.github
**/*.sqlite3
**/*.sqlite3-*
.irbrc
.irb_history
.yardoc
# rspec failure tracking
coverage
docker
Dockerfile
gemfiles/*.lock
pkg
spec/reports
tmp
10 changes: 5 additions & 5 deletions .github/workflows/benchmark_cache_stores.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ jobs:
id: read_app_image_tag
run: echo "APP_IMAGE_TAG=$(head -n1 .app-image-tag)" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_primary.dockerfile
- name: Calculate SHA256 for docker/postgres_primary/Dockerfile
id: calculate_primary_sha
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary/Dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_replica.dockerfile
- name: Calculate SHA256 for docker/postgres_replica/Dockerfile
id: calculate_replica_sha
run: |
checksum () {
sha256sum $@ | awk '{print substr($1, 1, 12)}'
}
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica.dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/cmd.sh)
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica/Dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/start_server)
echo "REPLICA_SHA=$(echo \"${REPLICA_DOCKERFILE_SHA}-${REPLICA_CMD_SHA}\" | checksum)" >> $GITHUB_OUTPUT

benchmark_cache_stores:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/prebuild_postgres_primary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build Postgres Primary Image
on:
pull_request:
paths:
- 'docker/postgres_primary.dockerfile'
- 'docker/postgres_primary/Dockerfile'

jobs:
export_variables:
Expand All @@ -20,9 +20,9 @@ jobs:
id: compute_container_registry_name
run: echo "CR_NAME=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_primary.dockerfile
- name: Calculate SHA256 for docker/postgres_primary/Dockerfile
id: calculate_primary_sha
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary/Dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

prebuild_primary:
needs: export_variables
Expand All @@ -40,7 +40,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and tag primary Docker image
run: docker build -f docker/postgres_primary.dockerfile -t ${{ needs.export_variables.outputs.primary_image }} .
run: docker build -t ${{ needs.export_variables.outputs.primary_image }} docker/postgres_primary

- name: Push primary Docker image
run: docker push ${{ needs.export_variables.outputs.primary_image }}
12 changes: 6 additions & 6 deletions .github/workflows/prebuild_postgres_replica.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: Build Postgres Replica Image
on:
pull_request:
paths:
- 'docker/postgres_replica.dockerfile'
- 'docker/postgres_replica/cmd.sh'
- 'docker/postgres_replica/Dockerfile'
- 'docker/postgres_replica/start_server'

jobs:
export_variables:
Expand All @@ -21,14 +21,14 @@ jobs:
id: compute_container_registry_name
run: echo "CR_NAME=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_replica.dockerfile
- name: Calculate SHA256 for docker/postgres_replica/Dockerfile
id: calculate_replica_sha
run: |
checksum () {
sha256sum $@ | awk '{print substr($1, 1, 12)}'
}
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica.dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/cmd.sh)
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica/Dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/start_server)
echo "REPLICA_SHA=$(echo \"${REPLICA_DOCKERFILE_SHA}-${REPLICA_CMD_SHA}\" | checksum)" >> $GITHUB_OUTPUT

prebuild_replica:
Expand All @@ -47,7 +47,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and tag replica Docker image
run: docker build -f docker/postgres_replica.dockerfile -t ${{ needs.export_variables.outputs.replica_image }} .
run: docker build -t ${{ needs.export_variables.outputs.replica_image }} docker/postgres_replica

- name: Push replica Docker image
run: docker push ${{ needs.export_variables.outputs.replica_image }}
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ jobs:
id: read_app_image_tag
run: echo "APP_IMAGE_TAG=$(head -n1 .app-image-tag)" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_primary.dockerfile
- name: Calculate SHA256 for docker/postgres_primary/Dockerfile
id: calculate_primary_sha
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT
run: echo "PRIMARY_SHA=$(sha256sum docker/postgres_primary/Dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for docker/postgres_replica.dockerfile
- name: Calculate SHA256 for docker/postgres_replica/Dockerfile
id: calculate_replica_sha
run: |
checksum () {
sha256sum $@ | awk '{print substr($1, 1, 12)}'
}
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica.dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/cmd.sh)
REPLICA_DOCKERFILE_SHA=$(checksum docker/postgres_replica/Dockerfile)
REPLICA_CMD_SHA=$(checksum docker/postgres_replica/start_server)
echo "REPLICA_SHA=$(echo \"${REPLICA_DOCKERFILE_SHA}-${REPLICA_CMD_SHA}\" | checksum)" >> $GITHUB_OUTPUT

rubocop:
Expand Down
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
.ash_history
.env
.irb_history
# rspec failure tracking
.rspec_status
/_yardoc/
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
.env
gemfiles/*.lock
db/*.sqlite3
db/*.sqlite3-*
# rspec failure tracking
.rspec_status
.irb_history
.ash_history
gemfiles/*.lock
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ WORKDIR /app
RUN gem install bundler -v $(cat Gemfile.lock | grep "BUNDLED WITH" -A1 | tail -n1)
RUN bundle install
RUN bundle exec appraisal install

CMD [ "bin/exec-latest", "irb" ]
11 changes: 11 additions & 0 deletions bin/exec-latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh

# Execs a command against the latest Active Record version defined in the Appraisals file
# Usage example
# bin/exec-latest irb (starts an IRB session with the latest AR version)

set -euo pipefail

latest_ar_version=$(cat Appraisals | grep -oE "ar-\d+(.\d+)+" | tail -n1 | sed -E 's/(ar-)(.*)/\2/g')

bin/appraisal-exec $latest_ar_version "$@"
39 changes: 28 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ name: active_record_proxy_adapters
x-app-common: &app-common
image: active_record_proxy_adapters-app:${ENV_TAG:-latest}

x-healthcheck_common: &healthcheck-common
interval: 5s
timeout: 10s
retries: 30
Comment thread
mateuscruz marked this conversation as resolved.

x-postgres-common: &postgres-common
restart: always
user: postgres
healthcheck:
<<: *healthcheck-common
test: 'pg_isready -U postgres_primary_test --dbname=postgres'
interval: 10s
timeout: 5s
retries: 5
networks:
- postgres

Expand Down Expand Up @@ -51,9 +54,12 @@ services:
- MEMCACHED_PORT=11211
- MEMCACHED_URL=${MEMCACHED_URL:-${MEMCACHED_HOST:-memcached}:${MEMCACHED_PORT:-11211}}
depends_on:
- postgres_primary
- postgres_replica
- mariadb
postgres_primary:
condition: service_healthy
postgres_replica:
condition: service_healthy
mariadb:
condition: service_healthy
networks:
- app
- postgres
Expand All @@ -66,8 +72,7 @@ services:
postgres_primary:
<<: *postgres-common
build:
context: .
dockerfile: docker/postgres_primary.dockerfile
context: docker/postgres_primary
args:
- POSTGRES_LOGGING_COLLECTOR=${POSTGRES_LOGGING_COLLECTOR:-}
- POSTGRES_LOG_DESTINATION=${POSTGRES_LOG_DESTINATION:-}
Expand All @@ -81,30 +86,39 @@ services:
POSTGRES_PASSWORD: postgres_primary_test
POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
volumes:
- postgres_primary:/var/lib/postgresql/data

postgres_replica:
<<: *postgres-common
build:
context: .
dockerfile: docker/postgres_replica.dockerfile
context: docker/postgres_replica
container_name: postgres_replica
environment:
PGUSER: replicator
PGPASSWORD: replicator
PRIMARY_DATABASE_HOST: postgres_primary
volumes:
- postgres_replica:/var/lib/postgresql/data
depends_on:
- postgres_primary
postgres_primary:
condition: service_healthy
# TODO: create mysql replica images
mariadb:
image: mariadb:11.4
container_name: mysql_primary
environment:
MARIADB_ROOT_PASSWORD: mysql
MARIADB_DATABASE: mysql
volumes:
- mariadb:/var/lib/mysql
ports:
- "3306:3306"
networks:
- mariadb
healthcheck:
<<: *healthcheck-common
test: 'mariadb-admin -pmysql ping'

redis:
image: redis:latest
Expand Down Expand Up @@ -146,4 +160,7 @@ networks:

volumes:
bundle:
mariadb:
postgres_primary:
postgres_replica:
yard:
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ ENV PRIMARY_DATABASE_PORT=5432
ENV PRIMARY_REPLICATION_SLOT=replication_slot


COPY docker/postgres_replica/cmd.sh cmd.sh
COPY start_server start_server
Comment thread
mateuscruz marked this conversation as resolved.

USER root
RUN chown -R postgres:postgres cmd.sh
RUN chown -R postgres:postgres start_server
USER postgres
RUN chmod u+x cmd.sh
RUN chmod u+x start_server

CMD [ "./cmd.sh" ]
CMD [ "./start_server" ]
9 changes: 0 additions & 9 deletions docker/postgres_replica/cmd.sh

This file was deleted.

35 changes: 35 additions & 0 deletions docker/postgres_replica/start_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env sh

set -e

# Path to the PGDATA directory used inside the container
DATA_DIR="/var/lib/postgresql/data"

# Ensure the data directory exists
mkdir -p "$DATA_DIR"

# If the data directory is empty, perform an initial base backup from the primary.
# We intentionally DO NOT delete existing data so that container restarts keep the
# current replica state and avoid a full re-copy unless explicitly desired.
if [ -z "$(ls -A "$DATA_DIR" 2>/dev/null)" ]; then
echo "[replica-init] Data directory is empty - starting pg_basebackup from primary..."
# Retry until the primary becomes available.
until pg_basebackup \
--pgdata="$DATA_DIR" \
-R \
--slot="${PRIMARY_REPLICATION_SLOT}" \
--host="${PRIMARY_DATABASE_HOST}" \
--port="${PRIMARY_DATABASE_PORT}"; do
echo "[replica-init] Primary not ready yet, retrying in 1s..."
sleep 1
done
echo "[replica-init] Base backup completed."
else
echo "[replica-init] Data directory not empty - skipping pg_basebackup."
fi

# Tighten permissions (required by PostgreSQL if not already 0700)
chmod 0700 "$DATA_DIR" || true

echo "[replica-init] Starting PostgreSQL replica..."
exec postgres