mutineer 1.8.2 → 1.9.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/CHANGELOG.md +611 -0
- data/README.md +84 -49
- data/lib/mutineer/changed_lines.rb +2 -1
- data/lib/mutineer/child_cause.rb +149 -0
- data/lib/mutineer/child_stdout.rb +12 -1
- data/lib/mutineer/cli.rb +536 -58
- data/lib/mutineer/command_redact.rb +38 -0
- data/lib/mutineer/config.rb +112 -6
- data/lib/mutineer/coverage_map.rb +1345 -156
- data/lib/mutineer/daemon_backend.rb +40 -17
- data/lib/mutineer/daemon_client.rb +63 -15
- data/lib/mutineer/daemon_server.rb +75 -5
- data/lib/mutineer/database_urls.rb +189 -0
- data/lib/mutineer/external_backend.rb +47 -1
- data/lib/mutineer/fs_path.rb +125 -0
- data/lib/mutineer/isolation.rb +374 -20
- data/lib/mutineer/job_plan.rb +349 -39
- data/lib/mutineer/minitest_integration/file_order.rb +56 -0
- data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
- data/lib/mutineer/minitest_integration.rb +26 -58
- data/lib/mutineer/mutant_id.rb +4 -1
- data/lib/mutineer/mutation.rb +6 -1
- data/lib/mutineer/mutator_registry.rb +6 -3
- data/lib/mutineer/mutators/collection_method.rb +195 -6
- data/lib/mutineer/mutators/literal_mutation.rb +33 -3
- data/lib/mutineer/mutators/range_literal.rb +78 -29
- data/lib/mutineer/mutators/rescue_body.rb +126 -0
- data/lib/mutineer/mutators/return_nil.rb +23 -6
- data/lib/mutineer/output_filter.rb +451 -0
- data/lib/mutineer/pairing.rb +463 -31
- data/lib/mutineer/parser.rb +14 -0
- data/lib/mutineer/project_path.rb +15 -8
- data/lib/mutineer/rails_worker_db.rb +356 -6
- data/lib/mutineer/reach_probe.rb +229 -0
- data/lib/mutineer/redaction.rb +264 -32
- data/lib/mutineer/reload_guards.rb +460 -39
- data/lib/mutineer/reporter.rb +465 -57
- data/lib/mutineer/result.rb +6 -7
- data/lib/mutineer/run_seed.rb +16 -0
- data/lib/mutineer/runner.rb +175 -51
- data/lib/mutineer/source_bytes.rb +187 -0
- data/lib/mutineer/statement_lines.rb +108 -14
- data/lib/mutineer/test_runners/rspec.rb +118 -10
- data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
- data/lib/mutineer/test_runners.rb +3 -2
- data/lib/mutineer/triage.rb +55 -10
- data/lib/mutineer/version.rb +1 -1
- data/lib/mutineer.rb +1 -0
- data/skills/mutineer/SKILL.md +12 -4
- metadata +12 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "fs_path"
|
|
4
|
+
|
|
3
5
|
module Mutineer
|
|
4
6
|
# Realpath-based path normalization against a project root. Shared by the
|
|
5
7
|
# coverage cache (map keys) and mutant ids (the path hashed into each id), so
|
|
@@ -18,10 +20,13 @@ module Mutineer
|
|
|
18
20
|
def relative(path, root)
|
|
19
21
|
abs = absolute(path, root)
|
|
20
22
|
real_root = root_real(root)
|
|
21
|
-
prefix
|
|
22
|
-
|
|
23
|
+
# Byte prefix: a non-UTF-8 file name is binary, and a non-ASCII root
|
|
24
|
+
# is UTF-8. start_with? raises on that mix (#346).
|
|
25
|
+
prefix = real_root.b
|
|
26
|
+
prefix += "/".b unless prefix.end_with?("/".b)
|
|
27
|
+
return abs unless FsPath.bytes_start_with?(abs, prefix)
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
FsPath.bytes_delete_prefix(abs, prefix)
|
|
25
30
|
end
|
|
26
31
|
|
|
27
32
|
# Expands `path` against `root`, resolved to its real path when it exists.
|
|
@@ -30,16 +35,18 @@ module Mutineer
|
|
|
30
35
|
# @param root [String] project root.
|
|
31
36
|
# @return [String] absolute path.
|
|
32
37
|
def absolute(path, root)
|
|
38
|
+
path = FsPath.read(path)
|
|
39
|
+
root = FsPath.read(root)
|
|
33
40
|
# Join, don't expand: File.expand_path collapses `..` textually, before any
|
|
34
41
|
# symlink is followed, so `link/../x.rb` would name the wrong file. The file
|
|
35
42
|
# system resolves `..` physically in File.realpath.
|
|
36
43
|
# A leading `~` names the home directory (as File.expand_path reads it), so
|
|
37
44
|
# expand it rather than joining it under the root.
|
|
38
45
|
raw = if File.absolute_path?(path) then path
|
|
39
|
-
elsif path.start_with?("~") then File.expand_path(path)
|
|
40
|
-
else File.
|
|
46
|
+
elsif path.b.start_with?("~".b) then File.expand_path(path)
|
|
47
|
+
else File.expand_path(root).b + File::SEPARATOR.b + path.to_s.b
|
|
41
48
|
end
|
|
42
|
-
File.exist?(raw) ? File.realpath(raw) : File.expand_path(raw)
|
|
49
|
+
FsPath.read(File.exist?(raw) ? File.realpath(raw) : File.expand_path(raw))
|
|
43
50
|
end
|
|
44
51
|
|
|
45
52
|
# Canonical project root (`/var` vs `/private/var`). Any file system error
|
|
@@ -49,9 +56,9 @@ module Mutineer
|
|
|
49
56
|
# @param root [String] project root.
|
|
50
57
|
# @return [String] realpath of the root when it exists, else its expanded path.
|
|
51
58
|
def root_real(root)
|
|
52
|
-
File.realpath(File.expand_path(root))
|
|
59
|
+
FsPath.read(File.realpath(File.expand_path(root)))
|
|
53
60
|
rescue SystemCallError
|
|
54
|
-
File.expand_path(root)
|
|
61
|
+
FsPath.read(File.expand_path(root))
|
|
55
62
|
end
|
|
56
63
|
end
|
|
57
64
|
end
|
|
@@ -54,6 +54,9 @@ module Mutineer
|
|
|
54
54
|
].freeze
|
|
55
55
|
# Infix that keeps a worker database off Rails' own `<base>-<n>` names.
|
|
56
56
|
WORKER_NAME_MARK = "-mutineer-"
|
|
57
|
+
# Suffix of the SQLite run lock beside the base database file.
|
|
58
|
+
# The file stays. The kernel frees the lock when the last holder exits.
|
|
59
|
+
SQLITE_LOCK_SUFFIX = "-mutineer.lock"
|
|
57
60
|
# Database we connect to while the test database is a template. Advisory
|
|
58
61
|
# locks are per database, so every run must take them here, not on a slot.
|
|
59
62
|
MAINTENANCE_DATABASE = "postgres"
|
|
@@ -287,7 +290,7 @@ module Mutineer
|
|
|
287
290
|
end
|
|
288
291
|
|
|
289
292
|
# Create each worker database before any worker daemon starts.
|
|
290
|
-
# SQLite
|
|
293
|
+
# SQLite takes the run lock and copies files on first use. Postgres drops and
|
|
291
294
|
# recreates each exact slot name from the test database. MySQL creates each
|
|
292
295
|
# slot, loads the schema, and copies rows. The caller must be the only
|
|
293
296
|
# daemon connected to that test database.
|
|
@@ -301,7 +304,10 @@ module Mutineer
|
|
|
301
304
|
|
|
302
305
|
count = Integer(slots)
|
|
303
306
|
return if count < 1
|
|
304
|
-
|
|
307
|
+
if sqlite_adapter?(current_adapter)
|
|
308
|
+
provision_sqlite
|
|
309
|
+
return
|
|
310
|
+
end
|
|
305
311
|
|
|
306
312
|
if postgres_adapter?(current_adapter)
|
|
307
313
|
provision_postgres(count)
|
|
@@ -316,15 +322,37 @@ module Mutineer
|
|
|
316
322
|
"worker-DB isolation does not support adapter #{current_adapter.inspect} yet."
|
|
317
323
|
end
|
|
318
324
|
|
|
319
|
-
# Take the
|
|
320
|
-
#
|
|
321
|
-
#
|
|
325
|
+
# Take the SQLite run lock for this process.
|
|
326
|
+
# Ask for it alone, then share it so worker daemons can join.
|
|
327
|
+
# A failed share raises the same message as a failed exclusive request.
|
|
328
|
+
#
|
|
329
|
+
# @return [void]
|
|
330
|
+
# @raise [RuntimeError] when another run holds the lock.
|
|
331
|
+
# @raise [SystemCallError] when the lock file cannot be opened.
|
|
332
|
+
def self.provision_sqlite
|
|
333
|
+
config = primary_db_config
|
|
334
|
+
base = sqlite_lock_base(config)
|
|
335
|
+
return unless base
|
|
336
|
+
|
|
337
|
+
lock = "#{base}#{SQLITE_LOCK_SUFFIX}"
|
|
338
|
+
hold_sqlite_lock(lock, base, shared: false)
|
|
339
|
+
hold_sqlite_lock(lock, base, shared: true)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# Take the shared run lock. Worker daemons call this at boot. They do not
|
|
343
|
+
# create databases. Postgres and MySQL also drop this process's connection
|
|
344
|
+
# to the test database. SQLite only takes the lock: the file copy still
|
|
345
|
+
# happens in {after_fork}.
|
|
322
346
|
#
|
|
323
347
|
# @return [void]
|
|
324
348
|
# @raise [RuntimeError] when another run holds the exclusive lock.
|
|
349
|
+
# @raise [SystemCallError] when the SQLite lock file cannot be opened.
|
|
325
350
|
def self.prepare_worker!
|
|
326
351
|
return unless available?
|
|
327
|
-
|
|
352
|
+
if sqlite_adapter?(current_adapter)
|
|
353
|
+
prepare_sqlite_worker!
|
|
354
|
+
return
|
|
355
|
+
end
|
|
328
356
|
|
|
329
357
|
if mysql_adapter?(current_adapter)
|
|
330
358
|
prepare_mysql_worker!
|
|
@@ -344,6 +372,20 @@ module Mutineer
|
|
|
344
372
|
disconnect_app!
|
|
345
373
|
end
|
|
346
374
|
|
|
375
|
+
# Take the shared SQLite run lock. A worker daemon calls this at boot.
|
|
376
|
+
# No file means there is nothing to lock (blank, `:memory:`, or a memory URI).
|
|
377
|
+
#
|
|
378
|
+
# @return [void]
|
|
379
|
+
# @raise [RuntimeError] when another run holds the exclusive lock.
|
|
380
|
+
# @raise [SystemCallError] when the lock file cannot be opened.
|
|
381
|
+
def self.prepare_sqlite_worker!
|
|
382
|
+
config = primary_db_config
|
|
383
|
+
base = sqlite_lock_base(config)
|
|
384
|
+
return unless base
|
|
385
|
+
|
|
386
|
+
hold_sqlite_lock("#{base}#{SQLITE_LOCK_SUFFIX}", base, shared: true)
|
|
387
|
+
end
|
|
388
|
+
|
|
347
389
|
# Point this process at one worker database. Postgres and MySQL only connect.
|
|
348
390
|
# The slot was copied at {provision}.
|
|
349
391
|
#
|
|
@@ -375,6 +417,13 @@ module Mutineer
|
|
|
375
417
|
ActiveRecord::Base.connection_db_config.name
|
|
376
418
|
end
|
|
377
419
|
|
|
420
|
+
# The database config `ActiveRecord::Base` is using.
|
|
421
|
+
#
|
|
422
|
+
# @return [ActiveRecord::DatabaseConfigurations::DatabaseConfig]
|
|
423
|
+
def self.primary_db_config
|
|
424
|
+
ActiveRecord::Base.connection_db_config
|
|
425
|
+
end
|
|
426
|
+
|
|
378
427
|
# The test environment's database configs, hidden ones and replicas included.
|
|
379
428
|
#
|
|
380
429
|
# @return [Array<ActiveRecord::DatabaseConfigurations::DatabaseConfig>]
|
|
@@ -452,6 +501,9 @@ module Mutineer
|
|
|
452
501
|
# @param schema_path [String] absolute path to the app's Ruby schema file.
|
|
453
502
|
# @return [Boolean]
|
|
454
503
|
def self.schema_current?(schema_path)
|
|
504
|
+
# UTF-8, not Parser.read. This file loads in the app's Ruby, which may
|
|
505
|
+
# not ship Prism. A require of parser.rb would disable worker-database
|
|
506
|
+
# routing (#346). Rails writes schema.rb as UTF-8.
|
|
455
507
|
text = File.read(schema_path, encoding: "UTF-8")
|
|
456
508
|
version = schema_file_version(text)
|
|
457
509
|
conn = ActiveRecord::Base.connection
|
|
@@ -611,6 +663,298 @@ module Mutineer
|
|
|
611
663
|
adapter.start_with?("sqlite")
|
|
612
664
|
end
|
|
613
665
|
|
|
666
|
+
# True for a SQLite config whose database name is a plain file path:
|
|
667
|
+
# not `:memory:`, not a `file:` URI, not blank.
|
|
668
|
+
# A `file:` URI is resolved by {sqlite_lock_base}, not by this predicate.
|
|
669
|
+
#
|
|
670
|
+
# @param config [ActiveRecord::DatabaseConfigurations::DatabaseConfig]
|
|
671
|
+
# @return [Boolean]
|
|
672
|
+
def self.sqlite_file_config?(config)
|
|
673
|
+
database = config.database.to_s
|
|
674
|
+
sqlite_adapter?(config.adapter.to_s) && !database.empty? && database != ":memory:" && !database.start_with?("file:")
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
# Absolute path of a plain SQLite config's file, resolved as the Rails
|
|
678
|
+
# adapter does: against `Rails.root` when it is set.
|
|
679
|
+
# Rails rewrites a plain name before SQLite opens it. A `file:` URI is
|
|
680
|
+
# not rewritten; {sqlite_uri_file} resolves that name the way SQLite does.
|
|
681
|
+
#
|
|
682
|
+
# @param config [ActiveRecord::DatabaseConfigurations::DatabaseConfig]
|
|
683
|
+
# @return [String]
|
|
684
|
+
def self.sqlite_file(config)
|
|
685
|
+
root = defined?(Rails.root) && Rails.root
|
|
686
|
+
root ? File.expand_path(config.database, root.to_s) : File.expand_path(config.database)
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
# Absolute path of the SQLite file this config locks, or nil when the
|
|
690
|
+
# database is not a shared file. Blank names, `:memory:`, a memory URI,
|
|
691
|
+
# and a URI SQLite rejects before opening a file have no lock.
|
|
692
|
+
#
|
|
693
|
+
# @param config [ActiveRecord::DatabaseConfigurations::DatabaseConfig]
|
|
694
|
+
# @return [String, nil]
|
|
695
|
+
def self.sqlite_lock_base(config)
|
|
696
|
+
database = config.database.to_s
|
|
697
|
+
if database.start_with?("file:")
|
|
698
|
+
return unless sqlite_adapter?(config.adapter.to_s)
|
|
699
|
+
|
|
700
|
+
sqlite_uri_file(database)
|
|
701
|
+
else
|
|
702
|
+
return unless sqlite_file_config?(config)
|
|
703
|
+
|
|
704
|
+
sqlite_file(config)
|
|
705
|
+
end
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
# Absolute path of the SQLite run lock, or nil when {sqlite_lock_base}
|
|
709
|
+
# finds no shared file.
|
|
710
|
+
#
|
|
711
|
+
# @param config [ActiveRecord::DatabaseConfigurations::DatabaseConfig]
|
|
712
|
+
# @return [String, nil]
|
|
713
|
+
def self.sqlite_lock_path(config)
|
|
714
|
+
base = sqlite_lock_base(config)
|
|
715
|
+
return unless base
|
|
716
|
+
|
|
717
|
+
"#{base}#{SQLITE_LOCK_SUFFIX}"
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
# Absolute path of the file a `file:` URI opens, or nil when that URI
|
|
721
|
+
# does not open a shared file.
|
|
722
|
+
#
|
|
723
|
+
# SQLite opens a relative URI path from the working directory. Rails does
|
|
724
|
+
# not rewrite a `file:` name, so this does not use `Rails.root`. The
|
|
725
|
+
# daemon has already changed to the project root, which is `Rails.root`
|
|
726
|
+
# for a run started in the app. A plain name for that same file then
|
|
727
|
+
# locks the same path. Extra leading slashes are collapsed so
|
|
728
|
+
# `file:////tmp/db.sqlite3` locks `/tmp/db.sqlite3`. The filesystem root
|
|
729
|
+
# is not a database file, so a path of `/` has no lock.
|
|
730
|
+
#
|
|
731
|
+
# @param uri [String] a database name that starts with `file:`.
|
|
732
|
+
# @return [String, nil]
|
|
733
|
+
def self.sqlite_uri_file(uri)
|
|
734
|
+
parsed = sqlite_uri_parts(uri)
|
|
735
|
+
return unless parsed
|
|
736
|
+
|
|
737
|
+
path, memory = parsed
|
|
738
|
+
return if memory || path.empty? || path == ":memory:"
|
|
739
|
+
|
|
740
|
+
expanded = File.expand_path(path).sub(%r{\A/{2,}}, "/")
|
|
741
|
+
# `/` is not a database file. The lock name would be `/-mutineer.lock`.
|
|
742
|
+
return if expanded == "/"
|
|
743
|
+
|
|
744
|
+
expanded
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
# Path and memory flag of a `file:` URI, or nil when SQLite rejects it.
|
|
748
|
+
#
|
|
749
|
+
# The steps match SQLite's `file:` parser. The authority is empty or
|
|
750
|
+
# `localhost`. `%HH` is decoded. `%00` ends the part being read. A raw
|
|
751
|
+
# `?` starts the query and `#` starts a fragment, which is ignored.
|
|
752
|
+
# `mode=memory` is in-memory. Any other `mode` or `cache` value makes
|
|
753
|
+
# SQLite refuse the open, so there is no shared file to lock.
|
|
754
|
+
# `vfs` and the other query keys do not change the path.
|
|
755
|
+
#
|
|
756
|
+
# @param uri [String] a database name that starts with `file:`.
|
|
757
|
+
# @return [Array(String, Boolean), nil] the decoded path and whether
|
|
758
|
+
# `mode=memory` won, or nil when the URI is rejected.
|
|
759
|
+
def self.sqlite_uri_parts(uri)
|
|
760
|
+
bytes = uri.b.bytes
|
|
761
|
+
index = 5
|
|
762
|
+
if bytes[index] == 0x2f && bytes[index + 1] == 0x2f
|
|
763
|
+
index += 2
|
|
764
|
+
auth_at = index
|
|
765
|
+
index += 1 while index < bytes.length && bytes[index] != 0x2f
|
|
766
|
+
authority = bytes[auth_at...index].pack("C*")
|
|
767
|
+
return if !authority.empty? && authority != "localhost"
|
|
768
|
+
end
|
|
769
|
+
|
|
770
|
+
path = []
|
|
771
|
+
name = []
|
|
772
|
+
value = []
|
|
773
|
+
state = :path
|
|
774
|
+
options = { mode: nil, cache: nil }
|
|
775
|
+
while index < bytes.length
|
|
776
|
+
byte = bytes[index]
|
|
777
|
+
break if byte == 0x23 # #
|
|
778
|
+
|
|
779
|
+
index += 1
|
|
780
|
+
if byte == 0x25 && sqlite_uri_hex?(bytes[index]) && sqlite_uri_hex?(bytes[index + 1]) # %
|
|
781
|
+
octet = bytes[index, 2].pack("C*").to_i(16)
|
|
782
|
+
index += 2
|
|
783
|
+
if octet.zero?
|
|
784
|
+
index = sqlite_uri_skip_null(bytes, index, state)
|
|
785
|
+
next
|
|
786
|
+
end
|
|
787
|
+
byte = octet
|
|
788
|
+
elsif state == :name && (byte == 0x26 || byte == 0x3d) # & or =
|
|
789
|
+
if name.empty?
|
|
790
|
+
index = sqlite_uri_skip_empty_option(bytes, index)
|
|
791
|
+
next
|
|
792
|
+
end
|
|
793
|
+
if byte == 0x26
|
|
794
|
+
return unless sqlite_uri_option(name, [], options)
|
|
795
|
+
|
|
796
|
+
name = []
|
|
797
|
+
next
|
|
798
|
+
end
|
|
799
|
+
state = :value
|
|
800
|
+
value = []
|
|
801
|
+
next
|
|
802
|
+
elsif (state == :path && byte == 0x3f) || (state == :value && byte == 0x26) # ? or &
|
|
803
|
+
if state == :value
|
|
804
|
+
return unless sqlite_uri_option(name, value, options)
|
|
805
|
+
|
|
806
|
+
name = []
|
|
807
|
+
value = []
|
|
808
|
+
end
|
|
809
|
+
state = :name
|
|
810
|
+
next
|
|
811
|
+
end
|
|
812
|
+
case state
|
|
813
|
+
when :path then path << byte
|
|
814
|
+
when :name then name << byte
|
|
815
|
+
when :value then value << byte
|
|
816
|
+
end
|
|
817
|
+
end
|
|
818
|
+
if state == :name && !name.empty?
|
|
819
|
+
return unless sqlite_uri_option(name, [], options)
|
|
820
|
+
elsif state == :value
|
|
821
|
+
return unless sqlite_uri_option(name, value, options)
|
|
822
|
+
end
|
|
823
|
+
|
|
824
|
+
memory = (options[:open_flags].to_i & 0x80) != 0
|
|
825
|
+
[sqlite_uri_text(path), memory]
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
# Advance to the delimiter that ends the part a `%00` truncated.
|
|
829
|
+
# The delimiter itself stays, so the caller can parse it.
|
|
830
|
+
#
|
|
831
|
+
# @param bytes [Array<Integer>]
|
|
832
|
+
# @param index [Integer]
|
|
833
|
+
# @param state [Symbol] `:path`, `:name`, or `:value`.
|
|
834
|
+
# @return [Integer]
|
|
835
|
+
def self.sqlite_uri_skip_null(bytes, index, state)
|
|
836
|
+
while index < bytes.length
|
|
837
|
+
byte = bytes[index]
|
|
838
|
+
break if byte == 0x23 # #
|
|
839
|
+
break if state == :path && byte == 0x3f # ?
|
|
840
|
+
break if state == :name && (byte == 0x3d || byte == 0x26) # = or &
|
|
841
|
+
break if state == :value && byte == 0x26 # &
|
|
842
|
+
|
|
843
|
+
index += 1
|
|
844
|
+
end
|
|
845
|
+
index
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
# Advance past the value of a query parameter whose name was empty.
|
|
849
|
+
# SQLite ignores that parameter. `index` already points at the next byte.
|
|
850
|
+
#
|
|
851
|
+
# @param bytes [Array<Integer>]
|
|
852
|
+
# @param index [Integer]
|
|
853
|
+
# @return [Integer]
|
|
854
|
+
def self.sqlite_uri_skip_empty_option(bytes, index)
|
|
855
|
+
index += 1 while index < bytes.length && bytes[index] != 0x23 && bytes[index - 1] != 0x26
|
|
856
|
+
index
|
|
857
|
+
end
|
|
858
|
+
|
|
859
|
+
# Record one query parameter. `mode` and `cache` are the keys that decide
|
|
860
|
+
# whether a file opens. An unknown value is a URI SQLite rejects.
|
|
861
|
+
# A later `mode` replaces the earlier one only when SQLite allows it.
|
|
862
|
+
# Adding write or create after a narrower mode is refused.
|
|
863
|
+
#
|
|
864
|
+
# @param name [Array<Integer>] parameter name bytes.
|
|
865
|
+
# @param value [Array<Integer>] parameter value bytes.
|
|
866
|
+
# @param options [Hash] `:open_flags` and `:cache`, updated in place.
|
|
867
|
+
# @return [Boolean] false when SQLite would reject this parameter.
|
|
868
|
+
def self.sqlite_uri_option(name, value, options)
|
|
869
|
+
key = sqlite_uri_text(name)
|
|
870
|
+
text = sqlite_uri_text(value)
|
|
871
|
+
case key
|
|
872
|
+
when "mode"
|
|
873
|
+
return false unless sqlite_uri_mode(text, options)
|
|
874
|
+
when "cache"
|
|
875
|
+
return false unless %w[shared private].include?(text)
|
|
876
|
+
|
|
877
|
+
options[:cache] = text
|
|
878
|
+
end
|
|
879
|
+
true
|
|
880
|
+
end
|
|
881
|
+
|
|
882
|
+
# Apply one `mode` value to the open flags SQLite starts with.
|
|
883
|
+
# The default is read-write and create, as in `sqlite3_open`.
|
|
884
|
+
# SQLite refuses a later mode whose non-memory bits are greater than the
|
|
885
|
+
# flags still set, for example `mode=ro&mode=rw`. `mode=memory` then
|
|
886
|
+
# `mode=rw` is a disk file again. The memory bit is `0x80`.
|
|
887
|
+
#
|
|
888
|
+
# @param text [String] the mode value.
|
|
889
|
+
# @param options [Hash] `:open_flags` is updated in place.
|
|
890
|
+
# @return [Boolean] false when the value is unknown or not allowed.
|
|
891
|
+
def self.sqlite_uri_mode(text, options)
|
|
892
|
+
readonly = 0x01
|
|
893
|
+
readwrite = 0x02
|
|
894
|
+
create = 0x04
|
|
895
|
+
memory = 0x80
|
|
896
|
+
bits = {
|
|
897
|
+
"ro" => readonly,
|
|
898
|
+
"rw" => readwrite,
|
|
899
|
+
"rwc" => readwrite | create,
|
|
900
|
+
"memory" => memory
|
|
901
|
+
}[text]
|
|
902
|
+
return false unless bits
|
|
903
|
+
|
|
904
|
+
mask = readonly | readwrite | create | memory
|
|
905
|
+
flags = options[:open_flags] ||= readwrite | create
|
|
906
|
+
return false if (bits & ~memory) > (mask & flags)
|
|
907
|
+
|
|
908
|
+
options[:open_flags] = (flags & ~mask) | bits
|
|
909
|
+
true
|
|
910
|
+
end
|
|
911
|
+
|
|
912
|
+
# @param bytes [Array<Integer>]
|
|
913
|
+
# @return [String] UTF-8 when the bytes are valid text, otherwise binary.
|
|
914
|
+
def self.sqlite_uri_text(bytes)
|
|
915
|
+
text = bytes.pack("C*")
|
|
916
|
+
utf8 = text.dup.force_encoding(Encoding::UTF_8)
|
|
917
|
+
return utf8 if utf8.valid_encoding?
|
|
918
|
+
|
|
919
|
+
text.force_encoding(Encoding::ASCII_8BIT)
|
|
920
|
+
end
|
|
921
|
+
|
|
922
|
+
# @param byte [Integer, nil]
|
|
923
|
+
# @return [Boolean]
|
|
924
|
+
def self.sqlite_uri_hex?(byte)
|
|
925
|
+
return false if byte.nil?
|
|
926
|
+
|
|
927
|
+
(byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x46) || (byte >= 0x61 && byte <= 0x66)
|
|
928
|
+
end
|
|
929
|
+
|
|
930
|
+
# Open the SQLite run lock once and take it.
|
|
931
|
+
# A second open in this process would fight the first, so the same file is reused.
|
|
932
|
+
# Never unlocks the file. The kernel frees the lock when the last holder exits.
|
|
933
|
+
#
|
|
934
|
+
# @param lock_path [String] absolute path of the lock file.
|
|
935
|
+
# @param base [String] absolute database path, named in the stop message.
|
|
936
|
+
# @param shared [Boolean] shared lock when true, exclusive when false.
|
|
937
|
+
# @return [void]
|
|
938
|
+
# @raise [ArgumentError] when this process already has the lock open for another path.
|
|
939
|
+
# @raise [RuntimeError] when another run holds a conflicting lock.
|
|
940
|
+
# @raise [SystemCallError] when the lock file cannot be opened.
|
|
941
|
+
def self.hold_sqlite_lock(lock_path, base, shared:)
|
|
942
|
+
file = @sqlite_lock_file
|
|
943
|
+
if file && !file.closed?
|
|
944
|
+
current = file.path
|
|
945
|
+
unless current == lock_path
|
|
946
|
+
raise ArgumentError, "sqlite run lock is already open for #{current}, not #{lock_path}"
|
|
947
|
+
end
|
|
948
|
+
else
|
|
949
|
+
file = File.open(lock_path, File::RDONLY | File::CREAT, 0o644)
|
|
950
|
+
@sqlite_lock_file = file
|
|
951
|
+
end
|
|
952
|
+
mode = shared ? File::LOCK_SH : File::LOCK_EX
|
|
953
|
+
return if file.flock(mode | File::LOCK_NB)
|
|
954
|
+
|
|
955
|
+
raise "another Mutineer run is using #{base}"
|
|
956
|
+
end
|
|
957
|
+
|
|
614
958
|
# @param adapter [String]
|
|
615
959
|
# @return [Boolean]
|
|
616
960
|
def self.postgres_adapter?(adapter)
|
|
@@ -883,11 +1227,17 @@ module Mutineer
|
|
|
883
1227
|
|
|
884
1228
|
# The child inherited the parent's lock session. Drop the Ruby wrapper
|
|
885
1229
|
# without sending a disconnect, so the parent's locks stay held.
|
|
1230
|
+
# Close the SQLite lock file when it is open. Never unlock it.
|
|
1231
|
+
# A second call in this process does nothing for that file.
|
|
886
1232
|
# Clearing the held-name list stops the child from sending RELEASE_LOCK
|
|
887
1233
|
# on that shared session.
|
|
888
1234
|
#
|
|
889
1235
|
# @return [void]
|
|
890
1236
|
def self.forget_lock_connection_after_fork!
|
|
1237
|
+
file = @sqlite_lock_file
|
|
1238
|
+
@sqlite_lock_file = nil
|
|
1239
|
+
file.close if file && !file.closed?
|
|
1240
|
+
|
|
891
1241
|
@mysql_held_locks = []
|
|
892
1242
|
conn = @lock_connection
|
|
893
1243
|
@lock_connection = nil
|