096_version_4_17_0.py
55 lines
| 1.6 KiB
| text/x-python
|
PythonLexer
r3623 | # -*- coding: utf-8 -*- | |||
import logging | ||||
from alembic.migration import MigrationContext | ||||
from alembic.operations import Operations | ||||
from sqlalchemy import String, Column | ||||
from sqlalchemy.sql import text | ||||
from rhodecode.lib.dbmigrate.versions import _reset_base | ||||
r3677 | from rhodecode.lib.utils2 import safe_str | |||
r3623 | from rhodecode.model import meta, init_model_encryption | |||
from rhodecode.model.db import RepoGroup | ||||
log = logging.getLogger(__name__) | ||||
def upgrade(migrate_engine): | ||||
""" | ||||
Upgrade operations go here. | ||||
Don't create your own engine; bind migrate_engine to your metadata | ||||
""" | ||||
_reset_base(migrate_engine) | ||||
from rhodecode.lib.dbmigrate.schema import db_4_16_0_2 | ||||
init_model_encryption(db_4_16_0_2) | ||||
context = MigrationContext.configure(migrate_engine.connect()) | ||||
op = Operations(context) | ||||
repo_group = db_4_16_0_2.RepoGroup.__table__ | ||||
with op.batch_alter_table(repo_group.name) as batch_op: | ||||
batch_op.add_column( | ||||
Column("repo_group_name_hash", String(1024), nullable=True, unique=False)) | ||||
_generate_repo_group_name_hashes(db_4_16_0_2, op, meta.Session) | ||||
def downgrade(migrate_engine): | ||||
pass | ||||
def _generate_repo_group_name_hashes(models, op, session): | ||||
repo_groups = models.RepoGroup.get_all() | ||||
for repo_group in repo_groups: | ||||
r3677 | print(safe_str(repo_group.group_name)) | |||
r3623 | hash_ = RepoGroup.hash_repo_group_name(repo_group.group_name) | |||
params = {'hash': hash_, 'id': repo_group.group_id} | ||||
query = text( | ||||
'UPDATE groups SET repo_group_name_hash = :hash' | ||||
' WHERE group_id = :id').bindparams(**params) | ||||
op.execute(query) | ||||
session().commit() | ||||