Skip to content

Commit cf03c4b

Browse files
authored
♻️ refactor(model): ??????????
Closes #97
1 parent 61422dc commit cf03c4b

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

‎src/dbjavagenix/core/models.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ColumnInfo:
6868
max_length: Optional[int] = None
6969
precision: Optional[int] = None
7070
scale: Optional[int] = None
71+
auto_increment: bool = False
7172

7273

7374
@dataclass

‎src/dbjavagenix/database/codegen_tools.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,11 @@ def _build_table_info(
160160
primary_key=col["primary_key"],
161161
default_value=col["default_value"],
162162
comment=col["comment"],
163+
auto_increment=col["auto_increment"],
164+
max_length=col["max_length"],
165+
precision=col.get("precision"),
166+
scale=col.get("scale"),
163167
)
164-
# 添加额外属性
165-
column_obj.auto_increment = col["auto_increment"]
166-
column_obj.max_length = col["max_length"]
167-
column_obj.precision = col.get("precision")
168-
column_obj.scale = col.get("scale")
169168
column_objects.append(column_obj)
170169

171170
table_obj = TableInfo(
@@ -193,10 +192,10 @@ def _column_to_dict(self, column: ColumnInfo) -> Dict[str, Any]:
193192
"primary_key": column.primary_key,
194193
"default_value": column.default_value,
195194
"comment": column.comment,
196-
"auto_increment": getattr(column, "auto_increment", False),
197-
"max_length": getattr(column, "max_length", None),
198-
"precision": getattr(column, "precision", None),
199-
"scale": getattr(column, "scale", None),
195+
"auto_increment": column.auto_increment,
196+
"max_length": column.max_length,
197+
"precision": column.precision,
198+
"scale": column.scale,
200199
"java_type": column.java_type,
201200
}
202201

‎tests/unit/test_codegen_analyzer.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Unit tests for the public code-generation analysis workflow."""
22

3+
import asyncio
34
from types import SimpleNamespace
45

56
import pytest
@@ -164,6 +165,39 @@ async def test_table_analysis_forwards_schema_to_all_metadata_queries():
164165
assert result["relationships"]["primary_keys"] == ["id"]
165166

166167

168+
def test_table_analysis_keeps_column_metadata_typed():
169+
analyzer = CodegenAnalyzer(object())
170+
introspector = _SchemaAwareIntrospector()
171+
172+
def get_columns(_connection_id, _table_name, _schema=None):
173+
return [
174+
{
175+
"name": "id",
176+
"type": "BIGINT",
177+
"nullable": False,
178+
"primary_key": True,
179+
"default_value": None,
180+
"comment": "Primary key",
181+
"auto_increment": True,
182+
"max_length": None,
183+
"precision": None,
184+
"scale": None,
185+
}
186+
]
187+
188+
introspector.get_columns = get_columns
189+
analyzer.introspector = introspector
190+
191+
result = asyncio.run(
192+
analyzer.analyze_table_for_codegen("mysql-1", "users", template_category="Default")
193+
)
194+
195+
column = result["table_info"]["columns"][0]
196+
assert column["auto_increment"] is True
197+
assert column["precision"] is None
198+
assert column["scale"] is None
199+
200+
167201
def test_codegen_entrypoints_expose_optional_schema():
168202
atomic = next(
169203
tool for tool in get_atomic_codegen_tools() if tool.name == "codegen_build_context"

‎tests/unit/test_models.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ def test_column_info_creation():
2121
assert column.name == "user_id"
2222
assert column.java_type == "Long"
2323
assert column.primary_key is True
24+
assert column.auto_increment is False
25+
26+
27+
def test_column_info_persists_auto_increment_metadata():
28+
column = ColumnInfo(
29+
name="id",
30+
data_type="BIGINT",
31+
java_type="Long",
32+
primary_key=True,
33+
auto_increment=True,
34+
)
35+
36+
assert column.auto_increment is True
2437

2538

2639
def test_table_info_entity_name():

0 commit comments

Comments
 (0)