Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 1f625325a6993fe1d4d1d50b89912ff03c02b181
Author: Viktor Roytman <vr2262@columbia.edu>
Date:   Mon Dec 19 14:54:05 2016 -0500

    Update version to 0.1.2

commit 094d8c44e242a1da0c0127340bba97577243b361
Author: Viktor Roytman <vr2262@columbia.edu>
Date:   Mon Dec 19 14:53:15 2016 -0500

    Use session_scope recipe from SQLAlchemy docs
  • Loading branch information
vr2262 committed Dec 19, 2016
1 parent 696a00f commit 0d5cd2e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 23 deletions.
4 changes: 2 additions & 2 deletions dev/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def create_user(*, email):
"""Create a user with the given e-mail."""
from minigrid import models
session = createdb(ensure=False)
with session.begin_nested():
session.add(models.User(email=email))
with models.transaction(session) as tx_session:
tx_session.add(models.User(email=email))
print('Created user with e-mail ' + email)


Expand Down
8 changes: 5 additions & 3 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class BaseHandler(tornado.web.RequestHandler):

@property
def session(self):
"""The db session. Use session.begin_nested() for transactions."""
"""The database session.
Use the models.transaction(session) context manager."""
return self.application.session

def get_current_user(self):
Expand Down Expand Up @@ -89,8 +91,8 @@ def post(self):
email = self.get_argument('email')
reason = None
try:
with self.session.begin_nested():
self.session.add(models.User(email=email))
with models.transaction(self.session) as session:
session.add(models.User(email=email))
except IntegrityError as error:
if 'user_email_check' in error.orig.pgerror:
reason = '{} is not a valid e-mail address'.format(email)
Expand Down
17 changes: 17 additions & 0 deletions minigrid/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""ORM models."""
from contextlib import contextmanager

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql as pg
from sqlalchemy.ext.declarative import declarative_base
Expand Down Expand Up @@ -31,6 +33,21 @@ def create_engine():
return sa.create_engine(connection_string)


@contextmanager
def transaction(session):
"""Provide a transactional scope around a series of operations.
Taken from http://docs.sqlalchemy.org/en/latest/orm/session_basics.html
#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it
"""
try:
yield session
session.commit()
except:
session.rollback()
raise


def pk():
"""Return a primary key UUID column."""
return sa.Column(
Expand Down
4 changes: 2 additions & 2 deletions prod/create_initial_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def main():
if users:
print('At least one user already exists. Log in as that user.')
sys.exit(1)
with session.begin_nested():
session.add(models.User(email=args.email))
with models.transaction(session) as tx_session:
tx_session.add(models.User(email=args.email))
print('Created initial user with e-mail', args.email)


Expand Down
2 changes: 1 addition & 1 deletion prod/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
minigrid:
image: selcolumbia/minigrid-server:0.1.1
image: selcolumbia/minigrid-server:0.1.2
command: ./prod/run.sh --db_host=db --redis_url=redis://redis:6379/0 --minigrid-website-url=https://www.example.com
depends_on:
- redis
Expand Down
6 changes: 3 additions & 3 deletions prod/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
# Minigrid Server installer for version 0.1.1
# Minigrid Server installer for version 0.1.2
set -e

# Do you have docker installed?
Expand Down Expand Up @@ -108,8 +108,8 @@ $SUDO openssl dhparam -out /etc/letsencrypt/live/$LETSENCRYPT_DIR/dhparam.pem 20
printf "========================================\n"
printf " Generating configuration \n"
printf "========================================\n"
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.1/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.1/prod/nginx.conf > nginx.conf
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.2/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.2/prod/nginx.conf > nginx.conf

sed -i s/www.example.com/$LETSENCRYPT_DIR/g docker-compose.yml
sed -i s/www.example.com/$LETSENCRYPT_DIR/g nginx.conf
Expand Down
20 changes: 10 additions & 10 deletions tests/python/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def BeautifulSoup(page):
class TestIndex(HTTPTest):
def setUp(self):
super().setUp()
with self.session.begin_nested():
with models.transaction(self.session) as session:
self.user = models.User(email='a@a.com')
self.session.add(self.user)
session.add(self.user)
self.minigrids = (
models.Minigrid(name='a', day_tariff=1, night_tariff=2),
models.Minigrid(name='b', day_tariff=10, night_tariff=20),
)
self.session.add_all(self.minigrids)
session.add_all(self.minigrids)

def test_get_not_logged_in(self):
response = self.fetch('/')
Expand Down Expand Up @@ -56,14 +56,14 @@ def test_get_logged_in(self, get_current_user):
class TestMinigridView(HTTPTest):
def setUp(self):
super().setUp()
with self.session.begin_nested():
with models.transaction(self.session) as session:
self.user = models.User(email='a@a.com')
self.session.add(self.user)
session.add(self.user)
self.minigrids = (
models.Minigrid(name='a', day_tariff=1, night_tariff=2),
models.Minigrid(name='b', day_tariff=10, night_tariff=20),
)
self.session.add_all(self.minigrids)
session.add_all(self.minigrids)

def test_get_not_logged_in(self):
response = self.fetch(
Expand Down Expand Up @@ -99,12 +99,12 @@ def test_get_success(self, get_current_user):
class TestUsersView(HTTPTest):
def setUp(self):
super().setUp()
with self.session.begin_nested():
with models.transaction(self.session) as session:
self.users = (
models.User(email='a@a.com'),
models.User(email='b@b.com'),
)
self.session.add_all(self.users)
session.add_all(self.users)

def test_get_not_logged_in(self):
response = self.fetch('/users', follow_redirects=False)
Expand Down Expand Up @@ -179,8 +179,8 @@ def test_verify_no_xsrf(self):

class TestAuthentication(HTTPTest):
def create_user(self, email='a@a.com'):
with self.session.begin_nested():
self.session.add(models.User(email=email))
with models.transaction(self.session) as session:
session.add(models.User(email=email))

def test_login_missing_email(self):
log_1 = ExpectLog('tornado.general', '.*Missing argument email')
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestUser(Test):
def test_create(self):
with self.session.begin_nested():
self.session.add(models.User(email='a@b.com'))
with models.transaction(self.session) as session:
session.add(models.User(email='a@b.com'))
user = self.session.query(models.User).one()
self.assertEqual(user.email, 'a@b.com')
15 changes: 15 additions & 0 deletions tests/python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import fakeredis

from sqlalchemy import event
from sqlalchemy.orm import sessionmaker

from tornado.testing import AsyncHTTPTestCase
Expand Down Expand Up @@ -43,6 +44,20 @@ def setUp(self):
self.connection = engine.connect()
self.transaction = self.connection.begin()
self.session = Session(bind=self.connection)
self.session.begin_nested()

@event.listens_for(self.session, 'after_transaction_end')
def restart_savepoint(session, transaction):
"""Taken from
http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html
#joining-a-session-into-an-external-transaction
-such-as-for-test-suites
"""
if transaction.nested and not transaction._parent.nested:
session.expire_all()
session.begin_nested()

super().setUp()

def tearDown(self):
Expand Down

0 comments on commit 0d5cd2e

Please sign in to comment.