Commits

jason kirtland committed ca771ef

- fixed specification of YEAR columns when generating schema

Comments (0)

Files changed (3)

 - engine
     - fixed another occasional race condition which could occur
       when using pool with threadlocal setting
+- mysql
+    - fixed specification of YEAR columns when generating schema
 - mssql
     - added support for TIME columns (simulated using DATETIME) [ticket:679]
     - index names are now quoted when dropping from reflected tables [ticket:684]

lib/sqlalchemy/databases/mysql.py

     def get_col_spec(self):
         return "TIMESTAMP"
 
-class MSYear(sqltypes.String):
+class MSYear(sqltypes.TypeEngine):
     """MySQL YEAR type, for single byte storage of years 1901-2155"""
 
+    def __init__(self, length=None):
+        self.length = length
+
     def get_col_spec(self):
         if self.length is None:
             return "YEAR"
         else:
-            return "YEAR(%d)" % self.length
+            return "YEAR(%s)" % self.length
 
 class MSText(_StringType, sqltypes.TEXT):
     """MySQL TEXT type, for text up to 2^16 characters""" 
     'tinytext' : MSTinyText,
     'varbinary' : MSVarBinary,
     'varchar' : MSString,
+    'year' : MSYear,
 }
 
 def descriptor():

test/dialect/mysql.py

         charset_table.drop()
 
     @testbase.supported('mysql')
+    def test_year(self):
+        """Exercise YEAR."""
+
+        meta = MetaData(testbase.db)
+        year_table = Table('mysql_year', meta,
+                           Column('y1', mysql.MSYear),
+                           Column('y2', mysql.MSYear),
+                           Column('y3', mysql.MSYear),
+                           Column('y4', mysql.MSYear(2)),
+                           Column('y5', mysql.MSYear(4)))
+
+        try:
+            year_table.create()
+            reflected = Table('mysql_year', MetaData(testbase.db),
+                              autoload=True)
+
+            for table in year_table, reflected:
+                table.insert(['1950', '50', None, 50, 1950]).execute()
+                row = list(table.select().execute())[0]
+                self.assert_(list(row) == [1950, 2050, None, 50, 1950])
+                table.delete().execute()
+                self.assert_(colspec(table.c.y1).startswith('y1 YEAR'))
+                self.assert_(colspec(table.c.y4) == 'y4 YEAR(2)')
+                self.assert_(colspec(table.c.y5) == 'y5 YEAR(4)')
+        finally:
+            meta.drop_all()
+
+    @testbase.supported('mysql')
     def test_enum(self):
         "Exercise the ENUM type"
 
 
         m.drop_all()
 
+
+def colspec(c):
+    return testbase.db.dialect.schemagenerator(
+        testbase.db, None, None).get_column_specification(c)
+
 if __name__ == "__main__":
     testbase.main()