Skip to content

Commit

Permalink
Add/fix fields in CREATE MIGRATION (#4550)
Browse files Browse the repository at this point in the history
1. `message` field is now reflected in `schema::Migration`
2. Added `generated_by` field to the migration
  • Loading branch information
tailhook committed Oct 21, 2022
1 parent 541e884 commit 748d873
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion edb/buildmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@


# Increment this whenever the database layout or stdlib changes.
EDGEDB_CATALOG_VERSION = 2022_10_17_00_00
EDGEDB_CATALOG_VERSION = 2022_10_19_00_00
EDGEDB_MAJOR_VERSION = 3


Expand Down
1 change: 0 additions & 1 deletion edb/edgeql/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,6 @@ class CreateMigration(CreateObject, MigrationCommand):

body: NestedQLBlock
parent: typing.Optional[ObjectRef] = None
message: typing.Optional[str] = None
metadata_only: bool = False


Expand Down
16 changes: 12 additions & 4 deletions edb/edgeql/parser/grammar/ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,21 @@ def result(self) -> typing.Any:
def _process_body(self, body):
fields = []
stmts = []
uniq_check = set()
for stmt in body:
if isinstance(stmt, qlast.SetField):
if stmt.name in self.allowed_fields:
fields.append(stmt)
else:
if stmt.name not in self.allowed_fields:
raise errors.InvalidSyntaxError(
f'unexpected field: {stmt.name!r}',
context=stmt.context,
)
if stmt.name in uniq_check:
raise errors.InvalidSyntaxError(
f'duplicate `SET {stmt.name} := ...`',
context=stmt.context,
)
uniq_check.add(stmt.name)
fields.append(stmt)
else:
stmts.append(stmt)

Expand Down Expand Up @@ -2818,7 +2824,7 @@ class CreateMigrationBodyBlock(NestedQLBlock):

@property
def allowed_fields(self) -> typing.FrozenSet[str]:
return frozenset({'message'})
return frozenset({'message', 'generated_by'})

@property
def result(self) -> typing.Any:
Expand Down Expand Up @@ -2869,6 +2875,7 @@ def reduce_CreateMigration(self, *kids):
name=kids[2].val.name,
parent=kids[2].val.parent,
body=kids[3].val.body,
commands=kids[3].val.fields,
)

def reduce_CreateAppliedMigration(self, *kids):
Expand All @@ -2881,6 +2888,7 @@ def reduce_CreateAppliedMigration(self, *kids):
parent=kids[3].val.parent,
body=kids[4].val.body,
metadata_only=True,
commands=kids[4].val.fields,
)


Expand Down
3 changes: 3 additions & 0 deletions edb/lib/schema.edgeql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ CREATE SCALAR TYPE schema::AccessPolicyAction
CREATE SCALAR TYPE schema::AccessKind
EXTENDING enum<`Select`, UpdateRead, UpdateWrite, `Delete`, `Insert`>;

CREATE SCALAR TYPE schema::MigrationGeneratedBy
EXTENDING enum<DevMode, DDLStatement>;

# Base type for all schema entities.
CREATE ABSTRACT TYPE schema::Object EXTENDING std::BaseObject {
Expand Down Expand Up @@ -494,6 +496,7 @@ CREATE TYPE schema::Migration
CREATE MULTI LINK parents -> schema::Migration;
CREATE REQUIRED PROPERTY script -> str;
CREATE PROPERTY message -> str;
CREATE PROPERTY generated_by -> schema::MigrationGeneratedBy;
};


Expand Down
6 changes: 6 additions & 0 deletions edb/schema/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class Migration(
allow_ddl_set=True,
)

generated_by = so.SchemaField(
str,
default=None,
allow_ddl_set=True,
)

script = so.SchemaField(
str,
)
Expand Down
5 changes: 5 additions & 0 deletions edb/server/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,11 @@ def _compile_and_apply_ddl_stmt(
body=qlast.NestedQLBlock(
commands=[stmt],
),
generated_by=qlast.Path(steps=[
qlast.ObjectRef(name='MigrationGeneratedBy',
module='schema'),
qlast.Ptr(ptr=qlast.ObjectRef(name='DDLStatement')),
]),
)
return self._compile_and_apply_ddl_stmt(ctx, cm)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_edgeql_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12070,6 +12070,26 @@ async def test_edgeql_ddl_create_migration_02(self):
};
''')

async def test_edgeql_ddl_create_migration_03(self):
await self.con.execute(f'''
CREATE MIGRATION
{{
SET message := "migration2";
SET generated_by := schema::MigrationGeneratedBy.DevMode;
CREATE TYPE Type2 {{
CREATE PROPERTY field2 -> int32;
}};
}};
''')

await self.assert_query_result(
'''
SELECT schema::Migration { generated_by }
FILTER .message = "migration2"
''',
[{'generated_by': 'DevMode'}]
)

async def test_edgeql_ddl_naked_backlink_in_computable(self):
await self.con.execute('''
CREATE TYPE User {
Expand Down

0 comments on commit 748d873

Please sign in to comment.