Skip to content

Instantly share code, notes, and snippets.

@jean
Created May 5, 2015 03:44
Show Gist options
  • Save jean/2ca9645aeb8c956306b2 to your computer and use it in GitHub Desktop.
Save jean/2ca9645aeb8c956306b2 to your computer and use it in GitHub Desktop.
# setup the db
from master import app
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
# XXX HACK
#
# Sqlite's DATETIME type doesn't support timezones, but in production
# we use postgres which does. So when we're using sqlite and pulling
# a datetime from the database, we force the value to take on the
# local timezone.
from sqlalchemy.dialects.sqlite.base import DATETIME
from dateutil.tz import tzlocal
old_processor = DATETIME.result_processor
def new_processor(self, *args, **kwargs):
def processor(value):
dt = old_processor(self, *args, **kwargs)(value)
if dt and dt.tzinfo is None:
dt = dt.replace(tzinfo=tzlocal())
return dt
return processor
DATETIME.result_processor = new_processor
@jean
Copy link
Author

jean commented May 5, 2015

$ python
>>> from common.support import db
>>> db.create_all()
2015-05-05 03:47:27,826 1801   userid:- sqlalchemy.engine.base.Engine INFO     select version()
2015-05-05 03:47:27,827 1801   userid:- sqlalchemy.engine.base.Engine INFO     {}
2015-05-05 03:47:27,829 1801   userid:- sqlalchemy.engine.base.Engine INFO     select current_schema()
2015-05-05 03:47:27,830 1801   userid:- sqlalchemy.engine.base.Engine INFO     {}
2015-05-05 03:47:27,833 1801   userid:- sqlalchemy.engine.base.Engine INFO     show standard_conforming_strings
2015-05-05 03:47:27,834 1801   userid:- sqlalchemy.engine.base.Engine INFO     {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment