Skip to content

Commit

Permalink
Update to Babelfish 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpotts committed Sep 16, 2022
1 parent 552585b commit b748b44
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*]
end_of_line = lf
tab_width = 2
indent_style = tab
131 changes: 83 additions & 48 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,84 +1,119 @@
# Build stage
FROM alpine:latest
FROM ubuntu:20.04
ARG BABELFISH_VERSION=BABEL_2_1_1__PG_14_3
ENV DEBIAN_FRONTEND=noninteractive
ENV BABELFISH_HOME=/opt/babelfish

# Install build dependencies
RUN apk add git icu-dev libxml2-dev openssl openssl-dev python3-dev openjdk8-jre pkgconf \
perl g++ gcc libc-dev linux-headers make cmake bison flex util-linux-dev \
&& apk add libpq-dev --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main \
&& apk add ossp-uuid-dev --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing

# Clone babelfish sources
RUN apt update && apt install -y --no-install-recommends \
build-essential flex libxml2-dev libxml2-utils \
libxslt-dev libssl-dev libreadline-dev zlib1g-dev \
libldap2-dev libpam0g-dev gettext uuid uuid-dev \
cmake lld apt-utils libossp-uuid-dev gnulib bison \
xsltproc icu-devtools libicu66 \
libicu-dev gawk \
curl openjdk-8-jre openssl \
g++ libssl-dev python-dev libpq-dev \
pkg-config libutfcpp-dev \
gnupg unixodbc-dev net-tools unzip wget

# Download babelfish sources
WORKDIR /workplace

RUN git clone https://github.com/babelfish-for-postgresql/postgresql_modified_for_babelfish.git \
&& git clone https://github.com/babelfish-for-postgresql/babelfish_extensions.git

# Build Postgres engine
WORKDIR /workplace/postgresql_modified_for_babelfish
RUN wget https://github.com/babelfish-for-postgresql/babelfish-for-postgresql/releases/download/${BABELFISH_VERSION}/${BABELFISH_VERSION}.tar.gz

RUN ./configure --without-readline --without-zlib --enable-debug CFLAGS="-ggdb" \
--with-libxml --with-uuid=ossp --with-icu
RUN tar -xvzf ${BABELFISH_VERSION}.tar.gz

RUN make && make install
# Set environment variables
ENV PG_SRC=/workplace/${BABELFISH_VERSION}

WORKDIR /workplace/postgresql_modified_for_babelfish/contrib
WORKDIR ${PG_SRC}

RUN make && make install
ENV PG_CONFIG=${BABELFISH_HOME}/bin/pg_config

# Build antlr4
WORKDIR /workplace/babelfish_extensions/contrib/babelfishpg_tsql/antlr/thirdparty/antlr/
# Compile ANTLR 4
ENV ANTLR4_VERSION=4.9.3
ENV ANTLR4_JAVA_BIN=/usr/bin/java
ENV ANTLR4_RUNTIME_LIBRARIES=/usr/include/antlr4-runtime
ENV ANTLR_EXECUTABLE=/usr/local/lib/antlr-${ANTLR4_VERSION}-complete.jar
ENV ANTLR_RUNTIME=/workplace/antlr4

RUN cp antlr-4.9.2-complete.jar /usr/local/lib
RUN cp ${PG_SRC}/contrib/babelfishpg_tsql/antlr/thirdparty/antlr/antlr-${ANTLR4_VERSION}-complete.jar /usr/local/lib

WORKDIR /workplace

RUN wget http://www.antlr.org/download/antlr4-cpp-runtime-4.9.2-source.zip
RUN unzip -d antlr4 antlr4-cpp-runtime-4.9.2-source.zip
RUN mkdir antlr4/build
RUN wget http://www.antlr.org/download/antlr4-cpp-runtime-${ANTLR4_VERSION}-source.zip
RUN unzip -d ${ANTLR_RUNTIME} antlr4-cpp-runtime-${ANTLR4_VERSION}-source.zip

WORKDIR /workplace/antlr4/build
WORKDIR ${ANTLR_RUNTIME}/build

RUN cmake .. -DANTLR_JAR_LOCATION=/usr/local/lib/antlr-4.9.2-complete.jar -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_DEMO=True
RUN cmake .. -D ANTLR_JAR_LOCATION=/usr/local/lib/antlr-${ANTLR4_VERSION}-complete.jar -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_DEMO=True
RUN make && make install
RUN cp /usr/local/lib/libantlr4-runtime.so.4.9.2 /usr/local/pgsql/lib

