annotate rhodecode/lib/dbmigrate/versions/008_version_1_5_0.py @ 3065:09e8623362ef beta

remove not null from user_id column for users log archiving
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 06 Dec 2012 00:53:49 +0100
parents ca2b21819dfd
children f9c44f3ed4c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3052
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 import logging
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 import datetime
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 from sqlalchemy import *
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 from sqlalchemy.exc import DatabaseError
3063
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
6 from sqlalchemy.orm import relation, backref, class_mapper, joinedload
3052
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 from sqlalchemy.orm.session import Session
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 from sqlalchemy.ext.declarative import declarative_base
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 from rhodecode.lib.dbmigrate.migrate import *
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 from rhodecode.lib.dbmigrate.migrate.changeset import *
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 from rhodecode.model.meta import Base
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 from rhodecode.model import meta
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 log = logging.getLogger(__name__)
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 def upgrade(migrate_engine):
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 """
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 Upgrade operations go here.
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 Don't create your own engine; bind migrate_engine to your metadata
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 """
3063
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
24 #==========================================================================
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
25 # USER LOGS
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
26 #==========================================================================
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
27 from rhodecode.lib.dbmigrate.schema.db_1_5_0 import UserLog
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
28 tbl = UserLog.__table__
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
29 username = Column("username", String(255, convert_unicode=False,
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
30 assert_unicode=None), nullable=True,
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
31 unique=None, default=None)
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
32 # create username column
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
33 username.create(table=tbl)
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
34
3065
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
35 #alter user_id to not null
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
36 from rhodecode.lib.dbmigrate.schema.db_1_5_0 import UserLog
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
37 tbl_name = UserLog.__tablename__
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
38 tbl = Table(tbl_name,
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
39 MetaData(bind=migrate_engine), autoload=True,
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
40 autoload_with=migrate_engine)
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
41 col = tbl.columns.user_id
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
42
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
43 # remove nullability from revision field
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
44 col.alter(nullable=True)
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
45
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
46
09e8623362ef remove not null from user_id column for users log archiving
Marcin Kuzminski <marcin@python-works.com>
parents: 3063
diff changeset
47
3063
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
48 ## after adding that column fix all usernames
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
49 users_log = UserLog.query()\
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
50 .options(joinedload(UserLog.user))\
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
51 .options(joinedload(UserLog.repository)).all()
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
52 for entry in users_log:
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
53 entry.username = entry.user.username
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
54 Session().add(entry)
ca2b21819dfd Implemented better support for Wildcard queries
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
55 Session().commit()
3052
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
3056
6104dfd35b16 Implemented #379 defaults settings page for creation of repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3052
diff changeset
57
3052
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 def downgrade(migrate_engine):
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 meta = MetaData()
d3200c58764e implemented #663 Admin/permission: specify default repogroup perms
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 meta.bind = migrate_engine