##// END OF EJS Templates
fixes for #143
marcink -
r1182:a9550b59 beta
parent child Browse files
Show More
@@ -1,237 +1,205
1 #==============================================================================
1 #==============================================================================
2 # DB INITIAL MODEL
2 # DB INITIAL MODEL
3 #==============================================================================
3 #==============================================================================
4 import logging
4 import logging
5 import datetime
5 import datetime
6
6
7 from sqlalchemy import *
7 from sqlalchemy import *
8 from sqlalchemy.exc import DatabaseError
8 from sqlalchemy.exc import DatabaseError
9 from sqlalchemy.orm import relation, backref, class_mapper
9 from sqlalchemy.orm import relation, backref, class_mapper
10 from sqlalchemy.orm.session import Session
10 from sqlalchemy.orm.session import Session
11 from rhodecode.model.meta import Base
11 from rhodecode.model.meta import Base
12
12
13 from rhodecode.lib.dbmigrate.migrate import *
13 from rhodecode.lib.dbmigrate.migrate import *
14
14
15 log = logging.getLogger(__name__)
15 log = logging.getLogger(__name__)
16
16
17 class BaseModel(object):
17 class RhodeCodeSettings(Base):
18
19 @classmethod
20 def _get_keys(cls):
21 """return column names for this model """
22 return class_mapper(cls).c.keys()
23
24 def get_dict(self):
25 """return dict with keys and values corresponding
26 to this model data """
27
28 d = {}
29 for k in self._get_keys():
30 d[k] = getattr(self, k)
31 return d
32
33 def get_appstruct(self):
34 """return list with keys and values tupples corresponding
35 to this model data """
36
37 l = []
38 for k in self._get_keys():
39 l.append((k, getattr(self, k),))
40 return l
41
42 def populate_obj(self, populate_dict):
43 """populate model with data from given populate_dict"""
44
45 for k in self._get_keys():
46 if k in populate_dict:
47 setattr(self, k, populate_dict[k])
48
49 class RhodeCodeSettings(Base, BaseModel):
50 __tablename__ = 'rhodecode_settings'
18 __tablename__ = 'rhodecode_settings'
51 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
19 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
52 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
20 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
53 app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
21 app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
54 app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
22 app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
55
23
56 def __init__(self, k, v):
24 def __init__(self, k, v):
57 self.app_settings_name = k
25 self.app_settings_name = k
58 self.app_settings_value = v
26 self.app_settings_value = v
59
27
60 def __repr__(self):
28 def __repr__(self):
61 return "<RhodeCodeSetting('%s:%s')>" % (self.app_settings_name,
29 return "<RhodeCodeSetting('%s:%s')>" % (self.app_settings_name,
62 self.app_settings_value)
30 self.app_settings_value)
63
31
64 class RhodeCodeUi(Base, BaseModel):
32 class RhodeCodeUi(Base):
65 __tablename__ = 'rhodecode_ui'
33 __tablename__ = 'rhodecode_ui'
66 __table_args__ = {'useexisting':True}
34 __table_args__ = {'useexisting':True}
67 ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
35 ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
68 ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
36 ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
69 ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
37 ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
70 ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
38 ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
71 ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True)
39 ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True)
72
40
73
41
74 class User(Base, BaseModel):
42 class User(Base):
75 __tablename__ = 'users'
43 __tablename__ = 'users'
76 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
44 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
77 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
45 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
78 username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
46 username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
79 password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
47 password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
80 active = Column("active", Boolean(), nullable=True, unique=None, default=None)
48 active = Column("active", Boolean(), nullable=True, unique=None, default=None)
81 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
49 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
82 name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
50 name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
83 lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
51 lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
84 email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
52 email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
85 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
53 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
86 is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False)
54 is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False)
87
55
88 user_log = relation('UserLog', cascade='all')
56 user_log = relation('UserLog', cascade='all')
89 user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
57 user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
90
58
91 repositories = relation('Repository')
59 repositories = relation('Repository')
92 user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
60 user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
93
61
94 @property
62 @property
95 def full_contact(self):
63 def full_contact(self):
96 return '%s %s <%s>' % (self.name, self.lastname, self.email)
64 return '%s %s <%s>' % (self.name, self.lastname, self.email)
97
65
98 def __repr__(self):
66 def __repr__(self):
99 return "<User('id:%s:%s')>" % (self.user_id, self.username)
67 return "<User('id:%s:%s')>" % (self.user_id, self.username)
100
68
101 def update_lastlogin(self):
69 def update_lastlogin(self):
102 """Update user lastlogin"""
70 """Update user lastlogin"""
103
71
104 try:
72 try:
105 session = Session.object_session(self)
73 session = Session.object_session(self)
106 self.last_login = datetime.datetime.now()
74 self.last_login = datetime.datetime.now()
107 session.add(self)
75 session.add(self)
108 session.commit()
76 session.commit()
109 log.debug('updated user %s lastlogin', self.username)
77 log.debug('updated user %s lastlogin', self.username)
110 except (DatabaseError,):
78 except (DatabaseError,):
111 session.rollback()
79 session.rollback()
112
80
113
81
114 class UserLog(Base, BaseModel):
82 class UserLog(Base):
115 __tablename__ = 'user_logs'
83 __tablename__ = 'user_logs'
116 __table_args__ = {'useexisting':True}
84 __table_args__ = {'useexisting':True}
117 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
85 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
118 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
86 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
119 repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
87 repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
120 repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
88 repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
121 user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
89 user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
122 action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
90 action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
123 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
91 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
124
92
125 user = relation('User')
93 user = relation('User')
126 repository = relation('Repository')
94 repository = relation('Repository')
127
95
128 class Repository(Base, BaseModel):
96 class Repository(Base):
129 __tablename__ = 'repositories'
97 __tablename__ = 'repositories'
130 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
98 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
131 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
99 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
132 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
100 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
133 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
101 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
134 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
102 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
135 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
103 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
136 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
104 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
137 description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
105 description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
138 fork_id = Column("fork_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None)
106 fork_id = Column("fork_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None)
139
107
140 user = relation('User')
108 user = relation('User')
141 fork = relation('Repository', remote_side=repo_id)
109 fork = relation('Repository', remote_side=repo_id)
142 repo_to_perm = relation('RepoToPerm', cascade='all')
110 repo_to_perm = relation('RepoToPerm', cascade='all')
143 stats = relation('Statistics', cascade='all', uselist=False)
111 stats = relation('Statistics', cascade='all', uselist=False)
144
112
145 repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
113 repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
146
114
147
115
148 def __repr__(self):
116 def __repr__(self):
149 return "<Repository('%s:%s')>" % (self.repo_id, self.repo_name)
117 return "<Repository('%s:%s')>" % (self.repo_id, self.repo_name)
150
118
151 class Permission(Base, BaseModel):
119 class Permission(Base):
152 __tablename__ = 'permissions'
120 __tablename__ = 'permissions'
153 __table_args__ = {'useexisting':True}
121 __table_args__ = {'useexisting':True}
154 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
122 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
155 permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
123 permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
156 permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
124 permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
157
125
158 def __repr__(self):
126 def __repr__(self):
159 return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name)
127 return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name)
160
128
161 class RepoToPerm(Base, BaseModel):
129 class RepoToPerm(Base):
162 __tablename__ = 'repo_to_perm'
130 __tablename__ = 'repo_to_perm'
163 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
131 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
164 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
132 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
165 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
133 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
166 permission_id = Column("permission_id", Integer(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
134 permission_id = Column("permission_id", Integer(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
167 repository_id = Column("repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
135 repository_id = Column("repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
168
136
169 user = relation('User')
137 user = relation('User')
170 permission = relation('Permission')
138 permission = relation('Permission')
171 repository = relation('Repository')
139 repository = relation('Repository')
172
140
173 class UserToPerm(Base, BaseModel):
141 class UserToPerm(Base):
174 __tablename__ = 'user_to_perm'
142 __tablename__ = 'user_to_perm'
175 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
143 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
176 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
144 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
177 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
145 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
178 permission_id = Column("permission_id", Integer(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
146 permission_id = Column("permission_id", Integer(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
179
147
180 user = relation('User')
148 user = relation('User')
181 permission = relation('Permission')
149 permission = relation('Permission')
182
150
183 class Statistics(Base, BaseModel):
151 class Statistics(Base):
184 __tablename__ = 'statistics'
152 __tablename__ = 'statistics'
185 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
153 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
186 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
154 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
187 repository_id = Column("repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None)
155 repository_id = Column("repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None)
188 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
156 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
189 commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data
157 commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data
190 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
158 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
191 languages = Column("languages", LargeBinary(), nullable=False)#JSON data
159 languages = Column("languages", LargeBinary(), nullable=False)#JSON data
192
160
193 repository = relation('Repository', single_parent=True)
161 repository = relation('Repository', single_parent=True)
194
162
195 class UserFollowing(Base, BaseModel):
163 class UserFollowing(Base):
196 __tablename__ = 'user_followings'
164 __tablename__ = 'user_followings'
197 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
165 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
198 UniqueConstraint('user_id', 'follows_user_id')
166 UniqueConstraint('user_id', 'follows_user_id')
199 , {'useexisting':True})
167 , {'useexisting':True})
200
168
201 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
169 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
202 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
170 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
203 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None)
171 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None)
204 follows_user_id = Column("follows_user_id", Integer(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
172 follows_user_id = Column("follows_user_id", Integer(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
205
173
206 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
174 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
207
175
208 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
176 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
209 follows_repository = relation('Repository')
177 follows_repository = relation('Repository')
210
178
211
179
212 class CacheInvalidation(Base, BaseModel):
180 class CacheInvalidation(Base):
213 __tablename__ = 'cache_invalidation'
181 __tablename__ = 'cache_invalidation'
214 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
182 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
215 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
183 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
216 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
184 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
217 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
185 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
218 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
186 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
219
187
220
188
221 def __init__(self, cache_key, cache_args=''):
189 def __init__(self, cache_key, cache_args=''):
222 self.cache_key = cache_key
190 self.cache_key = cache_key
223 self.cache_args = cache_args
191 self.cache_args = cache_args
224 self.cache_active = False
192 self.cache_active = False
225
193
226 def __repr__(self):
194 def __repr__(self):
227 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key)
195 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key)
228
196
229
197
230 def upgrade(migrate_engine):
198 def upgrade(migrate_engine):
231 # Upgrade operations go here. Don't create your own engine; bind migrate_engine
199 # Upgrade operations go here. Don't create your own engine; bind migrate_engine
232 # to your metadata
200 # to your metadata
233 Base.metadata.create_all(bind=migrate_engine, checkfirst=False)
201 Base.metadata.create_all(bind=migrate_engine, checkfirst=False)
234
202
235 def downgrade(migrate_engine):
203 def downgrade(migrate_engine):
236 # Operations to reverse the above upgrade go here.
204 # Operations to reverse the above upgrade go here.
237 Base.metadata.drop_all(bind=migrate_engine, checkfirst=False)
205 Base.metadata.drop_all(bind=migrate_engine, checkfirst=False)
@@ -1,88 +1,117
1 import logging
1 import logging
2 import datetime
2 import datetime
3
3
4 from sqlalchemy import *
4 from sqlalchemy import *
5 from sqlalchemy.exc import DatabaseError
5 from sqlalchemy.exc import DatabaseError
6 from sqlalchemy.orm import relation, backref, class_mapper
6 from sqlalchemy.orm import relation, backref, class_mapper
7 from sqlalchemy.orm.session import Session
7 from sqlalchemy.orm.session import Session
8 from rhodecode.model.meta import Base
8 from rhodecode.model.meta import Base
9 from rhodecode.model.db import BaseModel
10
9
11 from rhodecode.lib.dbmigrate.migrate import *
10 from rhodecode.lib.dbmigrate.migrate import *
12 from rhodecode.lib.dbmigrate.migrate.changeset import *
11 from rhodecode.lib.dbmigrate.migrate.changeset import *
13
12
14 log = logging.getLogger(__name__)
13 log = logging.getLogger(__name__)
15
14
16 def upgrade(migrate_engine):
15 def upgrade(migrate_engine):
17 """ Upgrade operations go here.
16 """ Upgrade operations go here.
18 Don't create your own engine; bind migrate_engine to your metadata
17 Don't create your own engine; bind migrate_engine to your metadata
19 """
18 """
20
19
21 #==========================================================================
20 #==========================================================================
22 # Upgrade of `users` table
21 # Upgrade of `users` table
23 #==========================================================================
22 #==========================================================================
24 tblname = 'users'
23 tblname = 'users'
25 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
24 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
26 autoload_with=migrate_engine)
25 autoload_with=migrate_engine)
27
26
28 #ADD is_ldap column
27 #ADD is_ldap column
29 is_ldap = Column("is_ldap", Boolean(), nullable=True,
28 is_ldap = Column("is_ldap", Boolean(), nullable=True,
30 unique=None, default=False)
29 unique=None, default=False)
31 is_ldap.create(tbl, populate_default=True)
30 is_ldap.create(tbl, populate_default=True)
32 is_ldap.alter(nullable=False)
31 is_ldap.alter(nullable=False)
33
32
34 #==========================================================================
33 #==========================================================================
35 # Upgrade of `user_logs` table
34 # Upgrade of `user_logs` table
36 #==========================================================================
35 #==========================================================================
37
36
38 tblname = 'users'
37 tblname = 'users'
39 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
38 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
40 autoload_with=migrate_engine)
39 autoload_with=migrate_engine)
41
40
42 #ADD revision column
41 #ADD revision column
43 revision = Column('revision', TEXT(length=None, convert_unicode=False,
42 revision = Column('revision', TEXT(length=None, convert_unicode=False,
44 assert_unicode=None),
43 assert_unicode=None),
45 nullable=True, unique=None, default=None)
44 nullable=True, unique=None, default=None)
46 revision.create(tbl)
45 revision.create(tbl)
47
46
48
47
49
48
50 #==========================================================================
49 #==========================================================================
51 # Upgrade of `repositories` table
50 # Upgrade of `repositories` table
52 #==========================================================================
51 #==========================================================================
53 tblname = 'repositories'
52 tblname = 'repositories'
54 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
53 tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True,
55 autoload_with=migrate_engine)
54 autoload_with=migrate_engine)
56
55
57 #ADD repo_type column#
56 #ADD repo_type column#
58 repo_type = Column("repo_type", String(length=None, convert_unicode=False,
57 repo_type = Column("repo_type", String(length=None, convert_unicode=False,
59 assert_unicode=None),
58 assert_unicode=None),
60 nullable=True, unique=False, default='hg')
59 nullable=True, unique=False, default='hg')
61
60
62 repo_type.create(tbl, populate_default=True)
61 repo_type.create(tbl, populate_default=True)
63 #repo_type.alter(nullable=False)
62 #repo_type.alter(nullable=False)
64
63
65 #ADD statistics column#
64 #ADD statistics column#
66 enable_statistics = Column("statistics", Boolean(), nullable=True,
65 enable_statistics = Column("statistics", Boolean(), nullable=True,
67 unique=None, default=True)
66 unique=None, default=True)
68 enable_statistics.create(tbl)
67 enable_statistics.create(tbl)
69
68
70 #==========================================================================
69 #==========================================================================
71 # Add table `user_followings`
70 # Add table `user_followings`
72 #==========================================================================
71 #==========================================================================
73 from rhodecode.model.db import UserFollowing
72 class UserFollowing(Base, BaseModel):
73 __tablename__ = 'user_followings'
74 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
75 UniqueConstraint('user_id', 'follows_user_id')
76 , {'useexisting':True})
77
78 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
79 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
80 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None)
81 follows_user_id = Column("follows_user_id", Integer(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
82
83 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
84
85 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
86 follows_repository = relation('Repository')
87
74 UserFollowing().__table__.create()
88 UserFollowing().__table__.create()
75
89
76 #==========================================================================
90 #==========================================================================
77 # Add table `cache_invalidation`
91 # Add table `cache_invalidation`
78 #==========================================================================
92 #==========================================================================
79 from rhodecode.model.db import CacheInvalidation
93 class CacheInvalidation(Base, BaseModel):
94 __tablename__ = 'cache_invalidation'
95 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
96 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
97 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
98 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
99 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
100
101
102 def __init__(self, cache_key, cache_args=''):
103 self.cache_key = cache_key
104 self.cache_args = cache_args
105 self.cache_active = False
106
107 def __repr__(self):
108 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key)
80 CacheInvalidation().__table__.create()
109 CacheInvalidation().__table__.create()
81
110
82 return
111 return
83
112
84 def downgrade(migrate_engine):
113 def downgrade(migrate_engine):
85 meta = MetaData()
114 meta = MetaData()
86 meta.bind = migrate_engine
115 meta.bind = migrate_engine
87
116
88
117
General Comments 0
You need to be logged in to leave comments. Login now