MCPcopy Create free account
hub / github.com/CloverHealth/temporal-sqlalchemy / build_clock_table

Function build_clock_table

temporal_sqlalchemy/clock.py:198–245  ·  view source on GitHub ↗

build clock table to be used with a clock model

(entity_table: sa.Table,
                      metadata: sa.MetaData,
                      schema: str,
                      activity_class=None)

Source from the content-addressed store, hash-verified

196
197
198def build_clock_table(entity_table: sa.Table,
199 metadata: sa.MetaData,
200 schema: str,
201 activity_class=None) -> sa.Table:
202 """ build clock table to be used with a clock model """
203 clock_table_name = util.truncate_identifier(
204 "%s_clock" % entity_table.name)
205 clock_table = sa.Table(
206 clock_table_name,
207 metadata,
208 sa.Column('id',
209 sap.UUID(as_uuid=True),
210 default=uuid.uuid4,
211 primary_key=True),
212 sa.Column('tick',
213 sa.Integer,
214 nullable=False,
215 autoincrement=False),
216 sa.Column('timestamp',
217 sa.DateTime(True),
218 server_default=sa.func.current_timestamp()),
219 schema=schema)
220
221 entity_keys = []
222 for fk in util.foreign_key_to(entity_table, nullable=False):
223 # this is done to support arbitrary primary key shape on entity
224 # We don't add additional indices on the foreign keys here because
225 # the uniqueness constraints will add an implicit index.
226 clock_table.append_column(fk)
227 entity_keys.append(fk.key)
228
229 tick_entity_unique_name = util.truncate_identifier('%s_tick_entity_id_key' % clock_table_name)
230 clock_table.append_constraint(
231 sa.UniqueConstraint(*(entity_keys + ['tick']), name=tick_entity_unique_name),
232 )
233
234 if activity_class:
235 activity_keys = []
236 # support arbitrary shaped activity primary keys
237 for fk in util.foreign_key_to(activity_class.__table__,
238 prefix='activity',
239 nullable=False):
240 clock_table.append_column(fk)
241 activity_keys.append(fk.key)
242 # ensure we have DB constraint on clock <> activity uniqueness
243 clock_table.append_constraint(sa.UniqueConstraint(*(entity_keys + activity_keys)))
244
245 return clock_table
246
247
248def build_history_class(

Callers 3

test_build_clock_tableFunction · 0.90
test_build_clock_tablesFunction · 0.90
temporal_mapFunction · 0.85

Calls

no outgoing calls

Tested by 2

test_build_clock_tableFunction · 0.72
test_build_clock_tablesFunction · 0.72