FROM debian:bullseye-slim

RUN apt-get update && apt-get install -y curl wget gnupg2 lsb-release

RUN ARCH=$(dpkg --print-architecture); \
    case ${ARCH} in \
        amd64) DBMATE_ARCH="linux-amd64" ;; \
        arm64) DBMATE_ARCH="linux-arm64" ;; \
        *) echo "Unsupported architecture: ${ARCH}"; exit 1 ;; \
    esac && \
    curl -fsSL -o /usr/local/bin/dbmate \
    https://github.com/amacneil/dbmate/releases/latest/download/dbmate-${DBMATE_ARCH} && \
    chmod +x /usr/local/bin/dbmate

RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y postgresql-client-%VERSION%

ENV PATH="/usr/lib/postgresql/%VERSION%/bin:${PATH}"

RUN dbmate --version

ENTRYPOINT ["dbmate"]