# Set environment variables for building extensions
ENV PG_CONFIG=/usr/local/pgsql/bin/pg_config
ENV PG_SRC=/workplace/postgresql_modified_for_babelfish
ENV cmake=/usr/bin/cmake
# Build modified PostgreSQL for Babelfish
WORKDIR ${PG_SRC}

# Build extensions
WORKDIR /workplace/babelfish_extensions/contrib/babelfishpg_money
RUN make && make install
RUN ./configure CFLAGS="-ggdb" \
--prefix=${BABELFISH_HOME}/ \
--enable-debug \
--with-ldap \
--with-libxml \
--with-pam \
--with-uuid=ossp \
--enable-nls \
--with-libxslt \
--with-icu

RUN make DESTDIR=${BABELFISH_HOME}/ 2>error.txt && make install

WORKDIR /workplace/babelfish_extensions/contrib/babelfishpg_common
RUN make && make install
WORKDIR ${PG_SRC}/contrib

WORKDIR /workplace/babelfish_extensions/contrib/babelfishpg_tds
RUN make && make install

WORKDIR /workplace/babelfish_extensions/contrib/babelfishpg_tsql
RUN make && make install
# Compile the ANTLR parser generator
RUN cp /usr/local/lib/libantlr4-runtime.so.${ANTLR4_VERSION} ${BABELFISH_HOME}/lib

WORKDIR ${PG_SRC}/contrib/babelfishpg_tsql/antlr
RUN cmake -Wno-dev .
RUN make all

# Compile the contrib modules and build Babelfish
WORKDIR ${PG_SRC}/contrib/babelfishpg_common
RUN make && make PG_CONFIG=${PG_CONFIG} install

WORKDIR ${PG_SRC}/contrib/babelfishpg_money
RUN make && make PG_CONFIG=${PG_CONFIG} install

WORKDIR ${PG_SRC}/contrib/babelfishpg_tds
RUN make && make PG_CONFIG=${PG_CONFIG} install

WORKDIR ${PG_SRC}/contrib/babelfishpg_tsql
RUN make && make PG_CONFIG=${PG_CONFIG} install

# Run stage
FROM alpine:latest
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
ENV BABELFISH_HOME=/opt/babelfish

# Copy binaries to run stage
WORKDIR /usr/local/pgsql
COPY --from=0 /usr/local/pgsql .
WORKDIR ${BABELFISH_HOME}
COPY --from=0 ${BABELFISH_HOME} .

# Install runtime dependencies
RUN apk add libxml2 icu openssl libuuid \
&& apk add ossp-uuid --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing

# Create postgres user
RUN addgroup -S postgres && adduser -S postgres -G postgres
RUN apt update && apt install -y --no-install-recommends \
libssl1.1 openssl libldap-2.4-2 libxml2 libpam0g uuid libossp-uuid16 \
libxslt1.1 libicu66 libpq5 unixodbc

# Enable data volume
ENV BABELFISH_DATA=/data/babelfish
RUN mkdir /data
VOLUME /data
RUN mkdir /data/postgres
RUN chown postgres /data/postgres
RUN mkdir ${BABELFISH_DATA}

# Create postgres user
RUN adduser postgres --home ${BABELFISH_DATA}
RUN chown postgres ${BABELFISH_DATA}
RUN chmod 750 ${BABELFISH_DATA}

# Change to postgres user
USER postgres
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Jonathan Potts
Copyright (c) Jonathan Potts

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The default login for Babelfish is:

If you specified a custom username and/or password, use those instead.

Babelfish does not currently support the SSMS Object Explorer. If you are using SSMS, you must connect via a **New Query**.
Many features in SQL Server Management Studio (SSMS) are currently unsupported.

### Connection string

Expand Down
6 changes: 3 additions & 3 deletions example_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TABLE example_db.authors (
[name] NVARCHAR (MAX) NOT NULL,
);

