Show More
@@ -1,87 +1,88 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | import hashlib |
|
4 | 4 | import logging |
|
5 | 5 | |
|
6 | 6 | from alembic.migration import MigrationContext |
|
7 | 7 | from alembic.operations import Operations |
|
8 | 8 | from sqlalchemy import Text, String, Column |
|
9 | 9 | from sqlalchemy.engine import reflection |
|
10 | 10 | from sqlalchemy.sql import text |
|
11 | 11 | |
|
12 | 12 | from rhodecode.lib.dbmigrate.versions import _reset_base |
|
13 | 13 | from rhodecode.lib.utils2 import safe_str |
|
14 | 14 | from rhodecode.model import meta, init_model_encryption |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | log = logging.getLogger(__name__) |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | def upgrade(migrate_engine): |
|
21 | 21 | """ |
|
22 | 22 | Upgrade operations go here. |
|
23 | 23 | Don't create your own engine; bind migrate_engine to your metadata |
|
24 | 24 | """ |
|
25 | 25 | _reset_base(migrate_engine) |
|
26 | 26 | from rhodecode.lib.dbmigrate.schema import db_3_7_0_0 |
|
27 | 27 | |
|
28 | 28 | init_model_encryption(db_3_7_0_0) |
|
29 | 29 | |
|
30 | 30 | context = MigrationContext.configure(migrate_engine.connect()) |
|
31 | 31 | op = Operations(context) |
|
32 | 32 | |
|
33 | 33 | repository = db_3_7_0_0.Repository.__table__ |
|
34 | 34 | repo_name_column = repository.columns.repo_name |
|
35 | 35 | clone_uri_column = repository.columns.clone_uri |
|
36 | 36 | |
|
37 | 37 | indexes = _get_indexes_list(migrate_engine, repository.name) |
|
38 | 38 | repo_name_indexes = [ |
|
39 | 39 | i['name'] for i in indexes if 'repo_name' in i['column_names']] |
|
40 | 40 | constraints = _get_unique_constraint_list(migrate_engine, repository.name) |
|
41 | 41 | repo_name_constraints = [ |
|
42 | 42 | c['name'] for c in constraints if 'repo_name' in c['column_names']] |
|
43 | 43 | |
|
44 | 44 | with op.batch_alter_table(repository.name) as batch_op: |
|
45 | 45 | repo_name_idx = 'r_repo_name_idx' |
|
46 | 46 | if repo_name_idx in repo_name_indexes: |
|
47 | 47 | batch_op.drop_index(repo_name_idx) |
|
48 | 48 | for name in repo_name_constraints: |
|
49 | batch_op.drop_constraint(name, type_='unique') | |
|
49 | if name: # sqlite can have this empty, then it raises an error | |
|
50 | batch_op.drop_constraint(name, type_='unique') | |
|
50 | 51 | |
|
51 | 52 | batch_op.alter_column(repo_name_column.name, type_=Text) |
|
52 | 53 | batch_op.alter_column(clone_uri_column.name, type_=Text) |
|
53 | 54 | batch_op.create_index( |
|
54 | 55 | 'r_repo_name_idx', ['repo_name'], mysql_length=255) |
|
55 | 56 | batch_op.add_column(Column('repo_name_hash', String(40), unique=False)) |
|
56 | 57 | |
|
57 | 58 | _generate_repo_name_hashes(db_3_7_0_0, op, meta.Session) |
|
58 | 59 | |
|
59 | 60 | with op.batch_alter_table(repository.name) as batch_op: |
|
60 | 61 | batch_op.create_unique_constraint( |
|
61 | 62 | 'uq_repo_name_hash', ['repo_name_hash']) |
|
62 | 63 | |
|
63 | 64 | |
|
64 | 65 | def downgrade(migrate_engine): |
|
65 | 66 | pass |
|
66 | 67 | |
|
67 | 68 | |
|
68 | 69 | def _generate_repo_name_hashes(models, op, session): |
|
69 | 70 | repositories = models.Repository.get_all() |
|
70 | 71 | for repository in repositories: |
|
71 | 72 | hash_ = hashlib.sha1(safe_str(repository.repo_name)).hexdigest() |
|
72 | 73 | params = {'hash': hash_, 'id': repository.repo_id} |
|
73 | 74 | query = text( |
|
74 | 75 | 'UPDATE repositories SET repo_name_hash = :hash' |
|
75 | 76 | ' WHERE repo_id = :id').bindparams(**params) |
|
76 | 77 | op.execute(query) |
|
77 | 78 | session().commit() |
|
78 | 79 | |
|
79 | 80 | |
|
80 | 81 | def _get_unique_constraint_list(migrate_engine, table_name): |
|
81 | 82 | inspector = reflection.Inspector.from_engine(migrate_engine) |
|
82 | 83 | return inspector.get_unique_constraints(table_name) |
|
83 | 84 | |
|
84 | 85 | |
|
85 | 86 | def _get_indexes_list(migrate_engine, table_name): |
|
86 | 87 | inspector = reflection.Inspector.from_engine(migrate_engine) |
|
87 | 88 | return inspector.get_indexes(table_name) |
General Comments 0
You need to be logged in to leave comments.
Login now