From d7fc006e82d5ce166b4df1dd9bac886ce613086b Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 26 Feb 2024 15:14:32 +0100 Subject: [PATCH] Write `ribasim_version` to TOML and warn in core if it is different --- core/src/config.jl | 1 + core/src/main.jl | 5 ++++- core/test/data/config_test.toml | 5 +---- core/test/data/logging_test_loglevel_debug.toml | 1 + core/test/data/logging_test_no_loglevel.toml | 1 + core/test/docs.toml | 2 ++ core/test/io_test.jl | 3 +++ core/test/main_test.jl | 10 ++++++++++ docs/contribute/release.qmd | 9 ++------- python/ribasim/ribasim/model.py | 2 ++ python/ribasim/tests/test_io.py | 11 +++++++++-- 11 files changed, 36 insertions(+), 14 deletions(-) diff --git a/core/src/config.jl b/core/src/config.jl index 593699a38..07dc6afa4 100644 --- a/core/src/config.jl +++ b/core/src/config.jl @@ -111,6 +111,7 @@ end @option @addnodetypes struct Toml <: TableOption starttime::DateTime endtime::DateTime + ribasim_version::String input_dir::String results_dir::String database::String = "database.gpkg" diff --git a/core/src/main.jl b/core/src/main.jl index 3df9f2249..1b05c10bb 100644 --- a/core/src/main.jl +++ b/core/src/main.jl @@ -57,8 +57,11 @@ function main(ARGS::Vector{String})::Cint logger = Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io) with_logger(logger) do - ribasim_version = pkgversion(Ribasim) + ribasim_version = string(pkgversion(Ribasim)) (; starttime, endtime) = config + if string(ribasim_version) != config.ribasim_version + @warn "The Ribasim version in the TOML config file does not match the used Ribasim CLI version." config.ribasim_version ribasim_version + end @info "Starting a Ribasim simulation." ribasim_version starttime endtime model = Ribasim.run(config) if successful_retcode(model) diff --git a/core/test/data/config_test.toml b/core/test/data/config_test.toml index 25cf747d6..d37450624 100644 --- a/core/test/data/config_test.toml +++ b/core/test/data/config_test.toml @@ -1,11 +1,8 @@ starttime = 2019-01-01 endtime = 2019-12-31 - -# optional, default is the path of the TOML input_dir = "../../generated_testmodels/lhm" results_dir = "../../generated_testmodels/lhm" - -database = "database.gpkg" +ribasim_version = "2024.2.0" [basin] time = "basin/time.arrow" diff --git a/core/test/data/logging_test_loglevel_debug.toml b/core/test/data/logging_test_loglevel_debug.toml index ef75199c3..eede39c23 100644 --- a/core/test/data/logging_test_loglevel_debug.toml +++ b/core/test/data/logging_test_loglevel_debug.toml @@ -2,6 +2,7 @@ starttime = 2019-01-01 endtime = 2019-12-31 input_dir = "." results_dir = "results" +ribasim_version = "2024.2.0" [logging] verbosity = "debug" diff --git a/core/test/data/logging_test_no_loglevel.toml b/core/test/data/logging_test_no_loglevel.toml index 61b795254..588af2bdb 100644 --- a/core/test/data/logging_test_no_loglevel.toml +++ b/core/test/data/logging_test_no_loglevel.toml @@ -2,3 +2,4 @@ starttime = 2019-01-01 endtime = 2019-12-31 input_dir = "." results_dir = "results" +ribasim_version = "2024.2.0" diff --git a/core/test/docs.toml b/core/test/docs.toml index f80b1646d..5b5420518 100644 --- a/core/test/docs.toml +++ b/core/test/docs.toml @@ -7,6 +7,8 @@ endtime = 2021-01-01 # required input_dir = "." # required results_dir = "results" # required +ribasim_version = "2024.2.0" # required + # Specific tables can also go into Arrow files rather than the database. # For large tables this can benefit from better compressed file sizes. # This is optional, tables are retrieved from the database if not specified in the TOML. diff --git a/core/test/io_test.jl b/core/test/io_test.jl index 2faa419ca..e113b5ccc 100644 --- a/core/test/io_test.jl +++ b/core/test/io_test.jl @@ -8,6 +8,7 @@ database = "path/to/file", input_dir = ".", results_dir = "results", + ribasim_version = string(Ribasim.pkgversion(Ribasim)), ) config = Ribasim.Config(toml, "model") @test Ribasim.input_path(config, "path/to/file") == @@ -20,6 +21,7 @@ database = "path/to/file", input_dir = "input", results_dir = "results", + ribasim_version = string(Ribasim.pkgversion(Ribasim)), ) config = Ribasim.Config(toml, "model") @test Ribasim.input_path(config, "path/to/file") == @@ -32,6 +34,7 @@ database = "/path/to/file", input_dir = ".", results_dir = "results", + ribasim_version = string(Ribasim.pkgversion(Ribasim)), ) config = Ribasim.Config(toml) @test Ribasim.input_path(config, "/path/to/file") == abspath("/path/to/file") diff --git a/core/test/main_test.jl b/core/test/main_test.jl index fd21a9537..042751719 100644 --- a/core/test/main_test.jl +++ b/core/test/main_test.jl @@ -10,9 +10,18 @@ end @testitem "toml_path" begin using IOCapture: capture + import TOML model_path = normpath(@__DIR__, "../../generated_testmodels/basic/") toml_path = normpath(model_path, "ribasim.toml") + + # change the ribasim_version in the toml file to check warning + toml_dict = TOML.parsefile(toml_path) + toml_dict["ribasim_version"] = "a_different_version" + open(toml_path, "w") do io + TOML.print(io, toml_dict) + end + @test ispath(toml_path) (; value, output, error, backtrace) = capture() do Ribasim.main([toml_path]) @@ -23,6 +32,7 @@ end @show error @show backtrace end + @test occursin("version in the TOML config file does not match", output) end @testitem "too many arguments for main" begin diff --git a/docs/contribute/release.qmd b/docs/contribute/release.qmd index e34951225..576f45789 100644 --- a/docs/contribute/release.qmd +++ b/docs/contribute/release.qmd @@ -25,13 +25,8 @@ Determine the new version number like `2023.1.0`, filling in the current year, a This follows `YYYY.MINOR.MICRO` from [calver](https://calver.org/). Update the version numbers in the repository to the new version number. -A single find and replace can update all 5 locations: - -- `pixi.toml` -- `core/Project.toml` -- `python/ribasim/ribasim/__init__.py` -- `python/ribasim_api/ribasim_api/__init__.py` -- `ribasim_qgis/metadata.txt` +A single find and replace can update all locations. +The Ribasim version number is not in `pixi.lock`, that file does not need updating. Now submit a pull request to update the version numbers. diff --git a/python/ribasim/ribasim/model.py b/python/ribasim/ribasim/model.py index 702804df8..0a5e457a8 100644 --- a/python/ribasim/ribasim/model.py +++ b/python/ribasim/ribasim/model.py @@ -16,6 +16,7 @@ model_validator, ) +import ribasim from ribasim.config import ( Allocation, Basin, @@ -229,6 +230,7 @@ def _write_toml(self, fn: FilePath): content = self.model_dump(exclude_unset=True, exclude_none=True, by_alias=True) # Filter empty dicts (default Nodes) content = dict(filter(lambda x: x[1], content.items())) + content["ribasim_version"] = ribasim.__version__ with open(fn, "wb") as f: tomli_w.dump(content, f) return fn diff --git a/python/ribasim/tests/test_io.py b/python/ribasim/tests/test_io.py index 542afc6f9..b7ec0229e 100644 --- a/python/ribasim/tests/test_io.py +++ b/python/ribasim/tests/test_io.py @@ -1,6 +1,7 @@ import pandas as pd import pytest import ribasim +import tomli from numpy.testing import assert_array_equal from pandas import DataFrame from pandas.testing import assert_frame_equal @@ -36,8 +37,14 @@ def __assert_equal(a: DataFrame, b: DataFrame, is_network=False) -> None: def test_basic(basic, tmp_path): model_orig = basic - model_orig.write(tmp_path / "basic/ribasim.toml") - model_loaded = ribasim.Model(filepath=tmp_path / "basic/ribasim.toml") + toml_path = tmp_path / "basic/ribasim.toml" + model_orig.write(toml_path) + model_loaded = ribasim.Model(filepath=toml_path) + + with open(toml_path, "rb") as f: + toml_dict = tomli.load(f) + + assert toml_dict["ribasim_version"] == ribasim.__version__ index_a = model_orig.network.node.df.index.to_numpy(int) index_b = model_loaded.network.node.df.index.to_numpy(int)