This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
93 lines (73 loc) · 2.46 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from sqlalchemy import (
create_engine, MetaData, Table, Column, Integer, String, ForeignKey,
Unicode, DateTime, UnicodeText, Boolean)
from sqlalchemy.orm import mapper, scoped_session, sessionmaker, relation
from config import url, params
if sys.version_info.major < 3:
input = raw_input
engine = create_engine(url, **params)
Session = scoped_session(sessionmaker(engine))
metadata = MetaData()
feeds_table = Table(
'feeds', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('title', Unicode(40), nullable=False),
Column('url', String(2038), nullable=False),
Column('updated', DateTime, nullable=True),
Column('description', Unicode(300))
)
class Feed(object):
pass
posts_table = Table(
'posts', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('feed_id', ForeignKey('feeds.id'), nullable=False),
Column('entry_id', String(300), nullable=False),
Column('title', Unicode(400), nullable=False),
Column('link', String(2038), nullable=False),
Column('author', Unicode(80), nullable=False),
Column('summary', UnicodeText),
Column('content', UnicodeText),
Column('read', Boolean, default=False),
Column('published', DateTime, nullable=False),
Column('updated', DateTime, nullable=True),
)
class Post(object):
pass
tags_table = Table(
'tags', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('slug', String(20), nullable=False),
Column('name', Unicode(20), nullable=False),
)
class Tag(object):
pass
feedtags_table = Table(
'feedtags', metadata,
Column('feed_id', ForeignKey('feeds.id'), primary_key=True),
Column('tag_id', ForeignKey('tags.id'), primary_key=True),
)
posttags_table = Table(
'posttags', metadata,
Column('post_id', ForeignKey('posts.id'), primary_key=True),
Column('tag_id', ForeignKey('tags.id'), primary_key=True),
)
mapper(Feed, feeds_table, properties={
'posts': relation(Post, backref='feed',
cascade='all, delete, delete-orphan')
})
mapper(Post, posts_table)
mapper(Tag, tags_table)
def main():
if input('Type YES to drop and recreate all tables: ') == 'YES':
metadata.drop_all(engine)
print('Dropped tables.')
metadata.create_all(engine)
print('Created tables.')
else:
print('Do nothing.')
if __name__ == '__main__':
main()