CREATE TABLE example_db.Books (
CREATE TABLE example_db.books (
book_id INT IDENTITY PRIMARY KEY,
title NVARCHAR (MAX) NOT NULL,
author_id INT NOT NULL,
Expand All @@ -23,8 +23,8 @@ GO

-- Seed tables
INSERT INTO example_db.authors ([name]) VALUES
('Kristin Hannah'),
('Andy Weir');
('Kristin Hannah'),
('Andy Weir');

INSERT INTO example_db.books (title, author_id, publish_date, price) VALUES
('The Four Winds', 1, '02-02-2021 00:00:00', 14.99),
Expand Down
69 changes: 43 additions & 26 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/sh
BABELFISH_HOME=/opt/babelfish
BABELFISH_DATA=/data/babelfish

cd /usr/local/pgsql/bin
cd ${BABELFISH_HOME}/bin

# Set default argument values
USERNAME=babelfish_user
Expand All @@ -9,33 +11,48 @@ DATABASE=babelfish_db
MIGRATION_MODE=single-db

# Populate argument values from command
while getopts u:p:d:m: flag
do
case "${flag}" in
u) USERNAME=${OPTARG};;
p) PASSWORD=${OPTARG};;
d) DATABASE=${OPTARG};;
m) MIGRATION_MODE=${OPTARG};;
esac
while getopts u:p:d:m: flag; do
case "${flag}" in
u) USERNAME=${OPTARG};;
p) PASSWORD=${OPTARG};;
d) DATABASE=${OPTARG};;
m) MIGRATION_MODE=${OPTARG};;
esac
done

# Initialize database cluster if it does not exist
[ ! -f /data/postgres/postgresql.conf ] && ./initdb -D /data/postgres \
&& printf "# Allow all connections\nhost\tall\t\tall\t\t0.0.0.0/0\t\tmd5\nhost\tall\t\tall\t\t::0/0\t\t\tmd5\n" >> /data/postgres/pg_hba.conf \
&& printf "\n# Configure babelfish\nshared_preload_libraries = 'babelfishpg_tds'\n" >> /data/postgres/postgresql.conf \
&& ./pg_ctl -D /data/postgres start \
&& ./psql -c "CREATE USER ${USERNAME} WITH CREATEDB CREATEROLE PASSWORD '${PASSWORD}' INHERIT;" \
-c "DROP DATABASE IF EXISTS ${DATABASE};" \
-c "CREATE DATABASE ${DATABASE} OWNER ${USERNAME};" \
-c "\c ${DATABASE}" \
-c "CREATE EXTENSION IF NOT EXISTS \"babelfishpg_tds\" CASCADE;" \
-c "GRANT ALL ON SCHEMA sys to ${USERNAME};" \
-c "ALTER SYSTEM SET babelfishpg_tsql.database_name = '${DATABASE}';" \
-c "ALTER SYSTEM SET babelfishpg_tds.set_db_session_property = true;" \
-c "ALTER DATABASE ${DATABASE} SET babelfishpg_tsql.migration_mode = '${MIGRATION_MODE}';" \
-c "SELECT pg_reload_conf();" \
-c "CALL SYS.INITIALIZE_BABELFISH('${USERNAME}');" \
&& ./pg_ctl -D /data/postgres stop
if [ ! -f ${BABELFISH_DATA}/postgresql.conf ]; then
./initdb -D ${BABELFISH_DATA}/ -E "UTF8"
cat <<- EOF >> ${BABELFISH_DATA}/pg_hba.conf
# Allow all connections
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
EOF
cat <<- EOF >> ${BABELFISH_DATA}/postgresql.conf
#------------------------------------------------------------------------------
# BABELFISH RELATED OPTIONS
# These are going to step over previous duplicated variables.
#------------------------------------------------------------------------------
listen_addresses = '*'
allow_system_table_mods = on
shared_preload_libraries = 'babelfishpg_tds'
babelfishpg_tds.listen_addresses = '*'
EOF
./pg_ctl -D ${BABELFISH_DATA}/ start
./psql -c "CREATE USER ${USERNAME} WITH SUPERUSER CREATEDB CREATEROLE PASSWORD '${PASSWORD}' INHERIT;" \
-c "DROP DATABASE IF EXISTS ${DATABASE};" \
-c "CREATE DATABASE ${DATABASE} OWNER ${USERNAME};" \
-c "\c ${DATABASE}" \
-c "CREATE EXTENSION IF NOT EXISTS \"babelfishpg_tds\" CASCADE;" \
-c "GRANT ALL ON SCHEMA sys to ${USERNAME};" \
-c "ALTER USER ${USERNAME} CREATEDB;" \
-c "ALTER SYSTEM SET babelfishpg_tsql.database_name = '${DATABASE}';" \
-c "SELECT pg_reload_conf();" \
-c "ALTER DATABASE ${DATABASE} SET babelfishpg_tsql.migration_mode = '${MIGRATION_MODE}';" \
-c "SELECT pg_reload_conf();" \
-c "CALL SYS.INITIALIZE_BABELFISH('${USERNAME}');"
./pg_ctl -D ${BABELFISH_DATA}/ stop
fi

# Start postgres engine
./postgres -D /data/postgres -i
./postgres -D ${BABELFISH_DATA}/ -i

0 comments on commit b748b44

Please sign in to comment.