Skip to content

Commit

Permalink
fix: git postgres config bug when a different version is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Nov 18, 2024
1 parent 55989cc commit 0cf0a2c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/install/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import re
from contextlib import suppress
Expand All @@ -8,6 +10,10 @@
from helperFunctions.install import InstallationError, OperateInDirectory, check_distribution

POSTGRES_VERSION = 17
POSTGRES_UPDATE_MESSAGE = (
'Please see "https://github.com/fkie-cad/FACT_core/wiki/Upgrading-the-PostgreSQL-Database" for information on how'
'to upgrade your PostgreSQL version.'
)


def install_postgres(version: int = POSTGRES_VERSION):
Expand Down Expand Up @@ -35,28 +41,27 @@ def configure_postgres(version: int = POSTGRES_VERSION):
run('sudo service postgresql restart', shell=True, check=True)


def postgres_is_up_to_date():
def get_postgres_version() -> int:
proc = run(split('psql --version'), text=True, capture_output=True, check=True)
match = re.search(r'PostgreSQL\)? (\d+).\d+', proc.stdout)
if match:
return int(match.groups()[0]) >= POSTGRES_VERSION
logging.warning('PostgreSQL version could not be identified. Is it installed?')
return True
return int(match.groups()[0])
raise InstallationError(
f'PostgreSQL is installed but version could not be identified: {proc.stdout}. {POSTGRES_UPDATE_MESSAGE}'
)


def main():
postgres_version: int | None = None
try:
if not postgres_is_up_to_date():
logging.warning(
'PostgreSQL is installed but the version is not up to date. Please see '
'"https://github.com/fkie-cad/FACT_core/wiki/Upgrading-the-PostgreSQL-Database" for information on how'
'to upgrade your PostgreSQL version.'
)
postgres_version = get_postgres_version()
if not postgres_version >= POSTGRES_VERSION:
logging.warning(f'PostgreSQL is installed but the version is not up to date. {POSTGRES_UPDATE_MESSAGE}')
logging.info('Skipping PostgreSQL installation. Reason: Already installed.')
except (CalledProcessError, FileNotFoundError): # psql binary was not found
logging.info('Setting up PostgreSQL database')
install_postgres()
configure_postgres()
configure_postgres(postgres_version)

# initializing DB
logging.info('Initializing PostgreSQL database')
Expand Down

0 comments on commit 0cf0a2c

Please sign in to comment.