1 from sqlalchemy
.ext
.declarative
import declarative_base
2 from sqlalchemy
import Table
, Column
, Integer
, String
, Binary
, Boolean
, \
3 ForeignKey
, create_engine
4 from sqlalchemy
.orm
import sessionmaker
5 from sqlalchemy
.orm
import relation
, backref
7 DeclarativeBase
= declarative_base()
9 engine
= create_engine('postgres://trackgit:trackgit@localhost/trackgit', echo
=False)
10 Session
= sessionmaker(bind
=engine
)
13 class Boundary(DeclarativeBase
):
14 __tablename__
= 'boundaries'
15 sha1
= Column(String(40), primary_key
=True)
16 def __init__(self
, sha1
):
20 class Blob(DeclarativeBase
):
21 __tablename__
= 'blobs'
23 sha1
= Column(String(40), primary_key
=True)
24 newest_commit_sha1
= Column(String(40), ForeignKey('commits.sha1'))
25 oldest_commit_sha1
= Column(String(40), ForeignKey('commits.sha1'))
27 newest_commit
= relation('Commit', primaryjoin
='Blob.newest_commit_sha1==Commit.sha1')
28 oldest_commit
= relation('Commit', primaryjoin
='Blob.oldest_commit_sha1==Commit.sha1')
30 def __init__(self
, sha1
):
32 def update_contained_in(self
, commit
):
33 #if not self.oldest_commit or self.oldest_commit.cdate > commit.cdate:
34 # self.oldest_commit = commit
35 if not self
.newest_commit
or self
.newest_commit
.cdate
< commit
.cdate
:
36 self
.newest_commit
= commit
39 class Commit(DeclarativeBase
):
40 __tablename__
= 'commits'
42 sha1
= Column(String(40), primary_key
=True)
43 cdate
= Column(Integer
)
44 adate
= Column(Integer
)
45 patch_id
= Column(String(40))
46 upstream
= Column(Boolean
)
48 def __init__(self
, sha1
, cdate
, adate
, patch_id
, upstream
=True):
52 self
.patch_id
= patch_id
53 self
.upstream
= upstream
54 def __cmp__(self
, other
):
55 return cmp(self
.cdate
, other
.cdate
)
58 class Reference(DeclarativeBase
):
59 __tablename__
= 'references'
61 id = Column(Integer
, primary_key
=True)
62 mail_id
= Column(Integer
, ForeignKey('mails.id'))
63 reference_id
= Column(String(255))
65 def __init__(self
, mail_id
, reference_id
):
66 self
.mail_id
= mail_id
67 self
.reference_id
= reference_id
70 class Mail(DeclarativeBase
):
71 __tablename__
= 'mails'
73 id = Column(Integer
, primary_key
=True)
74 message_id
= Column(String(255), nullable
=False, unique
=True)
75 in_reply_to
= Column(String(255))
76 post_date
= Column(Integer
)
77 author
= Column(String(255))
78 has_patch
= Column(Boolean
)
79 patch_id
= Column(String(40))
80 payload
= Column(Binary
)
82 references
= relation('Mail', secondary
=Reference
.__table
__,
83 primaryjoin
='Mail.id==Reference.mail_id',
84 secondaryjoin
='Mail.message_id==Reference.reference_id',
85 foreign_keys
=[Reference
.mail_id
, Reference
.reference_id
])
88 class Patch(DeclarativeBase
):
89 __tablename__
= 'patches'
91 id = Column(Integer
, primary_key
=True)
92 commit_sha1
= Column(String(40), ForeignKey('commits.sha1'))
93 mail_id
= Column(Integer
, ForeignKey('mails.id'))
94 extra_notes
= Column(Binary
)
96 commit
= relation('Commit', backref
='patch')
97 mail
= relation('Mail', backref
='patch')
99 def __init__(self
, commit
, mail_id
, extra_notes
):
101 self
.mail_id
= mail_id
102 self
.extra_notes
= extra_notes
105 class Topic(DeclarativeBase
):
106 __tablename__
= 'topics'
108 id = Column(Integer
, primary_key
=True)
109 name
= Column(String(255))
110 mail_id
= Column(Integer
, ForeignKey('mails.id'))
111 cooking_notes
= Column(Binary
)
113 mail
= relation('Mail', backref
='topic')
116 DeclarativeBase
.metadata
.create_all(engine
)
118 if __name__
== '__main__':
119 b
= Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
120 c
= Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
121 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
122 b
= Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")