Created
October 14, 2011 13:37
-
-
Save joshmarshall/1287127 to your computer and use it in GitHub Desktop.
Multiple Databases / Connections using Mogo With Statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mogo | |
DBNAME1 = "__testing__" | |
DBNAME2 = "__testing2__" | |
# Basic model | |
class Person(mogo.Model): | |
name = mogo.Field(unicode) | |
# Automatically opens and closes DB connection (or should) | |
with mogo.session(DBNAME1) as session1: | |
person1 = Person.use(session1)(name=u"Josh") | |
person1.save(safe=True) | |
# Using another db connection to another database | |
with mogo.session(DBNAME2) as session2: | |
person2 = Person.use(session2)(name=u"Troy") | |
person2.save(safe=True) | |
# checking that we only have one person in this db connection | |
assert(Person.use(session2).find().count() == 1) | |
assert(Person.use(session2).find().first() == person2) | |
# checking that we only have one person in the outer db connection | |
assert(Person.use(session1).find().count() == 1) | |
assert(Person.use(session1).find().first() == person1) | |
# Just clearing the database | |
connection = mogo.connect(DBNAME1) | |
connection.drop_database(DBNAME1) | |
connection.drop_database(DBNAME2) | |
connection.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment