-
Notifications
You must be signed in to change notification settings - Fork 1
/
seed_database.py
58 lines (45 loc) · 1.86 KB
/
seed_database.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
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Bundle, Links, User, Base
engine = create_engine('sqlite:///bundly.db')
# Clear database
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
# Bind the engine to the metadata of the Base class so that the
# declaratives can be accessed through a DBSession instance
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
# A DBSession() instance establishes all conversations with the database
# and represents a "staging zone" for all the objects loaded into the
# database session object. Any change made against the objects in the
# session won't be persisted into the database until you call
# session.commit(). If you're not happy about the changes, you can
# revert all of them back to the last commit by calling
# session.rollback()
session = DBSession()
# Create a user
nimit = User(name="Nimit Bhargava", email="[email protected]")
session.add(nimit)
session.commit()
bundle1 = Bundle(title="Top Movies", creator="[email protected]")
session.add(bundle1)
session.commit()
# URLs for Bundle1(Top Movies)
gwtdt = Links(url="http://www.imdb.com/title/tt0068646/", bundle_id=1)
session.add(gwtdt)
session.commit()
gwtdt = Links(url="http://www.imdb.com/title/tt0108052/", bundle_id=1)
session.add(gwtdt)
session.commit()
bundle2 = Bundle(title="Top Self Driving car tutorial", creator="[email protected]")
session.add(bundle2)
session.commit()
# URLs for Bundle2(Top Movies)
gwtdt = Links(url="https://udacity.com/course/intro-to-self-driving-cars--nd113", bundle_id=2)
session.add(gwtdt)
session.commit()
gwtdt = Links(url="https://github.com/mikesprague/udacity-nanodegrees#front-end-web-developer-nanodegree", bundle_id=2)
session.add(gwtdt)
session.commit()
print "Seeded database with categories and their items!"