Attempt to avoid having empty newest_commit_sha1 in blobs
[trackgit.git] / db.py
blob51eb1ea2ce409a27bf3a395dda352aa6f492c474
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 not self.newest_commit_sha1 or self.newest_commit.cdate < commit.cdate:
35 self.newest_commit = commit
38 class Filename(DeclarativeBase):
39 __tablename__ = 'filenames'
41 id = Column(Integer, primary_key=True)
42 name = Column(String(255), unique=True, index=True)
44 def __init__(self, name):
45 self.name = name
48 class Commit(DeclarativeBase):
49 __tablename__ = 'commits'
51 sha1 = Column(String(40), primary_key=True)
52 cdate = Column(Integer, index=True)
53 adate = Column(Integer)
54 patch_id = Column(String(40), index=True)
55 upstream = Column(Boolean)
57 def __init__(self, sha1, cdate, adate, patch_id, upstream=True):
58 self.sha1 = sha1
59 self.cdate = cdate
60 self.adate = adate
61 self.patch_id = patch_id
62 self.upstream = upstream
63 def __cmp__(self, other):
64 return cmp(self.cdate, other.cdate)
67 class Reference(DeclarativeBase):
68 __tablename__ = 'mailrefs'
70 id = Column(Integer, primary_key=True)
71 mail_id = Column(Integer, ForeignKey('mails.id'), index=True)
72 reference_id = Column(String(255))
74 def __init__(self, mail_id, reference_id):
75 self.mail_id = mail_id
76 self.reference_id = reference_id
79 class Mail(DeclarativeBase):
80 __tablename__ = 'mails'
82 id = Column(Integer, primary_key=True)
83 message_id = Column(String(255), nullable=False, unique=True)
84 in_reply_to = Column(String(255))
85 post_date = Column(Integer)
86 author = Column(String(255))
87 subject = Column(String(255))
88 gmane_id = Column(Integer)
89 has_patch = Column(Boolean)
90 stale = Column(Boolean, index=True)
91 patch_id = Column(String(40), index=True)
92 data = Column(Binary)
94 references = relation('Mail', secondary=Reference.__table__,
95 primaryjoin='Mail.id==Reference.mail_id',
96 secondaryjoin='Mail.message_id==Reference.reference_id',
97 foreign_keys=[Reference.mail_id, Reference.reference_id])
99 def __init__(self, message_id):
100 self.message_id = message_id
103 class Patch(DeclarativeBase):
104 __tablename__ = 'patches'
106 id = Column(Integer, primary_key=True)
107 commit_sha1 = Column(String(40), ForeignKey('commits.sha1'))
108 mail_id = Column(Integer, ForeignKey('mails.id'))
109 extra_notes = Column(Binary)
111 commit = relation('Commit', backref='patch')
112 mail = relation('Mail', backref='patch')
114 def __init__(self, commit, mail_id, extra_notes):
115 self.commit = commit
116 self.mail_id = mail_id
117 self.extra_notes = extra_notes
120 class Topic(DeclarativeBase):
121 __tablename__ = 'topics'
123 id = Column(Integer, primary_key=True)
124 name = Column(String(255), unique=True, index=True)
125 timestamp = Column(Integer)
126 mail_id = Column(Integer, ForeignKey('mails.id'))
127 cooking_notes = Column(Binary)
129 mail = relation('Mail', backref='topic')
132 DeclarativeBase.metadata.create_all(engine)
134 if __name__ == '__main__':
135 b = Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
136 c = Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
137 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
138 b = Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
139 m = Mail()
140 p = Patch()