stac-utils / pgstac

Schema, functions and a python library for storing and accessing STAC collections and items in PostgreSQL
MIT License
153 stars 39 forks source link

pypgstac migrate Can't Be Used on Developer Versions of Postgres #300

Closed MathewNWSH closed 2 months ago

MathewNWSH commented 3 months ago

Using the postgis/postgis:17beta3-master image:

eouser@pgstac-s2l2a:~$ pypgstac migrate
Traceback (most recent call last):
  File "/home/eouser/.local/bin/pypgstac", line 8, in <module>
    sys.exit(cli())
  File "/home/eouser/.local/lib/python3.10/site-packages/pypgstac/pypgstac.py", line 125, in cli
    fire.Fire(PgstacCLI)
  File "/home/eouser/.local/lib/python3.10/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/eouser/.local/lib/python3.10/site-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/home/eouser/.local/lib/python3.10/site-packages/fire/core.py", line 681, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/eouser/.local/lib/python3.10/site-packages/pypgstac/pypgstac.py", line 61, in migrate
    return migrator.run_migration(toversion=toversion)
  File "/home/eouser/.local/lib/python3.10/site-packages/pypgstac/migrate.py", line 125, in run_migration
    pg_version = self.db.pg_version
  File "/home/eouser/.local/lib/python3.10/site-packages/pypgstac/db.py", line 280, in pg_version
    if int(version.split(".")[0]) < 13:
ValueError: invalid literal for int() with base 10: '17beta3 (Debian 17~beta3-1'

A "goofy" workaround that bypasses the issue by attempting to catch the ValueError and proceeding accordingly:

    @property
    def pg_version(self) -> str:
        """Get the current pg version number from a pgstac database."""
        version = self.query_one(
            """
            SHOW server_version;
            """,
        )
        logger.debug(f"PG VERSION: {version}.")
        if isinstance(version, bytes):
            version = version.decode()
        if isinstance(version, str):
            try:
                if int(version.split(".")[0]) < 13:
                    raise Exception("PgSTAC requires PostgreSQL 13+")
                return version
            except ValueError:
                print("Warning: Developer version of PostgreSQL deteced")
                pass
        else:
            if self.connection is not None:
                self.connection.rollback()
            raise Exception("Could not find PG version.")
drnextgis commented 2 months ago

The issue seems resolved with the changes in https://github.com/stac-utils/pgstac/pull/304, so I believe it can be closed.

vincentsarago commented 2 months ago

Oh I didn't see the issue 😓

I've fixed the version issue but I still think there is a bigger problem with pgstac and pg17.

I'll try to run the test with pg17 and open an issue later 🙏