Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter alias even from ref propagation #6956

Merged
merged 14 commits into from
Mar 27, 2024
Merged

Alter alias even from ref propagation #6956

merged 14 commits into from
Mar 27, 2024

Conversation

aljazerzen
Copy link
Contributor

  • uncomment the hack
  • make commands produced by affected refs execute as causes instead of innards
  • fix for reflection writer when we create a type and drop it in the same migration

@aljazerzen aljazerzen changed the title alias propagate refs Alter alias even from ref propagation Mar 1, 2024
@aljazerzen
Copy link
Contributor Author

@msullivan Yesterday, we changed affected refs to be applied as "caused" instead of "innards". You said that you are scared what this will break. I think we have exactly one test that breaks:

create type X {
                create property bar -> int64 {
                    create constraint std::exclusive
                }
            };

alter type X alter property bar using ('1');

The alter produces the following delta root:

<DeltaRoot canonical=True at 0x7fe853cc6b90> (
    <AlterSchemaVersion classname=__schema_version__ at 0x7fe8691f8fd0> (
        version = UUID('d95d5c03-d7e2-11ee-ad61-d18567326038') | UUID('1cacc186-d7e3-11ee-a9b2-a180ec730ca2')
    ),
    <CreateMigration classname=m15cipax5pdr45grvgdjw5ejtq3twiran5gkn6pqy2txlskmnohrna at 0x7fe853c88510> (
        script       = None | "SET generated_by := (schema::MigrationGeneratedBy.DDLStatement);\nALTER TYPE X {\n    ALTER PROPERTY bar {\n        USING ('1');\n    };\n};",
        builtin      = None | False,
        internal     = None | False,
        parents      = None | [<ObjectShell Migration(<UnqualName m1kackz3nz3oketzgvnbqdbul3w546c7li46ydofri37piyt6kluna>) at 0x7fe8538966d0>],
        generated_by = None | 'DDLStatement',
        name         = None | <UnqualName m15cipax5pdr45grvgdjw5ejtq3twiran5gkn6pqy2txlskmnohrna>,
        <AlterObjectType source_context=<edb.common.context.ParserContext object at 0x7fe85382a5d0>, classname=default::X at 0x7fe853cd4150> (
            <AlterProperty source_context=<edb.common.context.ParserContext object at 0x7fe853b3dd10>, classname=default::__|bar@default|X at 0x7fe868fabe90> (
                main ,
                <AlterPropertyOwned classname=default::__|bar@default|X at 0x7fe8539f31d0>(owned=True | True),
                <SetPropertyType classname=default::__|bar@default|X at 0x7fe8539f0990> (
                    target          = <ScalarType 00000000-0000-0000-0000-000000000105 at 0x0x7fe8691860d0> | <TypeShell ScalarType(<QualName std::str>) at 0x7fe85388f950> # computed,
                    computed_fields = None | frozenset({'target'})
                ),
                computed_link_alias  = None | None,
                computed_link_alias_is_backward = None | None,
                expr                 = None | <CompiledExpression text="'1'", refs={00000000-0000-0000-0000-000000000101}, origin=None at 0x7fe853a119d0>,
                <AlterPropertyUpperCardinality classname=default::__|bar@default|X at 0x7fe853a10e10> (
                    cardinality     = None | <SchemaCardinality.One: 'One'> # computed,
                    computed_fields = edb.common.checked.FrozenCheckedSet[str]({'target'}) | frozenset({'target', 'cardinality'})
                ),
                <AlterPropertyLowerCardinality classname=default::__|bar@default|X at 0x7fe853a11450> (
                    required        = None | True # computed,
                    computed_fields = edb.common.checked.FrozenCheckedSet[str]({'target', 'cardinality'}) | frozenset({'target', 'required', 'cardinality'})
                ),
                computable           = None | True,
                caused ,
                <AlterConstraint classname=default::std|exclusive@default|__||bar&default||X@da39a3ee5e6b4b0d3255bfef95601890afd80709, ddl_identity={'args': edb.schema.expr.ExpressionList([])}, from_expr_propagation=True at 0x7fe853820090> (
                    finalexpr = <Expression text='SELECT false', refs={00000000-0000-0000-0000-000000000100, 00000000-0000-0000-0000-000000000101, 00000000-0000-0000-0000-000000000105, 00000000-0000-0000-0000-000000000109, 00000000-0000-0000-0000-00000000010f}, origin=None at 0x7fe853972410> | <CompiledExpression text='select std::_is_exclusive(__subject__)', refs={00000000-0000-0000-0000-000000000101, a58ae470-e626-5d1c-90ee-c73125d07418, 00000000-0000-0000-0000-000000000109}, origin=None at 0x7fe853972890>,
                    args      = edb.schema.expr.ExpressionList([]) | None,
                    params    = [b0d7669e-476d-5d5a-b1e3-87566e023465] | [b0d7669e-476d-5d5a-b1e3-87566e023465],
                    abstract  = False | False
                )
            )
        ),
        id           = None | UUID('1ca55b59-d7e3-11ee-a0c5-a9fbe2800863')
    )
)

... which fails in postgres, because we try to drop the column while constraint still refers to it.

Solution that I'm seeing is to move the "column drop" to the end of the "alter property" command.

@aljazerzen
Copy link
Contributor Author

The remaining error is this:

# from test_schema_migrations_computed_optionality_01
# schema 1:
            abstract type Removable {
                optional single property removed := false;
            };

            type Topic extending Removable { };
            type Definition { };

            alias VisibleTopic := (
                SELECT Topic {
                    defs := (SELECT Definition),
                }
                FILTER NOT .removed
            );

# schema 2:
            abstract type Removable {
                property removed := false;
            };

            type Topic extending Removable { };
            type Definition { };

            alias VisibleTopic := (
                SELECT Topic {
                    defs := (SELECT Definition),
                }
                FILTER NOT .removed
            );

We have an "alter property removed", which propagates via two paths:

  • via Topic, because extends Removable, which propagates into VisibleTopic, which drops alias types and recreates,
  • via reference to property removed in the VisibleTopic, which triggers alter alias again.

This fails because the property VisibleTopic.removed is found to be descendant of Removable.removed and is scheduled to be updated here:
https://github.com/edgedb/edgedb/blob/main/edb/schema/inheriting.py#L958
... but because one alter alias runs in the first iteration, the property does not exists anymore. Well it does exist, but with a different id.

@aljazerzen aljazerzen force-pushed the alias-propagate-refs branch from 129a3c0 to a4096ae Compare March 6, 2024 15:36
@aljazerzen aljazerzen force-pushed the alias-propagate-refs branch from f6691a6 to 46d45ed Compare March 21, 2024 18:15
@aljazerzen aljazerzen requested a review from msullivan March 21, 2024 18:31

for descendant_name in descendant_names:
descendant = schema.get(descendant_name, type=so.InheritingObject)
assert descendant, '.inherit_fields caused a drop of a descendant?'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get will raise an error by itself, right?
If you want it deferred to this assert, pass a default

@@ -426,7 +421,6 @@ def _alter_begin(

return super()._alter_begin(schema, context)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to enable ruff's prefix whitespace linting!

@aljazerzen aljazerzen merged commit c380265 into master Mar 27, 2024
24 checks passed
@aljazerzen aljazerzen deleted the alias-propagate-refs branch March 27, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants