|
6 | 6 | from sqlalchemy import Float
|
7 | 7 | from sqlalchemy import func
|
8 | 8 | from sqlalchemy import Identity
|
| 9 | +from sqlalchemy import Index |
9 | 10 | from sqlalchemy import inspect
|
10 | 11 | from sqlalchemy import Integer
|
11 | 12 | from sqlalchemy import MetaData
|
|
15 | 16 | from sqlalchemy import TIMESTAMP
|
16 | 17 | from sqlalchemy.dialects.mysql import VARCHAR
|
17 | 18 |
|
| 19 | +from alembic import autogenerate |
18 | 20 | from alembic import op
|
19 | 21 | from alembic import util
|
20 | 22 | from alembic.autogenerate import api
|
|
24 | 26 | from alembic.testing import assert_raises_message
|
25 | 27 | from alembic.testing import combinations
|
26 | 28 | from alembic.testing import config
|
| 29 | +from alembic.testing import eq_ignore_whitespace |
27 | 30 | from alembic.testing.env import clear_staging_env
|
28 | 31 | from alembic.testing.env import staging_env
|
29 | 32 | from alembic.testing.fixtures import AlterColRoundTripFixture
|
@@ -692,3 +695,79 @@ def test_compare_boolean_same(self):
|
692 | 695 |
|
693 | 696 | def test_compare_boolean_diff(self):
|
694 | 697 | self._compare_default_roundtrip(Boolean(), "1", "0")
|
| 698 | + |
| 699 | + |
| 700 | +class MySQLAutogenRenderTest(TestBase): |
| 701 | + def setUp(self): |
| 702 | + ctx_opts = { |
| 703 | + "sqlalchemy_module_prefix": "sa.", |
| 704 | + "alembic_module_prefix": "op.", |
| 705 | + "target_metadata": MetaData(), |
| 706 | + } |
| 707 | + context = MigrationContext.configure( |
| 708 | + dialect_name="mysql", opts=ctx_opts |
| 709 | + ) |
| 710 | + |
| 711 | + self.autogen_context = api.AutogenContext(context) |
| 712 | + |
| 713 | + def test_render_add_index_expr_binary(self): |
| 714 | + m = MetaData() |
| 715 | + t = Table( |
| 716 | + "t", |
| 717 | + m, |
| 718 | + Column("x", Integer, primary_key=True), |
| 719 | + Column("y", Integer), |
| 720 | + ) |
| 721 | + idx = Index("foo_idx", t.c.x > 5) |
| 722 | + |
| 723 | + eq_ignore_whitespace( |
| 724 | + autogenerate.render_op_text( |
| 725 | + self.autogen_context, ops.CreateIndexOp.from_index(idx) |
| 726 | + ), |
| 727 | + "op.create_index('foo_idx', 't', " |
| 728 | + "[sa.literal_column('(x > 5)')], unique=False)", |
| 729 | + ) |
| 730 | + |
| 731 | + def test_render_add_index_expr_unary(self): |
| 732 | + m = MetaData() |
| 733 | + t = Table( |
| 734 | + "t", |
| 735 | + m, |
| 736 | + Column("x", Integer, primary_key=True), |
| 737 | + Column("y", Integer), |
| 738 | + ) |
| 739 | + idx1 = Index("foo_idx", -t.c.x) |
| 740 | + idx2 = Index("foo_idx", t.c.x.desc()) |
| 741 | + |
| 742 | + eq_ignore_whitespace( |
| 743 | + autogenerate.render_op_text( |
| 744 | + self.autogen_context, ops.CreateIndexOp.from_index(idx1) |
| 745 | + ), |
| 746 | + "op.create_index('foo_idx', 't', " |
| 747 | + "[sa.literal_column('(-x)')], unique=False)", |
| 748 | + ) |
| 749 | + eq_ignore_whitespace( |
| 750 | + autogenerate.render_op_text( |
| 751 | + self.autogen_context, ops.CreateIndexOp.from_index(idx2) |
| 752 | + ), |
| 753 | + "op.create_index('foo_idx', 't', " |
| 754 | + "[sa.literal_column('x DESC')], unique=False)", |
| 755 | + ) |
| 756 | + |
| 757 | + def test_render_add_index_expr_func(self): |
| 758 | + m = MetaData() |
| 759 | + t = Table( |
| 760 | + "t", |
| 761 | + m, |
| 762 | + Column("x", Integer, primary_key=True), |
| 763 | + Column("y", Integer, nullable=True), |
| 764 | + ) |
| 765 | + idx = Index("foo_idx", t.c.x, func.coalesce(t.c.y, 0)) |
| 766 | + |
| 767 | + eq_ignore_whitespace( |
| 768 | + autogenerate.render_op_text( |
| 769 | + self.autogen_context, ops.CreateIndexOp.from_index(idx) |
| 770 | + ), |
| 771 | + "op.create_index('foo_idx', 't', " |
| 772 | + "['x', sa.literal_column('(coalesce(y, 0))')], unique=False)", |
| 773 | + ) |
0 commit comments