[Checkins] SVN: z3c.zalchemy/sandbox/src/z3c/zalchemy/ tried to seperate table creation TODO: do a commit on table creation

Bernd Dorn bernd.dorn at fhv.at
Sun Apr 23 12:11:29 EDT 2006


Log message for revision 67554:
  tried to seperate table creation TODO: do a commit on table creation

Changed:
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/README.txt
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/datamanager.py
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/TRANSACTION.txt
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_threads.py
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_zalchemy.py

-=-
Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/README.txt
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/README.txt	2006-04-23 16:06:09 UTC (rev 67553)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/README.txt	2006-04-23 16:11:28 UTC (rev 67554)
@@ -26,7 +26,7 @@
   >>> from z3c.zalchemy.datamanager import AlchemyEngineUtility
   >>> engineUtil = AlchemyEngineUtility(
   ...     'sqlite',
-  ...     dns='sqlite://',
+  ...     dns='sqlite://filename=%s.1' % dbFile,
   ...     )
 
 We create our tables as usual sqlalchemy table : 
@@ -34,14 +34,14 @@
   >>> import sqlalchemy
 
   >>> aTable = sqlalchemy.Table(
-  ...     'aTable',
+  ...     'aTable',sqlalchemy.ext.proxy.ProxyEngine(),
   ...     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
   ...     sqlalchemy.Column('value', sqlalchemy.Integer),
   ...     )
 
-Note that by not specifying an engine we use a ProxyEngine which is
-important here.  The real connection to database engine will be done
-later in our utility.
+We create our tables as usual sqlalchemy table : Note that we use a
+ProxyEngine which is important here.  The real connection to database
+engine will be done later in our utility.
 
   >>> aTable.engine
   <sqlalchemy.ext.proxy.ProxyEngine object at ...>
@@ -85,9 +85,12 @@
 
   >>> a = A()
   >>> a.value = 123
+  >>> aTable.engine.engine.filename
+  '....1'
 
   >>> transaction.get().commit()
 
+
 Now let's try to get the object back in a new transaction :
 
   >>> txn = transaction.begin()
@@ -107,14 +110,14 @@
 
   >>> engine2Util = AlchemyEngineUtility(
   ...     'sqlite2',
-  ...     dns='sqlite://',
+  ...     dns='sqlite://filename=%s.2' % dbFile,
   ...     )
 
   >>> engine = sqlalchemy.ext.proxy.ProxyEngine()
   >>> provideUtility(engine2Util, name='sqlite2')
 
   >>> bTable = sqlalchemy.Table(
-  ...     'bTable',
+  ...     'bTable',sqlalchemy.ext.proxy.ProxyEngine(),
   ...     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
   ...     sqlalchemy.Column('value', sqlalchemy.String),
   ...     )

Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/datamanager.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/datamanager.py	2006-04-23 16:06:09 UTC (rev 67553)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/datamanager.py	2006-04-23 16:11:28 UTC (rev 67554)
@@ -56,9 +56,13 @@
     def createTable(self,table):
 
         """tries to create the given table if not there"""
-        self.initEngine()
-        table.engine.engine = self.storage.engine
+        self.connectTablesForThread()
+        #table.engine.connect(self.dns,self.kw,echo=self.echo)
+        #table.engine.engine = self.storage.engine
         table.create()
+        # seems that this does not get commited, why?
+        objectstore.commit()
+        #self.dataManagerFinished()
 
         
     def initEngine(self):
@@ -76,12 +80,16 @@
 
     def connectTablesForThread(self):
         # create a thread local engine
-        if not self.initEngine():
+        #import pdb;pdb.set_trace()
+
+        self.initEngine()
+        if getattr(self.storage,'_connected',False):
             return
+
         engine = self.storage.engine
-        # create a data manager
         if self.echo:
             engine.log('adding data manager for %s'%self.name)
+        # create a data manager
         self.storage.dataManager = AlchemyDataManager(self)
         txn = manager.get()
         txn.join(self.storage.dataManager)
@@ -89,8 +97,11 @@
         for table in self.tables:
             table.engine.engine = engine
 
+        self.storage._connected=True
+
     def dataManagerFinished(self):
         self.storage.engine=None
