Skip to content

Commit

Permalink
TGIS DB v3 backward compatible with v2 (#1454)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Neteler <neteler@gmail.com>
  • Loading branch information
landam and neteler committed Dec 4, 2021
1 parent 79b77ba commit facbacc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
6 changes: 0 additions & 6 deletions lib/temporal/SQL/update_strds_metadata_template.sql
Expand Up @@ -41,9 +41,3 @@ UPDATE strds_metadata SET ewres_max =
(SELECT max(ewres) FROM raster_metadata WHERE raster_metadata.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE)
) WHERE id = 'SPACETIME_ID';
-- Update the number of registered bands
UPDATE strds_metadata SET number_of_bands =
(SELECT count(distinct semantic_label) FROM raster_metadata WHERE
raster_metadata.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE)
) WHERE id = 'SPACETIME_ID';
15 changes: 15 additions & 0 deletions lib/temporal/SQL/update_strds_metadata_template_v3.sql
@@ -0,0 +1,15 @@
--#############################################################################
-- This SQL is to update a space-time raster dataset metadata (version 3+)
--
-- Author: Soeren Gebbert soerengebbert <at> googlemail <dot> com
--#############################################################################

-- SPACETIME_REGISTER_TABLE is a placeholder for specific stds map register table name (SQL compliant)
-- SPACETIME_ID is a placeholder for specific stds id: name@mapset

-- Update the number of registered bands
UPDATE strds_metadata SET number_of_bands =
(SELECT count(distinct semantic_label) FROM raster_metadata WHERE
raster_metadata.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE)
) WHERE id = 'SPACETIME_ID';
13 changes: 12 additions & 1 deletion python/grass/temporal/abstract_space_time_dataset.py
Expand Up @@ -22,6 +22,7 @@ class that is the base class for all space time datasets.
get_tgis_metadata,
get_current_mapset,
get_enable_mapset_check,
get_tgis_db_version_from_metadata,
)
from .abstract_dataset import AbstractDataset, AbstractDatasetComparisonKeyStartTime
from .temporal_granularity import (
Expand Down Expand Up @@ -2513,7 +2514,6 @@ def update_from_registered_maps(self, dbif=None):
:param dbif: The database interface to be used
"""

if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
Expand Down Expand Up @@ -2576,6 +2576,17 @@ def update_from_registered_maps(self, dbif=None):
),
"r",
).read()

for version in range(3, get_tgis_db_version_from_metadata() + 1):
sqlfile = os.path.join(
sql_path,
"update_"
+ self.get_type()
+ "_metadata_template_v{}.sql".format(version),
)
if os.path.exists(sqlfile):
sql += open(sqlfile).read()

sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())

Expand Down
66 changes: 46 additions & 20 deletions python/grass/temporal/core.py
Expand Up @@ -363,8 +363,8 @@ def get_raise_on_error():


def get_tgis_version():
"""Get the version number of the temporal framework
:returns: The version number of the temporal framework as string
"""Get the supported version of the temporal framework
:returns: The version number of the temporal framework as integer
"""
global tgis_version
return tgis_version
Expand All @@ -374,13 +374,29 @@ def get_tgis_version():


def get_tgis_db_version():
"""Get the version number of the temporal framework
:returns: The version number of the temporal framework as string
"""Get the supported version of the temporal database
:returns: The version number of the temporal database as integer
"""
global tgis_db_version
return tgis_db_version


def get_tgis_db_version_from_metadata(metadata=None):
"""Get the version number of the temporal database from metadata
:param list metadata: list of metadata items or None
:returns: The version number of the temporal database as integer
"""
if metadata is None:
metadata = get_tgis_metadata()
for entry in metadata:
if "tgis_db_version" in entry:
return int(entry[1])

# return supported version if not possible to get from metadata
return get_tgis_db_version()


###############################################################################


Expand Down Expand Up @@ -697,10 +713,7 @@ def init(raise_fatal_error=False, skip_db_version_check=False):
db_exists = True

backup_howto = _(
"The format of your actual temporal database is not "
"supported any more.\n"
"Please create a backup of your temporal database "
"to avoid lossing data.\nSOLUTION: "
"Please create a backup of your temporal database to avoid losing data.\n"
)
if tgis_db_version > 2:
backup_howto += _("Run t.upgrade command to upgrade your temporal database.\n")
Expand Down Expand Up @@ -738,6 +751,8 @@ def init(raise_fatal_error=False, skip_db_version_check=False):
)
% ({"info": get_database_info_string()})
)

# temporal framework version check
for entry in metadata:
if "tgis_version" in entry and entry[1] != str(get_tgis_version()):
msgr.fatal(
Expand All @@ -756,22 +771,33 @@ def init(raise_fatal_error=False, skip_db_version_check=False):
}
)
)
if "tgis_db_version" in entry and entry[1] != str(get_tgis_db_version()):

# temporal database version check
tgis_db_version_meta = get_tgis_db_version_from_metadata(metadata)
if tgis_db_version_meta != tgis_db_version:
message = _(
"Temporal database version mismatch detected.\n{backup}"
"Supported temporal database version is: {tdb}\n"
"Your existing temporal database version: {ctdb}\n"
"Current temporal database info: {info}".format(
backup=backup_howto,
tdb=tgis_db_version,
ctdb=tgis_db_version_meta,
info=get_database_info_string(),
)
)

if tgis_db_version_meta == 2 and tgis_db_version == 3:
# version 3 is backward compatible with version 2
msgr.warning(message)
else:
msgr.fatal(
_(
"Unsupported temporal database: version mismatch."
"\n %(backup)sSupported temporal database version"
" is: %(tdb)i\nCurrent temporal database info:"
"%(info)s"
)
% (
{
"backup": backup_howto,
"tdb": get_tgis_version(),
"info": get_database_info_string(),
}
"The format of your actual temporal database is "
"not supported any more. {m}".format(m=message)
)
)

return

create_temporal_database(dbif)
Expand Down
11 changes: 8 additions & 3 deletions python/grass/temporal/metadata.py
Expand Up @@ -23,7 +23,7 @@
"""
from __future__ import print_function
from .base import SQLDatabaseInterface
from .core import SQLDatabaseInterfaceConnection
from .core import SQLDatabaseInterfaceConnection, get_tgis_db_version_from_metadata

###############################################################################

Expand Down Expand Up @@ -355,7 +355,8 @@ def __init__(
max,
)

self.set_semantic_label(semantic_label)
if get_tgis_db_version_from_metadata() > 2:
self.set_semantic_label(semantic_label)

def set_semantic_label(self, semantic_label):
"""Set the semantic label identifier"""
Expand Down Expand Up @@ -1372,7 +1373,8 @@ def __init__(self, ident=None, raster_register=None, title=None, description=Non
self, "strds_metadata", ident, title, description
)

self.D["number_of_bands"] = None
if get_tgis_db_version_from_metadata() > 2:
self.D["number_of_bands"] = None

self.set_raster_register(raster_register)

Expand Down Expand Up @@ -1403,6 +1405,9 @@ def get_band_names(self):
and fetched on-the-fly
:return: None if not found
"""
if get_tgis_db_version_from_metadata() <= 2:
# band names supported from TGIS DB version 3
return None

sql = "SELECT distinct semantic_label FROM %s WHERE %s.id " % (
"raster_metadata",
Expand Down

0 comments on commit facbacc

Please sign in to comment.