Skip to content

Commit f13b696

Browse files
author
Spencer
authored
[6.x] Remove dependency on doc versions (#29906) (#30036)
Backports the following commits to 6.x: - Remove dependency on doc versions (#29906)
1 parent 41eebe6 commit f13b696

File tree

60 files changed

+790
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+790
-263
lines changed

src/legacy/core_plugins/elasticsearch/lib/__tests__/health_check.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ describe('plugins/elasticsearch', () => {
6161

6262
cluster = { callWithInternalUser: sinon.stub() };
6363
cluster.callWithInternalUser.withArgs('index', sinon.match.any).returns(Promise.resolve());
64-
cluster.callWithInternalUser.withArgs('create', sinon.match.any).returns(Promise.resolve({ _id: '1', _version: 1 }));
6564
cluster.callWithInternalUser.withArgs('mget', sinon.match.any).returns(Promise.resolve({ ok: true }));
6665
cluster.callWithInternalUser.withArgs('get', sinon.match.any).returns(Promise.resolve({ found: false }));
6766
cluster.callWithInternalUser.withArgs('search', sinon.match.any).returns(Promise.resolve({ hits: { hits: [] } }));

src/legacy/core_plugins/kibana/server/tutorials/apm/saved_objects/index_pattern.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
},
88
"id": "apm-*",
99
"type": "index-pattern",
10-
"version": 1
10+
"version": "1"
1111
}

src/legacy/core_plugins/kibana/server/tutorials/apm/saved_objects/saved_objects.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"id": "1ffc5e20-7827-11e7-8c47-65b845b5cfb3",
1313
"type": "visualization",
14-
"version": 1
14+
"version": "1"
1515
},
1616
{
1717
"attributes": {
@@ -26,7 +26,7 @@
2626
},
2727
"id": "1bdca740-7828-11e7-8c47-65b845b5cfb3",
2828
"type": "visualization",
29-
"version": 2
29+
"version": "2"
3030
},
3131
{
3232
"attributes": {
@@ -41,7 +41,7 @@
4141
},
4242
"id": "804ffc40-7828-11e7-8c47-65b845b5cfb3",
4343
"type": "visualization",
44-
"version": 1
44+
"version": "1"
4545
},
4646
{
4747
"attributes": {
@@ -59,7 +59,7 @@
5959
},
6060
"id": "8d3ed660-7828-11e7-8c47-65b845b5cfb3",
6161
"type": "dashboard",
62-
"version": 1
62+
"version": "1"
6363
},
6464
{
6565
"attributes": {
@@ -74,7 +74,7 @@
7474
},
7575
"id": "c618e4e0-7c69-11e7-aa55-3b0d52c71c60",
7676
"type": "visualization",
77-
"version": 1
77+
"version": "1"
7878
},
7979
{
8080
"attributes": {
@@ -100,7 +100,7 @@
100100
},
101101
"id": "ceefd050-7c6a-11e7-aa55-3b0d52c71c60",
102102
"type": "search",
103-
"version": 1
103+
"version": "1"
104104
},
105105
{
106106
"attributes": {
@@ -118,7 +118,7 @@
118118
},
119119
"id": "5f08a870-7c6a-11e7-aa55-3b0d52c71c60",
120120
"type": "dashboard",
121-
"version": 1
121+
"version": "1"
122122
},
123123
{
124124
"attributes": {
@@ -133,7 +133,7 @@
133133
},
134134
"id": "22518e70-7c69-11e7-aa55-3b0d52c71c60",
135135
"type": "visualization",
136-
"version": 1
136+
"version": "1"
137137
},
138138
{
139139
"attributes": {
@@ -151,7 +151,7 @@
151151
},
152152
"id": "37f6fac0-7c6a-11e7-aa55-3b0d52c71c60",
153153
"type": "dashboard",
154-
"version": 1
154+
"version": "1"
155155
},
156156
{
157157
"attributes": {
@@ -166,7 +166,7 @@
166166
},
167167
"id": "a2e199b0-7820-11e7-8c47-65b845b5cfb3",
168168
"type": "visualization",
169-
"version": 1
169+
"version": "1"
170170
},
171171
{
172172
"attributes": {
@@ -181,7 +181,7 @@
181181
},
182182
"id": "09bcf890-7822-11e7-8c47-65b845b5cfb3",
183183
"type": "visualization",
184-
"version": 2
184+
"version": "2"
185185
},
186186
{
187187
"attributes": {
@@ -196,7 +196,7 @@
196196
},
197197
"id": "55606a60-7823-11e7-8c47-65b845b5cfb3",
198198
"type": "visualization",
199-
"version": 1
199+
"version": "1"
200200
},
201201
{
202202
"attributes": {
@@ -214,7 +214,7 @@
214214
},
215215
"id": "41b5d920-7821-11e7-8c47-65b845b5cfb3",
216216
"type": "dashboard",
217-
"version": 1
217+
"version": "1"
218218
},
219219
{
220220
"attributes": {
@@ -238,7 +238,7 @@
238238
},
239239
"id": "d7735b90-7ddf-11e7-b115-df9c90da2df1",
240240
"type": "search",
241-
"version": 1
241+
"version": "1"
242242
},
243243
{
244244
"attributes": {
@@ -256,5 +256,5 @@
256256
},
257257
"id": "3e3de700-7de0-11e7-b115-df9c90da2df1",
258258
"type": "dashboard",
259-
"version": 2
259+
"version": "2"
260260
}]

