From 74fc2a2d85cdf1b061605e517c150ca0dfe5e071 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sat, 17 Nov 2012 21:38:28 -0600 Subject: [PATCH] added heroku support --- Makefile | 5 +++-- VERSION | 2 +- gluon/contrib/heroku.py | 29 +++++++++++++++++++++++++++++ gluon/dal.py | 10 +++++++--- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 gluon/contrib/heroku.py diff --git a/Makefile b/Makefile index f8208a90..c19cba83 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,9 @@ src: rm -f applications/examples/databases/* rm -f applications/admin/uploads/* rm -f applications/welcome/uploads/* - rm -f applications/examples/uploads/* + rm -f applications/examples/uploads/* + ### make epydoc + make epydoc ### make welcome layout and appadmin the default cp applications/welcome/views/appadmin.html applications/admin/views cp applications/welcome/views/appadmin.html applications/examples/views @@ -54,7 +56,6 @@ src: cd ..; zip -r web2py/web2py_src.zip web2py/gluon/*.py web2py/gluon/contrib/* web2py/splashlogo.gif web2py/*.py web2py/README.markdown web2py/LICENSE web2py/CHANGELOG web2py/NEWINSTALL web2py/VERSION web2py/Makefile web2py/epydoc.css web2py/epydoc.conf web2py/app.example.yaml web2py/logging.example.conf web2py_exe.conf web2py/queue.example.yaml MANIFEST.in w2p_apps w2p_clone w2p_run startweb2py web2py/scripts/*.sh web2py/scripts/*.py web2py/applications/admin web2py/applications/examples/ web2py/applications/welcome web2py/applications/__init__.py web2py/site-packages/__init__.py web2py/gluon/tests/*.sh web2py/gluon/tests/*.py mdp: - make epydoc make src make app make win diff --git a/VERSION b/VERSION index 1d592369..1a8eb1d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.2.1 (2012-11-17 13:28:42) stable +Version 2.2.1 (2012-11-17 21:37:46) stable diff --git a/gluon/contrib/heroku.py b/gluon/contrib/heroku.py new file mode 100644 index 00000000..704b6c45 --- /dev/null +++ b/gluon/contrib/heroku.py @@ -0,0 +1,29 @@ +""" +Usage: in web2py models/db.py + +from gluon.contrib.heroku import get_db +db = get_db() + +""" +import os +from gluon import * +from gluon.dal import ADAPTERS, UseDatabaseStoredFile,PostgreSQLAdapter + +class HerokuPostgresAdapter(UseDatabaseStoredFile,PostgreSQLAdapter): + drivers = ('psycopg2',) + uploads_in_blob = True + +ADAPTERS['postgres'] = HerokuPostgresAdapter + +def get_db(name = None, pool_size=10): + if not name: + names = [n for n in os.environ.keys() + if n[:18]+n[-4:]=='HEROKU_POSTGRESQL__URL'] + if names: + name = names[0] + if name: + db = DAL(os.environ[name], pool_size=pool_size) + current.session.connect(current.request, current.response, db=db) + else: + db = DAL('sqlite://heroku.test.sqlite') + return db diff --git a/gluon/dal.py b/gluon/dal.py index 1714403e..d41a8116 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -3964,13 +3964,17 @@ class DatabaseStoredFile: return self.db._adapter.escape(obj) def __init__(self,db,filename,mode): - if db._adapter.dbengine != 'mysql': - raise RuntimeError("only MySQL can store metadata .table files in database for now") + if not db._adapter.dbengine in ('mysql', 'postgres'): + raise RuntimeError("only MySQL/Postgres can store metadata .table files in database for now") self.db = db self.filename = filename self.mode = mode if not self.web2py_filesystem: - self.db.executesql("CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content LONGTEXT, PRIMARY KEY(path) ) ENGINE=InnoDB;") + if db._adapter.dbengine == 'mysql': + sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content LONGTEXT, PRIMARY KEY(path) ) ENGINE=InnoDB;" + elif db._adapter.dbengine == 'postgres': + sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content TEXT, PRIMARY KEY(path));" + self.db.executesql(sql) DatabaseStoredFile.web2py_filesystem = True self.p=0 self.data = ''