Skip to content

Commit

Permalink
Trampoline standard library types (#7498)
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored Jun 26, 2024
1 parent 3db703d commit aea873a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion edb/buildmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# The merge conflict there is a nice reminder that you probably need
# to write a patch in edb/pgsql/patches.py, and then you should preserve
# the old value.
EDGEDB_CATALOG_VERSION = 2024_06_19_00_00
EDGEDB_CATALOG_VERSION = 2024_06_26_00_00
EDGEDB_MAJOR_VERSION = 6


Expand Down
90 changes: 90 additions & 0 deletions edb/pgsql/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,11 @@ def __init__(self, **kwargs):
self.inhview_updates = set()
self.post_inhview_update_commands = []

def schedule_trampoline(self, obj, schema, context):
create_trampolines = context.get(
sd.DeltaRootContext).op.create_trampolines
create_trampolines.table_targets.append(obj)

def _get_multicommand(
self,
context,
Expand Down Expand Up @@ -3140,6 +3145,49 @@ def get_source_and_pointer_ctx(cls, schema, context):

return source, pointer

@classmethod
def create_type_trampoline(
cls,
schema: s_schema.Schema,
obj: CompositeObject,
aspect: str='table',
) -> dbops.Command:
versioned_name = common.get_backend_name(
schema, obj, aspect=aspect, catenate=False
)
trampolined_name = common.get_backend_name(
schema, obj, aspect=aspect, catenate=False, versioned=False
)
if versioned_name != trampolined_name:
query = f'''
SELECT * FROM {q(*versioned_name)}
'''
view = dbops.View(name=trampolined_name, query=query)
return dbops.CreateView(view, or_replace=True)
else:
return dbops.CommandGroup()

@classmethod
def drop_type_trampoline(
cls,
schema: s_schema.Schema,
obj: CompositeObject,
aspect: str='table',
) -> dbops.Command:
versioned_name = common.get_backend_name(
schema, obj, aspect=aspect, catenate=False
)
trampolined_name = common.get_backend_name(
schema, obj, aspect=aspect, catenate=False, versioned=False
)
if versioned_name != trampolined_name:
return dbops.DropView(
trampolined_name,
conditions=[dbops.ViewExists(trampolined_name)],
)
else:
return dbops.CommandGroup()

@classmethod
def _get_select_from(
cls,
Expand Down Expand Up @@ -3412,6 +3460,10 @@ def create_inhview(
if alter_ancestors:
self.alter_ancestor_inhviews(schema, context, obj)

self.pgops.add(
self.create_type_trampoline(schema, obj, aspect='inhview')
)

def alter_inhview(
self,
schema: s_schema.Schema,
Expand Down Expand Up @@ -3471,6 +3523,8 @@ def drop_inhview(
obj: CompositeObject,
conditional: bool = False,
) -> None:
self.pgops.add(self.drop_type_trampoline(schema, obj, aspect='inhview'))

inhview_name = common.get_backend_name(
schema, obj, catenate=False, aspect='inhview')
conditions = []
Expand Down Expand Up @@ -3935,6 +3989,7 @@ def apply(
self.pgops.add(self.update_search_indexes)

self.schedule_endpoint_delete_action_update(self.scls, schema, context)
self.schedule_trampoline(self.scls, schema, context)

self._fixup_configs(schema, context)

Expand Down Expand Up @@ -5402,6 +5457,7 @@ def _create_begin(
def _create_finalize(self, schema, context):
schema = super()._create_finalize(schema, context)
self.apply_scheduled_inhview_updates(schema, context)
self.schedule_trampoline(self.scls, schema, context)
return schema


Expand Down Expand Up @@ -5867,6 +5923,7 @@ def _create_begin(
self.table_name = self._get_table_name(prop, schema)

self._create_property(prop, src, schema, orig_schema, context)
self.schedule_trampoline(self.scls, schema, context)

return schema

Expand Down Expand Up @@ -6068,6 +6125,35 @@ def _delete_innards(
return schema


class CreateTrampolines(MetaCommand):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.table_targets: list[s_objtypes.ObjectType | s_pointers.Pointer] = (
[]
)

def apply(
self,
schema: s_schema.Schema,
context: sd.CommandContext,
) -> s_schema.Schema:
for obj in self.table_targets:
if not (schema.has_object(obj.id) and types.has_table(obj, schema)):
continue
self.pgops.add(
CompositeMetaCommand.create_type_trampoline(
schema, obj
)
)
self.pgops.add(
CompositeMetaCommand.create_type_trampoline(
schema, obj, aspect='inhview',
)
)

return schema


class UpdateEndpointDeleteActions(MetaCommand):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand Down Expand Up @@ -7587,12 +7673,16 @@ def apply(
context: sd.CommandContext,
) -> s_schema.Schema:
self.update_endpoint_delete_actions = UpdateEndpointDeleteActions()
self.create_trampolines = CreateTrampolines()

schema = super().apply(schema, context)

self.update_endpoint_delete_actions.apply(schema, context)
self.pgops.add(self.update_endpoint_delete_actions)

self.create_trampolines.apply(schema, context)
self.pgops.add(self.create_trampolines)

return schema


Expand Down

0 comments on commit aea873a

Please sign in to comment.