Skip to content

Commit f6528be

Browse files
authored
Introduce database import and export protocol messages (#224)
## Release notes: usage and product changes Add `migration` protocol messages for usage in database import and database export operations: * a unidirectional `database_export` stream from a TypeDB server to export a specific database, similar to TypeDB 2.x; * a bidirectional `databases_import` stream between a client and a server to import an exported 2.x/3.x TypeDB database into a TypeDB 3.x server from a client. The format of migration items used for these operations is an extended version of TypeDB 2.x's migration items, so it is backward compatible with 2.x database files. **Important:** it's not intended to import 3.x databases into 2.x servers. ## Implementation Add `Migration { Item }` message. The format is an extended version of the [2.x protocol](https://github.com/typedb/typedb/blob/2.29.1/migrator/database/data.proto), so it contains "outdated" fields for its compatibility with old databases. Add `Migration { Export }` message. This operation consists of a single client `Req { database }` and multiple streamed server responses: 1. An initial response with the schema. 2. An unlimited number of migration items (multiple messages with multiple items in one message for potential optimizations) 3. A `Done` message to signal that the server is ready to close the stream without errors. It can be substituted by a silent stream closure, but I preferred explicitness here. Add `Migration { Import }` message. This operation consists of a stream of client requests: 1. An initial request with the name of the database and its schema string. 2. An unlimited number of migration items. 3. A `Done` message to signal that the client is finished without errors, and the server can perform the final validation. This `Done` message is required and cannot be removed because the client has to check whether there were finalization errors or not. and a stream of server responses (actually, there is either a single `Done` or a single error, but the stream is needed to return errors at any stage of the communication).
1 parent d4350c0 commit f6528be

File tree

14 files changed

+218
-39
lines changed

14 files changed

+218
-39
lines changed

grpc/java/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ java_grpc_library(
1313
"//proto:server-proto",
1414
"//proto:user-proto",
1515
"//proto:database-proto",
16+
"//proto:migration-proto",
17+
"//proto:error-proto",
1618
"//proto:answer-proto",
1719
"//proto:concept-proto",
1820
"//proto:connection-proto",

grpc/nodejs/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ ts_grpc_compile(
2929
"//proto:server-proto",
3030
"//proto:user-proto",
3131
"//proto:database-proto",
32+
"//proto:migration-proto",
33+
"//proto:error-proto",
3234
"//proto:answer-proto",
3335
"//proto:concept-proto",
3436
"//proto:connection-proto",

grpc/rust/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ rust_tonic_compile(
1616
"//proto:server-proto",
1717
"//proto:user-proto",
1818
"//proto:database-proto",
19+
"//proto:migration-proto",
20+
"//proto:error-proto",
1921
"//proto:answer-proto",
2022
"//proto:concept-proto",
2123
"//proto:connection-proto",

grpc/rust/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() -> std::io::Result<()> {
99
"../../proto/concept.proto",
1010
"../../proto/connection.proto",
1111
"../../proto/database.proto",
12+
"../../proto/error.proto",
13+
"../../proto/migration.proto",
1214
"../../proto/options.proto",
1315
"../../proto/query.proto",
1416
"../../proto/server.proto",

proto/BUILD

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ proto_library(
2727
proto_library(
2828
name = "database-proto",
2929
srcs = [":database.proto"],
30+
deps = [
31+
":migration-proto",
32+
],
33+
)
34+
35+
proto_library(
36+
name = "migration-proto",
37+
srcs = [":migration.proto"],
38+
deps = [":concept-proto"],
3039
)
3140

3241
proto_library(
@@ -70,17 +79,24 @@ proto_library(
7079
srcs = ["query.proto"],
7180
deps = [
7281
":answer-proto",
73-
":options-proto",
7482
":concept-proto",
83+
":error-proto",
84+
":options-proto",
7585
],
7686
)
7787

88+
proto_library(
89+
name = "error-proto",
90+
srcs = ["error.proto"],
91+
)
92+
7893
proto_library(
7994
name = "transaction-proto",
8095
srcs = ["transaction.proto"],
8196
deps = [
8297
":answer-proto",
8398
":concept-proto",
99+
":error-proto",
84100
":options-proto",
85101
":query-proto",
86102
]

proto/answer.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package typedb.protocol;
1010

1111
message ConceptRow {
1212
repeated RowEntry row = 1;
13-
// Explainables explainables = 2;
1413
}
1514

1615
message RowEntry {

proto/concept.proto

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,16 @@ message Thing {
2929

3030
message Entity {
3131
bytes iid = 1;
32-
// TODO: not sure we should include types by default, since now they can get quite large with annotations included in every single response!
33-
// perhaps it's a query option?
3432
optional EntityType entity_type = 2;
3533
}
3634

3735
message Relation {
3836
bytes iid = 1;
39-
// TODO: not sure we should include types by default, since now they can get quite large with annotations included in every single response!
40-
// perhaps it's a query option?
4137
optional RelationType relation_type = 2;
4238
}
4339

4440
message Attribute {
4541
bytes iid = 1;
46-
// TODO: not sure we should include types by default, since now they can get quite large with annotations included in every single response!
47-
// perhaps it's a query option?
4842
optional AttributeType attribute_type = 2;
4943
Value value = 3;
5044
}
@@ -57,7 +51,6 @@ message Value {
5751
Decimal decimal = 4;
5852
string string = 5;
5953
Date date = 6;
60-
// time since epoch in milliseconds
6154
Datetime datetime = 7;
6255
Datetime_TZ datetime_tz = 8;
6356
Duration duration = 9;
@@ -152,14 +145,3 @@ message ValueType {
152145
string name = 1;
153146
};
154147
}
155-
156-
message Annotation {
157-
oneof annotation {
158-
Key key = 1;
159-
Unique unique = 2;
160-
// TODO
161-
}
162-
163-
message Key {}
164-
message Unique {}
165-
}

proto/database.proto

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ syntax = "proto3";
66

77
package typedb.protocol;
88

9+
import "proto/migration.proto";
10+
911
message DatabaseManager {
1012

1113
message Get {
1214
message Req {
1315
string name = 1;
1416
}
17+
1518
message Res {
1619
DatabaseReplicas database = 1;
1720
}
@@ -28,6 +31,7 @@ message DatabaseManager {
2831
message Req {
2932
string name = 1;
3033
}
34+
3135
message Res {
3236
bool contains = 1;
3337
}
@@ -37,10 +41,21 @@ message DatabaseManager {
3741
message Req {
3842
string name = 1;
3943
}
44+
4045
message Res {
4146
DatabaseReplicas database = 1;
4247
}
4348
}
49+
50+
message Import {
51+
message Client {
52+
Migration.Import.Client client = 1;
53+
}
54+
55+
message Server {
56+
Migration.Import.Server server = 1;
57+
}
58+
}
4459
}
4560

4661
message DatabaseReplicas {
@@ -62,6 +77,7 @@ message Database {
6277
message Req {
6378
string name = 1;
6479
}
80+
6581
message Res {
6682
string schema = 1;
6783
}
@@ -71,19 +87,21 @@ message Database {
7187
message Req {
7288
string name = 1;
7389
}
90+
7491
message Res {
7592
string schema = 1;
7693
}
7794
}
7895

79-
// message RuleSchema {
80-
// message Req {
81-
// string name = 1;
82-
// }
83-
// message Res {
84-
// string schema = 1;
85-
// }
86-
// }
96+
message Export {
97+
message Req {
98+
Migration.Export.Req req = 1;
99+
}
100+
101+
message Server {
102+
Migration.Export.Server server = 1;
103+
}
104+
}
87105

88106
message Delete {
89107
message Req {

proto/error.proto

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
syntax = "proto3";
6+
7+
package typedb.protocol;
8+
9+
// This is an emulation of the google ErrorDetails message. Generally, ErrorDetails are submitted via the GRPC error
10+
// mechanism, but a manual error sending is required in streams
11+
message Error {
12+
string error_code = 1;
13+
string domain = 2;
14+
repeated string stack_trace = 3;
15+
}

proto/migration.proto

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
syntax = "proto3";
6+
7+
package typedb.protocol;
8+
9+
import "proto/concept.proto";
10+
11+
message Migration {
12+
message Export {
13+
message Req {
14+
string name = 1;
15+
}
16+
17+
message Server {
18+
oneof server {
19+
InitialRes initial_res = 1;
20+
ResPart res_part = 2;
21+
Done done = 3;
22+
}
23+
}
24+
25+
message InitialRes {
26+
string schema = 1;
27+
}
28+
29+
message ResPart {
30+
repeated Item items = 1;
31+
}
32+
33+
message Done {}
34+
}
35+
36+
message Import {
37+
message Client {
38+
oneof client {
39+
InitialReq initial_req = 1;
40+
ReqPart req_part = 2;
41+
Done done = 3;
42+
}
43+
44+
message InitialReq {
45+
string name = 1;
46+
string schema = 2;
47+
}
48+
49+
message ReqPart {
50+
repeated Item items = 1;
51+
}
52+
53+
message Done { }
54+
}
55+
56+
message Server {
57+
Done done = 1;
58+
59+
message Done { }
60+
}
61+
}
62+
63+
// _ _____ _____ _____ _ _ _____ ___ ___ _ _ _
64+
// / \|_ _|_ _| ____| \ | |_ _|_ _/ _ \| \ | | |
65+
// / _ \ | | | | | _| | \| | | | | | | | | \| | |
66+
// / ___ \| | | | | |___| |\ | | | | | |_| | |\ |_|
67+
// /_/ \_\_| |_| |_____|_| \_| |_| |___\___/|_| \_(_)
68+
//
69+
// ATTENTION: the messages below are used to import multiple versions of TypeDB.
70+
// DO NOT reorder or delete existing and reserved indices. Be careful while extending this.
71+
//
72+
73+
message Item {
74+
oneof item {
75+
Attribute attribute = 1;
76+
Entity entity = 2;
77+
Relation relation = 3;
78+
79+
Header header = 15;
80+
Checksums checksums = 16;
81+
}
82+
83+
message Entity {
84+
string id = 1;
85+
string label = 2;
86+
repeated OwnedAttribute attributes = 3;
87+
}
88+
89+
message Attribute {
90+
string id = 1;
91+
string label = 2;
92+
repeated OwnedAttribute attributes = 3; // empty in 3.x, used for backwards compatibility
93+
MigrationValue value = 4;
94+
}
95+
96+
message Relation {
97+
string id = 1;
98+
string label = 2;
99+
repeated OwnedAttribute attributes = 3;
100+
repeated Role roles = 4;
101+
message Role {
102+
string label = 1;
103+
repeated Player players = 2;
104+
message Player {
105+
string id = 1;
106+
}
107+
}
108+
}
109+
110+
message OwnedAttribute {
111+
string id = 1;
112+
}
113+
114+
message Header {
115+
string typedb_version = 1;
116+
string original_database = 2;
117+
}
118+
119+
message Checksums {
120+
int64 entity_count = 1;
121+
int64 attribute_count = 2;
122+
int64 relation_count = 3;
123+
int64 role_count = 4;
124+
int64 ownership_count = 5;
125+
reserved 6; // was deleted and cannot be used until a breaking change occurs
126+
}
127+
}
128+
129+
message MigrationValue {
130+
oneof value {
131+
string string = 1;
132+
bool boolean = 2;
133+
int64 integer = 3;
134+
double double = 4;
135+
int64 datetime_millis = 5; // compatibility with 2.x, milliseconds since epoch
136+
Value.Decimal decimal = 6;
137+
Value.Date date = 8;
138+
Value.Datetime datetime = 9;
139+
Value.Datetime_TZ datetime_tz = 10;
140+
Value.Duration duration = 11;
141+
Value.Struct struct = 12;
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)