Commits

Tarek Ziadé committed 60e605f

split the mappers in their own module

  • Participants
  • Parent commits 451aca2

Comments (0)

Files changed (3)

weave/server/storage/sql.py

 username and (collection)name are the ids/
 
 """
-from weave.server.storage import register
-from sqlalchemy import MetaData, create_engine, Integer, String, DateTime, Text
-from sqlalchemy.ext.declarative import declarative_base, Column
+from sqlalchemy import create_engine
 from sqlalchemy.sql import text
 
+from weave.server.storage import register
+from weave.server.storage.sqlmappers import tables
+
 _SQLURI = 'mysql://sync:sync@localhost/sync'
-metadata = MetaData()
 engine = create_engine(_SQLURI)
-_Base = declarative_base(bind=engine)
-tables = []
 
-
-class Collections(_Base):
-    __tablename__ = 'collections'
-
-    # XXX add indexes
-    userid = Column(Integer, primary_key=True, nullable=False)
-    collectionid = Column(Integer, primary_key=True, nullable=False)
-    name = Column(String, nullable=False)
-
-collections = Collections.__table__
-tables.append(collections)
-
-
-class Users(_Base):
-    __tablename__ = 'users'
-
-    id = Column(Integer, primary_key=True, nullable=False)
-    username = Column(String(32))
-    password_hash = Column(String(128))
-    email = Column(String(64))
-    status = Column(Integer)
-    alert = Column(Text)
-
-users = Users.__table__
-tables.append(users)
-
-
-class ResetCodes(_Base):
-    __tablename__ = 'reset_codes'
-
-    username = Column(String, primary_key=True, nullable=False)
-    reset = Column(String)
-    expiration = Column(DateTime)
-
-reset_code = ResetCodes.__table__
-tables.append(reset_code)
-
-
-class WBO(_Base):
-    __tablename__ = 'wbo'
-
-    id = Column(Integer, primary_key=True)
-    # XXX that's user id in fact
-    username = Column(Integer, primary_key=True)
-    collection = Column(Integer, primary_key=True)
-
-    parentid = Column(String)
-    predecessorid = Column(String)
-    sortindex = Column(Integer)
-    modified = Column(Integer)
-    payload = Column(Text)
-    payload_size = Column(Integer)
-
-wbo = WBO.__table__
-tables.append(wbo)
+for table in tables:
+    table.metadata.bind = engine
 
 
 class WeaveSQLStorage(object):
 
         XXX We need to cache this, or to alter the DB
         """
-        data = self.get_collection(user_name, collection_name, ['collectionid'])
+        data = self.get_collection(user_name, collection_name,
+                                   ['collectionid'])
         if data is None:
             return None
         return data[0]
         query = text('select id from wbo where '
                      'username = :user_id and collection = :collection_id')
         res = self._conn.execute(query, user_id=user_id,
-                                 collection_id=collection_name)
+                                 collection_id=collection_id)
         res = res.fetchone()
         return res is not None
 
         return self._conn.execute(query, user_id=user_id, item_id=item_id,
                                   collection_id=collection_id).first()
 
-
     def set_item(self, user_name, collection_name, item_id, **values):
         """Adds or update an item"""
         values['username'] = self._get_user_id(user_name)

weave/server/storage/sqlmappers.py

+""" SQL Mappers
+"""
+from sqlalchemy.ext.declarative import declarative_base, Column
+from sqlalchemy import Integer, String, DateTime, Text
+
+_Base = declarative_base()
+tables = []
+
+
+class Collections(_Base):
+    __tablename__ = 'collections'
+
+    # XXX add indexes
+    userid = Column(Integer, primary_key=True, nullable=False)
+    collectionid = Column(Integer, primary_key=True, nullable=False)
+    name = Column(String, nullable=False)
+
+collections = Collections.__table__
+tables.append(collections)
+
+
+class Users(_Base):
+    __tablename__ = 'users'
+
+    id = Column(Integer, primary_key=True, nullable=False)
+    username = Column(String(32))
+    password_hash = Column(String(128))
+    email = Column(String(64))
+    status = Column(Integer)
+    alert = Column(Text)
+
+users = Users.__table__
+tables.append(users)
+
+
+class ResetCodes(_Base):
+    __tablename__ = 'reset_codes'
+
+    username = Column(String, primary_key=True, nullable=False)
+    reset = Column(String)
+    expiration = Column(DateTime)
+
+reset_code = ResetCodes.__table__
+tables.append(reset_code)
+
+
+class WBO(_Base):
+    __tablename__ = 'wbo'
+
+    id = Column(Integer, primary_key=True)
+    # XXX that's user id in fact
+    username = Column(Integer, primary_key=True)
+    collection = Column(Integer, primary_key=True)
+
+    parentid = Column(String)
+    predecessorid = Column(String)
+    sortindex = Column(Integer)
+    modified = Column(Integer)
+    payload = Column(Text)
+    payload_size = Column(Integer)
+
+wbo = WBO.__table__
+tables.append(wbo)

weave/server/tests/test_sql.py

         sql._SQLURI = 'sqlite:///:memory:'
         self.old_engine = sql.engine
         sql.engine = create_engine(sql._SQLURI)
-        sql.metadata.bind = sql.engine
         for table in sql.tables:
-            table.metadata = sql.metadata
+            table.metadata.bind = sql.engine
             table.create()
 
         _BACKENDS['sql'] = sql.WeaveSQLStorage()
     def tearDown(self):
         sql._SQLURI = self.old_SQLURI
         sql.engine = self.old_engine
-        sql.metadata.bind = sql.engine
         for table in sql.tables:
-            table.metadata = sql.metadata
+            table.metadata.bind = sql.engine
 
     def test_user_exists(self):
         storage = get_storage('sql')