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 Filename(DeclarativeBase
):
40 __tablename__
= 'filenames'
42 id = Column(Integer
, primary_key
=True)
43 name
= Column(String(255), unique
=True, index
=True)
45 def __init__(self
, name
):
49 class Commit(DeclarativeBase
):
50 __tablename__
= 'commits'
52 sha1
= Column(String(40), primary_key
=True)
53 cdate
= Column(Integer
)
54 adate
= Column(Integer
)
55 patch_id
= Column(String(40), index
=True)
56 upstream
= Column(Boolean
)
58 def __init__(self
, sha1
, cdate
, adate
, patch_id
, upstream
=True):
62 self
.patch_id
= patch_id
63 self
.upstream
= upstream
64 def __cmp__(self
, other
):
65 return cmp(self
.cdate
, other
.cdate
)
68 class Reference(DeclarativeBase
):
69 __tablename__
= 'mailrefs'
71 id = Column(Integer
, primary_key
=True)
72 mail_id
= Column(Integer
, ForeignKey('mails.id'), index
=True)
73 reference_id
= Column(String(255))
75 def __init__(self
, mail_id
, reference_id
):
76 self
.mail_id
= mail_id
77 self
.reference_id
= reference_id
80 class Mail(DeclarativeBase
):
81 __tablename__
= 'mails'
83 id = Column(Integer
, primary_key
=True)
84 message_id
= Column(String(255), nullable
=False, unique
=True)
85 in_reply_to
= Column(String(255))
86 post_date
= Column(Integer
)
87 author
= Column(String(255))
88 subject
= Column(String(255))
89 gmane_id
= Column(Integer
)
90 has_patch
= Column(Boolean
)
91 stale
= Column(Boolean
, index
=True)
92 patch_id
= Column(String(40), index
=True)
95 references
= relation('Mail', secondary
=Reference
.__table
__,
96 primaryjoin
='Mail.id==Reference.mail_id',
97 secondaryjoin
='Mail.message_id==Reference.reference_id',
98 foreign_keys
=[Reference
.mail_id
, Reference
.reference_id
])
101 class Patch(DeclarativeBase
):
102 __tablename__
= 'patches'
104 id = Column(Integer
, primary_key
=True)
105 commit_sha1
= Column(String(40), ForeignKey('commits.sha1'))
106 mail_id
= Column(Integer
, ForeignKey('mails.id'))
107 extra_notes
= Column(Binary
)
109 commit
= relation('Commit', backref
='patch')
110 mail
= relation('Mail', backref
='patch')
112 def __init__(self
, commit
, mail_id
, extra_notes
):
114 self
.mail_id
= mail_id
115 self
.extra_notes
= extra_notes
118 class Topic(DeclarativeBase
):
119 __tablename__
= 'topics'
121 id = Column(Integer
, primary_key
=True)
122 name
= Column(String(255), unique
=True, index
=True)
123 timestamp
= Column(Integer
)
124 mail_id
= Column(Integer
, ForeignKey('mails.id'))
125 cooking_notes
= Column(Binary
)
127 mail
= relation('Mail', backref
='topic')
130 DeclarativeBase
.metadata
.create_all(engine
)
132 if __name__
== '__main__':
133 b
= Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
134 c
= Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
135 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
136 b
= Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")