+        self.storage._connected=False
         # disconnect the tables from the engine
         for table in self.tables:
             table.engine.engine = None

Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/TRANSACTION.txt
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/TRANSACTION.txt	2006-04-23 16:06:09 UTC (rev 67553)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/TRANSACTION.txt	2006-04-23 16:11:28 UTC (rev 67554)
@@ -15,17 +15,14 @@
   >>> from zope.component import provideUtility
   >>> from z3c.zalchemy.datamanager import AlchemyEngineUtility
   >>> engineUtility = AlchemyEngineUtility('database',
-  ...                                      'sqlite://filename=/tmp/abc.db',
-  ...                                      echo=False)
+  ...     dns='sqlite://filename=%s.1' % dbFile,echo=False)
   >>> provideUtility(engineUtility, name="test")
 
 Setup a sqlalchemy table and class :
 
   >>> import sqlalchemy
-  >>> proxy = sqlalchemy.ext.proxy.ProxyEngine()
   >>> aTable = sqlalchemy.Table(
-  ...     'aTable',
-  ...     proxy,
+  ...     'aTable',sqlalchemy.ext.proxy.ProxyEngine(),
   ...     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key = True),
   ...     sqlalchemy.Column('x', sqlalchemy.Integer),
   ...     )
@@ -132,8 +129,7 @@
 Use of a second table with the same engine :
 
   >>> bTable = sqlalchemy.Table(
-  ...     'bTable',
-  ...     proxy,
+  ...     'bTable',sqlalchemy.ext.proxy.ProxyEngine(),
   ...     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key = True),
   ...     sqlalchemy.Column('x', sqlalchemy.Integer),
   ...     )

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_threads.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_threads.py	2006-04-23 16:06:09 UTC (rev 67553)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_threads.py	2006-04-23 16:11:28 UTC (rev 67554)
@@ -0,0 +1,68 @@
+import unittest
+import doctest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing import setup
+from zope.app.testing.placelesssetup import PlacelessSetup
+import os, tempfile
+import shutil
+import sqlalchemy
+from z3c.zalchemy.datamanager import AlchemyEngineUtility
+from zope.component import provideUtility
+import transaction
+from z3c.zalchemy.datamanager import beforeTraversal
+class A(object):
+    pass
+
+
+class TestThreads(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(TestThreads,self).setUp()
+        self.tmpDir = tempfile.mkdtemp()
+        self.dbFile = os.path.join(self.tmpDir,'z3c.alchemy.test.db')
+
+    def tearDown(self):
+        shutil.rmtree(self.tmpDir)
+        super(TestThreads,self).tearDown()
+
+    def testReconnect(self):
+
+        """tests if we can get data back from a table"""
+        
+        engineUtil = AlchemyEngineUtility(
+            'sqlite',
+            dns='sqlite://filename=%s.1' % self.dbFile,
+            )
+        provideUtility(engineUtil, name='1')
+        
+        aTable = sqlalchemy.Table(
+            'aTable',sqlalchemy.ext.proxy.ProxyEngine(),
+            sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
+            sqlalchemy.Column('value', sqlalchemy.Integer),
+            )
+        sqlalchemy.assign_mapper(A, aTable)
+        engineUtil.addTable(aTable,create=True)
+
+        txn = transaction.begin()
+        beforeTraversal(None)
+
+        a = A()
+        a.value = 123
+        transaction.get().commit()
+
+        txn = transaction.begin()
+        beforeTraversal(None)
+
+        a = A.get(1)
+        a.value
+
+
+
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(TestThreads),
+        ))
+    
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_threads.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_zalchemy.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_zalchemy.py	2006-04-23 16:06:09 UTC (rev 67553)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/tests/test_zalchemy.py	2006-04-23 16:11:28 UTC (rev 67554)
@@ -2,12 +2,17 @@
 import doctest
 from zope.testing.doctestunit import DocFileSuite
 from zope.app.testing import setup
+import os, tempfile
+import shutil
 
+
 def setUp(test):
     setup.placefulSetUp()
+    test.tmpDir = tempfile.mkdtemp()
+    test.globs['dbFile'] = os.path.join(test.tmpDir,'z3c.alchemy.test.db')
 
-
 def tearDown(test):
+    shutil.rmtree(test.tmpDir)
     setup.placefulTearDown()
 
 
@@ -24,6 +29,8 @@
                      ),
         ))
 
+
+
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')
 



More information about the Checkins mailing list