Add commit author tracking
[trackgit.git] / db.py
blobdc7e934e44b275b5a7628f6832bf96a36595b5b9
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)
11 session = _Session()
12 query = session.query
14 class Boundary(DeclarativeBase):
15 __tablename__ = 'boundaries'
16 sha1 = Column(String(40), primary_key=True)
17 def __init__(self, sha1):
18 self.sha1 = sha1
21 class Blob(DeclarativeBase):
22 __tablename__ = 'blobs'
24 sha1 = Column(String(40), primary_key=True)
25 newest_commit_sha1 = Column(String(40), ForeignKey('commits.sha1'))
27 newest_commit = relation('Commit', primaryjoin='Blob.newest_commit_sha1==Commit.sha1')
29 def __init__(self, sha1):
30 self.sha1 = sha1
31 def update_contained_in(self, commit):
32 #if not self.oldest_commit or self.oldest_commit.cdate > commit.cdate:
33 # self.oldest_commit = commit
34 if self.newest_commit is None \
35 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):
46 self.name = name
49 class Commit(DeclarativeBase):
50 __tablename__ = 'commits'
52 sha1 = Column(String(40), primary_key=True)
53 cdate = Column(Integer, index=True)
54 adate = Column(Integer)
55 author = Column(String(255))
56 patch_id = Column(String(40), index=True)
57 upstream = Column(Boolean)
59 def __init__(self, sha1, cdate, adate, author, patch_id, upstream=True):
60 self.sha1 = sha1
61 self.cdate = cdate
62 self.adate = adate
63 self.author = author
64 self.patch_id = patch_id
65 self.upstream = upstream
66 def __cmp__(self, other):
67 return cmp(self.cdate, other.cdate)
70 class Reference(DeclarativeBase):
71 __tablename__ = 'mailrefs'
73 id = Column(Integer, primary_key=True)
74 mail_id = Column(Integer, ForeignKey('mails.id'), index=True)
75 reference_id = Column(String(255))
77 def __init__(self, mail_id, reference_id):
78 self.mail_id = mail_id
79 self.reference_id = reference_id
82 class Mail(DeclarativeBase):
83 __tablename__ = 'mails'
85 id = Column(Integer, primary_key=True)
86 message_id = Column(String(255), nullable=False, unique=True)
87 in_reply_to = Column(String(255))
88 post_date = Column(Integer)
89 author = Column(String(255))
90 subject = Column(String(255))
91 gmane_id = Column(Integer)
92 has_patch = Column(Boolean)
93 stale = Column(Boolean, index=True)
94 patch_id = Column(String(40), index=True)
95 data = Column(Binary)
97 references = relation('Mail', secondary=Reference.__table__,
98 primaryjoin='Mail.id==Reference.mail_id',
99 secondaryjoin='Mail.message_id==Reference.reference_id',
100 foreign_keys=[Reference.mail_id, Reference.reference_id])
102 def __init__(self, message_id):
103 self.message_id = message_id
106 class Patch(DeclarativeBase):
107 __tablename__ = 'patches'
109 id = Column(Integer, primary_key=True)
110 commit_sha1 = Column(String(40), ForeignKey('commits.sha1'))
111 mail_id = Column(Integer, ForeignKey('mails.id'))
112 extra_notes = Column(Binary)
114 commit = relation('Commit', backref='patch')
115 mail = relation('Mail', backref='patch')
117 def __init__(self, commit, mail_id, extra_notes):
118 self.commit = commit
119 self.mail_id = mail_id
120 self.extra_notes = extra_notes
123 class Topic(DeclarativeBase):
124 __tablename__ = 'topics'
126 id = Column(Integer, primary_key=True)
127 name = Column(String(255), unique=True, index=True)
128 timestamp = Column(Integer)
129 mail_id = Column(Integer, ForeignKey('mails.id'))
130 cooking_notes = Column(Binary)
132 mail = relation('Mail', backref='topic')
135 DeclarativeBase.metadata.create_all(engine)
137 if __name__ == '__main__':
138 b = Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
139 c = Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
140 "A U Thor <author@example.com>",
141 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
142 b = Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
143 m = Mail()
144 p = Patch()