Skip to content

Commit

Permalink
fix: on db & env init ch, check tables exist prior to sql select
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed May 2, 2023
1 parent 2a05d13 commit 13c21fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
19 changes: 9 additions & 10 deletions ckan/config/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from typing import Union, cast

import sqlalchemy
from sqlalchemy import engine_from_config, inspect
import sqlalchemy.exc

import ckan.model as model
Expand Down Expand Up @@ -220,7 +220,7 @@ def update_config() -> None:
# to eliminate database errors due to stale pooled connections
config.setdefault('sqlalchemy.pool_pre_ping', True)
# Initialize SQLAlchemy
engine = sqlalchemy.engine_from_config(config)
engine = engine_from_config(config)
model.init_model(engine)

for plugin in p.PluginImplementations(p.IConfigurable):
Expand All @@ -232,14 +232,13 @@ def update_config() -> None:
authz.clear_auth_functions_cache()

# Here we create the site user if they are not already in the database
try:
logic.get_action('get_site_user')({'ignore_auth': True}, {})
except (sqlalchemy.exc.ProgrammingError, sqlalchemy.exc.OperationalError):
# The database is not yet initialised. It happens in `ckan db init`
pass
except sqlalchemy.exc.IntegrityError:
# Race condition, user already exists.
pass
db_inspect = inspect(engine)
if db_inspect.has_table("user"):
try:
logic.get_action('get_site_user')({'ignore_auth': True}, {})
except sqlalchemy.exc.IntegrityError:
# Race condition, user already exists.
pass

# Close current session and open database connections to ensure a clean
# clean environment even if an error occurs later on
Expand Down
10 changes: 5 additions & 5 deletions ckan/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from time import sleep
from typing import Any, Optional

from sqlalchemy import MetaData, Table
from sqlalchemy.exc import ProgrammingError
from sqlalchemy import MetaData, Table, inspect

from alembic.command import (
upgrade as alembic_upgrade,
Expand Down Expand Up @@ -278,12 +277,13 @@ def setup_migration_version_control(self) -> None:
alembic_config.set_main_option(
"sqlalchemy.url", config.get("sqlalchemy.url")
)
try:

sqlalchemy_migrate_version = 0
db_inspect = inspect(self.metadata.bind)
if db_inspect.has_table("migrate_version"):
sqlalchemy_migrate_version = self.metadata.bind.execute(
u'select version from migrate_version'
).scalar()
except ProgrammingError:
sqlalchemy_migrate_version = 0

# this value is used for graceful upgrade from
# sqlalchemy-migrate to alembic
Expand Down

0 comments on commit 13c21fb

Please sign in to comment.