Skip to content

Commit 683be9a

Browse files
committed
Address PR feedback: restore new_deprecated_element factory method, tighten docs, expand defined_at test coverage
1 parent 72ed577 commit 683be9a

8 files changed

Lines changed: 59 additions & 35 deletions

File tree

elasticgraph-json_ingestion/lib/elastic_graph/json_ingestion/schema_definition/indexing/json_schema_with_metadata.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class Merger
5353

5454
def initialize(schema_def_results)
5555
@field_metadata_by_type_and_field_name = schema_def_results.json_schema_field_metadata_by_type_and_field_name
56-
@renamed_types_by_old_name = schema_def_results.state.renamed_types_by_old_name
57-
@deleted_types_by_old_name = schema_def_results.state.deleted_types_by_old_name
58-
@renamed_fields_by_type_name_and_old_field_name = schema_def_results.state.renamed_fields_by_type_name_and_old_field_name
59-
@deleted_fields_by_type_name_and_old_field_name = schema_def_results.state.deleted_fields_by_type_name_and_old_field_name
6056
@state = schema_def_results.state
57+
@renamed_types_by_old_name = @state.renamed_types_by_old_name
58+
@deleted_types_by_old_name = @state.deleted_types_by_old_name
59+
@renamed_fields_by_type_name_and_old_field_name = @state.renamed_fields_by_type_name_and_old_field_name
60+
@deleted_fields_by_type_name_and_old_field_name = @state.deleted_fields_by_type_name_and_old_field_name
6161
@derived_indexing_type_names = schema_def_results.derived_indexing_type_names
6262

