-
Notifications
You must be signed in to change notification settings - Fork 173
/
manage.py
266 lines (221 loc) · 8.42 KB
/
manage.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import math
from pathlib import Path
from typing import Dict
import click
import cssmin
from tortoise import Tortoise, run_async
from tortoise.exceptions import IntegrityError, OperationalError
from config import HERE
from ext import init_db
from models import create_user
from models.base import clear_mc, MC_KEY_PAGINATE, PER_PAGE
from models.blog import PAGEVIEW_FIELD, RK_ALL_POST_IDS, RK_PAGEVIEW, Post
from models.utils import get_redis
async def init() -> None:
await init_db(create_db=False)
await Tortoise._drop_databases()
await init_db(create_db=True)
await Tortoise.generate_schemas()
# Add Indexes
client = Tortoise.get_connection('default')
await client.execute_script(
'alter table posts add index `idx_slug` (`slug`)')
await client.execute_script(
'alter table post_tags add index `idx_post_tag` (`post_id`, `tag_id`)')
await client.execute_script(
'alter table comments add index `idx_target_kind` (`target_id`, `target_kind`)')
await client.execute_script(
'alter table react_items add index `idx_id_kind_user` (`target_id`, `target_kind`, `user_id`)') # noqa
for func in [_migrate_for_v25, _migrate_for_v27, _migrate_for_v30,
_migrate_for_v35, _migrate_for_v4]:
try:
await func()
except OperationalError:
...
async def _migrate_for_v25() -> None:
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'alter table posts add column `pageview` int(11) DEFAULT "0"')
async def _migrate_for_v27() -> None:
redis = await get_redis()
keys = await redis.keys('lyanna:pageview:*')
ids = []
for k in keys:
id = k.split(b':')[-1]
if id.isdigit():
await redis.hset(RK_PAGEVIEW.format(id.decode()), PAGEVIEW_FIELD,
int(await redis.get(k)))
ids.append(id)
if ids:
await redis.sadd(RK_ALL_POST_IDS, *ids)
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'''CREATE TABLE `special_item` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`index` smallint(6) NOT NULL,
`special_id` smallint(6) NOT NULL,
`created_at` datetime(6) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_special_post` (`special_id`,`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8''')
await client.execute_script(
'''CREATE TABLE `special_topic` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`intro` varchar(2000) NOT NULL,
`title` varchar(100) NOT NULL,
`created_at` datetime(6) NOT NULL,
`status` smallint(6) NOT NULL DEFAULT '0',
`slug` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`),
KEY `idx_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8''')
await client.execute_script(
('alter table post_tags add column `updated_at` datetime(6) '
'DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)'))
await client.execute_script('alter table users add column `avatar` varchar(100) DEFAULT ""') # noqa
async def _migrate_for_v30() -> None:
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'''CREATE TABLE `activity` (
`can_comment` tinyint(1) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime(6) NOT NULL,
`user_id` int(11) NOT NULL,
`target_id` int(11) NOT NULL,
`target_kind` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
await client.execute_script(
'''CREATE TABLE `statuses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime(6) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
await client.execute_script(
'ALTER TABLE comments CHANGE `post_id` `target_id` int(11) NOT NULL')
await client.execute_script(
'alter table comments add column `target_kind` smallint(6) DEFAULT 1001')
async def _migrate_for_v35() -> None:
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'alter table activity add index `idx_target_kind` (`target_id`, `target_kind`)') # noqa
async def _migrate_for_v4() -> None:
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'''CREATE TABLE `subject` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime(6) NOT NULL,
`post_id` int(11) NOT NULL,
`slug` varchar(100) NOT NULL,
`target_kind` tinyint(2) NOT NULL,
`title` varchar(100) NOT NULL,
`basename` varchar(100) NOT NULL,
`abstract` varchar(400) NOT NULL,
`target_url` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_post_id` (`post_id`),
KEY `idx_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
await client.execute_script(
'''CREATE TABLE `favorite` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime(6) NOT NULL,
`type` varchar(10) NOT NULL,
`index` tinyint(6) DEFAULT 0,
`subject_id` int(11) NOT NULL,
`rating` float DEFAULT '0',
`comment` varchar(200) NOT NULL,
`title` varchar(200) DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_type_sid` (`type`, `subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
@click.group()
def cli():
...
@cli.command()
def initdb():
run_async(init())
click.echo('Init Finished!')
@cli.command()
def migrate_for_v25():
run_async(_migrate_for_v25())
click.echo('Migrate Finished!')
@cli.command()
def migrate_for_v27():
run_async(_migrate_for_v27())
click.echo('Migrate Finished!')
@cli.command()
def migrate_for_v30():
run_async(_migrate_for_v30())
click.echo('Migrate Finished!')
@cli.command()
def migrate_for_v35():
run_async(_migrate_for_v35())
click.echo('Migrate Finished!')
@cli.command()
def migrate_for_v4():
run_async(_migrate_for_v4())
click.echo('Migrate Finished!')
async def _adduser(**kwargs) -> None:
await init_db()
try:
user = await create_user(**kwargs)
except IntegrityError as e:
click.echo(str(e))
else:
click.echo(f'User {user.name} created!!! ID: {user.id}')
@cli.command()
@click.option('--name', required=True, prompt=True)
@click.option('--email', required=False, default=None, prompt=True)
@click.option('--password', required=True, prompt=True, hide_input=True,
confirmation_prompt=True)
def adduser(name, email, password):
run_async(_adduser(name=name, password=password, email=email))
@cli.command()
def build_css():
build_map = {
'main.min.css': ['pure-min.css', 'base.css', 'iconfont.css'],
'index.min.css': ['main.min.css', 'balloon.min.css', 'lightbox.css',
'widget.css'],
'topic.min.css': ['main.min.css', 'topic.css'],
'favorites.min.css': ['font-awesome.css', 'main.min.css', 'favorites.css'],
'post.min.css': ['main.min.css', 'post.css', 'react.css',
'gitment.css', 'dracula.css', 'social-sharer.css']
}
css_map: Dict[str, str] = {}
css_dir = Path(HERE) / 'static/css/'
for css, files in build_map.items():
data = ''
for file in files:
if file in css_map:
data += css_map[file]
else:
with open(css_dir / file) as f:
data_ = f.read()
css_map[file] = data_
data += data_
with open(css_dir / css, 'w') as f:
f.write(cssmin.cssmin(data))
css_map[css] = data
@cli.command()
def clear_paginatior_cache():
run_async(_clear_paginatior_cache())
click.echo('Clear Paginatior cache Finished!')
async def _clear_paginatior_cache() -> None:
await init_db()
cls = Post
total = await cls.count()
page_count = math.ceil(total / PER_PAGE)
keys = [MC_KEY_PAGINATE % (cls.__name__, p, PER_PAGE, count)
for p in range(1, page_count + 1) for count in [True, False]]
await clear_mc(*keys)
if __name__ == '__main__':
cli()