Merge branches 'ci-all' and 'mssql-fixes'

Improve CI workflows and fix tests when using SQL Server.

See #1371 and #1372.
This commit is contained in:
Vadim Zeitlin
2026-09-01 02:48:20 +02:00
12 changed files with 286 additions and 17 deletions

View File

@@ -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:]' )"

View File

@@ -115,7 +115,7 @@ build_script:
- cmake --build build --config %CONFIGURATION% -- %BUILD_TOOL_OPTIONS%
test_script:
- ctest -V --timeout 300 --output-on-failure --build-config %CONFIGURATION% --exclude-regex "%SOCI_ODBC_SKIP_TESTS%"
- ctest --test-dir build -V --timeout 300 --output-on-failure --no-tests=error --build-config %CONFIGURATION% --exclude-regex "%SOCI_ODBC_SKIP_TESTS%"
# Uncomment this to wait for RDP connection after the build end.
#on_finish:

View File

@@ -309,6 +309,11 @@ private:
// First row with the error for bulk operations or -1.
int error_row_ = -1;
// True if the last executed statement returned a result set, false if it
// didn't return anything (e.g. because it was an UPDATE) or wasn't
// executed yet.
bool hasResultSet_ = false;
};
struct SOCI_ODBC_DECL odbc_rowid_backend : details::rowid_backend

View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Sets up MS SQL Server database for SOCI in CI builds
#
# Copyright (c) 2026 Vadim Zeitlin <vz-soci@zeitlins.org>
#
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

15
scripts/ci/build_mssql.sh Executable file
View File

@@ -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 <vz-soci@zeitlins.org>
#
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

View File

@@ -7,8 +7,12 @@
source ${SOCI_SOURCE_DIR}/scripts/ci/common.sh
# Note that we don't use the default options here, as we don't want to turn
# off all the backends (nor to enable ASAN which is incompatible with Valgrind).
# off all the backends. We also have to disable the sanitizers which are
# enabled for all the other builds, as ASAN is incompatible with Valgrind and
# UBSAN is not useful when running under it.
cmake ${SOCI_COMMON_CMAKE_OPTIONS} \
-DSOCI_ASAN=OFF \
-DSOCI_UBSAN=OFF \
-DSOCI_ODBC=OFF \
..

View File

@@ -70,9 +70,14 @@ if [ -n "${SOCI_BUILD_STATIC}" ]; then
SOCI_COMMON_CMAKE_OPTIONS="${SOCI_COMMON_CMAKE_OPTIONS} -DSOCI_SHARED=OFF"
fi
# These options are defaults and used by most builds, but not Valgrind one.
# Sanitizers are enabled for all builds except the Valgrind one, so enable them
# here and disable them just for Valgrind in its script.
SOCI_COMMON_CMAKE_OPTIONS="$SOCI_COMMON_CMAKE_OPTIONS -DSOCI_ASAN=ON -DSOCI_UBSAN=ON"
# Most builds test a single backend, so they start by turning all of them off
# and then enable just the one they need. The builds using all the backends
# (see build_all.sh and build_valgrind.sh) use the common options above.
SOCI_DEFAULT_CMAKE_OPTIONS="${SOCI_COMMON_CMAKE_OPTIONS}
-DSOCI_ASAN=ON
-DSOCI_DB2=OFF
-DSOCI_EMPTY=OFF
-DSOCI_FIREBIRD=OFF
@@ -83,11 +88,6 @@ SOCI_DEFAULT_CMAKE_OPTIONS="${SOCI_COMMON_CMAKE_OPTIONS}
-DSOCI_SQLITE3=OFF
"
# Most builds also enable UBSAN but it has to be disabled for some of them.
if [ -z "${SOCI_NO_UBSAN}" ]; then
SOCI_DEFAULT_CMAKE_OPTIONS="$SOCI_DEFAULT_CMAKE_OPTIONS -DSOCI_UBSAN=ON"
fi
#
# Functions
#

96
scripts/ci/install_mssql.sh Executable file
View File

@@ -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 <vz-soci@zeitlins.org>
#
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

76
scripts/ci/mssql.sh Normal file
View File

@@ -0,0 +1,76 @@
# Definitions used by SOCI when testing MS SQL Server in CI builds
#
# Copyright (c) 2026 Vadim Zeitlin <vz-soci@zeitlins.org>
#
# 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
}

10
scripts/ci/test_mssql.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Tests SOCI ODBC backend with MS SQL Server in CI builds
#
# Copyright (c) 2026 Vadim Zeitlin <vz-soci@zeitlins.org>
#
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

View File

@@ -243,7 +243,9 @@ odbc_statement_backend::execute(int number)
SQLSMALLINT colCount;
SQLNumResultCols(hstmt_, &colCount);
if (number > 0 && colCount > 0)
hasResultSet_ = colCount > 0;
if (number > 0 && hasResultSet_)
{
return fetch(number);
}
@@ -277,6 +279,15 @@ odbc_statement_backend::do_fetch(int beginRow, int endRow)
statement_backend::exec_fetch_result
odbc_statement_backend::fetch(int number)
{
if (!hasResultSet_)
{
// The last statement didn't return any results at all, so there is
// nothing to fetch: normally we shouldn't be even called in this case,
// but if we are, just do nothing instead of calling SQLFetch() which
// would fail with "invalid cursor state" error.
return ef_no_data;
}
numRowsFetched_ = 0;
for (auto & into : intos_)

View File

@@ -13,9 +13,10 @@
#include <cstdint>
#include <cstdio>
#include <forward_list>
#include <limits>
#include <string>
#include <thread>
#include <vector>
using namespace soci;
using namespace soci::tests;
@@ -976,14 +977,32 @@ TEST_CASE("SQLite synchronous option works from multiple threads",
FileRemover fileRemoverSHM("test.db-shm");
FileRemover fileRemoverWAL("test.db-wal");
std::forward_list<std::thread> threads;
for (int i = 0; i < 32; ++i)
// Note that Catch assertion macros can't be used from the worker threads,
// as they're not thread-safe, so we just store the error message, if any,
// for each thread and check them all from the main thread later.
constexpr int numThreads = 32;
std::vector<std::string> errors(numThreads);
std::vector<std::thread> threads;
threads.reserve(numThreads);
for (int i = 0; i < numThreads; ++i)
{
threads.emplace_front([]() -> void
threads.emplace_back([&errors, i]() -> void
{
REQUIRE_NOTHROW(
soci::session(backEnd, "db=test.db synchronous=extra timeout=2")
);
try
{
soci::session sql(backEnd,
"db=test.db synchronous=extra timeout=2");
}
catch (std::exception const& e)
{
errors[i] = e.what();
}
catch (...)
{
errors[i] = "unknown exception";
}
});
}
@@ -991,6 +1010,12 @@ TEST_CASE("SQLite synchronous option works from multiple threads",
{
thr.join();
}
for (int i = 0; i < numThreads; ++i)
{
INFO("Opening the database from thread #" << i << " failed");
CHECK(errors[i].empty());
}
}
// DDL Creation objects for common tests