6363
@unused_deprecated_elements = (

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def scalar_type(name, &block)
303303

304304
# Registers the name of a type that existed in a prior version of the schema but has been deleted.
305305
#
306-
# @note When `elasticgraph-json_ingestion` is in use and this API applies, ElasticGraph will give you an error message
306+
# @note In situations where this API is needed, ElasticGraph will give you an error message
307307
# indicating that you need to use this API or {SchemaElements::TypeWithSubfields#renamed_from}. Likewise, when
308308
# ElasticGraph no longer needs to know about this, it'll give you a warning indicating the call to this method can
309309
# be removed.

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/factory.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require "elastic_graph/schema_definition/schema_artifact_manager"
1818
require "elastic_graph/schema_definition/schema_elements/argument"
1919
require "elastic_graph/schema_definition/schema_elements/built_in_types"
20+
require "elastic_graph/schema_definition/schema_elements/deprecated_element"
2021
require "elastic_graph/schema_definition/schema_elements/directive"
2122
require "elastic_graph/schema_definition/schema_elements/enum_type"
2223
require "elastic_graph/schema_definition/schema_elements/enum_value"
@@ -79,6 +80,11 @@ def self.prevent_non_factory_instantiation_of(klass)
7980
end
8081
end
8182

83+
def new_deprecated_element(name, defined_at:, defined_via:)
84+
@@deprecated_element_new.call(schema_def_state: @state, name: name, defined_at: defined_at, defined_via: defined_via)
85+
end
86+
@@deprecated_element_new = prevent_non_factory_instantiation_of(SchemaElements::DeprecatedElement)
87+
8288
def new_argument(field, name, value_type)
8389
@@argument_new.call(@state, field, name, value_type).tap do |argument|
8490
yield argument if block_given?

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_elements/field.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,9 @@ def resolve_with(resolver_name, **config)
565565

566566
# Registers an old name that this field used to have in a prior version of the schema. Extensions
567567
# use this to deal with schema evolution — for example, `elasticgraph-json_ingestion` uses it to
568-
# migrate data ingested under the old JSON schema version.
568+
# ingest events that contain old field names that are no longer in the current schema.
569569
#
570-
# @note When `elasticgraph-json_ingestion` is in use and this API applies, ElasticGraph will give you an error message
570+
# @note In situations where this API is needed, ElasticGraph will give you an error message
571571
# indicating that you need to use this API or `deleted_field`. Likewise, when ElasticGraph no longer needs to know
572572
# about this, it'll give you a warning indicating the call to this method can be removed.
573573
#

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/schema_elements/type_with_subfields.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def relates_to_many(field_name, type, via:, dir:, singular: nil, indexing_only:
421421

422422
# Registers the name of a field that existed in a prior version of the schema but has been deleted.
423423
#
424-
# @note When `elasticgraph-json_ingestion` is in use and this API applies, ElasticGraph will give you an error message
424+
# @note In situations where this API is needed, ElasticGraph will give you an error message
425425
# indicating that you need to use this API or {Field#renamed_from}. Likewise, when ElasticGraph no longer needs to
426426
# know about this, it'll give you a warning indicating the call to this method can be removed.
427427
#
@@ -445,7 +445,7 @@ def deleted_field(field_name)
445445

446446
# Registers an old name that this type used to have in a prior version of the schema.
447447
#
448-
# @note When `elasticgraph-json_ingestion` is in use and this API applies, ElasticGraph will give you an error message
448+
# @note In situations where this API is needed, ElasticGraph will give you an error message
449449
# indicating that you need to use this API or {API#deleted_type}. Likewise, when ElasticGraph no longer needs to
450450
# know about this, it'll give you a warning indicating the call to this method can be removed.
451451
#

elasticgraph-schema_definition/lib/elastic_graph/schema_definition/state.rb

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
require "elastic_graph/errors"
1010
require "elastic_graph/schema_definition/factory"
1111
require "elastic_graph/schema_definition/mixins/has_readable_to_s_and_inspect"
12-
require "elastic_graph/schema_definition/schema_elements/deprecated_element"
1312
require "elastic_graph/schema_definition/schema_elements/enum_value_namer"
1413
require "elastic_graph/schema_definition/schema_elements/field_path"
1514
require "elastic_graph/schema_definition/schema_elements/sub_aggregation_path"
@@ -146,41 +145,21 @@ def register_user_defined_field(field)
146145
end
147146

148147
def register_renamed_type(type_name, from:, defined_at:, defined_via:)
149-
renamed_types_by_old_name[from] = SchemaElements::DeprecatedElement.new(
150-
schema_def_state: self,
151-
name: type_name,
152-
defined_at: defined_at,
153-
defined_via: defined_via
154-
)
148+
renamed_types_by_old_name[from] = factory.new_deprecated_element(type_name, defined_at: defined_at, defined_via: defined_via)
155149
end
156150

157151
def register_deleted_type(type_name, defined_at:, defined_via:)
158-
deleted_types_by_old_name[type_name] = SchemaElements::DeprecatedElement.new(
159-
schema_def_state: self,
160-
name: type_name,
161-
defined_at: defined_at,
162-
defined_via: defined_via
163-
)
152+
deleted_types_by_old_name[type_name] = factory.new_deprecated_element(type_name, defined_at: defined_at, defined_via: defined_via)
164153
end
165154

166155
def register_renamed_field(type_name, from:, to:, defined_at:, defined_via:)
167156
renamed_fields_by_old_field_name = renamed_fields_by_type_name_and_old_field_name[type_name]
168-
renamed_fields_by_old_field_name[from] = SchemaElements::DeprecatedElement.new(
169-
schema_def_state: self,
170-
name: to,
171-
defined_at: defined_at,
172-
defined_via: defined_via
173-
)
157+
renamed_fields_by_old_field_name[from] = factory.new_deprecated_element(to, defined_at: defined_at, defined_via: defined_via)
174158
end
175159

176160
def register_deleted_field(type_name, field_name, defined_at:, defined_via:)
177161
deleted_fields_by_old_field_name = deleted_fields_by_type_name_and_old_field_name[type_name]
178-
deleted_fields_by_old_field_name[field_name] = SchemaElements::DeprecatedElement.new(
179-
schema_def_state: self,
180-
name: field_name,
181-
defined_at: defined_at,
182-
defined_via: defined_via
183-
)
162+
deleted_fields_by_old_field_name[field_name] = factory.new_deprecated_element(field_name, defined_at: defined_at, defined_via: defined_via)
184163
end
185164

186165
def user_defined_field_references_by_type_name

elasticgraph-schema_definition/sig/elastic_graph/schema_definition/factory.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module ElasticGraph
77

88
def self.prevent_non_factory_instantiation_of: (::Class) -> ::Method
99

10+
def new_deprecated_element: (
11+
::String,
12+
defined_at: ::Thread::Backtrace::Location,
13+
defined_via: ::String
14+
) -> SchemaElements::DeprecatedElement
15+
@@deprecated_element_new: ::Method
16+
1017
def new_argument: (SchemaElements::Field, ::String, SchemaElements::TypeReference) ?{ (SchemaElements::Argument) -> void } -> SchemaElements::Argument
1118
@@argument_new: ::Method
1219

elasticgraph-schema_definition/spec/unit/elastic_graph/schema_definition/schema_elements/deprecated_element_spec.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,46 @@ module SchemaElements
4949
end
5050

5151
it "reports the caller's schema definition location (not ElasticGraph internals) as `defined_at`" do
52+
deleted_type_callsite = nil
53+
renamed_type_callsite = nil
54+
deleted_field_callsite = nil
55+
renamed_field_callsite = nil
56+
5257
state = define_schema(schema_element_name_form: "snake_case") do |schema|
58+
deleted_type_callsite = __LINE__ + 1
59+
schema.deleted_type "OldType"
60+
5361
schema.object_type "Widget" do |t|
62+
renamed_type_callsite = __LINE__ + 1
5463
t.renamed_from "OldWidget"
64+
65+
deleted_field_callsite = __LINE__ + 1
66+
t.deleted_field "legacy_name"
67+
5568
t.field "id", "ID!"
69+
t.field "name", "String" do |f|
70+
renamed_field_callsite = __LINE__ + 1
71+
f.renamed_from "old_name"
72+
end
5673
end
5774
end.state
5875

59-
expect(state.renamed_types_by_old_name.fetch("OldWidget").defined_at.path).to eq __FILE__
76+
expect(state.deleted_types_by_old_name.fetch("OldType").defined_at).to have_attributes(
77+
path: __FILE__,
78+
lineno: deleted_type_callsite
79+
)
80+
expect(state.renamed_types_by_old_name.fetch("OldWidget").defined_at).to have_attributes(
81+
path: __FILE__,
82+
lineno: renamed_type_callsite
83+
)
84+
expect(state.deleted_fields_by_type_name_and_old_field_name.fetch("Widget").fetch("legacy_name").defined_at).to have_attributes(
85+
path: __FILE__,
86+
lineno: deleted_field_callsite
87+
)
88+
expect(state.renamed_fields_by_type_name_and_old_field_name.fetch("Widget").fetch("old_name").defined_at).to have_attributes(
89+
path: __FILE__,
90+
lineno: renamed_field_callsite
91+
)
6092
end
6193
end
6294
end

0 commit comments

Comments
 (0)