Revert "... and stop calling the blobtracker"
[trackgit.git] / db.py
blob8b270eca3cf22288b9b88674010fff4f1c85765d
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, deferred
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 self.newest_commit is None \
33 or self.newest_commit.cdate < commit.cdate:
34 self.newest_commit = commit
37 class Filename(DeclarativeBase):
38 __tablename__ = 'filenames'
40 id = Column(Integer, primary_key=True)
41 name = Column(String(255), unique=True, index=True)
43 def __init__(self, name):
44 self.name = name
47 class Commit(DeclarativeBase):
48 __tablename__ = 'commits'
50 sha1 = Column(String(40), primary_key=True)
51 cdate = Column(Integer, index=True)
52 adate = Column(Integer)
53 author = Column(String(255))
54 patch_id = Column(String(40), index=True)
55 upstream = Column(Boolean)
57 def __init__(self, sha1, cdate, adate, author, patch_id, upstream=True):
58 self.sha1 = sha1
59 self.cdate = cdate
60 self.adate = adate
61 self.author = author
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)
93 data = deferred(Column(Binary))
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])
100 def __init__(self, message_id):
101 self.message_id = message_id
104 class Patch(DeclarativeBase):
105 __tablename__ = 'patches'
107 id = Column(Integer, primary_key=True)
108 commit_sha1 = Column(String(40), ForeignKey('commits.sha1'))
109 mail_id = Column(Integer, ForeignKey('mails.id'))
110 extra_notes = Column(Binary)
112 commit = relation('Commit', backref='patch')
113 mail = relation('Mail', backref='patch')
115 def __init__(self, commit, mail_id, extra_notes):
116 self.commit = commit
117 self.mail_id = mail_id
118 self.extra_notes = extra_notes
121 class Topic(DeclarativeBase):
122 __tablename__ = 'topics'
124 id = Column(Integer, primary_key=True)
125 name = Column(String(255), unique=True, index=True)
126 timestamp = Column(Integer)
127 mail_id = Column(Integer, ForeignKey('mails.id'))
128 cooking_notes = Column(Binary)
130 mail = relation('Mail', backref='topic')
133 DeclarativeBase.metadata.create_all(engine)
135 if __name__ == '__main__':
136 b = Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
137 c = Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
138 "A U Thor <author@example.com>",
139 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
140 b = Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
141 m = Mail()
142 p = Patch()