From: Mike Bayer Date: Thu, 28 Jul 2011 15:53:18 +0000 (-0400) Subject: - SQLite dialect no longer strips quotes X-Git-Tag: rel_0_7_2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=086ae95614b4848c486eeb7acf2bc1b03b2a6a37;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - SQLite dialect no longer strips quotes off of reflected default value, allowing a round trip CREATE TABLE to work. This is consistent with other dialects that also maintain the exact form of the default. [ticket:2189] --- diff --git a/CHANGES b/CHANGES index 7fa9ffd779..2c123a9d23 100644 --- a/CHANGES +++ b/CHANGES @@ -168,6 +168,14 @@ CHANGES "retryable" condition. Only Oracle ORA-01033 implemented for now. [ticket:2201] +- sqlite + - SQLite dialect no longer strips quotes + off of reflected default value, allowing + a round trip CREATE TABLE to work. + This is consistent with other dialects + that also maintain the exact form of + the default. [ticket:2189] + - postgresql - Added new "postgresql_ops" argument to Index, allows specification of PostgreSQL diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 331ab92d0a..577b9a3a02 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -630,17 +630,19 @@ class SQLiteDialect(default.DefaultDialect): else: pragma = "PRAGMA " qtable = quote(table_name) - c = _pragma_cursor(connection.execute("%stable_info(%s)" % (pragma, qtable))) + c = _pragma_cursor( + connection.execute("%stable_info(%s)" % + (pragma, qtable))) found_table = False columns = [] while True: row = c.fetchone() if row is None: break - (name, type_, nullable, default, has_default, primary_key) = (row[1], row[2].upper(), not row[3], row[4], row[4] is not None, row[5]) + (name, type_, nullable, default, has_default, primary_key) = \ + (row[1], row[2].upper(), not row[3], + row[4], row[4] is not None, row[5]) name = re.sub(r'^\"|\"$', '', name) - if default: - default = re.sub(r"^\'|\'$", '', default) match = re.match(r'(\w+)(\(.*?\))?', type_) if match: coltype = match.group(1) diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 400edc2cee..f3422cf0e1 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -10,6 +10,7 @@ from sqlalchemy.dialects.sqlite import base as sqlite, \ from sqlalchemy.engine.url import make_url from test.lib import * import os +from sqlalchemy.schema import CreateTable class TestTypes(fixtures.TestBase, AssertsExecutionResults): @@ -168,7 +169,7 @@ class TestTypes(fixtures.TestBase, AssertsExecutionResults): assert isinstance(t2.c.y.type, sqltypes.NullType) -class TestDefaults(fixtures.TestBase, AssertsExecutionResults): +class DefaultsTest(fixtures.TestBase, AssertsCompiledSQL): __only_on__ = 'sqlite' @@ -206,7 +207,7 @@ class TestDefaults(fixtures.TestBase, AssertsExecutionResults): db = testing.db m = MetaData(db) - expected = ['my_default', '0'] + expected = ["'my_default'", '0'] table = \ """CREATE TABLE r_defaults ( data VARCHAR(40) DEFAULT 'my_default', @@ -220,6 +221,30 @@ class TestDefaults(fixtures.TestBase, AssertsExecutionResults): finally: db.execute('DROP TABLE r_defaults') + def test_default_reflection_3(self): + db = testing.db + table = \ + """CREATE TABLE r_defaults ( + data VARCHAR(40) DEFAULT 'my_default', + val INTEGER NOT NULL DEFAULT 0 + )""" + try: + db.execute(table) + m1 = MetaData(db) + t1 = Table('r_defaults', m1, autoload=True) + db.execute("DROP TABLE r_defaults") + t1.create() + m2 = MetaData(db) + t2 = Table('r_defaults', m2, autoload=True) + self.assert_compile( + CreateTable(t2), + "CREATE TABLE r_defaults (data VARCHAR(40) " + "DEFAULT 'my_default', val INTEGER DEFAULT 0 " + "NOT NULL)" + ) + finally: + db.execute("DROP TABLE r_defaults") + class DialectTest(fixtures.TestBase, AssertsExecutionResults):