diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ebba017..13180100 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,6 +61,18 @@ jobs: backend: ODBC runner: ubuntu-24.04 unity_build: false + - lib_type: shared + backend: MSSQL + runner: ubuntu-24.04 + unity_build: false + mssql_driver: msodbcsql18 + name: MS SQL (Microsoft driver) + - lib_type: shared + backend: MSSQL + runner: ubuntu-24.04 + unity_build: false + mssql_driver: freetds + name: MS SQL (FreeTDS driver) - lib_type: shared backend: Empty runner: ubuntu-24.04 @@ -192,6 +204,9 @@ jobs: if [ "${{matrix.mariadb}}" = true ]; then set_env_var SOCI_MYSQL_USE_MARIADB YES fi + if [ -n "${{matrix.mssql_driver}}" ]; then + set_env_var SOCI_MSSQL_DRIVER ${{matrix.mssql_driver}} + fi # Ensure SOCI_CI_BACKEND is always lowercase set_env_var SOCI_CI_BACKEND "$( echo "$SOCI_CI_BACKEND" | tr '[:upper:]' '[:lower:]' )" diff --git a/scripts/ci/before_build_mssql.sh b/scripts/ci/before_build_mssql.sh new file mode 100755 index 00000000..d216c309 --- /dev/null +++ b/scripts/ci/before_build_mssql.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# Sets up MS SQL Server database for SOCI in CI builds +# +# Copyright (c) 2026 Vadim Zeitlin +# +source ${SOCI_SOURCE_DIR}/scripts/ci/common.sh + +echo "Creating the ${MSSQL_DB} database:" +echo "create database ${MSSQL_DB}" | mssql_sql master + +echo 'Testing connection to the database:' +echo 'select @@version, db_name(), suser_name()' | mssql_sql diff --git a/scripts/ci/build_mssql.sh b/scripts/ci/build_mssql.sh new file mode 100755 index 00000000..1e5ae46c --- /dev/null +++ b/scripts/ci/build_mssql.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Builds SOCI ODBC backend for testing with MS SQL Server in CI builds +# +# Copyright (c) 2026 Vadim Zeitlin +# +source ${SOCI_SOURCE_DIR}/scripts/ci/common.sh + +# Disable ASAN -> see https://github.com/SOCI/soci/issues/1008 +cmake ${SOCI_DEFAULT_CMAKE_OPTIONS} \ + -DSOCI_ASAN=OFF \ + -DSOCI_ODBC=ON \ + -DSOCI_ODBC_TEST_MSSQL_CONNSTR:STRING="$(mssql_connstr)" \ + .. + +run_make diff --git a/scripts/ci/install_mssql.sh b/scripts/ci/install_mssql.sh new file mode 100755 index 00000000..a09415b4 --- /dev/null +++ b/scripts/ci/install_mssql.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# Install MS SQL Server and the ODBC driver for it for SOCI in CI builds +# +# Copyright (c) 2026 Vadim Zeitlin +# +source ${SOCI_SOURCE_DIR}/scripts/ci/common.sh + +# Start the server first, as it takes a while to become available, and install +# the client side packages while it's starting up: we can't wait for it before +# doing it anyhow, as we use isql from unixODBC to check for its availability. +docker run --name ${MSSQL_CONTAINER} --detach --publish 1433:1433 \ + --env ACCEPT_EULA=Y --env MSSQL_SA_PASSWORD="${MSSQL_PASSWORD}" \ + ${SOCI_MSSQL_IMAGE} + +# Remove buggy versions of the packages from Microsoft repositories as well as +# their dependencies (which include the MS ODBC driver, if it is preinstalled, +# but we reinstall it below if we need it). +run_apt remove \ + libodbc1 odbcinst1debian2 \ + unixodbc unixodbc-dev + +packages_to_install="unixodbc unixodbc-dev" + +if [ -z "$DEBUGINFOD_URLS" ]; then + packages_to_install="$packages_to_install unixodbc-dbgsym" +fi + +case "${SOCI_MSSQL_DRIVER}" in + freetds) + packages_to_install="$packages_to_install tdsodbc" + ;; +esac + +run_apt install ${packages_to_install} + +case "${SOCI_MSSQL_DRIVER}" in + msodbcsql18) + # This driver is only available from the Microsoft repository, so add + # it, but pin all the packages in it to a lower than default priority + # to ensure that we keep using the distribution versions of unixODBC + # packages removed above. + sudo mkdir -p /etc/apt/keyrings + wget -q -O - https://packages.microsoft.com/keys/microsoft.asc | \ + sudo tee /etc/apt/keyrings/microsoft.asc > /dev/null + + ubuntu_release=$(lsb_release --release --short) + echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/microsoft.asc] https://packages.microsoft.com/ubuntu/${ubuntu_release}/prod $(lsb_release --codename --short) main" | \ + sudo tee /etc/apt/sources.list.d/mssql-release.list > /dev/null + + printf 'Package: *\nPin: origin packages.microsoft.com\nPin-Priority: 400\n' | \ + sudo tee /etc/apt/preferences.d/microsoft > /dev/null + + run_apt update + + # Installing this package requires accepting the EULA and run_apt + # doesn't preserve the environment, so run apt-get directly here. + sudo ACCEPT_EULA=Y apt-get $SOCI_APT_OPTIONS install msodbcsql18 + ;; + + freetds) + # The package registers the driver with unixODBC on its own, but does + # it using just the library name and not its full path, which can + # result in "Can't open lib 'libtdsodbc.so' : file not found" errors, + # so register it again using the full path to be on the safe side. + tdsodbc_lib=$(dpkg -L tdsodbc | grep -F 'libtdsodbc.so') + tdsodbc_ini=$(mktemp) + sed -e "s@^Driver.*=.*@Driver=${tdsodbc_lib}@" \ + /usr/share/tdsodbc/odbcinst.ini > ${tdsodbc_ini} + sudo odbcinst -i -d -f ${tdsodbc_ini} + rm ${tdsodbc_ini} + ;; +esac + +echo 'Available ODBC drivers:' +echo '---------------------------------- >8 --------------------------------------' +odbcinst -q -d +echo '---------------------------------- >8 --------------------------------------' + +echo 'Waiting for SQL Server startup...' +num_tries=1 +wait_time=3 # seconds +while true; do + if echo 'select @@version' | mssql_sql master > /dev/null 2>&1; then + echo 'SQL Server is available now' + break + fi + + if [[ $num_tries -gt 60 ]]; then + echo 'Timed out waiting for SQL Server startup, server log follows:' + docker logs ${MSSQL_CONTAINER} + exit 1 + fi + + sleep $wait_time + ((num_tries++)) +done diff --git a/scripts/ci/mssql.sh b/scripts/ci/mssql.sh new file mode 100644 index 00000000..15d1776f --- /dev/null +++ b/scripts/ci/mssql.sh @@ -0,0 +1,76 @@ +# Definitions used by SOCI when testing MS SQL Server in CI builds +# +# Copyright (c) 2026 Vadim Zeitlin +# +# Notice that this file is not executable, it is supposed to be sourced from +# the other files. + +# Docker image used for the server, override it to test with another version. +: ${SOCI_MSSQL_IMAGE:=mcr.microsoft.com/mssql/server:2025-latest} + +# This is arbitrary. +export MSSQL_CONTAINER=soci-mssql + +# "sa" is the only account existing in a freshly created container and the +# password must satisfy the (not configurable) SQL Server password policy. +export MSSQL_USER=sa +export MSSQL_PASSWORD='Password12!' + +# Database created by before_build_mssql.sh and used by the tests. +export MSSQL_DB=soci_test + +# ODBC driver to use. +case "${SOCI_MSSQL_DRIVER}" in + msodbcsql18) + # This driver uses the "host,port" syntax and refuses to connect to the + # server using the self-signed certificate unless explicitly told to. + mssql_odbc_driver='ODBC Driver 18 for SQL Server' + mssql_odbc_server='SERVER=127.0.0.1,1433;TrustServerCertificate=yes' + ;; + + freetds) + # This one uses a separate PORT. Specifying the protocol version is + # not really required, as it defaults to "auto" since FreeTDS 1.0, but + # pinning it avoids the round trips of the version negotiation. + mssql_odbc_driver='FreeTDS' + mssql_odbc_server='SERVER=127.0.0.1;PORT=1433;TDS_Version=7.4' + ;; + + '') + echo "SOCI_MSSQL_DRIVER must be set in the CI script." >&2 + exit 1 + ;; + + *) + echo "Unknown MS SQL ODBC driver \"${SOCI_MSSQL_DRIVER}\"." >&2 + exit 1 +esac + +# Return the ODBC connection string for the database given as the argument or +# for the database used by the tests if it is not specified. +mssql_connstr() +{ + echo "DRIVER={${mssql_odbc_driver}};${mssql_odbc_server};DATABASE=${1:-${MSSQL_DB}};UID=${MSSQL_USER};PWD=${MSSQL_PASSWORD}" +} + +# Execute the SQL statements given on stdin using the ODBC driver being tested, +# connecting to the database given as the argument (or the test one by default). +mssql_sql() +{ + # Note that isql exit code is 0 even if the SQL statements failed, so we + # have to also examine its output to detect the errors. + if ! mssql_sql_output=$(isql -b -v -k "$(mssql_connstr "$@")" 2>&1); then + echo "${mssql_sql_output}" + return 1 + fi + + echo "${mssql_sql_output}" + + case "${mssql_sql_output}" in + *'[ISQL]ERROR'*) + return 1 + ;; + esac + + return 0 +} diff --git a/scripts/ci/test_mssql.sh b/scripts/ci/test_mssql.sh new file mode 100755 index 00000000..d053f0f8 --- /dev/null +++ b/scripts/ci/test_mssql.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Tests SOCI ODBC backend with MS SQL Server in CI builds +# +# Copyright (c) 2026 Vadim Zeitlin +# +source ${SOCI_SOURCE_DIR}/scripts/ci/common.sh + +# Only the MS SQL test can be run here, the other ODBC tests are built but +# require the databases which are not available in this job. +LSAN_OPTIONS=suppressions=${SOCI_SOURCE_DIR}/scripts/suppressions/lsan-odbc run_test -R soci_odbc_test_mssql