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