diff --git a/infra/build/postgres/Dockerfile b/infra/build/postgres/Dockerfile index 3b4470c89..d35c5f8e3 100644 --- a/infra/build/postgres/Dockerfile +++ b/infra/build/postgres/Dockerfile @@ -1,9 +1,9 @@ -ARG IMAGE_TAG='15.4.0-debian-11-r45' +ARG IMAGE_TAG='15.5.0-debian-11-r15' FROM bitnami/postgresql:${IMAGE_TAG} ARG PG_MAIN_VERSION=15 -ARG PG_GRAPHQL_VERSION=1.3.0 +ARG PG_GRAPHQL_VERSION=1.4.2 ARG TARGETARCH USER root @@ -23,16 +23,22 @@ RUN set -eux; \ ;; \ esac; -RUN apt update && apt install -y curl +RUN apt update && apt install curl -y # Install precompiled pg_graphql extensions RUN curl -L "https://github.com/supabase/pg_graphql/releases/download/v${PG_GRAPHQL_VERSION}/pg_graphql-v${PG_GRAPHQL_VERSION}-pg${PG_MAIN_VERSION}-${TARGETARCH}-linux-gnu.deb" -o pg_graphql.deb RUN dpkg --install pg_graphql.deb -RUN cp /usr/share/postgresql/15/extension/pg_graphql* /opt/bitnami/postgresql/share/extension/ -RUN cp /usr/lib/postgresql/15/lib/pg_graphql* /opt/bitnami/postgresql/lib/ +RUN cp /usr/share/postgresql/${PG_MAIN_VERSION}/extension/pg_graphql* /opt/bitnami/postgresql/share/extension/ +RUN cp /usr/lib/postgresql/${PG_MAIN_VERSION}/lib/pg_graphql* /opt/bitnami/postgresql/lib/ + +# Install precompiled supabase wrappers extensions +RUN curl -L "https://github.com/supabase/wrappers/releases/download/v${WRAPPERS_VERSION}/wrappers-v${WRAPPERS_VERSION}-pg${PG_MAIN_VERSION}-${TARGETARCH}-linux-gnu.deb" -o wrappers.deb +RUN dpkg --install wrappers.deb +RUN cp /usr/share/postgresql/${PG_MAIN_VERSION}/extension/wrappers* /opt/bitnami/postgresql/share/extension/ +RUN cp /usr/lib/postgresql/${PG_MAIN_VERSION}/lib/wrappers* /opt/bitnami/postgresql/lib/ COPY ./infra/build/postgres/init.sql /docker-entrypoint-initdb.d/ USER 1001 ENTRYPOINT [ "/opt/bitnami/scripts/postgresql/entrypoint.sh" ] -CMD [ "/opt/bitnami/scripts/postgresql/run.sh" ] \ No newline at end of file +CMD [ "/opt/bitnami/scripts/postgresql/run.sh" ] diff --git a/infra/dev/postgres/Dockerfile b/infra/dev/postgres/Dockerfile index aa2114caf..335dff028 100644 --- a/infra/dev/postgres/Dockerfile +++ b/infra/dev/postgres/Dockerfile @@ -1,9 +1,10 @@ -ARG PG_MAIN_VERSION=14 +ARG PG_MAIN_VERSION=15.5 -FROM postgres:${PG_MAIN_VERSION} as postgres +FROM postgres:${PG_MAIN_VERSION}-bullseye as postgres -ARG PG_MAIN_VERSION +ARG PG_MAIN_VERSION=15 ARG PG_GRAPHQL_VERSION=1.4.2 +ARG WRAPPERS_VERSION=0.2.0 ARG TARGETARCH RUN set -eux; \ @@ -21,10 +22,14 @@ RUN set -eux; \ ;; \ esac; -RUN apt update && apt install -y curl +RUN apt update && apt install curl -y # Install precompiled pg_graphql extensions RUN curl -L "https://github.com/supabase/pg_graphql/releases/download/v${PG_GRAPHQL_VERSION}/pg_graphql-v${PG_GRAPHQL_VERSION}-pg${PG_MAIN_VERSION}-${TARGETARCH}-linux-gnu.deb" -o pg_graphql.deb RUN dpkg --install pg_graphql.deb +# Install precompiled supabase wrappers extensions +RUN curl -L "https://github.com/supabase/wrappers/releases/download/v${WRAPPERS_VERSION}/wrappers-v${WRAPPERS_VERSION}-pg${PG_MAIN_VERSION}-${TARGETARCH}-linux-gnu.deb" -o wrappers.deb +RUN dpkg --install wrappers.deb + COPY init.sql /docker-entrypoint-initdb.d/ diff --git a/server/scripts/setup-db.ts b/server/scripts/setup-db.ts index 5a36c2889..45cbd9af7 100644 --- a/server/scripts/setup-db.ts +++ b/server/scripts/setup-db.ts @@ -1,6 +1,6 @@ import console from 'console'; -import { connectionSource, performQuery } from './utils'; +import { camelToSnakeCase, connectionSource, performQuery } from './utils'; connectionSource .initialize() @@ -18,7 +18,7 @@ connectionSource 'create schema "core"', ); await performQuery( - 'CREATE EXTENSION IF NOT EXISTS pg_graphql', + 'CREATE EXTENSION IF NOT EXISTS "pg_graphql"', 'create extension pg_graphql', ); @@ -27,6 +27,39 @@ connectionSource 'create extension "uuid-ossp"', ); + await performQuery( + 'CREATE EXTENSION IF NOT EXISTS "postgres_fdw"', + 'create extension "postgres_fdw"', + ); + + await performQuery( + 'CREATE EXTENSION IF NOT EXISTS "wrappers"', + 'create extension "wrappers"', + ); + + const supabaseWrappers = [ + 'airtable', + 'bigQuery', + 'clickHouse', + 'firebase', + 'logflare', + 's3', + 'stripe', + ]; // See https://supabase.github.io/wrappers/ + + for (const wrapper of supabaseWrappers) { + await performQuery( + ` + CREATE FOREIGN DATA WRAPPER "${wrapper.toLowerCase()}_fdw" + HANDLER "${camelToSnakeCase(wrapper)}_fdw_handler" + VALIDATOR "${camelToSnakeCase(wrapper)}_fdw_validator"; + `, + `create ${wrapper} "wrappers"`, + true, + true, + ); + } + await performQuery( `COMMENT ON SCHEMA "core" IS '@graphql({"inflect_names": true})';`, 'inflect names for graphql', diff --git a/server/scripts/utils.ts b/server/scripts/utils.ts index 63fb11946..f944c4231 100644 --- a/server/scripts/utils.ts +++ b/server/scripts/utils.ts @@ -7,22 +7,36 @@ import { DataSource } from 'typeorm'; config(); const configService = new ConfigService(); + export const connectionSource = new DataSource({ type: 'postgres', logging: false, url: configService.get('PG_DATABASE_URL'), }); +export const camelToSnakeCase = (str) => + str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); + export const performQuery = async ( query: string, consoleDescription: string, withLog = true, + ignoreAlreadyExistsError = false, ) => { try { const result = await connectionSource.query(query); + withLog && console.log(`Performed '${consoleDescription}' successfully`); + return result; } catch (err) { - withLog && console.error(`Failed to perform '${consoleDescription}':`, err); + let message = ''; + + if (ignoreAlreadyExistsError && `${err}`.includes('already exists')) { + message = `Performed '${consoleDescription}' successfully`; + } else { + message = `Failed to perform '${consoleDescription}': ${err}`; + } + withLog && console.error(message); } };