diff --git a/CMakeLists.txt b/CMakeLists.txt index bbf102f..ac761fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,12 +12,22 @@ endif() include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/dbDependencyCheck.cmake") cppbessot_check_dependencies() -set(CPPBESSOT_ODB_TEST_SQLITE_CONNSTR "" CACHE STRING - "Optional SQLite connection string for ODB runtime tests") -set(CPPBESSOT_ODB_TEST_PGSQL_CONNSTR "" CACHE STRING - "Optional PostgreSQL conninfo string for ODB runtime tests") -set(CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR "" CACHE STRING - "Optional PostgreSQL admin conninfo string for real db-action test database lifecycle operations") +set(CPPBESSOT_DB_SQLITE_PROD_PATH "" CACHE STRING + "Optional SQLite DB path for DB_TARGET=prod") +set(CPPBESSOT_DB_SQLITE_DEV_PATH "" CACHE STRING + "Optional SQLite DB path for DB_TARGET=dev") +set(CPPBESSOT_DB_SQLITE_PRODDEV_PATH "" CACHE STRING + "Optional SQLite DB path for DB_TARGET=proddev") +set(CPPBESSOT_DB_SQLITE_TESTS_PATH "" CACHE STRING + "Optional SQLite DB path for DB_TARGET=tests") +set(CPPBESSOT_DB_PGSQL_PROD_CONNSTR "" CACHE STRING + "Optional PostgreSQL connstr for DB_TARGET=prod") +set(CPPBESSOT_DB_PGSQL_DEV_CONNSTR "" CACHE STRING + "Optional PostgreSQL connstr for DB_TARGET=dev") +set(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR "" CACHE STRING + "Optional PostgreSQL connstr for DB_TARGET=proddev") +set(CPPBESSOT_DB_PGSQL_TESTS_CONNSTR "" CACHE STRING + "Optional PostgreSQL connstr for DB_TARGET=tests") include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CppBeSSOT.cmake") diff --git a/README.md b/README.md index b38e43c..1f813f9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The OpenAPI file under a schema directory is the single source of truth. From th - generate Zod schemas - generate SQL migration artifacts between two schema versions - build linkable generated C++ libraries -- run live DB actions against `dev`, `prod`, or `proddev` +- run live DB actions against `dev`, `prod`, `proddev`, or `tests` ## Repository Layout @@ -79,14 +79,16 @@ When configuring the standalone `cppbessot` repo itself, `DB_SCHEMA_DIR_TO_GENER cmake -S cmake/cppbessot -B build-cppbessot -DDB_SCHEMA_DIR_TO_GENERATE=test-schema-v1.2 ``` -The standalone top-level file also exposes these test-related cache variables: +The standalone top-level file also exposes these DB target mapping cache variables: -- `CPPBESSOT_ODB_TEST_SQLITE_CONNSTR` - - optional SQLite connection string for ODB runtime tests -- `CPPBESSOT_ODB_TEST_PGSQL_CONNSTR` - - optional PostgreSQL conninfo string for ODB runtime tests -- `CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR` - - optional PostgreSQL admin conninfo string used only by the real db-action test harness +- `CPPBESSOT_DB_SQLITE_PROD_PATH` +- `CPPBESSOT_DB_SQLITE_DEV_PATH` +- `CPPBESSOT_DB_SQLITE_PRODDEV_PATH` +- `CPPBESSOT_DB_SQLITE_TESTS_PATH` +- `CPPBESSOT_DB_PGSQL_PROD_CONNSTR` +- `CPPBESSOT_DB_PGSQL_DEV_CONNSTR` +- `CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR` +- `CPPBESSOT_DB_PGSQL_TESTS_CONNSTR` ## Embedding In A Parent Project @@ -146,7 +148,7 @@ cppbessot_enable() - `DB_TARGET` - default: `dev` - selected live DB target for `db_createfrom` and `db_migrate` - - allowed values: `prod`, `proddev`, `dev` + - allowed values: `prod`, `proddev`, `dev`, `tests` - `DB_CREATEFROM_SCHEMA_DIR` - default: `DB_SCHEMA_DIR_TO_GENERATE` @@ -168,16 +170,18 @@ Exactly one backend mapping must be set for the selected `DB_TARGET`. - `CPPBESSOT_DB_SQLITE_PROD_PATH` - `CPPBESSOT_DB_SQLITE_DEV_PATH` - `CPPBESSOT_DB_SQLITE_PRODDEV_PATH` +- `CPPBESSOT_DB_SQLITE_TESTS_PATH` -These point at the SQLite DB file to act on for `prod`, `dev`, or `proddev`. +These point at the SQLite DB file to act on for `prod`, `dev`, `proddev`, or `tests`. ### PostgreSQL Live DB Mapping Variables - `CPPBESSOT_DB_PGSQL_PROD_CONNSTR` - `CPPBESSOT_DB_PGSQL_DEV_CONNSTR` - `CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR` +- `CPPBESSOT_DB_PGSQL_TESTS_CONNSTR` -These are `psql`-compatible PostgreSQL connection strings for `prod`, `dev`, or `proddev`. +These are `psql`-compatible PostgreSQL connection strings for `prod`, `dev`, `proddev`, or `tests`. ### Prod-To-Proddev Clone Hook Variables @@ -613,14 +617,14 @@ ctest --test-dir build-cppbessot-tests --output-on-failure ### ODB Runtime Tests -Provide test DB connection strings: +Provide DB target mappings: ```bash cmake -S cmake/cppbessot -B build-cppbessot-tests \ -DBUILD_TESTING=ON \ -DDB_SCHEMA_DIR_TO_GENERATE=test-schema-v1.2 \ - -DCPPBESSOT_ODB_TEST_SQLITE_CONNSTR=/tmp/cppbessot-odb.sqlite \ - -DCPPBESSOT_ODB_TEST_PGSQL_CONNSTR="host=127.0.0.1 port=5432 dbname=cppbessot_odb_test user=postgres password=postgres" + -DCPPBESSOT_DB_SQLITE_PRODDEV_PATH=/tmp/cppbessot-odb.sqlite \ + -DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR="host=127.0.0.1 port=5432 dbname=cppbessot_odb_test user=postgres password=postgres" ``` ### Real PostgreSQL DB-Action Tests @@ -629,7 +633,6 @@ These tests are only registered when all of the following are true: - `BUILD_TESTING=ON` - `psql` is available -- `CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR` is non-empty - the required target connstr variables for the individual test are non-empty Example: @@ -638,7 +641,6 @@ Example: cmake -S cmake/cppbessot -B build-cppbessot-tests \ -DBUILD_TESTING=ON \ -DDB_SCHEMA_DIR_TO_GENERATE=test-schema-v1.2 \ - -DCPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR="host=127.0.0.1 port=5432 dbname=postgres user=postgres password=postgres" \ -DCPPBESSOT_DB_PGSQL_DEV_CONNSTR="host=127.0.0.1 port=5432 dbname=cppbessot_dev user=postgres password=postgres" \ -DCPPBESSOT_DB_PGSQL_PROD_CONNSTR="host=127.0.0.1 port=5432 dbname=cppbessot_prod user=postgres password=postgres" \ -DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR="host=127.0.0.1 port=5432 dbname=cppbessot_proddev user=postgres password=postgres" diff --git a/cmake/CppBeSSOT.cmake b/cmake/CppBeSSOT.cmake index da6f676..5380f69 100644 --- a/cmake/CppBeSSOT.cmake +++ b/cmake/CppBeSSOT.cmake @@ -36,7 +36,7 @@ if(NOT DEFINED DB_SCHEMA_CHANGES_ARE_ERROR) endif() if(NOT DEFINED DB_TARGET) - set(DB_TARGET "dev" CACHE STRING "Live DB target to act on: prod, proddev, or dev") + set(DB_TARGET "dev" CACHE STRING "Live DB target to act on: prod, proddev, dev, or tests") endif() if(NOT DEFINED DB_CREATEFROM_SCHEMA_DIR) @@ -59,12 +59,16 @@ set(CPPBESSOT_DB_SQLITE_DEV_PATH "${CPPBESSOT_DB_SQLITE_DEV_PATH}" CACHE STRING "Parent-supplied SQLite DB path for DB_TARGET=dev") set(CPPBESSOT_DB_SQLITE_PRODDEV_PATH "${CPPBESSOT_DB_SQLITE_PRODDEV_PATH}" CACHE STRING "Parent-supplied SQLite DB path for DB_TARGET=proddev") +set(CPPBESSOT_DB_SQLITE_TESTS_PATH "${CPPBESSOT_DB_SQLITE_TESTS_PATH}" CACHE STRING + "Parent-supplied SQLite DB path for DB_TARGET=tests") set(CPPBESSOT_DB_PGSQL_PROD_CONNSTR "${CPPBESSOT_DB_PGSQL_PROD_CONNSTR}" CACHE STRING "Parent-supplied PostgreSQL connection string for DB_TARGET=prod") set(CPPBESSOT_DB_PGSQL_DEV_CONNSTR "${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" CACHE STRING "Parent-supplied PostgreSQL connection string for DB_TARGET=dev") set(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" CACHE STRING "Parent-supplied PostgreSQL connection string for DB_TARGET=proddev") +set(CPPBESSOT_DB_PGSQL_TESTS_CONNSTR "${CPPBESSOT_DB_PGSQL_TESTS_CONNSTR}" CACHE STRING + "Parent-supplied PostgreSQL connection string for DB_TARGET=tests") set(CPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND "${CPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND}" CACHE STRING "Parent-supplied command string that clones the prod SQLite DB into proddev") set(CPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND "${CPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND}" CACHE STRING diff --git a/cmake/dbActionCommon.cmake b/cmake/dbActionCommon.cmake index 7ec6d85..c1e14d0 100644 --- a/cmake/dbActionCommon.cmake +++ b/cmake/dbActionCommon.cmake @@ -37,9 +37,11 @@ function(_cppbessot_db_action_common_cache_args out_var) "-DCPPBESSOT_DB_SQLITE_PROD_PATH=${CPPBESSOT_DB_SQLITE_PROD_PATH}" "-DCPPBESSOT_DB_SQLITE_DEV_PATH=${CPPBESSOT_DB_SQLITE_DEV_PATH}" "-DCPPBESSOT_DB_SQLITE_PRODDEV_PATH=${CPPBESSOT_DB_SQLITE_PRODDEV_PATH}" + "-DCPPBESSOT_DB_SQLITE_TESTS_PATH=${CPPBESSOT_DB_SQLITE_TESTS_PATH}" "-DCPPBESSOT_DB_PGSQL_PROD_CONNSTR=${CPPBESSOT_DB_PGSQL_PROD_CONNSTR}" "-DCPPBESSOT_DB_PGSQL_DEV_CONNSTR=${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" - "-DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR=${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}") + "-DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR=${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" + "-DCPPBESSOT_DB_PGSQL_TESTS_CONNSTR=${CPPBESSOT_DB_PGSQL_TESTS_CONNSTR}") set(${out_var} "${_args}" PARENT_SCOPE) endfunction() diff --git a/cmake/dbActionShared.cmake b/cmake/dbActionShared.cmake index d22c216..b9faeb2 100644 --- a/cmake/dbActionShared.cmake +++ b/cmake/dbActionShared.cmake @@ -14,7 +14,8 @@ endfunction() function(_cppbessot_db_action_validate_db_target_impl db_target) if(NOT "${db_target}" STREQUAL "prod" AND NOT "${db_target}" STREQUAL "proddev" - AND NOT "${db_target}" STREQUAL "dev") - message(FATAL_ERROR "DB_TARGET must be one of: prod, proddev, dev.") + AND NOT "${db_target}" STREQUAL "dev" + AND NOT "${db_target}" STREQUAL "tests") + message(FATAL_ERROR "DB_TARGET must be one of: prod, proddev, dev, tests.") endif() endfunction() diff --git a/cmake/dbDependencyCheck.cmake b/cmake/dbDependencyCheck.cmake index 373da2c..4ab22b0 100644 --- a/cmake/dbDependencyCheck.cmake +++ b/cmake/dbDependencyCheck.cmake @@ -11,7 +11,6 @@ function(_cppbessot_publish_dependency_outputs) set(CPPBESSOT_JAVA_EXECUTABLE "${CPPBESSOT_JAVA_EXECUTABLE}" PARENT_SCOPE) set(CPPBESSOT_GIT_EXECUTABLE "${CPPBESSOT_GIT_EXECUTABLE}" PARENT_SCOPE) set(CPPBESSOT_SQLITE3_EXECUTABLE "${CPPBESSOT_SQLITE3_EXECUTABLE}" PARENT_SCOPE) - set(CPPBESSOT_PSQL_EXECUTABLE "${CPPBESSOT_PSQL_EXECUTABLE}" PARENT_SCOPE) set(CPPBESSOT_ODB_RUNTIME_LIB "${CPPBESSOT_ODB_RUNTIME_LIB}" PARENT_SCOPE) set(CPPBESSOT_ODB_SQLITE_RUNTIME_LIB "${CPPBESSOT_ODB_SQLITE_RUNTIME_LIB}" PARENT_SCOPE) set(CPPBESSOT_ODB_PGSQL_RUNTIME_LIB "${CPPBESSOT_ODB_PGSQL_RUNTIME_LIB}" PARENT_SCOPE) @@ -155,12 +154,10 @@ function(cppbessot_check_dependencies) CPPBESSOT_DB_PGSQL_PROD_CONNSTR CPPBESSOT_DB_PGSQL_DEV_CONNSTR CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR - CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR) + CPPBESSOT_DB_PGSQL_TESTS_CONNSTR) if(_cppbessot_require_psql) - _cppbessot_require_program(CPPBESSOT_PSQL_EXECUTABLE psql + _cppbessot_require_program(_cppbessot_psql_executable psql "Install the PostgreSQL client CLI so live PostgreSQL DB actions can run.") - else() - set(CPPBESSOT_PSQL_EXECUTABLE "") endif() _cppbessot_require_npm_package("${CPPBESSOT_NPM_EXECUTABLE}" "@openapitools/openapi-generator-cli") diff --git a/cmake/scripts/cppbessotDbActionClone.cmake b/cmake/scripts/cppbessotDbActionClone.cmake index 099775d..b1e4ae9 100644 --- a/cmake/scripts/cppbessotDbActionClone.cmake +++ b/cmake/scripts/cppbessotDbActionClone.cmake @@ -25,7 +25,7 @@ function(cppbessot_db_action_target_exists out_var backend sqlite_path pgsql_con endif() endfunction() -function(cppbessot_db_action_get_clone_command out_var backend) +function(cppbessot_db_action_get_clone_command out_var backend target) if("${backend}" STREQUAL "sqlite") set(_command "${CPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND}") else() @@ -34,11 +34,11 @@ function(cppbessot_db_action_get_clone_command out_var backend) set(${out_var} "${_command}" PARENT_SCOPE) endfunction() -function(cppbessot_db_action_invoke_clone_hook backend) - cppbessot_db_action_get_clone_command(_clone_command "${backend}") +function(cppbessot_db_action_invoke_clone_hook backend target) + cppbessot_db_action_get_clone_command(_clone_command "${backend}" "${target}") if("${_clone_command}" STREQUAL "") message(FATAL_ERROR - "No clone command is configured for backend `${backend}` while preparing proddev.") + "No clone command is configured for backend `${backend}` while preparing target `${target}`.") endif() execute_process( @@ -50,7 +50,7 @@ function(cppbessot_db_action_invoke_clone_hook backend) if(NOT _result EQUAL 0) message(FATAL_ERROR - "Proddev clone command failed for backend `${backend}`.\n${_stdout}\n${_stderr}") + "Clone command failed for backend `${backend}` target `${target}`.\n${_stdout}\n${_stderr}") endif() endfunction() @@ -68,7 +68,7 @@ function(cppbessot_db_action_prepare_proddev target backend use_stale sqlite_pat return() endif() - cppbessot_db_action_invoke_clone_hook("${backend}") + cppbessot_db_action_invoke_clone_hook("${backend}" "${target}") cppbessot_db_action_target_exists(_exists "${backend}" "${sqlite_path}" "${pgsql_connstr}") if(NOT _exists) message(FATAL_ERROR diff --git a/docs/prompts/add-tests-db-action-target.md b/docs/prompts/add-tests-db-action-target.md new file mode 100644 index 0000000..4a2da73 --- /dev/null +++ b/docs/prompts/add-tests-db-action-target.md @@ -0,0 +1,17 @@ +Ok. We've added a new db, test, and we should now add a new target for DB_TARGET. Add +the new DB_TARGET, "tests", so that from now on, migration actions like db_createfrom +and db_migrate can choose to target DB_TARGET=. Ofc, proddev +cannot have a db_createfrom command. + +Also the tests that need DB access should now use the CPPBESSOT_DB_[PGSQL|SQLITE]_PRODDEV_CONNSTR. So you should get rid of the legacy ODB test connstr variables and replace them with CPPBESSOT_DB_[PGSQL|SQLITE]_PRODDEV_CONNSTR. This unifies everything nicely. So now we have 4 DB targets, both for Postgre and sqlite. + +The legacy db-action PostgreSQL admin connstr variable should be removed as well. + +The local YugabyteDB instance is configured at /media/latentprion/aafe96c9-7fcd-40ce-991d-ca2d23b5ba17/db/yugabytedb. The runtime proddev mapping should remain the real proddev DB (`couresilient_proddev`) with the real app user (`couresilient`). The separate `couresilient_tests` user and `couresilient_tests` DB are for the `DB_TARGET=tests` mapping, not for `DB_TARGET=proddev`. + +If there are tests whose purpose is to test the clone command from prod to proddev, have them use the same clone hook as production: CPPBESSOT_DB_[PGSQL|SQLITE]_CLONE_PROD_TO_PRODDEV_COMMAND. + +No backward compatibility or anything. Hard cutover. +---- + +Make sure to update cppbessot.env.cmake[.example] and README.md inside of couresilient. diff --git a/tests/db-actions/CMakeLists.txt b/tests/db-actions/CMakeLists.txt index 05adff9..3f7e31c 100644 --- a/tests/db-actions/CMakeLists.txt +++ b/tests/db-actions/CMakeLists.txt @@ -3,10 +3,12 @@ function(_cppbessot_db_action_test_common_args out_var test_name script_name) "-DCPPBESSOT_TEST_NAME=${test_name}" "-DCPPBESSOT_TEST_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}" "-DCPPBESSOT_TEST_MODULE_SOURCE_DIR=${PROJECT_SOURCE_DIR}" - "-DCPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR=${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" "-DCPPBESSOT_DB_PGSQL_DEV_CONNSTR=${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" "-DCPPBESSOT_DB_PGSQL_PROD_CONNSTR=${CPPBESSOT_DB_PGSQL_PROD_CONNSTR}" "-DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR=${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" + "-DCPPBESSOT_DB_PGSQL_TESTS_CONNSTR=${CPPBESSOT_DB_PGSQL_TESTS_CONNSTR}" + "-DCPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND=${CPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND}" + "-DCPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND=${CPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND}" "-DCPPBESSOT_DB_ACTION_TEST_SCRIPT=${CMAKE_CURRENT_SOURCE_DIR}/scripts/${script_name}") set(${out_var} "${_args}" PARENT_SCOPE) endfunction() @@ -25,8 +27,8 @@ function(cppbessot_add_real_pgsql_db_action_test test_name script_name) return() endif() - if("${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" STREQUAL "") - message(STATUS "Skipping real PostgreSQL db-action test `${test_name}` because CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR is empty.") + if("${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" STREQUAL "") + message(STATUS "Skipping real PostgreSQL db-action test `${test_name}` because CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR is empty.") return() endif() @@ -51,26 +53,27 @@ cppbessot_add_db_action_test(cppbessot_db_action_missing_migrate_with missing_mi cppbessot_add_db_action_test(cppbessot_db_action_missing_migration_dir missing_migration_dir.cmake) cppbessot_add_db_action_test(cppbessot_db_action_proddev_createfrom_illegal proddev_createfrom_illegal.cmake) cppbessot_add_db_action_test(cppbessot_db_action_sqlite_createfrom sqlite_createfrom.cmake) +cppbessot_add_db_action_test(cppbessot_db_action_sqlite_tests_createfrom sqlite_tests_createfrom.cmake) cppbessot_add_db_action_test(cppbessot_db_action_sqlite_invalid_sql sqlite_invalid_sql.cmake) cppbessot_add_db_action_test(cppbessot_db_action_sqlite_migrate sqlite_migrate.cmake) cppbessot_add_db_action_test(cppbessot_db_action_sqlite_proddev_stale sqlite_proddev_stale.cmake) cppbessot_add_db_action_test(cppbessot_db_action_sqlite_proddev_clone sqlite_proddev_clone.cmake) cppbessot_add_db_action_test(cppbessot_db_action_pgsql_createfrom_mock pgsql_createfrom_mock.cmake) +cppbessot_add_db_action_test(cppbessot_db_action_pgsql_tests_createfrom_mock pgsql_tests_createfrom_mock.cmake) cppbessot_add_db_action_test(cppbessot_db_action_pgsql_migrate_order pgsql_migrate_order.cmake) cppbessot_add_db_action_test(cppbessot_db_action_pgsql_stale_abort pgsql_stale_abort.cmake) cppbessot_add_db_action_test(cppbessot_db_action_backfill_env_no_structural backfill_env_no_structural.cmake) cppbessot_add_real_pgsql_db_action_test( cppbessot_db_action_pgsql_createfrom_real pgsql_createfrom_real.cmake - CPPBESSOT_DB_PGSQL_DEV_CONNSTR) + CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_add_real_pgsql_db_action_test( cppbessot_db_action_pgsql_migrate_real pgsql_migrate_real.cmake - CPPBESSOT_DB_PGSQL_DEV_CONNSTR) + CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_add_real_pgsql_db_action_test( cppbessot_db_action_pgsql_proddev_clone_real pgsql_proddev_clone_real.cmake - CPPBESSOT_DB_PGSQL_PROD_CONNSTR CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_add_real_pgsql_db_action_test( cppbessot_db_action_pgsql_proddev_stale_real @@ -79,5 +82,5 @@ cppbessot_add_real_pgsql_db_action_test( cppbessot_add_real_pgsql_db_action_test( cppbessot_db_action_pgsql_backfill_real pgsql_backfill_real.cmake - CPPBESSOT_DB_PGSQL_DEV_CONNSTR) + CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_add_db_action_test(cppbessot_db_action_regression regression_targets.cmake) diff --git a/tests/db-actions/cmake/TestCommon.cmake b/tests/db-actions/cmake/TestCommon.cmake index 1595bb8..a1d292f 100644 --- a/tests/db-actions/cmake/TestCommon.cmake +++ b/tests/db-actions/cmake/TestCommon.cmake @@ -162,8 +162,8 @@ endfunction() function(cppbessot_test_has_real_pgsql_support out_var) find_program(_psql psql) - if(_psql AND DEFINED CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR - AND NOT "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" STREQUAL "") + if(_psql AND DEFINED CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR + AND NOT "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" STREQUAL "") set(${out_var} TRUE PARENT_SCOPE) else() set(${out_var} FALSE PARENT_SCOPE) @@ -174,10 +174,16 @@ function(cppbessot_test_require_real_pgsql_support) cppbessot_test_has_real_pgsql_support(_has_support) if(NOT _has_support) message(FATAL_ERROR - "Real PostgreSQL db-action test support requires `psql` and CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR.") + "Real PostgreSQL db-action test support requires `psql` and CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR.") endif() endfunction() +function(cppbessot_test_pgsql_admin_connstr out_var) + cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) + cppbessot_test_pgsql_connstr_replace_dbname(_admin_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "postgres") + set(${out_var} "${_admin_connstr}" PARENT_SCOPE) +endfunction() + function(cppbessot_test_pgsql_exec connstr sql_text) find_program(_psql psql REQUIRED) execute_process( @@ -270,23 +276,25 @@ endfunction() function(cppbessot_test_pgsql_drop_database connstr) cppbessot_test_require_real_pgsql_support() + cppbessot_test_pgsql_admin_connstr(_admin_connstr) cppbessot_test_pgsql_connstr_dbname(_dbname "${connstr}") cppbessot_test_pgsql_escape_identifier(_db_ident "${_dbname}") cppbessot_test_pgsql_escape_literal(_db_lit "${_dbname}") cppbessot_test_pgsql_exec( - "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" + "${_admin_connstr}" "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = ${_db_lit} AND pid <> pg_backend_pid();") cppbessot_test_pgsql_exec( - "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" + "${_admin_connstr}" "DROP DATABASE IF EXISTS ${_db_ident};") endfunction() function(cppbessot_test_pgsql_create_database connstr) cppbessot_test_require_real_pgsql_support() + cppbessot_test_pgsql_admin_connstr(_admin_connstr) cppbessot_test_pgsql_connstr_dbname(_dbname "${connstr}") cppbessot_test_pgsql_escape_identifier(_db_ident "${_dbname}") cppbessot_test_pgsql_exec( - "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" + "${_admin_connstr}" "CREATE DATABASE ${_db_ident};") endfunction() @@ -297,13 +305,14 @@ endfunction() function(cppbessot_test_pgsql_clone_database source_connstr target_connstr) cppbessot_test_require_real_pgsql_support() + cppbessot_test_pgsql_admin_connstr(_admin_connstr) cppbessot_test_pgsql_connstr_dbname(_source_db "${source_connstr}") cppbessot_test_pgsql_connstr_dbname(_target_db "${target_connstr}") cppbessot_test_pgsql_escape_identifier(_source_ident "${_source_db}") cppbessot_test_pgsql_escape_identifier(_target_ident "${_target_db}") cppbessot_test_pgsql_drop_database("${target_connstr}") cppbessot_test_pgsql_exec( - "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}" + "${_admin_connstr}" "CREATE DATABASE ${_target_ident} TEMPLATE ${_source_ident};") endfunction() @@ -314,13 +323,14 @@ endfunction() function(cppbessot_test_pgsql_clone_command out_var source_connstr target_connstr) cppbessot_test_require_real_pgsql_support() + cppbessot_test_pgsql_admin_connstr(_admin_connstr) cppbessot_test_pgsql_connstr_dbname(_source_db "${source_connstr}") cppbessot_test_pgsql_connstr_dbname(_target_db "${target_connstr}") cppbessot_test_pgsql_escape_identifier(_source_ident "${_source_db}") cppbessot_test_pgsql_escape_identifier(_target_ident "${_target_db}") cppbessot_test_pgsql_escape_literal(_source_lit "${_source_db}") cppbessot_test_pgsql_escape_literal(_target_lit "${_target_db}") - cppbessot_test_shell_single_quote(_admin_shell "${CPPBESSOT_DB_ACTION_TEST_PGSQL_ADMIN_CONNSTR}") + cppbessot_test_shell_single_quote(_admin_shell "${_admin_connstr}") cppbessot_test_shell_single_quote( _term_target_shell "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = ${_target_lit} AND pid <> pg_backend_pid();") diff --git a/tests/db-actions/scripts/pgsql_backfill_real.cmake b/tests/db-actions/scripts/pgsql_backfill_real.cmake index 0420e09..9213fe3 100644 --- a/tests/db-actions/scripts/pgsql_backfill_real.cmake +++ b/tests/db-actions/scripts/pgsql_backfill_real.cmake @@ -1,11 +1,11 @@ include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") cppbessot_test_require_real_pgsql_support() -cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_DEV_CONNSTR) +cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_test_case_dir(_case_dir) set(_log_file "${_case_dir}/events.log") cppbessot_test_write_file("${_log_file}" "") -cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" "dev") +cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "dev") cppbessot_test_pgsql_reset_database("${_dev_connstr}") cppbessot_test_pgsql_exec( diff --git a/tests/db-actions/scripts/pgsql_createfrom_real.cmake b/tests/db-actions/scripts/pgsql_createfrom_real.cmake index 2e0ffd5..2e9f6ba 100644 --- a/tests/db-actions/scripts/pgsql_createfrom_real.cmake +++ b/tests/db-actions/scripts/pgsql_createfrom_real.cmake @@ -1,9 +1,9 @@ include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") cppbessot_test_require_real_pgsql_support() -cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_DEV_CONNSTR) +cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_test_case_dir(_case_dir) -cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" "dev") +cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "dev") cppbessot_test_pgsql_reset_database("${_dev_connstr}") cppbessot_test_pgsql_exec( diff --git a/tests/db-actions/scripts/pgsql_migrate_real.cmake b/tests/db-actions/scripts/pgsql_migrate_real.cmake index 0924be7..e899885 100644 --- a/tests/db-actions/scripts/pgsql_migrate_real.cmake +++ b/tests/db-actions/scripts/pgsql_migrate_real.cmake @@ -1,9 +1,9 @@ include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") cppbessot_test_require_real_pgsql_support() -cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_DEV_CONNSTR) +cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_test_case_dir(_case_dir) -cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}" "dev") +cppbessot_test_pgsql_isolated_connstr(_dev_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "dev") cppbessot_test_pgsql_reset_database("${_dev_connstr}") cppbessot_test_pgsql_exec( diff --git a/tests/db-actions/scripts/pgsql_proddev_clone_real.cmake b/tests/db-actions/scripts/pgsql_proddev_clone_real.cmake index 9da79f6..8aea914 100644 --- a/tests/db-actions/scripts/pgsql_proddev_clone_real.cmake +++ b/tests/db-actions/scripts/pgsql_proddev_clone_real.cmake @@ -1,10 +1,9 @@ include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") cppbessot_test_require_real_pgsql_support() -cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PROD_CONNSTR) cppbessot_test_require_var(CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR) cppbessot_test_case_dir(_case_dir) -cppbessot_test_pgsql_isolated_connstr(_prod_connstr "${CPPBESSOT_DB_PGSQL_PROD_CONNSTR}" "prod") +cppbessot_test_pgsql_isolated_connstr(_prod_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "prod") cppbessot_test_pgsql_isolated_connstr(_proddev_connstr "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}" "proddev") cppbessot_test_pgsql_reset_database("${_prod_connstr}") diff --git a/tests/db-actions/scripts/pgsql_tests_createfrom_mock.cmake b/tests/db-actions/scripts/pgsql_tests_createfrom_mock.cmake new file mode 100644 index 0000000..5bd956f --- /dev/null +++ b/tests/db-actions/scripts/pgsql_tests_createfrom_mock.cmake @@ -0,0 +1,32 @@ +include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") + +cppbessot_test_case_dir(_case_dir) +set(_tool_dir "${_case_dir}/tools") +set(_log_file "${_case_dir}/psql.log") +set(CPPBESSOT_TEST_LOG "${_log_file}") +cppbessot_test_write_file("${_log_file}" "") +cppbessot_test_write_mock_psql("${_tool_dir}/psql") +cppbessot_test_set_path_with_tool_dir("${_tool_dir}") +set(ENV{CPPBESSOT_TEST_LOG} "${_log_file}") +set(ENV{CPPBESSOT_TEST_PSQL_FAIL_ALL} "0") +set(ENV{CPPBESSOT_TEST_PSQL_FAIL_SELECT} "0") + +cppbessot_test_write_project( + "${_case_dir}" + "set(DB_TARGET \"tests\" CACHE STRING \"\")\n" + "set(CPPBESSOT_DB_PGSQL_TESTS_CONNSTR \"dbname=tests_db\" CACHE STRING \"\")\n") +cppbessot_test_add_schema("${_case_dir}" "v1.1") +cppbessot_test_add_sql_file("${_case_dir}/db/v1.1/generated-sql-ddl/postgre/01-schema.sql" + "CREATE TABLE sample(id TEXT PRIMARY KEY);\n") +cppbessot_test_add_sql_file("${_case_dir}/db/v1.1/generated-sql-ddl/postgre/02-seed.sql" + "INSERT INTO sample(id) VALUES ('row-1');\n") + +cppbessot_test_configure_project("${_case_dir}" "${_case_dir}/build" _cfg_result _cfg_stdout _cfg_stderr) +cppbessot_test_assert_success("${_cfg_result}" "${_cfg_stderr}" "fixture configure") +cppbessot_test_build_target("${_case_dir}/build" "db_createfrom" _build_result _build_stdout _build_stderr) +cppbessot_test_assert_success("${_build_result}" "${_build_stderr}" "db_createfrom pgsql tests mock") + +file(READ "${_log_file}" _log_contents) +cppbessot_test_assert_contains("${_log_contents}" "sqlcmd:DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;" "pgsql reset log") +cppbessot_test_assert_contains("${_log_contents}" "sqlfile:01-schema.sql" "pgsql schema log") +cppbessot_test_assert_contains("${_log_contents}" "sqlfile:02-seed.sql" "pgsql seed log") diff --git a/tests/db-actions/scripts/regression_targets.cmake b/tests/db-actions/scripts/regression_targets.cmake index 530a768..bc7656d 100644 --- a/tests/db-actions/scripts/regression_targets.cmake +++ b/tests/db-actions/scripts/regression_targets.cmake @@ -14,6 +14,15 @@ execute_process( -DDB_SCHEMA_DIR_MIGRATION_FROM=v1.1 -DDB_SCHEMA_DIR_MIGRATION_TO=v1.2 -DCPPBESSOT_AUTO_ENABLE=ON + -DCOURESILIENT_USE_LOCAL_CPPBESSOT_ENV=OFF + -DCPPBESSOT_DB_PGSQL_PROD_CONNSTR= + -DCPPBESSOT_DB_PGSQL_DEV_CONNSTR= + -DCPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR= + -DCPPBESSOT_DB_PGSQL_TESTS_CONNSTR= + -DCPPBESSOT_DB_SQLITE_PROD_PATH= + -DCPPBESSOT_DB_SQLITE_DEV_PATH= + -DCPPBESSOT_DB_SQLITE_PRODDEV_PATH= + -DCPPBESSOT_DB_SQLITE_TESTS_PATH= RESULT_VARIABLE _cfg_result OUTPUT_VARIABLE _cfg_stdout ERROR_VARIABLE _cfg_stderr diff --git a/tests/db-actions/scripts/sqlite_tests_createfrom.cmake b/tests/db-actions/scripts/sqlite_tests_createfrom.cmake new file mode 100644 index 0000000..b3939b2 --- /dev/null +++ b/tests/db-actions/scripts/sqlite_tests_createfrom.cmake @@ -0,0 +1,30 @@ +include("${CMAKE_CURRENT_LIST_DIR}/../cmake/TestCommon.cmake") + +cppbessot_test_case_dir(_case_dir) +set(_db_path "${_case_dir}/live/tests.sqlite") +cppbessot_test_write_project( + "${_case_dir}" + "set(DB_TARGET \"tests\" CACHE STRING \"\")\n" + "set(CPPBESSOT_DB_SQLITE_TESTS_PATH \"${_db_path}\" CACHE STRING \"\")\n") +cppbessot_test_add_schema("${_case_dir}" "v1.1") +cppbessot_test_add_sql_file("${_case_dir}/db/v1.1/generated-sql-ddl/sqlite/01-schema.sql" + "CREATE TABLE sample(id TEXT PRIMARY KEY, note TEXT);\n") +cppbessot_test_add_sql_file("${_case_dir}/db/v1.1/generated-sql-ddl/sqlite/02-seed.sql" + "INSERT INTO sample(id, note) VALUES ('seed-1', 'created-second');\n") +cppbessot_test_sqlite_exec("${_db_path}" + "CREATE TABLE old_data(id TEXT); INSERT INTO old_data(id) VALUES ('legacy');") + +cppbessot_test_configure_project("${_case_dir}" "${_case_dir}/build" _cfg_result _cfg_stdout _cfg_stderr) +cppbessot_test_assert_success("${_cfg_result}" "${_cfg_stderr}" "fixture configure") +cppbessot_test_build_target("${_case_dir}/build" "db_createfrom" _build_result _build_stdout _build_stderr) +cppbessot_test_assert_success("${_build_result}" "${_build_stderr}" "db_createfrom sqlite tests") + +cppbessot_test_sqlite_query_scalar(_row_count "${_db_path}" "SELECT COUNT(*) FROM sample;") +if(NOT "${_row_count}" STREQUAL "1") + message(FATAL_ERROR "Expected seeded sample row after recreate, got `${_row_count}`.") +endif() +cppbessot_test_sqlite_query_scalar(_old_table_count "${_db_path}" + "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='old_data';") +if(NOT "${_old_table_count}" STREQUAL "0") + message(FATAL_ERROR "Expected old_data table to be removed during recreate.") +endif() diff --git a/tests/odb-orm/CMakeLists.txt b/tests/odb-orm/CMakeLists.txt index 50af432..f68a83e 100644 --- a/tests/odb-orm/CMakeLists.txt +++ b/tests/odb-orm/CMakeLists.txt @@ -40,13 +40,13 @@ endfunction() cppbessot_add_odb_orm_test( "sqlite" "sqlite" - CPPBESSOT_ODB_TEST_SQLITE_CONNSTR + CPPBESSOT_DB_SQLITE_PRODDEV_PATH "${CMAKE_CURRENT_SOURCE_DIR}/sqlite_orm_test.cpp" cppbessot::odb_sqlite) cppbessot_add_odb_orm_test( "pgsql" "pgsql" - CPPBESSOT_ODB_TEST_PGSQL_CONNSTR + CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR "${CMAKE_CURRENT_SOURCE_DIR}/pgsql_orm_test.cpp" cppbessot::odb_pgsql) diff --git a/tests/odb-orm/pgsql_orm_test.cpp b/tests/odb-orm/pgsql_orm_test.cpp index 69d502d..7e72357 100644 --- a/tests/odb-orm/pgsql_orm_test.cpp +++ b/tests/odb-orm/pgsql_orm_test.cpp @@ -54,8 +54,8 @@ void apply_pgsql_ddl(const std::string& connstr) TEST(PgsqlOdbOrm, PersistsLoadsQueriesAndErases) { const std::string connstr = cppbessot_env_or_default( - "CPPBESSOT_ODB_TEST_PGSQL_CONNSTR", - CPPBESSOT_ODB_TEST_PGSQL_CONNSTR_DEFAULT); + "CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR", + CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR_DEFAULT); apply_pgsql_ddl(connstr); odb::pgsql::database db(connstr); cppbessot_run_agent_orm_roundtrip(db); diff --git a/tests/odb-orm/sqlite_orm_test.cpp b/tests/odb-orm/sqlite_orm_test.cpp index 4367221..2ce3a72 100644 --- a/tests/odb-orm/sqlite_orm_test.cpp +++ b/tests/odb-orm/sqlite_orm_test.cpp @@ -43,8 +43,8 @@ void apply_sqlite_ddl(const std::string& connstr) TEST(SqliteOdbOrm, PersistsLoadsQueriesAndErases) { const std::string connstr = cppbessot_env_or_default( - "CPPBESSOT_ODB_TEST_SQLITE_CONNSTR", - CPPBESSOT_ODB_TEST_SQLITE_CONNSTR_DEFAULT); + "CPPBESSOT_DB_SQLITE_PRODDEV_PATH", + CPPBESSOT_DB_SQLITE_PRODDEV_PATH_DEFAULT); std::filesystem::remove(connstr); apply_sqlite_ddl(connstr); odb::sqlite::database db(connstr, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);