mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
[1.7.x] Fixed #23303 -- Added BEGIN and COMMIT statements to the output of sqlmigrate.
Backport of 5853c87a458f62ebd62d7809168355610de2570c from master. Conflicts: django/core/management/commands/sqlmigrate.py tests/user_commands/tests.py
This commit is contained in:
parent
126606c5b8
commit
3a80189479
@ -19,6 +19,14 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
|
|
||||||
help = "Prints the SQL statements for the named migration."
|
help = "Prints the SQL statements for the named migration."
|
||||||
|
output_transaction = True
|
||||||
|
|
||||||
|
def execute(self, *args, **options):
|
||||||
|
# sqlmigrate doesn't support coloring its output but we need to force
|
||||||
|
# no_color=True so that the BEGIN/COMMIT statements added by
|
||||||
|
# output_transaction don't get colored either.
|
||||||
|
options['no_color'] = True
|
||||||
|
return super(Command, self).execute(*args, **options)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
|
||||||
@ -50,5 +58,4 @@ class Command(BaseCommand):
|
|||||||
# for it
|
# for it
|
||||||
plan = [(executor.loader.graph.nodes[targets[0]], options.get("backwards", False))]
|
plan = [(executor.loader.graph.nodes[targets[0]], options.get("backwards", False))]
|
||||||
sql_statements = executor.collect_sql(plan)
|
sql_statements = executor.collect_sql(plan)
|
||||||
for statement in sql_statements:
|
return '\n'.join(sql_statements)
|
||||||
self.stdout.write(statement)
|
|
||||||
|
@ -480,6 +480,7 @@ readability):
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. code-block:: sql
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
CREATE TABLE polls_question (
|
CREATE TABLE polls_question (
|
||||||
"id" serial NOT NULL PRIMARY KEY,
|
"id" serial NOT NULL PRIMARY KEY,
|
||||||
"question_text" varchar(200) NOT NULL,
|
"question_text" varchar(200) NOT NULL,
|
||||||
@ -500,6 +501,7 @@ readability):
|
|||||||
FOREIGN KEY ("question_id")
|
FOREIGN KEY ("question_id")
|
||||||
REFERENCES "polls_question" ("id")
|
REFERENCES "polls_question" ("id")
|
||||||
DEFERRABLE INITIALLY DEFERRED;
|
DEFERRABLE INITIALLY DEFERRED;
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
|
||||||
Note the following:
|
Note the following:
|
||||||
|
@ -285,6 +285,7 @@ This command should produce the following output:
|
|||||||
|
|
||||||
.. code-block:: sql
|
.. code-block:: sql
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
CREATE TABLE "world_worldborder" (
|
CREATE TABLE "world_worldborder" (
|
||||||
"id" serial NOT NULL PRIMARY KEY,
|
"id" serial NOT NULL PRIMARY KEY,
|
||||||
"name" varchar(50) NOT NULL,
|
"name" varchar(50) NOT NULL,
|
||||||
@ -302,6 +303,7 @@ This command should produce the following output:
|
|||||||
)
|
)
|
||||||
;
|
;
|
||||||
CREATE INDEX "world_worldborder_mpoly_id" ON "world_worldborder" USING GIST ( "mpoly" );
|
CREATE INDEX "world_worldborder_mpoly_id" ON "world_worldborder" USING GIST ( "mpoly" );
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
@ -1127,6 +1127,8 @@ Prints the SQL for the named migration. This requires an active database
|
|||||||
connection, which it will use to resolve constraint names; this means you must
|
connection, which it will use to resolve constraint names; this means you must
|
||||||
generate the SQL against a copy of the database you wish to later apply it on.
|
generate the SQL against a copy of the database you wish to later apply it on.
|
||||||
|
|
||||||
|
Note that ``sqlmigrate`` doesn't colorize its output.
|
||||||
|
|
||||||
The :djadminopt:`--database` option can be used to specify the database for
|
The :djadminopt:`--database` option can be used to specify the database for
|
||||||
which to generate the SQL.
|
which to generate the SQL.
|
||||||
|
|
||||||
|
@ -93,6 +93,13 @@ class MigrateTests(MigrationTestBase):
|
|||||||
"""
|
"""
|
||||||
Makes sure that sqlmigrate does something.
|
Makes sure that sqlmigrate does something.
|
||||||
"""
|
"""
|
||||||
|
# Make sure the output is wrapped in a transaction
|
||||||
|
stdout = six.StringIO()
|
||||||
|
call_command("sqlmigrate", "migrations", "0001", stdout=stdout)
|
||||||
|
output = stdout.getvalue().lower()
|
||||||
|
self.assertIn("begin;", output)
|
||||||
|
self.assertIn("commit;", output)
|
||||||
|
|
||||||
# Test forwards. All the databases agree on CREATE TABLE, at least.
|
# Test forwards. All the databases agree on CREATE TABLE, at least.
|
||||||
stdout = six.StringIO()
|
stdout = six.StringIO()
|
||||||
call_command("sqlmigrate", "migrations", "0001", stdout=stdout)
|
call_command("sqlmigrate", "migrations", "0001", stdout=stdout)
|
||||||
|
10
tests/user_commands/management/commands/transaction.py
Normal file
10
tests/user_commands/management/commands/transaction.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Say hello."
|
||||||
|
args = ''
|
||||||
|
output_transaction = True
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
return 'Hello!'
|
@ -74,6 +74,11 @@ class CommandTests(SimpleTestCase):
|
|||||||
if current_path is not None:
|
if current_path is not None:
|
||||||
os.environ['PATH'] = current_path
|
os.environ['PATH'] = current_path
|
||||||
|
|
||||||
|
def test_output_transaction(self):
|
||||||
|
out = StringIO()
|
||||||
|
management.call_command('transaction', stdout=out, no_color=True)
|
||||||
|
self.assertEqual(out.getvalue(), 'BEGIN;\nHello!\n\nCOMMIT;\n')
|
||||||
|
|
||||||
|
|
||||||
class UtilsTests(SimpleTestCase):
|
class UtilsTests(SimpleTestCase):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user