-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtasks.py
55 lines (41 loc) · 1.29 KB
/
tasks.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
import os
from invoke import task
from app.widget.model import Widget
from app.fizz.model import Fizz
from app.db import get_db, Base, engine
from wsgi import app
@task
def init_db(ctx):
print("Creating all resources.")
Base.metadata.create_all()
engine.execute("insert into widget values (1, 'hey', 'there');")
print(engine.execute("select * from widget;"))
@task
def drop_all(ctx):
if input("Are you sure you want to drop all tables? (y/N)\n").lower() == "y":
print("Dropping tables...")
Base.metadata.drop_all()
def seed_things():
classes = [Widget, Fizz]
for klass in classes:
seed_thing(klass)
def seed_thing(cls):
session = next(get_db())
things = [
{"name": "Pizza Slicer", "purpose": "Cut delicious pizza"},
{"name": "Rolling Pin", "purpose": "Roll delicious pizza"},
{"name": "Pizza Oven", "purpose": "Bake delicious pizza"},
]
session.bulk_insert_mappings(cls, things)
session.commit()
@task
def seed_db(ctx):
if (
input("Are you sure you want to drop all tables and recreate? (y/N)\n").lower()
== "y"
):
print("Dropping tables...")
Base.metadata.drop_all()
Base.metadata.create_all()
seed_things()
print("DB successfully seeded.")