src/server/saved_objects/routes/bulk_create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const createBulkCreateRoute = prereqs => ({
3535
type: Joi.string().required(),
3636
id: Joi.string(),
3737
attributes: Joi.object().required(),
38-
version: Joi.number(),
38+
version: Joi.string(),
3939
migrationVersion: Joi.object().optional(),
4040
}).required()
4141
),

src/server/saved_objects/routes/bulk_get.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('POST /api/saved_objects/_bulk_get', () => {
5959
id: 'abc123',
6060
type: 'index-pattern',
6161
title: 'logstash-*',
62-
version: 2
62+
version: 'foo',
6363
}]
6464
};
6565

src/server/saved_objects/routes/update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createUpdateRoute = (prereqs) => {
3232
}).required(),
3333
payload: Joi.object({
3434
attributes: Joi.object().required(),
35-
version: Joi.number().min(1)
35+
version: Joi.string(),
3636
}).required()
3737
},
3838
handler(request) {

src/server/saved_objects/routes/update.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('PUT /api/saved_objects/{type}/{id?}', () => {
6666

6767
it('calls upon savedObjectClient.update', async () => {
6868
const attributes = { title: 'Testing' };
69-
const options = { version: 2 };
69+
const options = { version: 'foo' };
7070
const request = {
7171
method: 'PUT',
7272
url: '/api/saved_objects/index-pattern/logstash-*',

src/server/saved_objects/serialization/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import uuid from 'uuid';
2626
import { SavedObjectsSchema } from '../schema';
27+
import { decodeVersion, encodeVersion } from '../version';
2728

2829
/**
2930
* The root document type. In 7.0, this needs to change to '_doc'.
@@ -37,7 +38,8 @@ export interface RawDoc {
3738
_id: string;
3839
_source: any;
3940
_type?: string;
40-
_version?: number;
41+
_seq_no?: number;
42+
_primary_term?: number;
4143
}
4244

4345
/**
@@ -60,7 +62,7 @@ export interface SavedObjectDoc {
6062
type: string;
6163
namespace?: string;
6264
migrationVersion?: MigrationVersion;
63-
version?: number;
65+
version?: string;
6466
updated_at?: Date;
6567

6668
[rootProp: string]: any;
@@ -99,16 +101,22 @@ export class SavedObjectsSerializer {
99101
*
100102
* @param {RawDoc} rawDoc - The raw ES document to be converted to saved object format.
101103
*/
102-
public rawToSavedObject({ _id, _source, _version }: RawDoc): SavedObjectDoc {
104+
public rawToSavedObject({ _id, _source, _seq_no, _primary_term }: RawDoc): SavedObjectDoc {
103105
const { type, namespace } = _source;
106+
107+
const version =
108+
_seq_no != null || _primary_term != null
109+
? encodeVersion(_seq_no!, _primary_term!)
110+
: undefined;
111+
104112
return {
105113
type,
106114
id: this.trimIdPrefix(namespace, type, _id),
107115
...(namespace && !this.schema.isNamespaceAgnostic(type) && { namespace }),
108116
attributes: _source[type],
109117
...(_source.migrationVersion && { migrationVersion: _source.migrationVersion }),
110118
...(_source.updated_at && { updated_at: _source.updated_at }),
111-
...(_version != null && { version: _version }),
119+
...(version && { version }),
112120
};
113121
}
114122

@@ -131,7 +139,7 @@ export class SavedObjectsSerializer {
131139
_id: this.generateRawId(namespace, type, id),
132140
_source: source,
133141
_type: ROOT_TYPE,
134-
...(version != null && { _version: version }),
142+
...(version != null && decodeVersion(version)),
135143
};
136144
}
137145

src/server/saved_objects/serialization/serialization.test.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import _ from 'lodash';
2121
import { ROOT_TYPE, SavedObjectsSerializer } from '.';
2222
import { SavedObjectsSchema } from '../schema';
23+
import { encodeVersion } from '../version';
2324

2425
describe('saved object conversion', () => {
2526
describe('#rawToSavedObject', () => {
@@ -69,7 +70,8 @@ describe('saved object conversion', () => {
6970
const actual = serializer.rawToSavedObject({
7071
_id: 'hello:world',
7172
_type: ROOT_TYPE,
72-
_version: 3,
73+
_seq_no: 3,
74+
_primary_term: 1,
7375
_source: {
7476
type: 'hello',
7577
hello: {
@@ -86,7 +88,7 @@ describe('saved object conversion', () => {
8688
const expected = {
8789
id: 'world',
8890
type: 'hello',
89-
version: 3,
91+
version: encodeVersion(3, 1),
9092
attributes: {
9193
a: 'b',
9294
c: 'd',
@@ -112,17 +114,46 @@ describe('saved object conversion', () => {
112114
expect(actual).not.toHaveProperty('version');
113115
});
114116

115-
test(`if specified it copies _version to version`, () => {
117+
test(`if specified it encodes _seq_no and _primary_term to version`, () => {
116118
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
117119
const actual = serializer.rawToSavedObject({
118120
_id: 'foo:bar',
119-
_version: 4,
121+
_seq_no: 4,
122+
_primary_term: 1,
120123
_source: {
121124
type: 'foo',
122125
hello: {},
123126
},
124127
});
125-
expect(actual).toHaveProperty('version', 4);
128+
expect(actual).toHaveProperty('version', encodeVersion(4, 1));
129+
});
130+
131+
test(`if only _seq_no is specified it throws`, () => {
132+
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
133+
expect(() =>
134+
serializer.rawToSavedObject({
135+
_id: 'foo:bar',
136+
_seq_no: 4,
137+
_source: {
138+
type: 'foo',
139+
hello: {},
140+
},
141+
})
142+
).toThrowErrorMatchingInlineSnapshot(`"_primary_term from elasticsearch must be an integer"`);
143+
});
144+
145+
test(`if only _primary_term is throws`, () => {
146+
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
147+
expect(() =>
148+
serializer.rawToSavedObject({
149+
_id: 'foo:bar',
150+
_primary_term: 1,
151+
_source: {
152+
type: 'foo',
153+
hello: {},
154+
},
155+
})
156+
).toThrowErrorMatchingInlineSnapshot(`"_seq_no from elasticsearch must be an integer"`);
126157
});
127158

128159
test('if specified it copies the _source.updated_at property to updated_at', () => {
@@ -206,7 +237,8 @@ describe('saved object conversion', () => {
206237
const raw = {
207238
_id: 'foo-namespace:foo:bar',
208239
_type: ROOT_TYPE,
209-
_version: 24,
240+
_primary_term: 24,
241+
_seq_no: 42,
210242
_source: {
211243
type: 'foo',
212244
foo: {
@@ -440,25 +472,38 @@ describe('saved object conversion', () => {
440472
expect(actual._source).not.toHaveProperty('migrationVersion');
441473
});
442474

443-
test('it copies the version property to _version', () => {
475+
test('it decodes the version property to _seq_no and _primary_term', () => {
444476
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
445477
const actual = serializer.savedObjectToRaw({
446478
type: '',
447479
attributes: {},
448-
version: 4,
480+
version: encodeVersion(1, 2),
449481
} as any);
450482

451-
expect(actual).toHaveProperty('_version', 4);
483+
expect(actual).toHaveProperty('_seq_no', 1);
484+
expect(actual).toHaveProperty('_primary_term', 2);
452485
});
453486

454-
test(`if unspecified it doesn't add _version property`, () => {
487+
test(`if unspecified it doesn't add _seq_no or _primary_term properties`, () => {
455488
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
456489
const actual = serializer.savedObjectToRaw({
457490
type: '',
458491
attributes: {},
459492
} as any);
460493

461-
expect(actual).not.toHaveProperty('_version');
494+
expect(actual).not.toHaveProperty('_seq_no');
495+
expect(actual).not.toHaveProperty('_primary_term');
496+
});
497+
498+
test(`if version invalid it throws`, () => {
499+
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
500+
expect(() =>
501+
serializer.savedObjectToRaw({
502+
type: '',
503+
attributes: {},
504+
version: 'foo',
505+
} as any)
506+
).toThrowErrorMatchingInlineSnapshot(`"Invalid version [foo]"`);
462507
});
463508

464509
test('it copies attributes to _source[type]', () => {

src/server/saved_objects/service/lib/errors.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ export function isNotFoundError(maybeError: any): boolean;
2525
export function isConflictError(maybeError: any): boolean;
2626
export function isEsUnavailableError(maybeError: any): boolean;
2727
export function isEsAutoCreateIndexError(maybeError: any): boolean;
28+
29+
export function createInvalidVersionError(version: any): Error;
30+
export function isInvalidVersionError(maybeError: Error): boolean;

0 commit comments

Comments
 (0)