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