|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | +from typing import Optional |
| 6 | +from typing import TYPE_CHECKING |
| 7 | +from typing import Union |
| 8 | + |
| 9 | +from ...operations import ops |
| 10 | +from ...util import PriorityDispatchResult |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + |
| 14 | + from sqlalchemy.sql.elements import quoted_name |
| 15 | + from sqlalchemy.sql.schema import Column |
| 16 | + from sqlalchemy.sql.schema import Table |
| 17 | + |
| 18 | + from ..api import AutogenContext |
| 19 | + from ...operations.ops import AlterColumnOp |
| 20 | + from ...operations.ops import ModifyTableOps |
| 21 | + from ...runtime.plugins import Plugin |
| 22 | + |
| 23 | +log = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +def _compare_column_comment( |
| 27 | + autogen_context: AutogenContext, |
| 28 | + alter_column_op: AlterColumnOp, |
| 29 | + schema: Optional[str], |
| 30 | + tname: Union[quoted_name, str], |
| 31 | + cname: quoted_name, |
| 32 | + conn_col: Column[Any], |
| 33 | + metadata_col: Column[Any], |
| 34 | +) -> PriorityDispatchResult: |
| 35 | + assert autogen_context.dialect is not None |
| 36 | + if not autogen_context.dialect.supports_comments: |
| 37 | + return PriorityDispatchResult.CONTINUE |
| 38 | + |
| 39 | + metadata_comment = metadata_col.comment |
| 40 | + conn_col_comment = conn_col.comment |
| 41 | + if conn_col_comment is None and metadata_comment is None: |
| 42 | + return PriorityDispatchResult.CONTINUE |
| 43 | + |
| 44 | + alter_column_op.existing_comment = conn_col_comment |
| 45 | + |
| 46 | + if conn_col_comment != metadata_comment: |
| 47 | + alter_column_op.modify_comment = metadata_comment |
| 48 | + log.info("Detected column comment '%s.%s'", tname, cname) |
| 49 | + |
| 50 | + return PriorityDispatchResult.STOP |
| 51 | + else: |
| 52 | + return PriorityDispatchResult.CONTINUE |
| 53 | + |
| 54 | + |
| 55 | +def _compare_table_comment( |
| 56 | + autogen_context: AutogenContext, |
| 57 | + modify_table_ops: ModifyTableOps, |
| 58 | + schema: Optional[str], |
| 59 | + tname: Union[quoted_name, str], |
| 60 | + conn_table: Optional[Table], |
| 61 | + metadata_table: Optional[Table], |
| 62 | +) -> PriorityDispatchResult: |
| 63 | + assert autogen_context.dialect is not None |
| 64 | + if not autogen_context.dialect.supports_comments: |
| 65 | + return PriorityDispatchResult.CONTINUE |
| 66 | + |
| 67 | + # if we're doing CREATE TABLE, comments will be created inline |
| 68 | + # with the create_table op. |
| 69 | + if conn_table is None or metadata_table is None: |
| 70 | + return PriorityDispatchResult.CONTINUE |
| 71 | + |
| 72 | + if conn_table.comment is None and metadata_table.comment is None: |
| 73 | + return PriorityDispatchResult.CONTINUE |
| 74 | + |
| 75 | + if metadata_table.comment is None and conn_table.comment is not None: |
| 76 | + modify_table_ops.ops.append( |
| 77 | + ops.DropTableCommentOp( |
| 78 | + tname, existing_comment=conn_table.comment, schema=schema |
| 79 | + ) |
| 80 | + ) |
| 81 | + return PriorityDispatchResult.STOP |
| 82 | + elif metadata_table.comment != conn_table.comment: |
| 83 | + modify_table_ops.ops.append( |
| 84 | + ops.CreateTableCommentOp( |
| 85 | + tname, |
| 86 | + metadata_table.comment, |
| 87 | + existing_comment=conn_table.comment, |
| 88 | + schema=schema, |
| 89 | + ) |
| 90 | + ) |
| 91 | + return PriorityDispatchResult.STOP |
| 92 | + |
| 93 | + return PriorityDispatchResult.CONTINUE |
| 94 | + |
| 95 | + |
| 96 | +def setup(plugin: Plugin) -> None: |
| 97 | + plugin.add_autogenerate_comparator( |
| 98 | + _compare_column_comment, |
| 99 | + "column", |
| 100 | + "comments", |
| 101 | + ) |
| 102 | + plugin.add_autogenerate_comparator( |
| 103 | + _compare_table_comment, |
| 104 | + "table", |
| 105 | + "comments", |
| 106 | + ) |
0 commit comments