def METHOD_NAME(spec):
    try:
        table_definition = AggregateTableDefinition.objects.get(
            domain=spec.domain,
            table_id=spec.table_id,
        )
    except AggregateTableDefinition.DoesNotExist:
        table_definition = AggregateTableDefinition(
            domain=spec.domain,
            table_id=spec.table_id,
        )
    table_definition.display_name = spec.display_name
    table_definition.primary_data_source_id = UUID(spec.primary_table.data_source_id)
    table_definition.primary_data_source_key = spec.primary_table.key_column
    if spec.time_aggregation:
        db_aggregation_spec = table_definition.time_aggregation or TimeAggregationDefinition()
        db_aggregation_spec.aggregation_unit = spec.time_aggregation.unit
        db_aggregation_spec.start_column = spec.time_aggregation.start_column
        db_aggregation_spec.end_column = spec.time_aggregation.end_column
        db_aggregation_spec.save()
        table_definition.time_aggregation = db_aggregation_spec
    table_definition.save()
    return table_definition