Skip to content

Commit

Permalink
Merge pull request ckan#8336 from ckan/create-from-models-alembic
Browse files Browse the repository at this point in the history
ckan db create-from-model: update alembic too
  • Loading branch information
tino097 authored Jul 25, 2024
2 parents e86b3a3 + 6c14ff3 commit 3eaa299
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions changes/8336.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`ckan db upgrade` can now be used after `ckan db create-from-model`
13 changes: 11 additions & 2 deletions ckan/cli/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ def create_from_model():
"""
try:
model.repo.create_db()
except Exception as e:
error_shout(e)
model.repo.stamp_alembic_head()

# also mark plugins as migrated
# FIXME: move to model.repo?
pending = _get_pending_plugins()
for plugin in sorted(pending):
with _repo_for_plugin(plugin) as repo:
print(plugin, repo)
repo.stamp_alembic_head()
except Exception:
raise
else:
click.secho('Create DB from model: SUCCESS', fg='green', bold=True)

Expand Down
12 changes: 11 additions & 1 deletion ckan/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from alembic.command import (
upgrade as alembic_upgrade,
downgrade as alembic_downgrade,
current as alembic_current
current as alembic_current,
stamp as alembic_stamp,
)
from alembic.config import Config as AlembicConfig

Expand Down Expand Up @@ -241,8 +242,17 @@ def create_db(self) -> None:
'''
with ensure_engine().begin() as conn:
self.metadata.create_all(conn)

log.info('Database tables created')

def stamp_alembic_head(self):
'''mark database as up to date for alembic'''
alembic_config = AlembicConfig(self._alembic_ini)
alembic_config.set_main_option(
"sqlalchemy.url", config.get("sqlalchemy.url")
)
alembic_stamp(alembic_config, 'head')

def rebuild_db(self) -> None:
'''Clean and init the db'''
if self.tables_created_and_initialised:
Expand Down

0 comments on commit 3eaa299

Please sign in to comment.