diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-04-26 10:22:48 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-04-26 17:46:23 +0200 |
| commit | 8b5b199e20ad2d8d3e91873ce0cd5d3035e05ece (patch) | |
| tree | 38cd2cf71bc8bb95b681b663c81391d7476ce2a3 /tests | |
| parent | 2128b3a6887a45e99724f52ac2eaf4a5f4d97c84 (diff) | |
Fixed #3214 -- Stopped parsing SQL with regex.
Avoided introducing a new regex-based SQL splitter in the migrations
framework, before we're bound by backwards compatibility.
Adapted this change to the legacy "initial SQL data" feature, even
though it's already deprecated, in order to facilitate the transition
to migrations.
sqlparse becomes mandatory for RunSQL on some databases (all but
PostgreSQL). There's no API to provide a single statement and tell
Django not to attempt splitting. Since we have a more robust splitting
implementation, that seems like a good tradeoff. It's easier to add a
new keyword argument later if necessary than to remove one.
Many people contributed to both tickets, thank you all, and especially
Claude for the review.
Refs #22401.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/initial_sql_regress/tests.py | 1 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 15 | ||||
| -rw-r--r-- | tests/requirements/base.txt | 1 |
3 files changed, 15 insertions, 2 deletions
diff --git a/tests/initial_sql_regress/tests.py b/tests/initial_sql_regress/tests.py index 428d993667..ebbe36d35d 100644 --- a/tests/initial_sql_regress/tests.py +++ b/tests/initial_sql_regress/tests.py @@ -27,7 +27,6 @@ class InitialSQLTests(TestCase): """ connection = connections[DEFAULT_DB_ALIAS] custom_sql = custom_sql_for_model(Simple, no_style(), connection) - self.assertEqual(len(custom_sql), 9) with connection.cursor() as cursor: for sql in custom_sql: cursor.execute(sql) diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index e1b4682d58..d294bdcd26 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1,5 +1,10 @@ import unittest +try: + import sqlparse +except ImportError: + sqlparse = None + from django.db import connection, migrations, models, router from django.db.migrations.migration import Migration from django.db.migrations.state import ProjectState @@ -640,6 +645,7 @@ class OperationTests(MigrationTestBase): operation.database_backwards("test_alinto", editor, new_state, project_state) self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"]) + @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse") def test_run_sql(self): """ Tests the RunSQL operation. @@ -647,7 +653,10 @@ class OperationTests(MigrationTestBase): project_state = self.set_up_test_model("test_runsql") # Create the operation operation = migrations.RunSQL( - "CREATE TABLE i_love_ponies (id int, special_thing int)", + # Use a multi-line string with a commment to test splitting on SQLite and MySQL respectively + "CREATE TABLE i_love_ponies (id int, special_thing int);\n" + "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 42); -- this is magic!\n" + "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, 51);\n", "DROP TABLE i_love_ponies", state_operations=[migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])], ) @@ -661,6 +670,10 @@ class OperationTests(MigrationTestBase): with connection.schema_editor() as editor: operation.database_forwards("test_runsql", editor, project_state, new_state) self.assertTableExists("i_love_ponies") + # Make sure all the SQL was processed + with connection.cursor() as cursor: + cursor.execute("SELECT COUNT(*) FROM i_love_ponies") + self.assertEqual(cursor.fetchall()[0][0], 2) # And test reversal self.assertTrue(operation.reversible) with connection.schema_editor() as editor: diff --git a/tests/requirements/base.txt b/tests/requirements/base.txt index 3d982bc00a..c3f77234fd 100644 --- a/tests/requirements/base.txt +++ b/tests/requirements/base.txt @@ -5,3 +5,4 @@ Pillow PyYAML pytz > dev selenium +sqlparse |
