Mercurial > kallithea
annotate rhodecode/lib/dbmigrate/schema/db_1_1_0.py @ 1632:5b2cf21b1947 beta
Synced with latest sqlalchemy-migrate, added new upcomming migration for 1.3
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 02 Nov 2011 03:04:54 +0200 |
parents | 5cacb51f25f1 |
children | cf51bbfb120e |
rev | line source |
---|---|
1631
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 from sqlalchemy import * |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 from sqlalchemy.exc import DatabaseError |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 from sqlalchemy.orm import relation, backref, class_mapper |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 from sqlalchemy.orm.session import Session |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 from rhodecode.model.meta import Base |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 class BaseModel(object): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 """Base Model for all classess |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 """ |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 @classmethod |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 def _get_keys(cls): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 """return column names for this model """ |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 return class_mapper(cls).c.keys() |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 def get_dict(self): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 """return dict with keys and values corresponding |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 to this model data """ |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 d = {} |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 for k in self._get_keys(): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 d[k] = getattr(self, k) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 return d |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 def get_appstruct(self): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 """return list with keys and values tupples corresponding |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 to this model data """ |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 l = [] |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 for k in self._get_keys(): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 l.append((k, getattr(self, k),)) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 return l |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 def populate_obj(self, populate_dict): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 """populate model with data from given populate_dict""" |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 for k in self._get_keys(): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 if k in populate_dict: |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 setattr(self, k, populate_dict[k]) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 @classmethod |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 def query(cls): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 return Session.query(cls) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 @classmethod |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 def get(cls, id_): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 if id_: |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 return cls.query().get(id_) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 @classmethod |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 def getAll(cls): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 return cls.query().all() |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 @classmethod |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 def delete(cls, id_): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 obj = cls.query().get(id_) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 Session.delete(obj) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 Session.commit() |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 class UserFollowing(Base, BaseModel): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 __tablename__ = 'user_followings' |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 UniqueConstraint('user_id', 'follows_user_id') |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 , {'useexisting':True}) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 user_id = Column("user_id", Integer(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 follows_user_id = Column("follows_user_id", Integer(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id') |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 follows_repository = relation('Repository') |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 class CacheInvalidation(Base, BaseModel): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 __tablename__ = 'cache_invalidation' |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 def __init__(self, cache_key, cache_args=''): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 self.cache_key = cache_key |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 self.cache_args = cache_args |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 self.cache_active = False |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 def __repr__(self): |
5cacb51f25f1
freeze of models for future migrations using small schema files snapshoting current model state
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key) |