Change Message data format somewhat
[trackgit.git] / db.py
blob80a5f8dbc1c9561c9fb4998111b883877851cbe7
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):
17 self.sha1 = 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):
31 self.sha1 = 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):
49 self.sha1 = sha1
50 self.cdate = cdate
51 self.adate = adate
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 subject = Column(String(255))
79 gmane_id = Column(Integer)
80 has_patch = Column(Boolean)
81 stale = Column(Boolean)
82 patch_id = Column(String(40))
83 data = Column(Binary)
85 references = relation('Mail', secondary=Reference.__table__,
86 primaryjoin='Mail.id==Reference.mail_id',
87 secondaryjoin='Mail.message_id==Reference.reference_id',
88 foreign_keys=[Reference.mail_id, Reference.reference_id])
91 class Patch(DeclarativeBase):
92 __tablename__ = 'patches'
94 id = Column(Integer, primary_key=True)
95 commit_sha1 = Column(String(40), ForeignKey('commits.sha1'))
96 mail_id = Column(Integer, ForeignKey('mails.id'))
97 extra_notes = Column(Binary)
99 commit = relation('Commit', backref='patch')
100 mail = relation('Mail', backref='patch')
102 def __init__(self, commit, mail_id, extra_notes):
103 self.commit = commit
104 self.mail_id = mail_id
105 self.extra_notes = extra_notes
108 class Topic(DeclarativeBase):
109 __tablename__ = 'topics'
111 id = Column(Integer, primary_key=True)
112 name = Column(String(255))
113 mail_id = Column(Integer, ForeignKey('mails.id'))
114 cooking_notes = Column(Binary)
116 mail = relation('Mail', backref='topic')
119 DeclarativeBase.metadata.create_all(engine)
121 if __name__ == '__main__':
122 b = Boundary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
123 c = Commit("cccccccccccccccccccccccccccccccccccccccc", 0, 0,
124 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
125 b = Blob("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
126 m = Mail()
127 p = Patch()