Hi there,
I'd like to suggest we create a +package+/lib/database.py for projects
that provides stubs for database setup at application time,
per-request database setup, per-request database cleanup,
per-model-test model setup, and per-model-test model reset.
This is based on the Tesla work, and with this, Tesla should only need
to implement lib/database.py for the Elixir-related database stuff to
work. (I used this for my tiny amount of testing, and it worked fine
for the standard controller case.)
Basically, the core of the change is this:
------------------------------------------------------------
"""
Database lifecycle functions
"""
def connect():
"""
connect is called once at the start of the Pylons lifecycle to
configure the database connectivity.
"""
pass
def request_setup():
"""
request_setup is called at the beginning of each request to set up
the database environment for use by the controllers.
"""
pass
def request_cleanup():
"""
request_cleanup is called at the end of each request to clear the
database environment after use by the controllers.
For example, it is intended that changed objects be flushed to the
database at this point, if they haven't already been.
"""
pass
def test_setup():
"""
test_setup is called at the start of every TestModel model unit test
to populate the test database with data.
This also usually expects an empty database, and creates all the
tables used by the models.
"""
pass
def test_reset():
"""
test_reset is called at the end of every TestModel model unit test
to restore the test database to a particular state.
The end state is usually the empty database (ie, with no tables)
"""
pass
connected = False
__all__ = [
'connect',
'connected',
'request_setup',
'request_cleanup',
'test_setup',
'test_reset',
]
------------------------------------------------------------
There are minor changes to:
websetup.py_tmpl
tests/__init__.py_tmpl
models/__init__.py_tmpl
lib/app_globals.py_tmpl
lib/base.py_tmpl
These mimic the changes Tesla makes.
The point of this is twofold - firstly to document correctly where the
code locations you need to do different types of things are, and
secondly to make providing and setting up database connectivity in
Pylons projects a matter of changing just one file (and possibly just
copying that file from someone else, or having it set up using a
project template).
The main risk, as I see it, is that the places that different database
engines (SQLAlchemy vs. SQLObject vs. ZODB vs. DBM) need to hook may
be different, and creating new functions for these hooks and calling
them would mean that older lib/database.py files would raise errors
because the functions don't exist.
(One possibly workaround is to make a class which inherits from a
Pylons class that defaults to doing nothing in those functions. I'm
not sure I like it.)
Is this change the right way to go?
Cheers,
Neil
--
Neil Blakey-Milner
http://nxsy.org/
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---
Index: pylons/templates/default_project/+package+/websetup.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/websetup.py_tmpl (revision 2042)
+++ pylons/templates/default_project/+package+/websetup.py_tmpl (working copy)
@@ -8,3 +8,6 @@
conf.update(dict(app_conf=conf.local_conf, global_conf=conf.global_conf))
paste.deploy.CONFIG.push_process_config(conf)
+ import ${package}.lib.database as database
+ database.connect()
+
Index: pylons/templates/default_project/+package+/tests/__init__.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/tests/__init__.py_tmpl (revision 2042)
+++ pylons/templates/default_project/+package+/tests/__init__.py_tmpl (working copy)
@@ -31,4 +31,14 @@
self.app = paste.fixture.TestApp(wsgiapp)
TestCase.__init__(self, *args)
-__all__ = ['url_for', 'TestController']
+wsgiapp = loadapp('config:test.ini', relative_to=conf_dir)
+import ${package}.lib.database as database
+database.connect()
+
+class TestModel(TestCase):
+ def setUp(self):
+ database.test_setup()
+ def tearDown(self):
+ database.test_reset()
+
+__all__ = ['url_for', 'TestController', 'TestModel']
Index: pylons/templates/default_project/+package+/models/__init__.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/models/__init__.py_tmpl (revision 2042)
+++ pylons/templates/default_project/+package+/models/__init__.py_tmpl (working copy)
@@ -1,3 +1,5 @@
+from ${package}.lib.database import *
+
## NOTE
## If you plan on using SQLObject, the following should be un-commented and provides
## a starting point for setting up your schema
Index: pylons/templates/default_project/+package+/lib/app_globals.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/lib/app_globals.py_tmpl (revision 2042)
+++ pylons/templates/default_project/+package+/lib/app_globals.py_tmpl (working copy)
@@ -25,7 +25,9 @@
your global variables.
"""
- pass
+
+ import ${package}.lib.database as database
+ database.connect()
def __del__(self):
"""
Index: pylons/templates/default_project/+package+/lib/database.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/lib/database.py_tmpl (revision 0)
+++ pylons/templates/default_project/+package+/lib/database.py_tmpl (revision 0)
@@ -0,0 +1,57 @@
+"""
+Database lifecycle functions
+"""
+
+def connect():
+ """
+ connect is called once at the start of the Pylons lifecycle to
+ configure the database connectivity.
+ """
+ pass
+
+def request_setup():
+ """
+ request_setup is called at the beginning of each request to set up
+ the database environment for use by the controllers.
+ """
+ pass
+
+def request_cleanup():
+ """
+ request_cleanup is called at the end of each request to clear the
+ database environment after use by the controllers.
+
+ For example, it is intended that changed objects be flushed to the
+ database at this point, if they haven't already been.
+ """
+ pass
+
+def test_setup():
+ """
+ test_setup is called at the start of every TestModel model unit test
+ to populate the test database with data.
+
+ This also usually expects an empty database, and creates all the
+ tables used by the models.
+ """
+ pass
+
+def test_reset():
+ """
+ test_reset is called at the end of every TestModel model unit test
+ to restore the test database to a particular state.
+
+ The end state is usually the empty database (ie, with no tables)
+ """
+ pass
+
+connected = False
+
+__all__ = [
+ 'connect',
+ 'connected',
+ 'request_setup',
+ 'request_cleanup',
+ 'test_setup',
+ 'test_reset',
+]
Index: pylons/templates/default_project/+package+/lib/base.py_tmpl
===================================================================
--- pylons/templates/default_project/+package+/lib/base.py_tmpl (revision 2042)
+++ pylons/templates/default_project/+package+/lib/base.py_tmpl (working copy)
@@ -6,9 +6,13 @@
from pylons.i18n import N_, _, ungettext
import ${package}.models as model
import ${package}.lib.helpers as h
+import ${package}.lib.database as database
class BaseController(WSGIController):
def __call__(self, environ, start_response):
+ # Per-request database setup
+ database.request_setup()
+
# Insert any code to be run per request here. The Routes match
# is under environ['pylons.routes_dict'] should you want to check
# the action or route vars here