Skip to content

Commit

Permalink
Merge branch 'sqlalchemy:main' into implement-executesqlop-to_diff_tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
BayerSe authored Oct 24, 2023
2 parents f71df1e + 28bad0b commit 4ec6638
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
7 changes: 5 additions & 2 deletions alembic/autogenerate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import contextlib
from typing import Any
from typing import Callable
from typing import Dict
from typing import Iterator
from typing import List
Expand Down Expand Up @@ -35,6 +34,7 @@
from ..operations.ops import UpgradeOps
from ..runtime.environment import NameFilterParentNames
from ..runtime.environment import NameFilterType
from ..runtime.environment import ProcessRevisionDirectiveFn
from ..runtime.environment import RenderItemFn
from ..runtime.migration import MigrationContext
from ..script.base import Script
Expand Down Expand Up @@ -510,13 +510,16 @@ class RevisionContext:
file generation operation."""

generated_revisions: List[MigrationScript]
process_revision_directives: Optional[ProcessRevisionDirectiveFn]

def __init__(
self,
config: Config,
script_directory: ScriptDirectory,
command_args: Dict[str, Any],
process_revision_directives: Optional[Callable] = None,
process_revision_directives: Optional[
ProcessRevisionDirectiveFn
] = None,
) -> None:
self.config = config
self.script_directory = script_directory
Expand Down
5 changes: 5 additions & 0 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ def _add_table(autogen_context: AutogenContext, op: ops.CreateTableOp) -> str:
comment = table.comment
if comment:
text += ",\ncomment=%r" % _ident(comment)

info = table.info
if info:
text += f",\ninfo={info!r}"

for k in sorted(op.kw):
text += ",\n%s=%r" % (k.replace(" ", "_"), op.kw[k])

Expand Down
38 changes: 19 additions & 19 deletions alembic/autogenerate/rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
from typing import TYPE_CHECKING
from typing import Union

from alembic import util
from alembic.operations import ops
from .. import util
from ..operations import ops

if TYPE_CHECKING:
from alembic.operations.ops import AddColumnOp
from alembic.operations.ops import AlterColumnOp
from alembic.operations.ops import CreateTableOp
from alembic.operations.ops import MigrateOperation
from alembic.operations.ops import MigrationScript
from alembic.operations.ops import ModifyTableOps
from alembic.operations.ops import OpContainer
from alembic.runtime.migration import MigrationContext
from alembic.script.revision import Revision
from ..operations.ops import AddColumnOp
from ..operations.ops import AlterColumnOp
from ..operations.ops import CreateTableOp
from ..operations.ops import MigrateOperation
from ..operations.ops import MigrationScript
from ..operations.ops import ModifyTableOps
from ..operations.ops import OpContainer
from ..runtime.environment import _GetRevArg
from ..runtime.migration import MigrationContext


class Rewriter:
Expand Down Expand Up @@ -119,7 +119,7 @@ def add_column_nullable(context, revision, op):
def _rewrite(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directive: MigrateOperation,
) -> Iterator[MigrateOperation]:
try:
Expand All @@ -142,7 +142,7 @@ def _rewrite(
def __call__(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directives: List[MigrationScript],
) -> None:
self.process_revision_directives(context, revision, directives)
Expand All @@ -153,7 +153,7 @@ def __call__(
def _traverse_script(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directive: MigrationScript,
) -> None:
upgrade_ops_list = []
Expand All @@ -180,7 +180,7 @@ def _traverse_script(
def _traverse_op_container(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directive: OpContainer,
) -> None:
self._traverse_list(context, revision, directive.ops)
Expand All @@ -189,15 +189,15 @@ def _traverse_op_container(
def _traverse_any_directive(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directive: MigrateOperation,
) -> None:
pass

def _traverse_for(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directive: MigrateOperation,
) -> Any:
directives = list(self._rewrite(context, revision, directive))
Expand All @@ -209,7 +209,7 @@ def _traverse_for(
def _traverse_list(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directives: Any,
) -> None:
dest = []
Expand All @@ -221,7 +221,7 @@ def _traverse_list(
def process_revision_directives(
self,
context: MigrationContext,
revision: Revision,
revision: _GetRevArg,
directives: List[MigrationScript],
) -> None:
self._traverse_list(context, revision, directives)
8 changes: 7 additions & 1 deletion alembic/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from typing import Callable
from typing import Collection
from typing import ContextManager
from typing import Dict
from typing import Iterable
from typing import List
from typing import Literal
from typing import Mapping
Expand Down Expand Up @@ -143,7 +144,12 @@ def configure(
include_schemas: bool = False,
process_revision_directives: Optional[
Callable[
[MigrationContext, Tuple[str, str], List[MigrationScript]], None
[
MigrationContext,
Union[str, Iterable[Optional[str]], Iterable[str]],
List[MigrationScript],
],
None,
]
] = None,
compare_type: Union[
Expand Down
3 changes: 2 additions & 1 deletion alembic/runtime/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .migration import MigrationContext
from .. import util
from ..operations import Operations
from ..script.revision import _GetRevArg

if TYPE_CHECKING:
from sqlalchemy.engine import URL
Expand All @@ -42,7 +43,7 @@
_RevNumber = Optional[Union[str, Tuple[str, ...]]]

ProcessRevisionDirectiveFn = Callable[
[MigrationContext, Tuple[str, str], List["MigrationScript"]], None
[MigrationContext, _GetRevArg, List["MigrationScript"]], None
]

RenderItemFn = Callable[
Expand Down
12 changes: 3 additions & 9 deletions alembic/script/revision.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@
_RevIdType = Union[str, List[str], Tuple[str, ...]]
_GetRevArg = Union[
str,
List[Optional[str]],
Tuple[Optional[str], ...],
FrozenSet[Optional[str]],
Set[Optional[str]],
List[str],
Tuple[str, ...],
FrozenSet[str],
Set[str],
Iterable[Optional[str]],
Iterable[str],
]
_RevisionIdentifierType = Union[str, Tuple[str, ...], None]
_RevisionOrStr = Union["Revision", str]
Expand Down Expand Up @@ -738,7 +732,7 @@ def _shares_lineage(
)

def _resolve_revision_number(
self, id_: Optional[str]
self, id_: Optional[_GetRevArg]
) -> Tuple[Tuple[str, ...], Optional[str]]:
branch_label: Optional[str]
if isinstance(id_, str) and "@" in id_:
Expand Down
8 changes: 8 additions & 0 deletions docs/build/unreleased/1329.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, autogenerate, regression
:tickets: 1329

Fixed regression caused by :ticket:`879` released in 1.7.0 where the
".info" dictionary of ``Table`` would not render in autogenerate create
table statements. This can be useful for custom create table DDL rendering
schemes so it is restored.
21 changes: 21 additions & 0 deletions tests/test_autogen_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,27 @@ def test_render_table_with_comment(self):
")",
)

def test_render_table_with_info(self):
m = MetaData()
t = Table(
"test",
m,
Column("id", Integer, primary_key=True),
Column("q", Integer, ForeignKey("address.id")),
info={"oracle_partition": "PARTITION BY ..."},
)
op_obj = ops.CreateTableOp.from_table(t)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.create_table('test',"
"sa.Column('id', sa.Integer(), nullable=False),"
"sa.Column('q', sa.Integer(), nullable=True),"
"sa.ForeignKeyConstraint(['q'], ['address.id'], ),"
"sa.PrimaryKeyConstraint('id'),"
"info={'oracle_partition': 'PARTITION BY ...'}"
")",
)

def test_render_add_column_with_comment(self):
op_obj = ops.AddColumnOp(
"foo", Column("x", Integer, comment="This is a Column")
Expand Down

0 comments on commit 4ec6638

Please sign in to comment.