neo4j_bolt 0.4.5 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +52 -12
- data/bin/neo4j_bolt +16 -5
- data/lib/neo4j_bolt/version.rb +1 -1
- data/lib/neo4j_bolt.rb +176 -7
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f9f38fc0e16014ae13f2f31d2ba77f119185249821da7d4f7858fa0ef3f9010f
|
|
4
|
+
data.tar.gz: 5ab718f1dd9829aa1f118a6614f7d29d184d63eb20b4ce7d3e36fc92fea5f204
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: df61ebc16c46698ccab04c94e9a8abd6120ef07d899b7e52e8e9a80d13bcc4b2f8d17bddff330f6d10ff794faabd705ebfba3d3531fdad311cb8ea5de02dbc8c
|
|
7
|
+
data.tar.gz: 6522b70e44f279f182c289ae3397dafd11d5dff4e72a3437247a5f25bd66d4185e6696b2aa2c96bce976f518d63a30c98c48ea86b2823d28383c3989df24b7eb
|
data/README.md
CHANGED
|
@@ -4,14 +4,14 @@ Neo4jBolt 0.4 is a small compatibility and convenience layer for Ruby applicatio
|
|
|
4
4
|
|
|
5
5
|
Neo4jBolt no longer implements the Bolt wire protocol itself.
|
|
6
6
|
|
|
7
|
-
`0.
|
|
7
|
+
`0.5.0` requires Ruby 3.4 or newer and pins `neo4j-ruby-driver` to `6.2.1.beta.4`. Applications on older Rubies can remain on the Neo4jBolt 0.3.x line; this prerelease is intentionally not an automatic upgrade for them.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
11
|
For this prerelease, specify the version explicitly:
|
|
12
12
|
|
|
13
13
|
```ruby
|
|
14
|
-
gem "neo4j_bolt", "0.
|
|
14
|
+
gem "neo4j_bolt", "0.5.0"
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
Then run `bundle install`. A running Neo4j database is required.
|
|
@@ -163,14 +163,31 @@ Managed entries retain the `neo4j_bolt_` prefix. Setup removes obsolete entries
|
|
|
163
163
|
|
|
164
164
|
## Dump and load
|
|
165
165
|
|
|
166
|
-
The textual format
|
|
166
|
+
The textual format remains line-oriented and backwards compatible. Dumps now include supported schema entries by default:
|
|
167
167
|
|
|
168
168
|
```text
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
u {"name":"person_email","entity_type":"NODE","label_or_type":"Person","properties":["email"]}
|
|
170
|
+
k {"name":"person_id","entity_type":"NODE","label_or_type":"Person","properties":["id"]}
|
|
171
|
+
i {"name":"person_name","entity_type":"NODE","label_or_type":"Person","properties":["name"]}
|
|
172
|
+
n {"id":0,"labels":["Person"],"properties":{"id":1,"name":"Ada","email":"ada@example.test"}}
|
|
173
|
+
n {"id":1,"labels":["Person"],"properties":{"id":2,"name":"Grace","email":"grace@example.test"}}
|
|
171
174
|
r {"from":0,"to":1,"type":"KNOWS","properties":{"since":2024}}
|
|
172
175
|
```
|
|
173
176
|
|
|
177
|
+
The record prefixes are:
|
|
178
|
+
|
|
179
|
+
- `u` — property uniqueness constraint
|
|
180
|
+
- `k` — node or relationship key constraint
|
|
181
|
+
- `i` — ordinary property index
|
|
182
|
+
- `n` — node
|
|
183
|
+
- `r` — relationship
|
|
184
|
+
|
|
185
|
+
Schema records preserve the schema entry name, whether it belongs to nodes or relationships, the label or relationship type, and the indexed/constrained properties. Constraint-backing indexes are not written as separate `i` records. Ordinary Neo4j 4.4 `BTREE` indexes and modern `RANGE` indexes are treated as the same portable property-index concept, so a logical dump can move between supported Neo4j generations. Neo4j's token lookup indexes are intentionally omitted because they are database infrastructure rather than application property indexes.
|
|
186
|
+
|
|
187
|
+
Other constraint or index types are not silently dropped. A schema dump fails with a clear error instead; use data-only dumping if the application intentionally needs only nodes and relationships.
|
|
188
|
+
|
|
189
|
+
Key constraints are dumped when they exist. Loading a dump containing a `k` record requires a Neo4j edition/version that can create that key; otherwise the load fails instead of silently weakening the schema.
|
|
190
|
+
|
|
174
191
|
```ruby
|
|
175
192
|
File.open("database.dump", "w") { |io| dump_database(io, progress_io: $stderr) }
|
|
176
193
|
File.open("database.dump", "r") do |io|
|
|
@@ -178,6 +195,13 @@ File.open("database.dump", "r") do |io|
|
|
|
178
195
|
end
|
|
179
196
|
```
|
|
180
197
|
|
|
198
|
+
Schema dumping and restoration can be disabled explicitly:
|
|
199
|
+
|
|
200
|
+
```ruby
|
|
201
|
+
File.open("data.dump", "w") { |io| dump_database(io, schema: false) }
|
|
202
|
+
File.open("database.dump", "r") { |io| load_database_dump(io, schema: false) }
|
|
203
|
+
```
|
|
204
|
+
|
|
181
205
|
Passing `progress_io:` is optional for the Ruby API. The CLI always reports dump/load progress on stderr, so dump data written to stdout remains safe to redirect or pipe. In `auto` mode a terminal gets a colored, in-place progress bar while redirected stderr receives periodic plain-text progress lines. Set `NEO4J_BOLT_PROGRESS=pretty` to force the terminal display through a container or other wrapper that hides the TTY, or `NEO4J_BOLT_PROGRESS=plain` to force log-friendly output. `NO_COLOR` disables colors without disabling the in-place display.
|
|
182
206
|
|
|
183
207
|
Both `dump` and `load` accept `--color COLOR` to choose the spinner and filled progress-bar accent. The default is `cyan`. The Ruby API exposes the same setting as `progress_color:`. Available colors are `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `bright-red`, `bright-green`, `bright-yellow`, `bright-blue`, `bright-magenta`, `bright-cyan`, and `bright-white`.
|
|
@@ -187,13 +211,24 @@ neo4j_bolt dump --color magenta -o database.dump
|
|
|
187
211
|
neo4j_bolt load --color bright-blue database.dump
|
|
188
212
|
```
|
|
189
213
|
|
|
190
|
-
|
|
214
|
+
Both commands also accept `--data-only`. On `dump`, it omits `u`, `k`, and `i` records. On `load`, it accepts a full dump but ignores its schema records:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
neo4j_bolt dump --data-only -o data.dump
|
|
218
|
+
neo4j_bolt load --data-only database.dump
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The `0, 1, 2, ...` IDs are synthetic dump-local IDs. Database-internal IDs and element IDs are never written to the persistent format. Dumping keeps the count, node, and relationship reads in one transaction, so the driver-provided entity identity used to connect the two streamed result sets stays within Neo4j's transaction-scoped identity guarantee. To keep dump numbering deterministic, ordering uses `elementId()` on modern Neo4j and an internal `id()` fallback only on Neo4j 4.4, where `elementId()` does not exist. Old Neo4jBolt dumps containing only `n` and `r` records remain loadable.
|
|
191
222
|
|
|
192
223
|
For relational loads, the adapter assigns a random temporary label and the fixed reserved property `__neo4j_bolt_load_id` to imported nodes. After the nodes are committed it builds a temporary `neo4j_bolt_` index on that property, uses indexed lookups while creating relationships, drops the index, and removes the temporary metadata in batches. The fixed property key is reused by every load instead of registering a new random property-key token each time. Neo4j can keep the property key registered after all values have been removed, so it may remain visible in Browser's property-key list; no imported node retains the property after successful cleanup. Relational loads refuse to run if the dump itself, or existing data during `force_append`, currently uses the reserved property. No database-internal identity is carried from one load transaction to another. Loading a relational dump therefore requires permission to create and drop an index.
|
|
193
224
|
|
|
194
225
|
Loads start with batches of 5,000 records by default. This is an initial ceiling rather than a claimed optimum: if Neo4j returns a transaction-memory/resource error whose server semantics guarantee rollback, the loader halves the failed batch and retries it, then keeps the smaller size for the rest of that phase. Node and relationship phases adapt independently. Other errors are not retried, because a generic connection failure cannot safely prove that a `CREATE` transaction did not commit. Callers can change the initial ceiling with `initial_batch_size:`; the CLI exposes the same setting as `--batch-size`.
|
|
195
226
|
|
|
196
|
-
Loading requires an empty database unless `force_append: true` is passed. `
|
|
227
|
+
Loading data requires an empty database unless `force_append: true` is passed. For a dump that contains `u`, `k`, or `i` records and is being loaded with schema enabled, the target must also have no application constraints or property indexes. Neo4j's built-in token lookup indexes do not count as application schema. Old `n`/`r`-only dumps, and loads using `schema: false` / `--data-only`, keep the old data-only emptiness rule and may load into a data-empty database that already has application schema.
|
|
228
|
+
|
|
229
|
+
With a normal full restore, the schema preflight happens before any data is written and the saved schema is created strictly after the data load. With `force_append: true`, existing schema is allowed and restored schema entries use `IF NOT EXISTS`. Existing constraints and indexes are never removed by dump loading.
|
|
230
|
+
|
|
231
|
+
Data is loaded before schema records are restored, so bulk loading does not pay constraint/index maintenance costs for every inserted record. `load_database_dump` owns its batch transactions and therefore rejects being called from inside `transaction`.
|
|
197
232
|
|
|
198
233
|
## CLI
|
|
199
234
|
|
|
@@ -203,8 +238,8 @@ The `neo4j_bolt` executable retains these commands:
|
|
|
203
238
|
| --- | --- |
|
|
204
239
|
| `neo4j_bolt console` | Open an IRB console with Neo4jBolt loaded |
|
|
205
240
|
| `neo4j_bolt clear --srsly` | Delete all nodes and relationships |
|
|
206
|
-
| `neo4j_bolt dump [--color COLOR]` | Write the textual database dump |
|
|
207
|
-
| `neo4j_bolt load [--force] [--batch-size N] [--color COLOR] PATH` | Load a textual dump |
|
|
241
|
+
| `neo4j_bolt dump [--data-only] [--color COLOR]` | Write the textual database dump |
|
|
242
|
+
| `neo4j_bolt load [--force] [--data-only] [--batch-size N] [--color COLOR] PATH` | Load a textual dump |
|
|
208
243
|
| `neo4j_bolt index ls` | List constraints and indexes |
|
|
209
244
|
| `neo4j_bolt index rm --force` | Remove all constraints and indexes |
|
|
210
245
|
| `neo4j_bolt visualize` | Generate a GraphViz document |
|
|
@@ -213,13 +248,18 @@ Use `--host HOST:PORT`, `--username USER`, `--password PASSWORD`, and `--databas
|
|
|
213
248
|
|
|
214
249
|
## Tested Neo4j versions
|
|
215
250
|
|
|
216
|
-
The
|
|
251
|
+
The complete integration suite is run against both Community and Enterprise for each pinned Neo4j generation:
|
|
217
252
|
|
|
218
253
|
- `neo4j:4.4.48-community`
|
|
254
|
+
- `neo4j:4.4.48-enterprise`
|
|
219
255
|
- `neo4j:5.26.28-community`
|
|
256
|
+
- `neo4j:5.26.28-enterprise`
|
|
220
257
|
- `neo4j:2026.06.0-community`
|
|
258
|
+
- `neo4j:2026.06.0-enterprise`
|
|
259
|
+
|
|
260
|
+
No compatibility beyond this matrix is claimed for `0.5.0`.
|
|
221
261
|
|
|
222
|
-
|
|
262
|
+
Enterprise test containers are started with `NEO4J_ACCEPT_LICENSE_AGREEMENT=yes`. The Community targets verify that Community-compatible features remain portable; the Enterprise targets additionally exercise successful node-key dump/load instead of merely observing that Community rejects node keys.
|
|
223
263
|
|
|
224
264
|
Run one modern LTS target:
|
|
225
265
|
|
|
@@ -257,7 +297,7 @@ To point RSpec itself at an already disposable database, set `NEO4J_BOLT_TEST_HO
|
|
|
257
297
|
| CLI commands | Preserved |
|
|
258
298
|
| `BoltSocket`, `BoltBuffer`, protocol markers/state/parser/packer | Intentionally removed private implementation details |
|
|
259
299
|
|
|
260
|
-
## 0.
|
|
300
|
+
## 0.5.0 migration notes
|
|
261
301
|
|
|
262
302
|
- Ruby 3.4 or newer is required; Ruby 2.x/3.0–3.3 applications should stay on 0.3.x until upgraded.
|
|
263
303
|
- The exact prerelease upstream dependency is pinned while no stable `neo4j-ruby-driver` 6.2.x exists.
|
data/bin/neo4j_bolt
CHANGED
|
@@ -53,13 +53,20 @@ class App
|
|
|
53
53
|
# --------------------------------------------
|
|
54
54
|
|
|
55
55
|
desc 'Dump database'
|
|
56
|
-
long_desc 'Dump
|
|
56
|
+
long_desc 'Dump nodes, relationships, constraints, and property indexes.'
|
|
57
57
|
command :dump do |c|
|
|
58
58
|
c.flag [:o, 'out-file'.to_sym], :default_value => '/dev/stdout'
|
|
59
59
|
c.flag [:color], :default_value => 'cyan', :desc => 'progress accent color'
|
|
60
|
+
c.switch ['data-only'.to_sym], :default_value => false, :negatable => false,
|
|
61
|
+
:desc => 'dump nodes and relationships only'
|
|
60
62
|
c.action do |global_options, options|
|
|
61
63
|
File.open(options['out-file'.to_sym], 'w') do |f|
|
|
62
|
-
dump_database(
|
|
64
|
+
dump_database(
|
|
65
|
+
f,
|
|
66
|
+
progress_io: $stderr,
|
|
67
|
+
progress_color: options[:color],
|
|
68
|
+
schema: !options['data-only'.to_sym]
|
|
69
|
+
)
|
|
63
70
|
end
|
|
64
71
|
end
|
|
65
72
|
end
|
|
@@ -67,13 +74,16 @@ class App
|
|
|
67
74
|
# --------------------------------------------
|
|
68
75
|
|
|
69
76
|
desc 'Load database dump'
|
|
70
|
-
long_desc 'Load nodes and
|
|
77
|
+
long_desc 'Load nodes, relationships, constraints, and property indexes from a database dump.'
|
|
71
78
|
command :load do |c|
|
|
72
79
|
# c.flag [:i, :in_file], :desc => 'input path', :required => true
|
|
73
|
-
c.switch [:f, :force], :default_value => false,
|
|
80
|
+
c.switch [:f, :force], :default_value => false,
|
|
81
|
+
:desc => 'force loading into a database that already contains data or application schema'
|
|
74
82
|
c.flag [:b, 'batch-size'.to_sym], :default_value => Neo4jBolt::LOAD_INITIAL_BATCH_SIZE,
|
|
75
83
|
:desc => 'initial batch size; automatically reduced on transaction-memory errors'
|
|
76
84
|
c.flag [:color], :default_value => 'cyan', :desc => 'progress accent color'
|
|
85
|
+
c.switch ['data-only'.to_sym], :default_value => false, :negatable => false,
|
|
86
|
+
:desc => 'ignore constraints and indexes stored in the dump'
|
|
77
87
|
c.action do |global_options, options, args|
|
|
78
88
|
help_now!('input path is required') if args.empty?
|
|
79
89
|
path = args.shift
|
|
@@ -83,7 +93,8 @@ class App
|
|
|
83
93
|
force_append: options[:force],
|
|
84
94
|
progress_io: $stderr,
|
|
85
95
|
progress_color: options[:color],
|
|
86
|
-
initial_batch_size: options['batch-size'.to_sym]
|
|
96
|
+
initial_batch_size: options['batch-size'.to_sym],
|
|
97
|
+
schema: !options['data-only'.to_sym]
|
|
87
98
|
)
|
|
88
99
|
end
|
|
89
100
|
end
|
data/lib/neo4j_bolt/version.rb
CHANGED
data/lib/neo4j_bolt.rb
CHANGED
|
@@ -594,11 +594,13 @@ module Neo4jBolt
|
|
|
594
594
|
nil
|
|
595
595
|
end
|
|
596
596
|
|
|
597
|
-
def dump_database(io, progress_io: nil, progress_color: "cyan")
|
|
597
|
+
def dump_database(io, progress_io: nil, progress_color: "cyan", schema: true)
|
|
598
598
|
progress = ProgressReporter.new(progress_io, color: progress_color)
|
|
599
599
|
dumped_nodes = 0
|
|
600
600
|
dumped_relationships = 0
|
|
601
601
|
|
|
602
|
+
dump_schema(io) if schema
|
|
603
|
+
|
|
602
604
|
transaction do
|
|
603
605
|
identity_function = dump_identity_function
|
|
604
606
|
total_nodes = neo4j_query_expect_one("MATCH (n) RETURN count(n) AS count")["count"]
|
|
@@ -655,7 +657,7 @@ module Neo4jBolt
|
|
|
655
657
|
end
|
|
656
658
|
|
|
657
659
|
def load_database_dump(io, force_append: false, progress_io: nil, progress_color: "cyan",
|
|
658
|
-
initial_batch_size: LOAD_INITIAL_BATCH_SIZE)
|
|
660
|
+
initial_batch_size: LOAD_INITIAL_BATCH_SIZE, schema: true)
|
|
659
661
|
raise Error, "load_database_dump cannot run inside a transaction" if transaction_context
|
|
660
662
|
|
|
661
663
|
initial_batch_size = Integer(initial_batch_size)
|
|
@@ -669,7 +671,12 @@ module Neo4jBolt
|
|
|
669
671
|
progress = ProgressReporter.new(progress_io, color: progress_color)
|
|
670
672
|
node_batches = Hash.new { |hash, key| hash[key] = [] }
|
|
671
673
|
relationship_batches = Hash.new { |hash, key| hash[key] = [] }
|
|
672
|
-
total_nodes, total_relationships
|
|
674
|
+
total_nodes, total_relationships, schema_entries =
|
|
675
|
+
parse_dump(io, node_batches, relationship_batches, progress)
|
|
676
|
+
|
|
677
|
+
if !force_append && schema && !schema_entries.empty?
|
|
678
|
+
ensure_database_has_no_application_schema!
|
|
679
|
+
end
|
|
673
680
|
|
|
674
681
|
temporary_token = SecureRandom.hex(12)
|
|
675
682
|
temporary_label = "__neo4j_bolt_load_#{temporary_token}"
|
|
@@ -802,6 +809,10 @@ module Neo4jBolt
|
|
|
802
809
|
raise cleanup_error if original_error.nil? && cleanup_error
|
|
803
810
|
end
|
|
804
811
|
|
|
812
|
+
if schema && !schema_entries.empty?
|
|
813
|
+
restore_dump_schema(schema_entries, progress, if_not_exists: force_append)
|
|
814
|
+
end
|
|
815
|
+
|
|
805
816
|
progress.finish("Loaded: #{loaded_nodes} nodes, #{loaded_relationships} relationships")
|
|
806
817
|
nil
|
|
807
818
|
ensure
|
|
@@ -927,6 +938,157 @@ module Neo4jBolt
|
|
|
927
938
|
"`#{identifier.to_s.gsub("`", "``")}`"
|
|
928
939
|
end
|
|
929
940
|
|
|
941
|
+
def dump_schema(io)
|
|
942
|
+
entries = []
|
|
943
|
+
constraint_names = Set.new
|
|
944
|
+
|
|
945
|
+
neo4j_query("SHOW CONSTRAINTS").each do |row|
|
|
946
|
+
kind = dump_constraint_kind(row["type"])
|
|
947
|
+
value = schema_dump_value(row, kind)
|
|
948
|
+
constraint_names << value.fetch(:name)
|
|
949
|
+
entries << [kind, value]
|
|
950
|
+
end
|
|
951
|
+
|
|
952
|
+
neo4j_query("SHOW INDEXES").each do |row|
|
|
953
|
+
next unless dumpable_property_index?(row, constraint_names)
|
|
954
|
+
|
|
955
|
+
entries << ["i", schema_dump_value(row, "i")]
|
|
956
|
+
end
|
|
957
|
+
|
|
958
|
+
kind_order = { "u" => 0, "k" => 1, "i" => 2 }
|
|
959
|
+
entries.sort_by { |kind, value| [kind_order.fetch(kind), value.fetch(:name)] }.each do |kind, value|
|
|
960
|
+
io.puts "#{kind} #{JSON.generate(value)}"
|
|
961
|
+
end
|
|
962
|
+
end
|
|
963
|
+
|
|
964
|
+
def dump_constraint_kind(type)
|
|
965
|
+
case type
|
|
966
|
+
when "UNIQUENESS", "NODE_PROPERTY_UNIQUENESS", "RELATIONSHIP_PROPERTY_UNIQUENESS"
|
|
967
|
+
"u"
|
|
968
|
+
when "NODE_KEY", "RELATIONSHIP_KEY"
|
|
969
|
+
"k"
|
|
970
|
+
else
|
|
971
|
+
raise Error,
|
|
972
|
+
"Cannot dump #{type.inspect} constraint; use schema: false (or --data-only in the CLI)"
|
|
973
|
+
end
|
|
974
|
+
end
|
|
975
|
+
|
|
976
|
+
def dumpable_property_index?(row, constraint_names)
|
|
977
|
+
return false if row["owningConstraint"]
|
|
978
|
+
return false if row["uniqueness"] == "UNIQUE"
|
|
979
|
+
return false if constraint_names.include?(row["name"])
|
|
980
|
+
return false if row["type"] == "LOOKUP"
|
|
981
|
+
return true if %w[BTREE RANGE].include?(row["type"])
|
|
982
|
+
|
|
983
|
+
raise Error,
|
|
984
|
+
"Cannot dump #{row['type'].inspect} index #{row['name'].inspect}; " \
|
|
985
|
+
"use schema: false (or --data-only in the CLI)"
|
|
986
|
+
end
|
|
987
|
+
|
|
988
|
+
def schema_dump_value(row, kind)
|
|
989
|
+
name = row["name"]
|
|
990
|
+
entity_type = row["entityType"]
|
|
991
|
+
labels_or_types = row["labelsOrTypes"]
|
|
992
|
+
properties = row["properties"]
|
|
993
|
+
|
|
994
|
+
unless name.is_a?(String) && !name.empty? &&
|
|
995
|
+
%w[NODE RELATIONSHIP].include?(entity_type) &&
|
|
996
|
+
labels_or_types.is_a?(Array) && labels_or_types.size == 1 &&
|
|
997
|
+
properties.is_a?(Array) && !properties.empty? &&
|
|
998
|
+
properties.all? { |property| property.is_a?(String) && !property.empty? }
|
|
999
|
+
raise Error, "Cannot dump #{kind} schema entry #{name.inspect}: unsupported schema shape"
|
|
1000
|
+
end
|
|
1001
|
+
|
|
1002
|
+
{
|
|
1003
|
+
name: name,
|
|
1004
|
+
entity_type: entity_type,
|
|
1005
|
+
label_or_type: labels_or_types.first,
|
|
1006
|
+
properties: properties
|
|
1007
|
+
}
|
|
1008
|
+
end
|
|
1009
|
+
|
|
1010
|
+
def restore_dump_schema(entries, progress, if_not_exists: false)
|
|
1011
|
+
progress&.note("Restoring schema: #{entries.size} entries...")
|
|
1012
|
+
entries.each do |kind, value|
|
|
1013
|
+
neo4j_query(schema_create_statement(kind, value, if_not_exists: if_not_exists))
|
|
1014
|
+
end
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
def schema_create_statement(kind, value, if_not_exists: false)
|
|
1018
|
+
validate_schema_dump_value!(kind, value)
|
|
1019
|
+
|
|
1020
|
+
name = quote_identifier(value.fetch("name"))
|
|
1021
|
+
if_not_exists_clause = if_not_exists ? " IF NOT EXISTS" : ""
|
|
1022
|
+
entity_type = value.fetch("entity_type")
|
|
1023
|
+
label_or_type = quote_identifier(value.fetch("label_or_type"))
|
|
1024
|
+
properties = value.fetch("properties")
|
|
1025
|
+
variable = entity_type == "NODE" ? "n" : "r"
|
|
1026
|
+
pattern = if entity_type == "NODE"
|
|
1027
|
+
"(#{variable}:#{label_or_type})"
|
|
1028
|
+
else
|
|
1029
|
+
"()-[#{variable}:#{label_or_type}]-()"
|
|
1030
|
+
end
|
|
1031
|
+
property_expressions = properties.map { |property| "#{variable}.#{quote_identifier(property)}" }
|
|
1032
|
+
|
|
1033
|
+
case kind
|
|
1034
|
+
when "u", "k"
|
|
1035
|
+
property_expression = if property_expressions.one?
|
|
1036
|
+
property_expressions.first
|
|
1037
|
+
else
|
|
1038
|
+
"(#{property_expressions.join(', ')})"
|
|
1039
|
+
end
|
|
1040
|
+
constraint_type = if kind == "u"
|
|
1041
|
+
"UNIQUE"
|
|
1042
|
+
elsif entity_type == "NODE"
|
|
1043
|
+
"NODE KEY"
|
|
1044
|
+
else
|
|
1045
|
+
"RELATIONSHIP KEY"
|
|
1046
|
+
end
|
|
1047
|
+
"CREATE CONSTRAINT #{name}#{if_not_exists_clause} FOR #{pattern} " \
|
|
1048
|
+
"REQUIRE #{property_expression} IS #{constraint_type}"
|
|
1049
|
+
when "i"
|
|
1050
|
+
"CREATE INDEX #{name}#{if_not_exists_clause} FOR #{pattern} ON (#{property_expressions.join(', ')})"
|
|
1051
|
+
else
|
|
1052
|
+
raise Error, "Unexpected schema dump kind: #{kind}"
|
|
1053
|
+
end
|
|
1054
|
+
end
|
|
1055
|
+
|
|
1056
|
+
def validate_schema_dump_value!(kind, value)
|
|
1057
|
+
unless %w[u k i].include?(kind) && value.is_a?(Hash)
|
|
1058
|
+
raise Error, "Invalid #{kind} schema dump entry"
|
|
1059
|
+
end
|
|
1060
|
+
|
|
1061
|
+
name = value.fetch("name")
|
|
1062
|
+
entity_type = value.fetch("entity_type")
|
|
1063
|
+
label_or_type = value.fetch("label_or_type")
|
|
1064
|
+
properties = value.fetch("properties")
|
|
1065
|
+
|
|
1066
|
+
return if name.is_a?(String) && !name.empty? &&
|
|
1067
|
+
%w[NODE RELATIONSHIP].include?(entity_type) &&
|
|
1068
|
+
label_or_type.is_a?(String) && !label_or_type.empty? &&
|
|
1069
|
+
properties.is_a?(Array) && !properties.empty? &&
|
|
1070
|
+
properties.all? { |property| property.is_a?(String) && !property.empty? }
|
|
1071
|
+
|
|
1072
|
+
raise Error, "Invalid #{kind} schema dump entry"
|
|
1073
|
+
end
|
|
1074
|
+
|
|
1075
|
+
def ensure_database_has_no_application_schema!
|
|
1076
|
+
constraints = neo4j_query("SHOW CONSTRAINTS").filter_map { |row| row["name"] }
|
|
1077
|
+
indexes = neo4j_query("SHOW INDEXES").filter_map do |row|
|
|
1078
|
+
next if row["type"] == "LOOKUP"
|
|
1079
|
+
next if row["owningConstraint"]
|
|
1080
|
+
next if row["uniqueness"] == "UNIQUE"
|
|
1081
|
+
|
|
1082
|
+
row["name"]
|
|
1083
|
+
end
|
|
1084
|
+
schema_names = constraints + indexes
|
|
1085
|
+
return if schema_names.empty?
|
|
1086
|
+
|
|
1087
|
+
raise Error,
|
|
1088
|
+
"There are constraints or indexes in this database, exiting now: " \
|
|
1089
|
+
"#{schema_names.sort.join(', ')}"
|
|
1090
|
+
end
|
|
1091
|
+
|
|
930
1092
|
# Neo4j 4.4 has no elementId() function. Its id() fallback is used only for
|
|
931
1093
|
# deterministic in-process ordering; those database IDs never enter a dump.
|
|
932
1094
|
def dump_identity_function
|
|
@@ -939,6 +1101,7 @@ module Neo4jBolt
|
|
|
939
1101
|
def parse_dump(io, node_batches, relationship_batches, progress = nil)
|
|
940
1102
|
node_count = 0
|
|
941
1103
|
relationship_count = 0
|
|
1104
|
+
schema_entries = []
|
|
942
1105
|
progress&.update("Reading dump: 0 nodes, 0 relationships", force: true)
|
|
943
1106
|
|
|
944
1107
|
io.each_line.with_index(1) do |line, line_number|
|
|
@@ -946,16 +1109,22 @@ module Neo4jBolt
|
|
|
946
1109
|
next if line.empty?
|
|
947
1110
|
|
|
948
1111
|
kind, json = line.split(" ", 2)
|
|
949
|
-
|
|
1112
|
+
unless json && %w[n r u k i].include?(kind)
|
|
1113
|
+
raise Error, "Invalid dump entry on line #{line_number}"
|
|
1114
|
+
end
|
|
950
1115
|
|
|
951
1116
|
value = JSON.parse(json)
|
|
952
|
-
|
|
1117
|
+
case kind
|
|
1118
|
+
when "n"
|
|
953
1119
|
labels = value.fetch("labels")
|
|
954
1120
|
node_batches[labels.sort] << value
|
|
955
1121
|
node_count += 1
|
|
956
|
-
|
|
1122
|
+
when "r"
|
|
957
1123
|
relationship_batches[value.fetch("type")] << value
|
|
958
1124
|
relationship_count += 1
|
|
1125
|
+
else
|
|
1126
|
+
validate_schema_dump_value!(kind, value)
|
|
1127
|
+
schema_entries << [kind, value]
|
|
959
1128
|
end
|
|
960
1129
|
if ((node_count + relationship_count) % LOAD_PROGRESS_STEP).zero?
|
|
961
1130
|
progress&.update("Reading dump: #{node_count} nodes, #{relationship_count} relationships")
|
|
@@ -965,7 +1134,7 @@ module Neo4jBolt
|
|
|
965
1134
|
end
|
|
966
1135
|
|
|
967
1136
|
progress&.finish("Read dump: #{node_count} nodes, #{relationship_count} relationships")
|
|
968
|
-
[node_count, relationship_count]
|
|
1137
|
+
[node_count, relationship_count, schema_entries]
|
|
969
1138
|
end
|
|
970
1139
|
|
|
971
1140
|
def adaptive_each_slice(items, batch_size, progress, kind)
|