MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / main

Function main

examples/sharding/separate_schema_translates.py:176–250  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

174
175
176def main():
177 setup()
178
179 # save and load objects!
180
181 tokyo = WeatherLocation("Asia", "Tokyo")
182 newyork = WeatherLocation("North America", "New York")
183 toronto = WeatherLocation("North America", "Toronto")
184 london = WeatherLocation("Europe", "London")
185 dublin = WeatherLocation("Europe", "Dublin")
186 brasilia = WeatherLocation("South America", "Brasila")
187 quito = WeatherLocation("South America", "Quito")
188
189 tokyo.reports.append(Report(80.0))
190 newyork.reports.append(Report(75))
191 quito.reports.append(Report(85))
192
193 with Session() as sess:
194 sess.add_all(
195 [tokyo, newyork, toronto, london, dublin, brasilia, quito]
196 )
197
198 sess.commit()
199
200 t = sess.get(
201 WeatherLocation,
202 tokyo.id,
203 identity_token="asia",
204 )
205 assert t.city == tokyo.city
206 assert t.reports[0].temperature == 80.0
207
208 # select across shards
209 asia_and_europe = sess.execute(
210 select(WeatherLocation).filter(
211 WeatherLocation.continent.in_(["Europe", "Asia"])
212 )
213 ).scalars()
214
215 assert {c.city for c in asia_and_europe} == {
216 "Tokyo",
217 "London",
218 "Dublin",
219 }
220
221 # optionally set a shard id for the query and all related loaders
222 north_american_cities_w_t = sess.execute(
223 select(WeatherLocation)
224 .filter(WeatherLocation.city.startswith("T"))
225 .options(set_shard_id("north_america"))
226 ).scalars()
227
228 # Tokyo not included since not in the north_america shard
229 assert {c.city for c in north_american_cities_w_t} == {
230 "Toronto",
231 }
232
233 # the Report class uses a simple integer primary key. So across two

Callers 1

Calls 15

selectFunction · 0.90
set_shard_idClass · 0.90
inspectFunction · 0.90
SessionClass · 0.85
setupFunction · 0.70
WeatherLocationClass · 0.70
ReportClass · 0.70
appendMethod · 0.45
add_allMethod · 0.45
commitMethod · 0.45
getMethod · 0.45
scalarsMethod · 0.45

Tested by

no test coverage detected