diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-02-16 17:20:18 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-02-16 17:20:18 -0500 |
commit | 5e100a70c51362b522c8d4a05c0149c7d77672fb (patch) | |
tree | ef92cedadf4a0277c247e39497f28ef7efa08cff | |
parent | 79f801eae889ee6ee098ce3cd3b0ca98b852d2f5 (diff) | |
download | sqlalchemy-5e100a70c51362b522c8d4a05c0149c7d77672fb.tar.gz |
- The SQLite dialect will now skip unsupported arguments when reflecting
types; such as if it encounters a string like ``INTEGER(5)``, the
:class:`.INTEGER` type will be instantiated without the "5" being included,
based on detecting a ``TypeError`` on the first attempt.
-rw-r--r-- | doc/build/changelog/changelog_09.rst | 10 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/sqlite/base.py | 9 | ||||
-rw-r--r-- | test/dialect/test_sqlite.py | 20 |
3 files changed, 33 insertions, 6 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index 8e113f217..3b2a65f7a 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -15,7 +15,15 @@ :version: 0.9.3 .. change:: - :tags: sqlite + :tags: sqlite, bug + + The SQLite dialect will now skip unsupported arguments when reflecting + types; such as if it encounters a string like ``INTEGER(5)``, the + :class:`.INTEGER` type will be instantiated without the "5" being included, + based on detecting a ``TypeError`` on the first attempt. + + .. change:: + :tags: sqlite, bug :pullreq: github:65 Support has been added to SQLite type reflection to fully support diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index b285bc8ca..a1bd05d38 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -867,7 +867,14 @@ class SQLiteDialect(default.DefaultDialect): coltype = self._resolve_type_affinity(coltype) if args is not None: args = re.findall(r'(\d+)', args) - coltype = coltype(*[int(a) for a in args]) + try: + coltype = coltype(*[int(a) for a in args]) + except TypeError: + util.warn( + "Could not instantiate type %s with " + "reflected arguments %s; using no arguments." % + (coltype, args)) + coltype = coltype() if default is not None: default = util.text_type(default) diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 319b708e7..38bb78304 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -11,7 +11,9 @@ from sqlalchemy import Table, String, select, Text, CHAR, bindparam, Column,\ Unicode, Date, MetaData, UnicodeText, Time, Integer, TIMESTAMP, \ Boolean, func, NUMERIC, DateTime, extract, ForeignKey, text, Numeric,\ DefaultClause, and_, DECIMAL, TypeDecorator, create_engine, Float, \ - INTEGER, UniqueConstraint, DATETIME, DATE, TIME, BOOLEAN, BIGINT + INTEGER, UniqueConstraint, DATETIME, DATE, TIME, BOOLEAN, BIGINT, \ + VARCHAR +from sqlalchemy.types import UserDefinedType from sqlalchemy.util import u, ue from sqlalchemy import exc, sql, schema, pool, types as sqltypes, util from sqlalchemy.dialects.sqlite import base as sqlite, \ @@ -30,7 +32,7 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults): """Test that the boolean only treats 1 as True """ - + meta = MetaData(testing.db) t = Table('bool_table', meta, Column('id', Integer, primary_key=True), Column('boo', @@ -161,12 +163,20 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults): assert not bindproc or \ isinstance(bindproc(util.u('some string')), util.text_type) + @testing.emits_warning("Could not instantiate") @testing.provide_metadata def test_type_reflection(self): metadata = self.metadata # (ask_for, roundtripped_as_if_different) + class AnyType(UserDefinedType): + def __init__(self, spec): + self.spec = spec + + def get_col_spec(self): + return self.spec + specs = [ (String(), String()), (String(1), String(1)), @@ -198,11 +208,14 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults): (Time, Time()), (BOOLEAN, BOOLEAN()), (Boolean, Boolean()), + # types with unsupported arguments + (AnyType("INTEGER(5)"), INTEGER()), + (AnyType("DATETIME(6, 12)"), DATETIME()), ] columns = [Column('c%i' % (i + 1), t[0]) for (i, t) in enumerate(specs)] db = testing.db - t_table = Table('types', metadata, *columns) + Table('types', metadata, *columns) metadata.create_all() m2 = MetaData(db) rt = Table('types', m2, autoload=True) @@ -218,7 +231,6 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults): finally: db.execute('DROP VIEW types_v') - @testing.emits_warning('Did not recognize') @testing.provide_metadata def test_unknown_reflection(self): metadata = self.metadata |