Skip to content

Commit 9df74f1

Browse files
kesmit13claude
andcommitted
refactor!: Modernize DDL element APIs with variable positional arguments
BREAKING CHANGE: Updated all DDL element classes to use variable positional arguments (*columns) instead of list-based parameters. This modernizes the API while maintaining functionality but intentionally breaks backward compatibility. Key Changes: - ShardKey: Changed from ShardKey(['col1', 'col2']) to ShardKey('col1', 'col2') - SortKey: Changed from SortKey(['col1']) to SortKey('col1') - VectorKey: Changed from VectorKey(['col1']) to VectorKey('col1') - FullTextIndex: Added ASC/DESC support and modernized API - MultiValueIndex: Changed from MultiValueIndex('col') to MultiValueIndex('col1', 'col2') All keyword arguments are now keyword-only for improved API clarity. Updated 9 files including all examples, tests, and documentation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c231c22 commit 9df74f1

File tree

9 files changed

+272
-226
lines changed

9 files changed

+272
-226
lines changed

sqlalchemy_singlestoredb/ddlelement.py

Lines changed: 184 additions & 130 deletions
Large diffs are not rendered by default.

sqlalchemy_singlestoredb/examples/shard_key_examples.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class MultiColumnShardTable(Base): # type: ignore
5151
amount = Column(Numeric(10, 2))
5252

5353
__table_args__ = {
54-
'info': {'singlestoredb_shard_key': ShardKey(['user_id', 'category_id'])},
54+
'info': {'singlestoredb_shard_key': ShardKey('user_id', 'category_id')},
5555
}
5656

5757

@@ -94,7 +94,7 @@ class ComplexTable(Base): # type: ignore
9494
__table_args__ = {
9595
'info': {
9696
'singlestoredb_shard_key': ShardKey('user_id'),
97-
'singlestoredb_sort_key': SortKey(['order_id', 'amount']),
97+
'singlestoredb_sort_key': SortKey('order_id', 'amount'),
9898
},
9999
}
100100

@@ -111,7 +111,7 @@ class MultiColumnOnlyTable(Base): # type: ignore
111111
__table_args__ = {
112112
'info': {
113113
'singlestoredb_shard_key': ShardKey(
114-
['user_id', 'region_id'], metadata_only=True,
114+
'user_id', 'region_id', metadata_only=True,
115115
),
116116
},
117117
}

sqlalchemy_singlestoredb/examples/table_constructor_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def main() -> None:
8686
Column('user_id', Integer, primary_key=True),
8787
Column('region_id', Integer, primary_key=True),
8888
Column('amount', Integer),
89-
ShardKey(['user_id', 'region_id']),
89+
ShardKey('user_id', 'region_id'),
9090
)
9191
table4.create(mock_engine, checkfirst=False)
9292

@@ -99,7 +99,7 @@ def main() -> None:
9999
Column('timestamp', String(50)),
100100
Column('event_type', String(50)),
101101
ShardKey('user_id'),
102-
SortKey(['timestamp']),
102+
SortKey('timestamp'),
103103
)
104104
table5.create(mock_engine, checkfirst=False)
105105

sqlalchemy_singlestoredb/examples/universal/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MyTable(Base): # type: ignore
4040
{
4141
'info': {
4242
'singlestoredb_shard_key': ShardKey('id'),
43-
'singlestoredb_sort_key': SortKey(['id', 'data']),
43+
'singlestoredb_sort_key': SortKey('id', 'data'),
4444
},
4545
}
4646
)

sqlalchemy_singlestoredb/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Table(SQLATable):
5757
... Column('id', Integer, primary_key=True),
5858
... Column('title', String(200)),
5959
... Column('content', Text),
60-
... FullTextIndex(['title', 'content']), # Multiple columns, auto-named
60+
... FullTextIndex('title', 'content'), # Multiple columns, auto-named
6161
... FullTextIndex('title', name='ft_title', version=2) # Single column, named v2
6262
... )
6363

sqlalchemy_singlestoredb/tests/test_advanced_indexes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ def test_fulltext_index_compilation_with_special_column_names(self) -> None:
600600

601601
# Test multiple columns with special characters
602602
ft_idx = FullTextIndex(
603-
['title column', 'content-field'], name='search_idx',
603+
'title column', 'content-field', name='search_idx',
604604
)
605605
result = compile_fulltext_index(ft_idx, None)
606606
expected = 'FULLTEXT search_idx (`title column`, `content-field`)'

sqlalchemy_singlestoredb/tests/test_shard_key.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def test_basic_shard_key(self) -> None:
2929

3030
def test_multi_column_shard_key(self) -> None:
3131
"""Test shard key with multiple columns."""
32-
shard_key = ShardKey(['user_id', 'category_id'])
32+
shard_key = ShardKey('user_id', 'category_id')
3333
assert shard_key.columns == ('user_id', 'category_id')
3434
assert shard_key.metadata_only is False
35-
assert repr(shard_key) == "ShardKey(['user_id', 'category_id'])"
35+
assert repr(shard_key) == "ShardKey('user_id', 'category_id')"
3636

3737
def test_empty_shard_key(self) -> None:
3838
"""Test empty shard key for keyless sharding."""
@@ -50,30 +50,22 @@ def test_shard_key_metadata_only_single_column(self) -> None:
5050

