Skip to content

Commit

Permalink
Merge pull request #7681 from ckan/db-migration-fixes
Browse files Browse the repository at this point in the history
`ckan db upgrade` fixes
  • Loading branch information
smotornyuk committed Jul 25, 2023
2 parents 38cc9bd + 6ed5aa1 commit 31333cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/7681.misc
@@ -0,0 +1 @@
Fix errors when running the `ckan db upgrade` command
5 changes: 5 additions & 0 deletions ckan/config/environment.py
Expand Up @@ -249,6 +249,11 @@ def update_config() -> None:
if user_table_exists:
try:
logic.get_action('get_site_user')({'ignore_auth': True}, {})
except sqlalchemy.exc.ProgrammingError as e:
if "UndefinedColumn" in repr(e.orig):
log.debug("Old user model detected")
else:
raise
except sqlalchemy.exc.IntegrityError:
# Race condition, user already exists.
log.debug("Site user already exists")
Expand Down
9 changes: 9 additions & 0 deletions ckan/migration/migrate_package_activity.py
Expand Up @@ -32,6 +32,7 @@
import argparse
from collections import defaultdict
from typing import Any
import sys


# not importing anything from ckan until after the arg parsing, to fail on bad
Expand Down Expand Up @@ -274,6 +275,8 @@ def print_errors(errors):
args = parser.parse_args()
assert args.config, u'You must supply a --config'
print(u'Loading config')

from ckan.plugins import plugin_loaded
try:
from ckan.cli import load_config
from ckan.config.middleware import make_app
Expand All @@ -291,6 +294,12 @@ class Options(object):
cmd._load_config()
return
load_config(args.config)

if not plugin_loaded("activity"):
print(
"Please add the `activity` plugin to your `ckan.plugins` setting")
sys.exit(1)

if not args.dataset:
migrate_all_datasets()
wipe_activity_detail(delete_activity_detail=args.delete)
Expand Down
8 changes: 4 additions & 4 deletions ckan/migration/revision_legacy_code.py
Expand Up @@ -26,7 +26,7 @@
# This is based on ckan.lib.dictization.model_dictize:package_dictize
# BUT you can ask for a old revision to the package by specifying 'revision_id'
# in the context
def package_dictize_with_revisions(pkg, context):
def package_dictize_with_revisions(pkg, context, include_plugin_data=False):
'''
Given a Package object, returns an equivalent dictionary.
Expand Down Expand Up @@ -306,7 +306,8 @@ def copy_table_columns(table):
# Copied from vdm
def copy_table(table, newtable):
for key in table.c.keys():
copy_column(key, table, newtable)
if key != "plugin_data":
copy_column(key, table, newtable)


# Copied from vdm
Expand All @@ -325,8 +326,7 @@ def make_revision_table(metadata):

# Copied from vdm
def make_Revision(mapper, revision_table): # noqa
mapper(Revision, revision_table, properties={},
order_by=revision_table.c.timestamp.desc())
mapper(Revision, revision_table, properties={})
return Revision


Expand Down

0 comments on commit 31333cb

Please sign in to comment.