Show More
@@ -1,494 +1,499 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.lib.db_manage |
|
3 | rhodecode.lib.db_manage | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Database creation, and setup module for RhodeCode. Used for creation |
|
6 | Database creation, and setup module for RhodeCode. Used for creation | |
7 | of database as well as for migration operations |
|
7 | of database as well as for migration operations | |
8 |
|
8 | |||
9 | :created_on: Apr 10, 2010 |
|
9 | :created_on: Apr 10, 2010 | |
10 | :author: marcink |
|
10 | :author: marcink | |
11 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
11 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
12 | :license: GPLv3, see COPYING for more details. |
|
12 | :license: GPLv3, see COPYING for more details. | |
13 | """ |
|
13 | """ | |
14 | # This program is free software; you can redistribute it and/or |
|
14 | # This program is free software; you can redistribute it and/or | |
15 | # modify it under the terms of the GNU General Public License |
|
15 | # modify it under the terms of the GNU General Public License | |
16 | # as published by the Free Software Foundation; version 2 |
|
16 | # as published by the Free Software Foundation; version 2 | |
17 | # of the License or (at your opinion) any later version of the license. |
|
17 | # of the License or (at your opinion) any later version of the license. | |
18 | # |
|
18 | # | |
19 | # This program is distributed in the hope that it will be useful, |
|
19 | # This program is distributed in the hope that it will be useful, | |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | # GNU General Public License for more details. |
|
22 | # GNU General Public License for more details. | |
23 | # |
|
23 | # | |
24 | # You should have received a copy of the GNU General Public License |
|
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program; if not, write to the Free Software |
|
25 | # along with this program; if not, write to the Free Software | |
26 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
27 | # MA 02110-1301, USA. |
|
27 | # MA 02110-1301, USA. | |
28 |
|
28 | |||
29 | import os |
|
29 | import os | |
30 | import sys |
|
30 | import sys | |
31 | import uuid |
|
31 | import uuid | |
32 | import logging |
|
32 | import logging | |
33 | from os.path import dirname as dn, join as jn |
|
33 | from os.path import dirname as dn, join as jn | |
34 |
|
34 | |||
35 | from rhodecode import __dbversion__ |
|
35 | from rhodecode import __dbversion__ | |
36 | from rhodecode.model import meta |
|
36 | from rhodecode.model import meta | |
37 |
|
37 | |||
38 | from rhodecode.lib.auth import get_crypt_password |
|
38 | from rhodecode.lib.auth import get_crypt_password | |
39 | from rhodecode.lib.utils import ask_ok |
|
39 | from rhodecode.lib.utils import ask_ok | |
40 | from rhodecode.model import init_model |
|
40 | from rhodecode.model import init_model | |
41 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ |
|
41 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ | |
42 | UserToPerm, DbMigrateVersion |
|
42 | UserToPerm, DbMigrateVersion | |
43 |
|
43 | |||
44 | from sqlalchemy.engine import create_engine |
|
44 | from sqlalchemy.engine import create_engine | |
45 |
|
45 | |||
46 | log = logging.getLogger(__name__) |
|
46 | log = logging.getLogger(__name__) | |
47 |
|
47 | |||
48 | class DbManage(object): |
|
48 | class DbManage(object): | |
49 | def __init__(self, log_sql, dbconf, root, tests=False): |
|
49 | def __init__(self, log_sql, dbconf, root, tests=False): | |
50 | self.dbname = dbconf.split('/')[-1] |
|
50 | self.dbname = dbconf.split('/')[-1] | |
51 | self.tests = tests |
|
51 | self.tests = tests | |
52 | self.root = root |
|
52 | self.root = root | |
53 | self.dburi = dbconf |
|
53 | self.dburi = dbconf | |
54 | engine = create_engine(self.dburi, echo=log_sql) |
|
54 | self.log_sql = log_sql | |
|
55 | self.db_exists = False | |||
|
56 | self.init_db() | |||
|
57 | ||||
|
58 | def init_db(self): | |||
|
59 | engine = create_engine(self.dburi, echo=self.log_sql) | |||
55 | init_model(engine) |
|
60 | init_model(engine) | |
56 | self.sa = meta.Session() |
|
61 | self.sa = meta.Session() | |
57 | self.db_exists = False |
|
|||
58 |
|
62 | |||
59 | def check_for_db(self, override): |
|
63 | def check_for_db(self, override): | |
60 | db_path = jn(self.root, self.dbname) |
|
64 | db_path = jn(self.root, self.dbname) | |
61 | if self.dburi.startswith('sqlite'): |
|
65 | if self.dburi.startswith('sqlite'): | |
62 | log.info('checking for existing db in %s', db_path) |
|
66 | log.info('checking for existing db in %s', db_path) | |
63 | if os.path.isfile(db_path): |
|
67 | if os.path.isfile(db_path): | |
64 |
|
68 | |||
65 | self.db_exists = True |
|
69 | self.db_exists = True | |
66 | if not override: |
|
70 | if not override: | |
67 | raise Exception('database already exists') |
|
71 | raise Exception('database already exists') | |
68 |
|
72 | |||
69 | def create_tables(self, override=False): |
|
73 | def create_tables(self, override=False): | |
70 | """Create a auth database |
|
74 | """Create a auth database | |
71 | """ |
|
75 | """ | |
72 |
|
76 | |||
73 | self.check_for_db(override) |
|
77 | self.check_for_db(override) | |
74 | if self.db_exists: |
|
78 | if self.db_exists: | |
75 | log.info("database exist and it's going to be destroyed") |
|
79 | log.info("database exist and it's going to be destroyed") | |
76 | if self.tests: |
|
80 | if self.tests: | |
77 | destroy = True |
|
81 | destroy = True | |
78 | else: |
|
82 | else: | |
79 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
83 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
80 | if not destroy: |
|
84 | if not destroy: | |
81 | sys.exit() |
|
85 | sys.exit() | |
82 | if self.db_exists and destroy: |
|
86 | if self.db_exists and destroy: | |
83 | os.remove(jn(self.root, self.dbname)) |
|
87 | os.remove(jn(self.root, self.dbname)) | |
84 | checkfirst = not override |
|
88 | checkfirst = not override | |
85 | meta.Base.metadata.create_all(checkfirst=checkfirst) |
|
89 | meta.Base.metadata.create_all(checkfirst=checkfirst) | |
86 | log.info('Created tables for %s', self.dbname) |
|
90 | log.info('Created tables for %s', self.dbname) | |
87 |
|
91 | |||
88 |
|
92 | |||
89 |
|
93 | |||
90 | def set_db_version(self): |
|
94 | def set_db_version(self): | |
91 | try: |
|
95 | try: | |
92 | ver = DbMigrateVersion() |
|
96 | ver = DbMigrateVersion() | |
93 | ver.version = __dbversion__ |
|
97 | ver.version = __dbversion__ | |
94 | ver.repository_id = 'rhodecode_db_migrations' |
|
98 | ver.repository_id = 'rhodecode_db_migrations' | |
95 | ver.repository_path = 'versions' |
|
99 | ver.repository_path = 'versions' | |
96 | self.sa.add(ver) |
|
100 | self.sa.add(ver) | |
97 | self.sa.commit() |
|
101 | self.sa.commit() | |
98 | except: |
|
102 | except: | |
99 | self.sa.rollback() |
|
103 | self.sa.rollback() | |
100 | raise |
|
104 | raise | |
101 | log.info('db version set to: %s', __dbversion__) |
|
105 | log.info('db version set to: %s', __dbversion__) | |
102 |
|
106 | |||
103 |
|
107 | |||
104 | def upgrade(self): |
|
108 | def upgrade(self): | |
105 | """Upgrades given database schema to given revision following |
|
109 | """Upgrades given database schema to given revision following | |
106 | all needed steps, to perform the upgrade |
|
110 | all needed steps, to perform the upgrade | |
107 |
|
111 | |||
108 | """ |
|
112 | """ | |
109 |
|
113 | |||
110 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
114 | from rhodecode.lib.dbmigrate.migrate.versioning import api | |
111 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
115 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ | |
112 | DatabaseNotControlledError |
|
116 | DatabaseNotControlledError | |
113 |
|
117 | |||
114 | upgrade = ask_ok('You are about to perform database upgrade, make ' |
|
118 | upgrade = ask_ok('You are about to perform database upgrade, make ' | |
115 | 'sure You backed up your database before. ' |
|
119 | 'sure You backed up your database before. ' | |
116 | 'Continue ? [y/n]') |
|
120 | 'Continue ? [y/n]') | |
117 | if not upgrade: |
|
121 | if not upgrade: | |
118 | sys.exit('Nothing done') |
|
122 | sys.exit('Nothing done') | |
119 |
|
123 | |||
120 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
124 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), | |
121 | 'rhodecode/lib/dbmigrate') |
|
125 | 'rhodecode/lib/dbmigrate') | |
122 | db_uri = self.dburi |
|
126 | db_uri = self.dburi | |
123 |
|
127 | |||
124 | try: |
|
128 | try: | |
125 | curr_version = api.db_version(db_uri, repository_path) |
|
129 | curr_version = api.db_version(db_uri, repository_path) | |
126 | msg = ('Found current database under version' |
|
130 | msg = ('Found current database under version' | |
127 | ' control with version %s' % curr_version) |
|
131 | ' control with version %s' % curr_version) | |
128 |
|
132 | |||
129 | except (RuntimeError, DatabaseNotControlledError), e: |
|
133 | except (RuntimeError, DatabaseNotControlledError), e: | |
130 | curr_version = 1 |
|
134 | curr_version = 1 | |
131 | msg = ('Current database is not under version control. Setting' |
|
135 | msg = ('Current database is not under version control. Setting' | |
132 | ' as version %s' % curr_version) |
|
136 | ' as version %s' % curr_version) | |
133 | api.version_control(db_uri, repository_path, curr_version) |
|
137 | api.version_control(db_uri, repository_path, curr_version) | |
134 |
|
138 | |||
135 | print (msg) |
|
139 | print (msg) | |
136 |
|
140 | |||
137 | if curr_version == __dbversion__: |
|
141 | if curr_version == __dbversion__: | |
138 | sys.exit('This database is already at the newest version') |
|
142 | sys.exit('This database is already at the newest version') | |
139 |
|
143 | |||
140 | #====================================================================== |
|
144 | #====================================================================== | |
141 | # UPGRADE STEPS |
|
145 | # UPGRADE STEPS | |
142 | #====================================================================== |
|
146 | #====================================================================== | |
143 | class UpgradeSteps(object): |
|
147 | class UpgradeSteps(object): | |
144 | """Those steps follow schema versions so for example schema |
|
148 | """Those steps follow schema versions so for example schema | |
145 | for example schema with seq 002 == step_2 and so on. |
|
149 | for example schema with seq 002 == step_2 and so on. | |
146 | """ |
|
150 | """ | |
147 |
|
151 | |||
148 | def __init__(self, klass): |
|
152 | def __init__(self, klass): | |
149 | self.klass = klass |
|
153 | self.klass = klass | |
150 |
|
154 | |||
151 | def step_0(self): |
|
155 | def step_0(self): | |
152 | #step 0 is the schema upgrade, and than follow proper upgrades |
|
156 | #step 0 is the schema upgrade, and than follow proper upgrades | |
153 | print ('attempting to do database upgrade to version %s' \ |
|
157 | print ('attempting to do database upgrade to version %s' \ | |
154 | % __dbversion__) |
|
158 | % __dbversion__) | |
155 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
159 | api.upgrade(db_uri, repository_path, __dbversion__) | |
156 | print ('Schema upgrade completed') |
|
160 | print ('Schema upgrade completed') | |
157 |
|
161 | |||
158 | def step_1(self): |
|
162 | def step_1(self): | |
159 | pass |
|
163 | pass | |
160 |
|
164 | |||
161 | def step_2(self): |
|
165 | def step_2(self): | |
162 | print ('Patching repo paths for newer version of RhodeCode') |
|
166 | print ('Patching repo paths for newer version of RhodeCode') | |
163 | self.klass.fix_repo_paths() |
|
167 | self.klass.fix_repo_paths() | |
164 |
|
168 | |||
165 | print ('Patching default user of RhodeCode') |
|
169 | print ('Patching default user of RhodeCode') | |
166 | self.klass.fix_default_user() |
|
170 | self.klass.fix_default_user() | |
167 |
|
171 | |||
168 | log.info('Changing ui settings') |
|
172 | log.info('Changing ui settings') | |
169 | self.klass.create_ui_settings() |
|
173 | self.klass.create_ui_settings() | |
170 |
|
174 | |||
171 | def step_3(self): |
|
175 | def step_3(self): | |
172 | print ('Adding additional settings into RhodeCode db') |
|
176 | print ('Adding additional settings into RhodeCode db') | |
173 | self.klass.fix_settings() |
|
177 | self.klass.fix_settings() | |
174 |
|
178 | |||
175 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) |
|
179 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) | |
176 |
|
180 | |||
177 | #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
181 | #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE | |
178 | for step in upgrade_steps: |
|
182 | for step in upgrade_steps: | |
179 | print ('performing upgrade step %s' % step) |
|
183 | print ('performing upgrade step %s' % step) | |
180 | callable = getattr(UpgradeSteps(self), 'step_%s' % step)() |
|
184 | callable = getattr(UpgradeSteps(self), 'step_%s' % step)() | |
181 |
|
185 | |||
182 |
|
186 | |||
183 |
|
187 | |||
184 | def fix_repo_paths(self): |
|
188 | def fix_repo_paths(self): | |
185 | """Fixes a old rhodecode version path into new one without a '*' |
|
189 | """Fixes a old rhodecode version path into new one without a '*' | |
186 | """ |
|
190 | """ | |
187 |
|
191 | |||
188 | paths = self.sa.query(RhodeCodeUi)\ |
|
192 | paths = self.sa.query(RhodeCodeUi)\ | |
189 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
193 | .filter(RhodeCodeUi.ui_key == '/')\ | |
190 | .scalar() |
|
194 | .scalar() | |
191 |
|
195 | |||
192 | paths.ui_value = paths.ui_value.replace('*', '') |
|
196 | paths.ui_value = paths.ui_value.replace('*', '') | |
193 |
|
197 | |||
194 | try: |
|
198 | try: | |
195 | self.sa.add(paths) |
|
199 | self.sa.add(paths) | |
196 | self.sa.commit() |
|
200 | self.sa.commit() | |
197 | except: |
|
201 | except: | |
198 | self.sa.rollback() |
|
202 | self.sa.rollback() | |
199 | raise |
|
203 | raise | |
200 |
|
204 | |||
201 | def fix_default_user(self): |
|
205 | def fix_default_user(self): | |
202 | """Fixes a old default user with some 'nicer' default values, |
|
206 | """Fixes a old default user with some 'nicer' default values, | |
203 | used mostly for anonymous access |
|
207 | used mostly for anonymous access | |
204 | """ |
|
208 | """ | |
205 | def_user = self.sa.query(User)\ |
|
209 | def_user = self.sa.query(User)\ | |
206 | .filter(User.username == 'default')\ |
|
210 | .filter(User.username == 'default')\ | |
207 | .one() |
|
211 | .one() | |
208 |
|
212 | |||
209 | def_user.name = 'Anonymous' |
|
213 | def_user.name = 'Anonymous' | |
210 | def_user.lastname = 'User' |
|
214 | def_user.lastname = 'User' | |
211 | def_user.email = 'anonymous@rhodecode.org' |
|
215 | def_user.email = 'anonymous@rhodecode.org' | |
212 |
|
216 | |||
213 | try: |
|
217 | try: | |
214 | self.sa.add(def_user) |
|
218 | self.sa.add(def_user) | |
215 | self.sa.commit() |
|
219 | self.sa.commit() | |
216 | except: |
|
220 | except: | |
217 | self.sa.rollback() |
|
221 | self.sa.rollback() | |
218 | raise |
|
222 | raise | |
219 |
|
223 | |||
220 | def fix_settings(self): |
|
224 | def fix_settings(self): | |
221 | """Fixes rhodecode settings adds ga_code key for google analytics |
|
225 | """Fixes rhodecode settings adds ga_code key for google analytics | |
222 | """ |
|
226 | """ | |
223 |
|
227 | |||
224 | hgsettings3 = RhodeCodeSettings('ga_code', '') |
|
228 | hgsettings3 = RhodeCodeSettings('ga_code', '') | |
|
229 | ||||
225 | try: |
|
230 | try: | |
226 | self.sa.add(hgsettings3) |
|
231 | self.sa.add(hgsettings3) | |
227 | self.sa.commit() |
|
232 | self.sa.commit() | |
228 | except: |
|
233 | except: | |
229 | self.sa.rollback() |
|
234 | self.sa.rollback() | |
230 | raise |
|
235 | raise | |
231 |
|
236 | |||
232 | def admin_prompt(self, second=False): |
|
237 | def admin_prompt(self, second=False): | |
233 | if not self.tests: |
|
238 | if not self.tests: | |
234 | import getpass |
|
239 | import getpass | |
235 |
|
240 | |||
236 |
|
241 | |||
237 | def get_password(): |
|
242 | def get_password(): | |
238 | password = getpass.getpass('Specify admin password (min 6 chars):') |
|
243 | password = getpass.getpass('Specify admin password (min 6 chars):') | |
239 | confirm = getpass.getpass('Confirm password:') |
|
244 | confirm = getpass.getpass('Confirm password:') | |
240 |
|
245 | |||
241 | if password != confirm: |
|
246 | if password != confirm: | |
242 | log.error('passwords mismatch') |
|
247 | log.error('passwords mismatch') | |
243 | return False |
|
248 | return False | |
244 | if len(password) < 6: |
|
249 | if len(password) < 6: | |
245 | log.error('password is to short use at least 6 characters') |
|
250 | log.error('password is to short use at least 6 characters') | |
246 | return False |
|
251 | return False | |
247 |
|
252 | |||
248 | return password |
|
253 | return password | |
249 |
|
254 | |||
250 | username = raw_input('Specify admin username:') |
|
255 | username = raw_input('Specify admin username:') | |
251 |
|
256 | |||
252 | password = get_password() |
|
257 | password = get_password() | |
253 | if not password: |
|
258 | if not password: | |
254 | #second try |
|
259 | #second try | |
255 | password = get_password() |
|
260 | password = get_password() | |
256 | if not password: |
|
261 | if not password: | |
257 | sys.exit() |
|
262 | sys.exit() | |
258 |
|
263 | |||
259 | email = raw_input('Specify admin email:') |
|
264 | email = raw_input('Specify admin email:') | |
260 | self.create_user(username, password, email, True) |
|
265 | self.create_user(username, password, email, True) | |
261 | else: |
|
266 | else: | |
262 | log.info('creating admin and regular test users') |
|
267 | log.info('creating admin and regular test users') | |
263 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) |
|
268 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) | |
264 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) |
|
269 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) | |
265 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) |
|
270 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) | |
266 |
|
271 | |||
267 | def create_ui_settings(self): |
|
272 | def create_ui_settings(self): | |
268 | """Creates ui settings, fills out hooks |
|
273 | """Creates ui settings, fills out hooks | |
269 | and disables dotencode |
|
274 | and disables dotencode | |
270 |
|
275 | |||
271 | """ |
|
276 | """ | |
272 | #HOOKS |
|
277 | #HOOKS | |
273 | hooks1_key = 'changegroup.update' |
|
278 | hooks1_key = 'changegroup.update' | |
274 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
279 | hooks1_ = self.sa.query(RhodeCodeUi)\ | |
275 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
280 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() | |
276 |
|
281 | |||
277 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
282 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ | |
278 | hooks1.ui_section = 'hooks' |
|
283 | hooks1.ui_section = 'hooks' | |
279 | hooks1.ui_key = hooks1_key |
|
284 | hooks1.ui_key = hooks1_key | |
280 | hooks1.ui_value = 'hg update >&2' |
|
285 | hooks1.ui_value = 'hg update >&2' | |
281 | hooks1.ui_active = False |
|
286 | hooks1.ui_active = False | |
282 |
|
287 | |||
283 | hooks2_key = 'changegroup.repo_size' |
|
288 | hooks2_key = 'changegroup.repo_size' | |
284 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
289 | hooks2_ = self.sa.query(RhodeCodeUi)\ | |
285 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
290 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() | |
286 |
|
291 | |||
287 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
292 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ | |
288 | hooks2.ui_section = 'hooks' |
|
293 | hooks2.ui_section = 'hooks' | |
289 | hooks2.ui_key = hooks2_key |
|
294 | hooks2.ui_key = hooks2_key | |
290 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
295 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' | |
291 |
|
296 | |||
292 | hooks3 = RhodeCodeUi() |
|
297 | hooks3 = RhodeCodeUi() | |
293 | hooks3.ui_section = 'hooks' |
|
298 | hooks3.ui_section = 'hooks' | |
294 | hooks3.ui_key = 'pretxnchangegroup.push_logger' |
|
299 | hooks3.ui_key = 'pretxnchangegroup.push_logger' | |
295 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
300 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' | |
296 |
|
301 | |||
297 | hooks4 = RhodeCodeUi() |
|
302 | hooks4 = RhodeCodeUi() | |
298 | hooks4.ui_section = 'hooks' |
|
303 | hooks4.ui_section = 'hooks' | |
299 | hooks4.ui_key = 'preoutgoing.pull_logger' |
|
304 | hooks4.ui_key = 'preoutgoing.pull_logger' | |
300 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
305 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' | |
301 |
|
306 | |||
302 | #For mercurial 1.7 set backward comapatibility with format |
|
307 | #For mercurial 1.7 set backward comapatibility with format | |
303 | dotencode_disable = RhodeCodeUi() |
|
308 | dotencode_disable = RhodeCodeUi() | |
304 | dotencode_disable.ui_section = 'format' |
|
309 | dotencode_disable.ui_section = 'format' | |
305 | dotencode_disable.ui_key = 'dotencode' |
|
310 | dotencode_disable.ui_key = 'dotencode' | |
306 | dotencode_disable.ui_value = 'false' |
|
311 | dotencode_disable.ui_value = 'false' | |
307 |
|
312 | |||
308 | try: |
|
313 | try: | |
309 | self.sa.add(hooks1) |
|
314 | self.sa.add(hooks1) | |
310 | self.sa.add(hooks2) |
|
315 | self.sa.add(hooks2) | |
311 | self.sa.add(hooks3) |
|
316 | self.sa.add(hooks3) | |
312 | self.sa.add(hooks4) |
|
317 | self.sa.add(hooks4) | |
313 | self.sa.add(dotencode_disable) |
|
318 | self.sa.add(dotencode_disable) | |
314 | self.sa.commit() |
|
319 | self.sa.commit() | |
315 | except: |
|
320 | except: | |
316 | self.sa.rollback() |
|
321 | self.sa.rollback() | |
317 | raise |
|
322 | raise | |
318 |
|
323 | |||
319 |
|
324 | |||
320 | def create_ldap_options(self): |
|
325 | def create_ldap_options(self): | |
321 | """Creates ldap settings""" |
|
326 | """Creates ldap settings""" | |
322 |
|
327 | |||
323 | try: |
|
328 | try: | |
324 | for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', |
|
329 | for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', | |
325 | 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: |
|
330 | 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: | |
326 |
|
331 | |||
327 | setting = RhodeCodeSettings(k, '') |
|
332 | setting = RhodeCodeSettings(k, '') | |
328 | self.sa.add(setting) |
|
333 | self.sa.add(setting) | |
329 | self.sa.commit() |
|
334 | self.sa.commit() | |
330 | except: |
|
335 | except: | |
331 | self.sa.rollback() |
|
336 | self.sa.rollback() | |
332 | raise |
|
337 | raise | |
333 |
|
338 | |||
334 | def config_prompt(self, test_repo_path=''): |
|
339 | def config_prompt(self, test_repo_path=''): | |
335 | log.info('Setting up repositories config') |
|
340 | log.info('Setting up repositories config') | |
336 |
|
341 | |||
337 | if not self.tests and not test_repo_path: |
|
342 | if not self.tests and not test_repo_path: | |
338 | path = raw_input('Specify valid full path to your repositories' |
|
343 | path = raw_input('Specify valid full path to your repositories' | |
339 | ' you can change this later in application settings:') |
|
344 | ' you can change this later in application settings:') | |
340 | else: |
|
345 | else: | |
341 | path = test_repo_path |
|
346 | path = test_repo_path | |
342 |
|
347 | |||
343 | if not os.path.isdir(path): |
|
348 | if not os.path.isdir(path): | |
344 | log.error('You entered wrong path: %s', path) |
|
349 | log.error('You entered wrong path: %s', path) | |
345 | sys.exit() |
|
350 | sys.exit() | |
346 |
|
351 | |||
347 | self.create_ui_settings() |
|
352 | self.create_ui_settings() | |
348 |
|
353 | |||
349 | #HG UI OPTIONS |
|
354 | #HG UI OPTIONS | |
350 | web1 = RhodeCodeUi() |
|
355 | web1 = RhodeCodeUi() | |
351 | web1.ui_section = 'web' |
|
356 | web1.ui_section = 'web' | |
352 | web1.ui_key = 'push_ssl' |
|
357 | web1.ui_key = 'push_ssl' | |
353 | web1.ui_value = 'false' |
|
358 | web1.ui_value = 'false' | |
354 |
|
359 | |||
355 | web2 = RhodeCodeUi() |
|
360 | web2 = RhodeCodeUi() | |
356 | web2.ui_section = 'web' |
|
361 | web2.ui_section = 'web' | |
357 | web2.ui_key = 'allow_archive' |
|
362 | web2.ui_key = 'allow_archive' | |
358 | web2.ui_value = 'gz zip bz2' |
|
363 | web2.ui_value = 'gz zip bz2' | |
359 |
|
364 | |||
360 | web3 = RhodeCodeUi() |
|
365 | web3 = RhodeCodeUi() | |
361 | web3.ui_section = 'web' |
|
366 | web3.ui_section = 'web' | |
362 | web3.ui_key = 'allow_push' |
|
367 | web3.ui_key = 'allow_push' | |
363 | web3.ui_value = '*' |
|
368 | web3.ui_value = '*' | |
364 |
|
369 | |||
365 | web4 = RhodeCodeUi() |
|
370 | web4 = RhodeCodeUi() | |
366 | web4.ui_section = 'web' |
|
371 | web4.ui_section = 'web' | |
367 | web4.ui_key = 'baseurl' |
|
372 | web4.ui_key = 'baseurl' | |
368 | web4.ui_value = '/' |
|
373 | web4.ui_value = '/' | |
369 |
|
374 | |||
370 | paths = RhodeCodeUi() |
|
375 | paths = RhodeCodeUi() | |
371 | paths.ui_section = 'paths' |
|
376 | paths.ui_section = 'paths' | |
372 | paths.ui_key = '/' |
|
377 | paths.ui_key = '/' | |
373 | paths.ui_value = path |
|
378 | paths.ui_value = path | |
374 |
|
379 | |||
375 |
|
380 | |||
376 | hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') |
|
381 | hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') | |
377 | hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') |
|
382 | hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') | |
378 | hgsettings3 = RhodeCodeSettings('ga_code', '') |
|
383 | hgsettings3 = RhodeCodeSettings('ga_code', '') | |
379 |
|
384 | |||
380 |
|
385 | |||
381 | try: |
|
386 | try: | |
382 | self.sa.add(web1) |
|
387 | self.sa.add(web1) | |
383 | self.sa.add(web2) |
|
388 | self.sa.add(web2) | |
384 | self.sa.add(web3) |
|
389 | self.sa.add(web3) | |
385 | self.sa.add(web4) |
|
390 | self.sa.add(web4) | |
386 | self.sa.add(paths) |
|
391 | self.sa.add(paths) | |
387 | self.sa.add(hgsettings1) |
|
392 | self.sa.add(hgsettings1) | |
388 | self.sa.add(hgsettings2) |
|
393 | self.sa.add(hgsettings2) | |
389 | self.sa.add(hgsettings3) |
|
394 | self.sa.add(hgsettings3) | |
390 |
|
395 | |||
391 | self.sa.commit() |
|
396 | self.sa.commit() | |
392 | except: |
|
397 | except: | |
393 | self.sa.rollback() |
|
398 | self.sa.rollback() | |
394 | raise |
|
399 | raise | |
395 |
|
400 | |||
396 | self.create_ldap_options() |
|
401 | self.create_ldap_options() | |
397 |
|
402 | |||
398 | log.info('created ui config') |
|
403 | log.info('created ui config') | |
399 |
|
404 | |||
400 | def create_user(self, username, password, email='', admin=False): |
|
405 | def create_user(self, username, password, email='', admin=False): | |
401 | log.info('creating administrator user %s', username) |
|
406 | log.info('creating administrator user %s', username) | |
402 | new_user = User() |
|
407 | new_user = User() | |
403 | new_user.username = username |
|
408 | new_user.username = username | |
404 | new_user.password = get_crypt_password(password) |
|
409 | new_user.password = get_crypt_password(password) | |
405 | new_user.name = 'RhodeCode' |
|
410 | new_user.name = 'RhodeCode' | |
406 | new_user.lastname = 'Admin' |
|
411 | new_user.lastname = 'Admin' | |
407 | new_user.email = email |
|
412 | new_user.email = email | |
408 | new_user.admin = admin |
|
413 | new_user.admin = admin | |
409 | new_user.active = True |
|
414 | new_user.active = True | |
410 |
|
415 | |||
411 | try: |
|
416 | try: | |
412 | self.sa.add(new_user) |
|
417 | self.sa.add(new_user) | |
413 | self.sa.commit() |
|
418 | self.sa.commit() | |
414 | except: |
|
419 | except: | |
415 | self.sa.rollback() |
|
420 | self.sa.rollback() | |
416 | raise |
|
421 | raise | |
417 |
|
422 | |||
418 | def create_default_user(self): |
|
423 | def create_default_user(self): | |
419 | log.info('creating default user') |
|
424 | log.info('creating default user') | |
420 | #create default user for handling default permissions. |
|
425 | #create default user for handling default permissions. | |
421 | def_user = User() |
|
426 | def_user = User() | |
422 | def_user.username = 'default' |
|
427 | def_user.username = 'default' | |
423 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) |
|
428 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) | |
424 | def_user.name = 'Anonymous' |
|
429 | def_user.name = 'Anonymous' | |
425 | def_user.lastname = 'User' |
|
430 | def_user.lastname = 'User' | |
426 | def_user.email = 'anonymous@rhodecode.org' |
|
431 | def_user.email = 'anonymous@rhodecode.org' | |
427 | def_user.admin = False |
|
432 | def_user.admin = False | |
428 | def_user.active = False |
|
433 | def_user.active = False | |
429 | try: |
|
434 | try: | |
430 | self.sa.add(def_user) |
|
435 | self.sa.add(def_user) | |
431 | self.sa.commit() |
|
436 | self.sa.commit() | |
432 | except: |
|
437 | except: | |
433 | self.sa.rollback() |
|
438 | self.sa.rollback() | |
434 | raise |
|
439 | raise | |
435 |
|
440 | |||
436 | def create_permissions(self): |
|
441 | def create_permissions(self): | |
437 | #module.(access|create|change|delete)_[name] |
|
442 | #module.(access|create|change|delete)_[name] | |
438 | #module.(read|write|owner) |
|
443 | #module.(read|write|owner) | |
439 | perms = [('repository.none', 'Repository no access'), |
|
444 | perms = [('repository.none', 'Repository no access'), | |
440 | ('repository.read', 'Repository read access'), |
|
445 | ('repository.read', 'Repository read access'), | |
441 | ('repository.write', 'Repository write access'), |
|
446 | ('repository.write', 'Repository write access'), | |
442 | ('repository.admin', 'Repository admin access'), |
|
447 | ('repository.admin', 'Repository admin access'), | |
443 | ('hg.admin', 'Hg Administrator'), |
|
448 | ('hg.admin', 'Hg Administrator'), | |
444 | ('hg.create.repository', 'Repository create'), |
|
449 | ('hg.create.repository', 'Repository create'), | |
445 | ('hg.create.none', 'Repository creation disabled'), |
|
450 | ('hg.create.none', 'Repository creation disabled'), | |
446 | ('hg.register.none', 'Register disabled'), |
|
451 | ('hg.register.none', 'Register disabled'), | |
447 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), |
|
452 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), | |
448 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), |
|
453 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), | |
449 | ] |
|
454 | ] | |
450 |
|
455 | |||
451 | for p in perms: |
|
456 | for p in perms: | |
452 | new_perm = Permission() |
|
457 | new_perm = Permission() | |
453 | new_perm.permission_name = p[0] |
|
458 | new_perm.permission_name = p[0] | |
454 | new_perm.permission_longname = p[1] |
|
459 | new_perm.permission_longname = p[1] | |
455 | try: |
|
460 | try: | |
456 | self.sa.add(new_perm) |
|
461 | self.sa.add(new_perm) | |
457 | self.sa.commit() |
|
462 | self.sa.commit() | |
458 | except: |
|
463 | except: | |
459 | self.sa.rollback() |
|
464 | self.sa.rollback() | |
460 | raise |
|
465 | raise | |
461 |
|
466 | |||
462 | def populate_default_permissions(self): |
|
467 | def populate_default_permissions(self): | |
463 | log.info('creating default user permissions') |
|
468 | log.info('creating default user permissions') | |
464 |
|
469 | |||
465 | default_user = self.sa.query(User)\ |
|
470 | default_user = self.sa.query(User)\ | |
466 | .filter(User.username == 'default').scalar() |
|
471 | .filter(User.username == 'default').scalar() | |
467 |
|
472 | |||
468 | reg_perm = UserToPerm() |
|
473 | reg_perm = UserToPerm() | |
469 | reg_perm.user = default_user |
|
474 | reg_perm.user = default_user | |
470 | reg_perm.permission = self.sa.query(Permission)\ |
|
475 | reg_perm.permission = self.sa.query(Permission)\ | |
471 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ |
|
476 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ | |
472 | .scalar() |
|
477 | .scalar() | |
473 |
|
478 | |||
474 | create_repo_perm = UserToPerm() |
|
479 | create_repo_perm = UserToPerm() | |
475 | create_repo_perm.user = default_user |
|
480 | create_repo_perm.user = default_user | |
476 | create_repo_perm.permission = self.sa.query(Permission)\ |
|
481 | create_repo_perm.permission = self.sa.query(Permission)\ | |
477 | .filter(Permission.permission_name == 'hg.create.repository')\ |
|
482 | .filter(Permission.permission_name == 'hg.create.repository')\ | |
478 | .scalar() |
|
483 | .scalar() | |
479 |
|
484 | |||
480 | default_repo_perm = UserToPerm() |
|
485 | default_repo_perm = UserToPerm() | |
481 | default_repo_perm.user = default_user |
|
486 | default_repo_perm.user = default_user | |
482 | default_repo_perm.permission = self.sa.query(Permission)\ |
|
487 | default_repo_perm.permission = self.sa.query(Permission)\ | |
483 | .filter(Permission.permission_name == 'repository.read')\ |
|
488 | .filter(Permission.permission_name == 'repository.read')\ | |
484 | .scalar() |
|
489 | .scalar() | |
485 |
|
490 | |||
486 | try: |
|
491 | try: | |
487 | self.sa.add(reg_perm) |
|
492 | self.sa.add(reg_perm) | |
488 | self.sa.add(create_repo_perm) |
|
493 | self.sa.add(create_repo_perm) | |
489 | self.sa.add(default_repo_perm) |
|
494 | self.sa.add(default_repo_perm) | |
490 | self.sa.commit() |
|
495 | self.sa.commit() | |
491 | except: |
|
496 | except: | |
492 | self.sa.rollback() |
|
497 | self.sa.rollback() | |
493 | raise |
|
498 | raise | |
494 |
|
499 |
@@ -1,127 +1,88 b'' | |||||
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 |
|
9 | from rhodecode.model.db import BaseModel | |
10 |
|
10 | |||
11 | from rhodecode.lib.dbmigrate.migrate import * |
|
11 | from rhodecode.lib.dbmigrate.migrate import * | |
|
12 | from rhodecode.lib.dbmigrate.migrate.changeset import * | |||
12 |
|
13 | |||
13 | log = logging.getLogger(__name__) |
|
14 | log = logging.getLogger(__name__) | |
14 |
|
15 | |||
15 | def upgrade(migrate_engine): |
|
16 | def upgrade(migrate_engine): | |
16 | """ Upgrade operations go here. |
|
17 | """ Upgrade operations go here. | |
17 | Don't create your own engine; bind migrate_engine to your metadata |
|
18 | Don't create your own engine; bind migrate_engine to your metadata | |
18 | """ |
|
19 | """ | |
19 |
|
20 | |||
20 | #========================================================================== |
|
21 | #========================================================================== | |
21 | # Upgrade of `users` table |
|
22 | # Upgrade of `users` table | |
22 | #========================================================================== |
|
23 | #========================================================================== | |
23 | tblname = 'users' |
|
24 | tblname = 'users' | |
24 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, |
|
25 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, | |
25 | autoload_with=migrate_engine) |
|
26 | autoload_with=migrate_engine) | |
26 |
|
27 | |||
27 | #ADD is_ldap column |
|
28 | #ADD is_ldap column | |
28 | is_ldap = Column("is_ldap", Boolean(), nullable=True, |
|
29 | is_ldap = Column("is_ldap", Boolean(), nullable=True, | |
29 | unique=None, default=False) |
|
30 | unique=None, default=False) | |
30 | is_ldap.create(tbl, populate_default=True) |
|
31 | is_ldap.create(tbl, populate_default=True) | |
31 | is_ldap.alter(nullable=False) |
|
32 | is_ldap.alter(nullable=False) | |
32 |
|
33 | |||
33 | #========================================================================== |
|
34 | #========================================================================== | |
34 | # Upgrade of `user_logs` table |
|
35 | # Upgrade of `user_logs` table | |
35 | #========================================================================== |
|
36 | #========================================================================== | |
36 |
|
37 | |||
37 | tblname = 'users' |
|
38 | tblname = 'users' | |
38 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, |
|
39 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, | |
39 | autoload_with=migrate_engine) |
|
40 | autoload_with=migrate_engine) | |
40 |
|
41 | |||
41 | #ADD revision column |
|
42 | #ADD revision column | |
42 | revision = Column('revision', TEXT(length=None, convert_unicode=False, |
|
43 | revision = Column('revision', TEXT(length=None, convert_unicode=False, | |
43 | assert_unicode=None), |
|
44 | assert_unicode=None), | |
44 | nullable=True, unique=None, default=None) |
|
45 | nullable=True, unique=None, default=None) | |
45 | revision.create(tbl) |
|
46 | revision.create(tbl) | |
46 |
|
47 | |||
47 |
|
48 | |||
48 |
|
49 | |||
49 | #========================================================================== |
|
50 | #========================================================================== | |
50 | # Upgrade of `repositories` table |
|
51 | # Upgrade of `repositories` table | |
51 | #========================================================================== |
|
52 | #========================================================================== | |
52 | tblname = 'repositories' |
|
53 | tblname = 'repositories' | |
53 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, |
|
54 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, | |
54 | autoload_with=migrate_engine) |
|
55 | autoload_with=migrate_engine) | |
55 |
|
56 | |||
56 | #ADD repo_type column# |
|
57 | #ADD repo_type column# | |
57 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, |
|
58 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, | |
58 | assert_unicode=None), |
|
59 | assert_unicode=None), | |
59 | nullable=True, unique=False, default='hg') |
|
60 | nullable=True, unique=False, default='hg') | |
60 |
|
61 | |||
61 | repo_type.create(tbl, populate_default=True) |
|
62 | repo_type.create(tbl, populate_default=True) | |
62 | #repo_type.alter(nullable=False) |
|
63 | #repo_type.alter(nullable=False) | |
63 |
|
64 | |||
64 | #ADD statistics column# |
|
65 | #ADD statistics column# | |
65 | enable_statistics = Column("statistics", Boolean(), nullable=True, |
|
66 | enable_statistics = Column("statistics", Boolean(), nullable=True, | |
66 | unique=None, default=True) |
|
67 | unique=None, default=True) | |
67 | enable_statistics.create(tbl) |
|
68 | enable_statistics.create(tbl) | |
68 |
|
69 | |||
69 | #========================================================================== |
|
70 | #========================================================================== | |
70 | # Add table `user_followings` |
|
71 | # Add table `user_followings` | |
71 | #========================================================================== |
|
72 | #========================================================================== | |
72 | tblname = 'user_followings' |
|
73 | from rhodecode.model.db import UserFollowing | |
73 |
|
74 | UserFollowing().__table__.create() | ||
74 | class UserFollowing(Base, BaseModel): |
|
|||
75 | __tablename__ = 'user_followings' |
|
|||
76 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
|||
77 | UniqueConstraint('user_id', 'follows_user_id') |
|
|||
78 | , {'useexisting':True}) |
|
|||
79 |
|
||||
80 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
|||
81 | user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
|
|||
82 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None) |
|
|||
83 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None) |
|
|||
84 |
|
||||
85 | user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
|||
86 |
|
||||
87 | follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
|||
88 | follows_repository = relation('Repository') |
|
|||
89 |
|
||||
90 | Base.metadata.tables[tblname].create(migrate_engine) |
|
|||
91 |
|
75 | |||
92 | #========================================================================== |
|
76 | #========================================================================== | |
93 | # Add table `cache_invalidation` |
|
77 | # Add table `cache_invalidation` | |
94 | #========================================================================== |
|
78 | #========================================================================== | |
95 | tblname = 'cache_invalidation' |
|
79 | from rhodecode.model.db import CacheInvalidation | |
96 |
|
80 | CacheInvalidation().__table__.create() | ||
97 | class CacheInvalidation(Base, BaseModel): |
|
|||
98 | __tablename__ = 'cache_invalidation' |
|
|||
99 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
|
|||
100 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
|||
101 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
|||
102 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
|||
103 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
|||
104 |
|
||||
105 |
|
||||
106 | def __init__(self, cache_key, cache_args=''): |
|
|||
107 | self.cache_key = cache_key |
|
|||
108 | self.cache_args = cache_args |
|
|||
109 | self.cache_active = False |
|
|||
110 |
|
||||
111 | def __repr__(self): |
|
|||
112 | return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key) |
|
|||
113 |
|
||||
114 | Base.metadata.tables[tblname].create(migrate_engine) |
|
|||
115 |
|
81 | |||
116 | return |
|
82 | return | |
117 |
|
83 | |||
118 |
|
||||
119 |
|
||||
120 |
|
||||
121 |
|
||||
122 |
|
||||
123 | def downgrade(migrate_engine): |
|
84 | def downgrade(migrate_engine): | |
124 | meta = MetaData() |
|
85 | meta = MetaData() | |
125 | meta.bind = migrate_engine |
|
86 | meta.bind = migrate_engine | |
126 |
|
87 | |||
127 |
|
88 |
@@ -1,87 +1,59 b'' | |||||
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 | ||||
|
9 | ||||
|
10 | from rhodecode.lib.dbmigrate.migrate import * | |||
|
11 | from rhodecode.lib.dbmigrate.migrate.changeset import * | |||
|
12 | ||||
8 | from rhodecode.model.meta import Base |
|
13 | from rhodecode.model.meta import Base | |
9 | from rhodecode.model.db import BaseModel |
|
14 | from rhodecode.model.db import BaseModel | |
10 |
|
15 | |||
11 | from rhodecode.lib.dbmigrate.migrate import * |
|
|||
12 |
|
||||
13 | log = logging.getLogger(__name__) |
|
16 | log = logging.getLogger(__name__) | |
14 |
|
17 | |||
15 | def upgrade(migrate_engine): |
|
18 | def upgrade(migrate_engine): | |
16 | """ Upgrade operations go here. |
|
19 | """ Upgrade operations go here. | |
17 | Don't create your own engine; bind migrate_engine to your metadata |
|
20 | Don't create your own engine; bind migrate_engine to your metadata | |
18 | """ |
|
21 | """ | |
19 |
|
22 | |||
20 | #========================================================================== |
|
23 | #========================================================================== | |
21 | # Add table `groups`` |
|
24 | # Add table `groups`` | |
22 | #========================================================================== |
|
25 | #========================================================================== | |
23 | tblname = 'groups' |
|
26 | from rhodecode.model.db import Group | |
24 |
|
27 | Group().__table__.create() | ||
25 | class Group(Base, BaseModel): |
|
|||
26 | __tablename__ = 'groups' |
|
|||
27 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) |
|
|||
28 |
|
||||
29 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
|||
30 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
|||
31 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) |
|
|||
32 |
|
||||
33 | parent_group = relation('Group', remote_side=group_id) |
|
|||
34 |
|
||||
35 |
|
||||
36 | def __init__(self, group_name='', parent_group=None): |
|
|||
37 | self.group_name = group_name |
|
|||
38 | self.parent_group = parent_group |
|
|||
39 |
|
||||
40 | def __repr__(self): |
|
|||
41 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
|||
42 | self.group_name) |
|
|||
43 |
|
||||
44 | Base.metadata.tables[tblname].create(migrate_engine) |
|
|||
45 |
|
28 | |||
46 | #========================================================================== |
|
29 | #========================================================================== | |
47 | # Add table `group_to_perm` |
|
30 | # Add table `group_to_perm` | |
48 | #========================================================================== |
|
31 | #========================================================================== | |
49 | tblname = 'group_to_perm' |
|
32 | from rhodecode.model.db import GroupToPerm | |
50 |
|
33 | GroupToPerm().__table__.create() | ||
51 | class GroupToPerm(Base, BaseModel): |
|
|||
52 | __tablename__ = 'group_to_perm' |
|
|||
53 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) |
|
|||
54 |
|
||||
55 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
|||
56 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
|||
57 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
|||
58 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) |
|
|||
59 |
|
||||
60 | user = relation('User') |
|
|||
61 | permission = relation('Permission') |
|
|||
62 | group = relation('Group') |
|
|||
63 |
|
||||
64 | Base.metadata.tables[tblname].create(migrate_engine) |
|
|||
65 |
|
34 | |||
66 | #========================================================================== |
|
35 | #========================================================================== | |
67 | # Upgrade of `repositories` table |
|
36 | # Upgrade of `repositories` table | |
68 | #========================================================================== |
|
37 | #========================================================================== | |
69 | tblname = 'repositories' |
|
38 | tblname = 'repositories' | |
70 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, |
|
39 | tbl = Table(tblname, MetaData(bind=migrate_engine), autoload=True, | |
71 | autoload_with=migrate_engine) |
|
40 | autoload_with=migrate_engine) | |
72 |
|
41 | |||
73 | #ADD group_id column# |
|
42 | #ADD group_id column# | |
74 |
group_id = Column("group_id", Integer(), ForeignKey( |
|
43 | group_id = Column("group_id", Integer(), #ForeignKey('groups.group_id'), | |
75 | nullable=True, unique=False, default=None) |
|
44 | nullable=True, unique=False, default=None) | |
76 |
|
45 | |||
77 |
group_id.create(tbl |
|
46 | group_id.create(tbl) | |
78 |
|
47 | |||
79 |
|
48 | # TODO: fix this somehow ?! | ||
|
49 | # cons = ForeignKeyConstraint([tbl.c.group_id], ['groups.group_id'], table=tbl) | |||
|
50 | # | |||
|
51 | # cons.create() | |||
80 | return |
|
52 | return | |
81 |
|
53 | |||
82 |
|
54 | |||
83 | def downgrade(migrate_engine): |
|
55 | def downgrade(migrate_engine): | |
84 | meta = MetaData() |
|
56 | meta = MetaData() | |
85 | meta.bind = migrate_engine |
|
57 | meta.bind = migrate_engine | |
86 |
|
58 | |||
87 |
|
59 |
@@ -1,298 +1,298 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.db |
|
3 | rhodecode.model.db | |
4 | ~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Database Models for RhodeCode |
|
6 | Database Models for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Apr 08, 2010 |
|
8 | :created_on: Apr 08, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 | import logging |
|
27 | import logging | |
28 | import datetime |
|
28 | import datetime | |
29 |
|
29 | |||
30 | from sqlalchemy import * |
|
30 | from sqlalchemy import * | |
31 | from sqlalchemy.exc import DatabaseError |
|
31 | from sqlalchemy.exc import DatabaseError | |
32 | from sqlalchemy.orm import relation, backref, class_mapper |
|
32 | from sqlalchemy.orm import relation, backref, class_mapper | |
33 | from sqlalchemy.orm.session import Session |
|
33 | from sqlalchemy.orm.session import Session | |
34 |
|
34 | |||
35 | from rhodecode.model.meta import Base |
|
35 | from rhodecode.model.meta import Base | |
36 |
|
36 | |||
37 | log = logging.getLogger(__name__) |
|
37 | log = logging.getLogger(__name__) | |
38 |
|
38 | |||
39 | class BaseModel(object): |
|
39 | class BaseModel(object): | |
40 |
|
40 | |||
41 | @classmethod |
|
41 | @classmethod | |
42 | def _get_keys(cls): |
|
42 | def _get_keys(cls): | |
43 | """return column names for this model """ |
|
43 | """return column names for this model """ | |
44 | return class_mapper(cls).c.keys() |
|
44 | return class_mapper(cls).c.keys() | |
45 |
|
45 | |||
46 | def get_dict(self): |
|
46 | def get_dict(self): | |
47 | """return dict with keys and values corresponding |
|
47 | """return dict with keys and values corresponding | |
48 | to this model data """ |
|
48 | to this model data """ | |
49 |
|
49 | |||
50 | d = {} |
|
50 | d = {} | |
51 | for k in self._get_keys(): |
|
51 | for k in self._get_keys(): | |
52 | d[k] = getattr(self, k) |
|
52 | d[k] = getattr(self, k) | |
53 | return d |
|
53 | return d | |
54 |
|
54 | |||
55 | def get_appstruct(self): |
|
55 | def get_appstruct(self): | |
56 | """return list with keys and values tupples corresponding |
|
56 | """return list with keys and values tupples corresponding | |
57 | to this model data """ |
|
57 | to this model data """ | |
58 |
|
58 | |||
59 | l = [] |
|
59 | l = [] | |
60 | for k in self._get_keys(): |
|
60 | for k in self._get_keys(): | |
61 | l.append((k, getattr(self, k),)) |
|
61 | l.append((k, getattr(self, k),)) | |
62 | return l |
|
62 | return l | |
63 |
|
63 | |||
64 | def populate_obj(self, populate_dict): |
|
64 | def populate_obj(self, populate_dict): | |
65 | """populate model with data from given populate_dict""" |
|
65 | """populate model with data from given populate_dict""" | |
66 |
|
66 | |||
67 | for k in self._get_keys(): |
|
67 | for k in self._get_keys(): | |
68 | if k in populate_dict: |
|
68 | if k in populate_dict: | |
69 | setattr(self, k, populate_dict[k]) |
|
69 | setattr(self, k, populate_dict[k]) | |
70 |
|
70 | |||
71 | class RhodeCodeSettings(Base, BaseModel): |
|
71 | class RhodeCodeSettings(Base, BaseModel): | |
72 | __tablename__ = 'rhodecode_settings' |
|
72 | __tablename__ = 'rhodecode_settings' | |
73 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) |
|
73 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) | |
74 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
74 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
75 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
75 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
76 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
76 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
77 |
|
77 | |||
78 | def __init__(self, k, v): |
|
78 | def __init__(self, k='', v=''): | |
79 | self.app_settings_name = k |
|
79 | self.app_settings_name = k | |
80 | self.app_settings_value = v |
|
80 | self.app_settings_value = v | |
81 |
|
81 | |||
82 | def __repr__(self): |
|
82 | def __repr__(self): | |
83 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
83 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
84 | self.app_settings_name, self.app_settings_value) |
|
84 | self.app_settings_name, self.app_settings_value) | |
85 |
|
85 | |||
86 | class RhodeCodeUi(Base, BaseModel): |
|
86 | class RhodeCodeUi(Base, BaseModel): | |
87 | __tablename__ = 'rhodecode_ui' |
|
87 | __tablename__ = 'rhodecode_ui' | |
88 | __table_args__ = {'useexisting':True} |
|
88 | __table_args__ = {'useexisting':True} | |
89 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
89 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
90 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
90 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
91 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
91 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
92 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
92 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
93 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) |
|
93 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | class User(Base, BaseModel): |
|
96 | class User(Base, BaseModel): | |
97 | __tablename__ = 'users' |
|
97 | __tablename__ = 'users' | |
98 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) |
|
98 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) | |
99 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
99 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
100 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
100 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
101 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
101 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
102 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) |
|
102 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) | |
103 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) |
|
103 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) | |
104 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
104 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
105 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
105 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
106 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
106 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
107 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
107 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) | |
108 | is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False) |
|
108 | is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False) | |
109 |
|
109 | |||
110 | user_log = relation('UserLog', cascade='all') |
|
110 | user_log = relation('UserLog', cascade='all') | |
111 | user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
111 | user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') | |
112 |
|
112 | |||
113 | repositories = relation('Repository') |
|
113 | repositories = relation('Repository') | |
114 | user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
114 | user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') | |
115 |
|
115 | |||
116 | @property |
|
116 | @property | |
117 | def full_contact(self): |
|
117 | def full_contact(self): | |
118 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
118 | return '%s %s <%s>' % (self.name, self.lastname, self.email) | |
119 |
|
119 | |||
120 |
|
120 | |||
121 | @property |
|
121 | @property | |
122 | def is_admin(self): |
|
122 | def is_admin(self): | |
123 | return self.admin |
|
123 | return self.admin | |
124 |
|
124 | |||
125 | def __repr__(self): |
|
125 | def __repr__(self): | |
126 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
126 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, | |
127 | self.user_id, self.username) |
|
127 | self.user_id, self.username) | |
128 |
|
128 | |||
129 | def update_lastlogin(self): |
|
129 | def update_lastlogin(self): | |
130 | """Update user lastlogin""" |
|
130 | """Update user lastlogin""" | |
131 |
|
131 | |||
132 | try: |
|
132 | try: | |
133 | session = Session.object_session(self) |
|
133 | session = Session.object_session(self) | |
134 | self.last_login = datetime.datetime.now() |
|
134 | self.last_login = datetime.datetime.now() | |
135 | session.add(self) |
|
135 | session.add(self) | |
136 | session.commit() |
|
136 | session.commit() | |
137 | log.debug('updated user %s lastlogin', self.username) |
|
137 | log.debug('updated user %s lastlogin', self.username) | |
138 | except (DatabaseError,): |
|
138 | except (DatabaseError,): | |
139 | session.rollback() |
|
139 | session.rollback() | |
140 |
|
140 | |||
141 |
|
141 | |||
142 | class UserLog(Base, BaseModel): |
|
142 | class UserLog(Base, BaseModel): | |
143 | __tablename__ = 'user_logs' |
|
143 | __tablename__ = 'user_logs' | |
144 | __table_args__ = {'useexisting':True} |
|
144 | __table_args__ = {'useexisting':True} | |
145 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
145 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
146 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
146 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
147 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
147 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
148 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
148 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
149 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
149 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
150 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
150 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
151 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
151 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) | |
152 |
|
152 | |||
153 | user = relation('User') |
|
153 | user = relation('User') | |
154 | repository = relation('Repository') |
|
154 | repository = relation('Repository') | |
155 |
|
155 | |||
156 | class Repository(Base, BaseModel): |
|
156 | class Repository(Base, BaseModel): | |
157 | __tablename__ = 'repositories' |
|
157 | __tablename__ = 'repositories' | |
158 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) |
|
158 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) | |
159 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
159 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
160 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
160 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
161 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') |
|
161 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') | |
162 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) |
|
162 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) | |
163 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) |
|
163 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) | |
164 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) |
|
164 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) | |
165 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
165 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
166 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) |
|
166 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) | |
167 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) |
|
167 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) | |
168 |
|
168 | |||
169 | user = relation('User') |
|
169 | user = relation('User') | |
170 | fork = relation('Repository', remote_side=repo_id) |
|
170 | fork = relation('Repository', remote_side=repo_id) | |
171 | group = relation('Group') |
|
171 | group = relation('Group') | |
172 | repo_to_perm = relation('RepoToPerm', cascade='all') |
|
172 | repo_to_perm = relation('RepoToPerm', cascade='all') | |
173 | stats = relation('Statistics', cascade='all', uselist=False) |
|
173 | stats = relation('Statistics', cascade='all', uselist=False) | |
174 |
|
174 | |||
175 | repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') |
|
175 | repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') | |
176 |
|
176 | |||
177 | def __repr__(self): |
|
177 | def __repr__(self): | |
178 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
178 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
179 | self.repo_id, self.repo_name) |
|
179 | self.repo_id, self.repo_name) | |
180 |
|
180 | |||
181 | class Group(Base, BaseModel): |
|
181 | class Group(Base, BaseModel): | |
182 | __tablename__ = 'groups' |
|
182 | __tablename__ = 'groups' | |
183 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) |
|
183 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) | |
184 |
|
184 | |||
185 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
185 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
186 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
186 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
187 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) |
|
187 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) | |
188 |
|
188 | |||
189 | parent_group = relation('Group', remote_side=group_id) |
|
189 | parent_group = relation('Group', remote_side=group_id) | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | def __init__(self, group_name='', parent_group=None): |
|
192 | def __init__(self, group_name='', parent_group=None): | |
193 | self.group_name = group_name |
|
193 | self.group_name = group_name | |
194 | self.parent_group = parent_group |
|
194 | self.parent_group = parent_group | |
195 |
|
195 | |||
196 | def __repr__(self): |
|
196 | def __repr__(self): | |
197 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
197 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, | |
198 | self.group_name) |
|
198 | self.group_name) | |
199 |
|
199 | |||
200 | class Permission(Base, BaseModel): |
|
200 | class Permission(Base, BaseModel): | |
201 | __tablename__ = 'permissions' |
|
201 | __tablename__ = 'permissions' | |
202 | __table_args__ = {'useexisting':True} |
|
202 | __table_args__ = {'useexisting':True} | |
203 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
203 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
204 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
204 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
205 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
205 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
206 |
|
206 | |||
207 | def __repr__(self): |
|
207 | def __repr__(self): | |
208 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
208 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
209 | self.permission_id, self.permission_name) |
|
209 | self.permission_id, self.permission_name) | |
210 |
|
210 | |||
211 | class RepoToPerm(Base, BaseModel): |
|
211 | class RepoToPerm(Base, BaseModel): | |
212 | __tablename__ = 'repo_to_perm' |
|
212 | __tablename__ = 'repo_to_perm' | |
213 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) |
|
213 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) | |
214 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
214 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
215 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
215 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
216 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
216 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
217 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
217 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
218 |
|
218 | |||
219 | user = relation('User') |
|
219 | user = relation('User') | |
220 | permission = relation('Permission') |
|
220 | permission = relation('Permission') | |
221 | repository = relation('Repository') |
|
221 | repository = relation('Repository') | |
222 |
|
222 | |||
223 | class UserToPerm(Base, BaseModel): |
|
223 | class UserToPerm(Base, BaseModel): | |
224 | __tablename__ = 'user_to_perm' |
|
224 | __tablename__ = 'user_to_perm' | |
225 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) |
|
225 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) | |
226 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
226 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
227 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
227 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
228 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
228 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
229 |
|
229 | |||
230 | user = relation('User') |
|
230 | user = relation('User') | |
231 | permission = relation('Permission') |
|
231 | permission = relation('Permission') | |
232 |
|
232 | |||
233 | class GroupToPerm(Base, BaseModel): |
|
233 | class GroupToPerm(Base, BaseModel): | |
234 | __tablename__ = 'group_to_perm' |
|
234 | __tablename__ = 'group_to_perm' | |
235 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) |
|
235 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) | |
236 |
|
236 | |||
237 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
237 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
238 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
238 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
239 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
239 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
240 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) |
|
240 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) | |
241 |
|
241 | |||
242 | user = relation('User') |
|
242 | user = relation('User') | |
243 | permission = relation('Permission') |
|
243 | permission = relation('Permission') | |
244 | group = relation('Group') |
|
244 | group = relation('Group') | |
245 |
|
245 | |||
246 | class Statistics(Base, BaseModel): |
|
246 | class Statistics(Base, BaseModel): | |
247 | __tablename__ = 'statistics' |
|
247 | __tablename__ = 'statistics' | |
248 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) |
|
248 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) | |
249 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
249 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
250 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) |
|
250 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) | |
251 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) |
|
251 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) | |
252 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data |
|
252 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data | |
253 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data |
|
253 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data | |
254 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data |
|
254 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data | |
255 |
|
255 | |||
256 | repository = relation('Repository', single_parent=True) |
|
256 | repository = relation('Repository', single_parent=True) | |
257 |
|
257 | |||
258 | class UserFollowing(Base, BaseModel): |
|
258 | class UserFollowing(Base, BaseModel): | |
259 | __tablename__ = 'user_followings' |
|
259 | __tablename__ = 'user_followings' | |
260 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
260 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), | |
261 | UniqueConstraint('user_id', 'follows_user_id') |
|
261 | UniqueConstraint('user_id', 'follows_user_id') | |
262 | , {'useexisting':True}) |
|
262 | , {'useexisting':True}) | |
263 |
|
263 | |||
264 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
264 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
265 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
265 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
266 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) |
|
266 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) | |
267 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) |
|
267 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) | |
268 |
|
268 | |||
269 | user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
269 | user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id') | |
270 |
|
270 | |||
271 | follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
271 | follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') | |
272 | follows_repository = relation('Repository') |
|
272 | follows_repository = relation('Repository') | |
273 |
|
273 | |||
274 | class CacheInvalidation(Base, BaseModel): |
|
274 | class CacheInvalidation(Base, BaseModel): | |
275 | __tablename__ = 'cache_invalidation' |
|
275 | __tablename__ = 'cache_invalidation' | |
276 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
|
276 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) | |
277 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
277 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
278 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
278 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
279 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
279 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
280 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
280 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) | |
281 |
|
281 | |||
282 |
|
282 | |||
283 | def __init__(self, cache_key, cache_args=''): |
|
283 | def __init__(self, cache_key, cache_args=''): | |
284 | self.cache_key = cache_key |
|
284 | self.cache_key = cache_key | |
285 | self.cache_args = cache_args |
|
285 | self.cache_args = cache_args | |
286 | self.cache_active = False |
|
286 | self.cache_active = False | |
287 |
|
287 | |||
288 | def __repr__(self): |
|
288 | def __repr__(self): | |
289 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
289 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
290 | self.cache_id, self.cache_key) |
|
290 | self.cache_id, self.cache_key) | |
291 |
|
291 | |||
292 | class DbMigrateVersion(Base, BaseModel): |
|
292 | class DbMigrateVersion(Base, BaseModel): | |
293 | __tablename__ = 'db_migrate_version' |
|
293 | __tablename__ = 'db_migrate_version' | |
294 | __table_args__ = {'useexisting':True} |
|
294 | __table_args__ = {'useexisting':True} | |
295 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
295 | repository_id = Column('repository_id', String(250), primary_key=True) | |
296 | repository_path = Column('repository_path', Text) |
|
296 | repository_path = Column('repository_path', Text) | |
297 | version = Column('version', Integer) |
|
297 | version = Column('version', Integer) | |
298 |
|
298 |
General Comments 0
You need to be logged in to leave comments.
Login now