5151
def test_shard_key_metadata_only_multi_column(self) -> None:
5252
"""Test SHARD KEY ONLY with multiple columns."""
53-
shard_key = ShardKey(['user_id', 'category_id'], metadata_only=True)
53+
shard_key = ShardKey('user_id', 'category_id', metadata_only=True)
5454
assert shard_key.columns == ('user_id', 'category_id')
5555
assert shard_key.metadata_only is True
56-
expected_repr = "ShardKey(['user_id', 'category_id'], metadata_only=True)"
56+
expected_repr = "ShardKey('user_id', 'category_id', metadata_only=True)"
5757
assert repr(shard_key) == expected_repr
5858

5959
def test_shard_key_metadata_only_empty(self) -> None:
6060
"""Test SHARD KEY ONLY with no columns (should fallback to empty)."""
6161
shard_key = ShardKey(metadata_only=True)
6262
assert shard_key.columns == ()
6363
assert shard_key.metadata_only is True
64-
assert repr(shard_key) == 'ShardKey(None, metadata_only=True)'
64+
assert repr(shard_key) == 'ShardKey(metadata_only=True)'
6565

6666

6767
class TestShardKeyCompiler:
68-
"""Test ShardKey DDL compilation."""
69-
70-
def setup_method(self) -> None:
71-
"""Set up mock engine for DDL compilation."""
72-
def dump(sql: Any, *multiparams: Any, **params: Any) -> None:
73-
self.compiled_sql = str(sql.compile(dialect=self.mock_engine.dialect))
74-
75-
self.mock_engine = create_mock_engine('singlestoredb://', dump)
76-
self.compiled_sql = ''
68+
"""Test ShardKey SQL compilation."""
7769

7870
def test_compile_basic_shard_key(self) -> None:
7971
"""Test compilation of basic shard key."""
@@ -87,7 +79,7 @@ def test_compile_multi_column_shard_key(self) -> None:
8779
"""Test compilation of multi-column shard key."""
8880
from sqlalchemy_singlestoredb.ddlelement import compile_shard_key
8981

90-
shard_key = ShardKey(['user_id', 'category_id'])
82+
shard_key = ShardKey('user_id', 'category_id')
9183
result = compile_shard_key(shard_key, None)
9284
assert result == 'SHARD KEY (user_id, category_id)'
9385

@@ -111,7 +103,7 @@ def test_compile_shard_key_metadata_only_multi_column(self) -> None:
111103
"""Test compilation of SHARD KEY ONLY with multiple columns."""
112104
from sqlalchemy_singlestoredb.ddlelement import compile_shard_key
113105

114-
shard_key = ShardKey(['user_id', 'category_id'], metadata_only=True)
106+
shard_key = ShardKey('user_id', 'category_id', metadata_only=True)
115107
result = compile_shard_key(shard_key, None)
116108
assert result == 'SHARD KEY (user_id, category_id) METADATA_ONLY'
117109

@@ -138,7 +130,7 @@ def test_compile_shard_key_with_special_column_names(self) -> None:
138130
assert result == 'SHARD KEY (`user id`)'
139131

140132
# Test multiple columns with special characters
141-
shard_key = ShardKey(['user-id', 'tenant id', 'normal_column'])
133+
shard_key = ShardKey('user-id', 'tenant id', 'normal_column')
142134
result = compile_shard_key(shard_key, None)
143135
assert result == 'SHARD KEY (`user-id`, `tenant id`, normal_column)'
144136

@@ -247,7 +239,7 @@ class MyTable(Base): # type: ignore
247239
data = Column(String(50))
248240

249241
__table_args__ = {
250-
'info': {'singlestoredb_shard_key': ShardKey(['user_id', 'category_id'])},
242+
'info': {'singlestoredb_shard_key': ShardKey('user_id', 'category_id')},
251243
}
252244

253245
Base.metadata.create_all(self.mock_engine, checkfirst=False)
@@ -640,7 +632,7 @@ def test_table_constructor_multi_column_shard_key(self) -> None:
640632
Column('user_id', Integer, primary_key=True),
641633
Column('category_id', Integer, primary_key=True),
642634
Column('amount', Integer),
643-
ShardKey(['user_id', 'category_id']),
635+
ShardKey('user_id', 'category_id'),
644636
)
645637

646638
# Verify info is set correctly
@@ -661,7 +653,7 @@ def test_table_constructor_with_both_keys(self) -> None:
661653
Column('order_id', Integer, primary_key=True),
662654
Column('created_at', String(50)),
663655
ShardKey('user_id'),
664-
SortKey(['created_at']),
656+
SortKey('created_at'),
665657
)
666658

667659
# Verify info is set correctly
@@ -737,6 +729,6 @@ def test_table_constructor_multiple_sort_keys_error(self) -> None:
737729
Column('id', Integer, primary_key=True),
738730
Column('created_at', String(50)),
739731
Column('updated_at', String(50)),
740-
SortKey(['created_at']),
741-
SortKey(['updated_at']), # This should cause an error
732+
SortKey('created_at'),
733+
SortKey('updated_at'), # This should cause an error
742734
)

0 commit comments

Comments
 (0)