polyrun 3.2.0-java
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 +7 -0
- data/CHANGELOG.md +234 -0
- data/CODE_OF_CONDUCT.md +26 -0
- data/CONTRIBUTING.md +23 -0
- data/LICENSE +21 -0
- data/README.md +251 -0
- data/SECURITY.md +25 -0
- data/bin/polyrun +6 -0
- data/docs/QUALITY.md +82 -0
- data/docs/QUALITY_CONFIGURATION.md +95 -0
- data/docs/SETUP_PROFILE.md +139 -0
- data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
- data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
- data/lib/polyrun/benchmark/pressure.rb +144 -0
- data/lib/polyrun/benchmark/profile.rb +191 -0
- data/lib/polyrun/benchmark/report.rb +87 -0
- data/lib/polyrun/cli/benchmark_commands.rb +66 -0
- data/lib/polyrun/cli/ci_shard_hooks.rb +129 -0
- data/lib/polyrun/cli/ci_shard_run_command.rb +177 -0
- data/lib/polyrun/cli/ci_shard_run_parse.rb +68 -0
- data/lib/polyrun/cli/config_command.rb +42 -0
- data/lib/polyrun/cli/coverage_commands.rb +150 -0
- data/lib/polyrun/cli/coverage_merge_io.rb +150 -0
- data/lib/polyrun/cli/database_commands.rb +163 -0
- data/lib/polyrun/cli/default_run.rb +144 -0
- data/lib/polyrun/cli/doctor_command.rb +94 -0
- data/lib/polyrun/cli/env_commands.rb +43 -0
- data/lib/polyrun/cli/failure_commands.rb +99 -0
- data/lib/polyrun/cli/graph_command.rb +68 -0
- data/lib/polyrun/cli/help.rb +83 -0
- data/lib/polyrun/cli/helpers.rb +162 -0
- data/lib/polyrun/cli/hooks_command.rb +97 -0
- data/lib/polyrun/cli/init_command.rb +108 -0
- data/lib/polyrun/cli/mutation_command.rb +67 -0
- data/lib/polyrun/cli/partition_diagnostics.rb +22 -0
- data/lib/polyrun/cli/plan_command.rb +184 -0
- data/lib/polyrun/cli/prepare_command.rb +70 -0
- data/lib/polyrun/cli/prepare_recipe.rb +82 -0
- data/lib/polyrun/cli/quality_commands.rb +65 -0
- data/lib/polyrun/cli/queue_command.rb +161 -0
- data/lib/polyrun/cli/quick_command.rb +13 -0
- data/lib/polyrun/cli/report_commands.rb +108 -0
- data/lib/polyrun/cli/run_queue_command.rb +127 -0
- data/lib/polyrun/cli/run_shards_command.rb +164 -0
- data/lib/polyrun/cli/run_shards_parallel_children.rb +112 -0
- data/lib/polyrun/cli/run_shards_parallel_wait.rb +252 -0
- data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +207 -0
- data/lib/polyrun/cli/run_shards_plan_options.rb +74 -0
- data/lib/polyrun/cli/run_shards_planning.rb +123 -0
- data/lib/polyrun/cli/run_shards_run.rb +171 -0
- data/lib/polyrun/cli/run_shards_worker_interrupt.rb +101 -0
- data/lib/polyrun/cli/skip_env.rb +34 -0
- data/lib/polyrun/cli/spec_quality_commands.rb +149 -0
- data/lib/polyrun/cli/start_bootstrap.rb +126 -0
- data/lib/polyrun/cli/timing_command.rb +31 -0
- data/lib/polyrun/cli/tool_options.rb +48 -0
- data/lib/polyrun/cli/validate_command.rb +26 -0
- data/lib/polyrun/cli.rb +272 -0
- data/lib/polyrun/config/dotted_path.rb +21 -0
- data/lib/polyrun/config/effective.rb +82 -0
- data/lib/polyrun/config/resolver.rb +114 -0
- data/lib/polyrun/config/schema.rb +39 -0
- data/lib/polyrun/config/suite.rb +109 -0
- data/lib/polyrun/config/tool_defaults.rb +42 -0
- data/lib/polyrun/config/tool_validation.rb +79 -0
- data/lib/polyrun/config/tools.rb +120 -0
- data/lib/polyrun/config.rb +97 -0
- data/lib/polyrun/coverage/cobertura_zero_lines.rb +32 -0
- data/lib/polyrun/coverage/collector.rb +169 -0
- data/lib/polyrun/coverage/collector_blob.rb +53 -0
- data/lib/polyrun/coverage/collector_finish.rb +98 -0
- data/lib/polyrun/coverage/collector_fragment_meta.rb +57 -0
- data/lib/polyrun/coverage/config_resolve.rb +131 -0
- data/lib/polyrun/coverage/example_diff.rb +184 -0
- data/lib/polyrun/coverage/example_diff_snapshot.rb +44 -0
- data/lib/polyrun/coverage/example_diff_track_filter.rb +51 -0
- data/lib/polyrun/coverage/file_stats_report.rb +36 -0
- data/lib/polyrun/coverage/filter.rb +22 -0
- data/lib/polyrun/coverage/formatter.rb +137 -0
- data/lib/polyrun/coverage/merge/formatters.rb +188 -0
- data/lib/polyrun/coverage/merge/formatters_html.rb +221 -0
- data/lib/polyrun/coverage/merge/html/_file_list.html.erb +21 -0
- data/lib/polyrun/coverage/merge/html/_file_section.html.erb +26 -0
- data/lib/polyrun/coverage/merge/html/_groups_table.html.erb +18 -0
- data/lib/polyrun/coverage/merge/html/_overview.html.erb +47 -0
- data/lib/polyrun/coverage/merge/html/report.css +147 -0
- data/lib/polyrun/coverage/merge/html/report.js +48 -0
- data/lib/polyrun/coverage/merge/html/template.html.erb +30 -0
- data/lib/polyrun/coverage/merge.rb +160 -0
- data/lib/polyrun/coverage/merge_fragment_meta.rb +58 -0
- data/lib/polyrun/coverage/merge_merge_two.rb +168 -0
- data/lib/polyrun/coverage/merge_native.rb +39 -0
- data/lib/polyrun/coverage/rails.rb +126 -0
- data/lib/polyrun/coverage/reporting.rb +41 -0
- data/lib/polyrun/coverage/result.rb +18 -0
- data/lib/polyrun/coverage/track_files.rb +150 -0
- data/lib/polyrun/data/cached_fixtures.rb +122 -0
- data/lib/polyrun/data/factory_counts.rb +48 -0
- data/lib/polyrun/data/factory_instrumentation.rb +50 -0
- data/lib/polyrun/data/fixtures.rb +68 -0
- data/lib/polyrun/data/parallel_provisioning.rb +93 -0
- data/lib/polyrun/data/snapshot.rb +84 -0
- data/lib/polyrun/database/clone_shards.rb +106 -0
- data/lib/polyrun/database/provision.rb +83 -0
- data/lib/polyrun/database/shard.rb +64 -0
- data/lib/polyrun/database/url_builder/connection/infer.rb +53 -0
- data/lib/polyrun/database/url_builder/connection/url_builders.rb +43 -0
- data/lib/polyrun/database/url_builder/connection.rb +191 -0
- data/lib/polyrun/database/url_builder/template_prepare.rb +21 -0
- data/lib/polyrun/database/url_builder.rb +188 -0
- data/lib/polyrun/debug.rb +81 -0
- data/lib/polyrun/env/ci.rb +65 -0
- data/lib/polyrun/error.rb +3 -0
- data/lib/polyrun/export/csv.rb +25 -0
- data/lib/polyrun/export/markdown.rb +47 -0
- data/lib/polyrun/graph/client.rb +64 -0
- data/lib/polyrun/graph/error.rb +13 -0
- data/lib/polyrun/graph/index.rb +75 -0
- data/lib/polyrun/graph/inspect.rb +58 -0
- data/lib/polyrun/graph/plan.rb +134 -0
- data/lib/polyrun/graph/planner.rb +117 -0
- data/lib/polyrun/graph/syntax.rb +114 -0
- data/lib/polyrun/graph/transaction.rb +81 -0
- data/lib/polyrun/graph/worker.rb +55 -0
- data/lib/polyrun/graph/workspace.rb +117 -0
- data/lib/polyrun/hooks/dsl.rb +128 -0
- data/lib/polyrun/hooks/shell_runner.rb +33 -0
- data/lib/polyrun/hooks/worker_runner.rb +27 -0
- data/lib/polyrun/hooks/worker_shell.rb +50 -0
- data/lib/polyrun/hooks.rb +190 -0
- data/lib/polyrun/log.rb +86 -0
- data/lib/polyrun/minitest_support.rb +60 -0
- data/lib/polyrun/mutation/candidates.rb +91 -0
- data/lib/polyrun/mutation/operators.rb +51 -0
- data/lib/polyrun/mutation/runner.rb +133 -0
- data/lib/polyrun/mutation/subjects.rb +88 -0
- data/lib/polyrun/partition/constraints.rb +80 -0
- data/lib/polyrun/partition/hrw.rb +70 -0
- data/lib/polyrun/partition/min_heap.rb +64 -0
- data/lib/polyrun/partition/paths.rb +175 -0
- data/lib/polyrun/partition/paths_build.rb +141 -0
- data/lib/polyrun/partition/plan.rb +250 -0
- data/lib/polyrun/partition/plan_lpt.rb +91 -0
- data/lib/polyrun/partition/plan_sharding.rb +56 -0
- data/lib/polyrun/partition/reports.rb +139 -0
- data/lib/polyrun/partition/stable_shuffle.rb +18 -0
- data/lib/polyrun/partition/suite.rb +120 -0
- data/lib/polyrun/partition/timing_diagnostics.rb +138 -0
- data/lib/polyrun/partition/timing_keys.rb +86 -0
- data/lib/polyrun/prepare/artifacts.rb +40 -0
- data/lib/polyrun/prepare/assets.rb +64 -0
- data/lib/polyrun/process_stdio.rb +96 -0
- data/lib/polyrun/quality/command.rb +84 -0
- data/lib/polyrun/quality/measure.rb +38 -0
- data/lib/polyrun/quality/observe.rb +53 -0
- data/lib/polyrun/quality/observe_hook.rb +103 -0
- data/lib/polyrun/quality/sandbox.rb +70 -0
- data/lib/polyrun/queue/duration.rb +30 -0
- data/lib/polyrun/queue/file_store.rb +413 -0
- data/lib/polyrun/queue/file_store_intent.rb +188 -0
- data/lib/polyrun/queue/file_store_pending.rb +48 -0
- data/lib/polyrun/queue/worker_loop.rb +73 -0
- data/lib/polyrun/quick/assertions.rb +32 -0
- data/lib/polyrun/quick/config.rb +56 -0
- data/lib/polyrun/quick/errors.rb +6 -0
- data/lib/polyrun/quick/example_group.rb +111 -0
- data/lib/polyrun/quick/example_runner.rb +129 -0
- data/lib/polyrun/quick/matchers.rb +159 -0
- data/lib/polyrun/quick/matchers_predicates.rb +122 -0
- data/lib/polyrun/quick/reporter.rb +76 -0
- data/lib/polyrun/quick/runner.rb +224 -0
- data/lib/polyrun/quick.rb +1 -0
- data/lib/polyrun/railtie.rb +7 -0
- data/lib/polyrun/reporting/failure_merge.rb +171 -0
- data/lib/polyrun/reporting/junit.rb +126 -0
- data/lib/polyrun/reporting/junit_emit.rb +58 -0
- data/lib/polyrun/reporting/junit_report.rb +87 -0
- data/lib/polyrun/reporting/rspec_failure_fragment_formatter.rb +95 -0
- data/lib/polyrun/reporting/rspec_junit.rb +39 -0
- data/lib/polyrun/rspec_support/active_record_timeout_recovery.rb +51 -0
- data/lib/polyrun/rspec_support/example_debug.rb +167 -0
- data/lib/polyrun/rspec_support/example_debug_instrumentation.rb +73 -0
- data/lib/polyrun/rspec_support/sharded_formatter_compat.rb +57 -0
- data/lib/polyrun/rspec_support.rb +110 -0
- data/lib/polyrun/spec_quality/config.rb +139 -0
- data/lib/polyrun/spec_quality/fragment.rb +39 -0
- data/lib/polyrun/spec_quality/merge.rb +78 -0
- data/lib/polyrun/spec_quality/minitest_hook.rb +42 -0
- data/lib/polyrun/spec_quality/plan_loader.rb +47 -0
- data/lib/polyrun/spec_quality/profile.rb +102 -0
- data/lib/polyrun/spec_quality/report.rb +363 -0
- data/lib/polyrun/spec_quality/rspec_hook.rb +55 -0
- data/lib/polyrun/spec_quality/sql_counter.rb +34 -0
- data/lib/polyrun/spec_quality.rb +212 -0
- data/lib/polyrun/sus_support.rb +55 -0
- data/lib/polyrun/templates/POLYRUN.md +51 -0
- data/lib/polyrun/templates/ci_matrix.polyrun.yml +22 -0
- data/lib/polyrun/templates/minimal_gem.polyrun.yml +13 -0
- data/lib/polyrun/templates/polyrun_hooks_spec_quality.rb +12 -0
- data/lib/polyrun/templates/polyrun_spec_quality.yml +20 -0
- data/lib/polyrun/templates/rails_prepare.polyrun.yml +51 -0
- data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
- data/lib/polyrun/timing/merge.rb +36 -0
- data/lib/polyrun/timing/rspec_example_formatter.rb +60 -0
- data/lib/polyrun/timing/stats.rb +76 -0
- data/lib/polyrun/timing/summary.rb +78 -0
- data/lib/polyrun/timing/variance_report.rb +51 -0
- data/lib/polyrun/version.rb +3 -0
- data/lib/polyrun/worker_output.rb +181 -0
- data/lib/polyrun/worker_output_forwarders.rb +94 -0
- data/lib/polyrun/worker_ping.rb +77 -0
- data/lib/polyrun.rb +59 -0
- data/polyrun.gemspec +49 -0
- data/sig/polyrun/cli.rbs +6 -0
- data/sig/polyrun/config.rbs +20 -0
- data/sig/polyrun/debug.rbs +12 -0
- data/sig/polyrun/log.rbs +12 -0
- data/sig/polyrun/minitest_support.rbs +7 -0
- data/sig/polyrun/quick.rbs +19 -0
- data/sig/polyrun/rspec_support.rbs +21 -0
- data/sig/polyrun/sus_support.rbs +7 -0
- data/sig/polyrun/worker_output.rbs +11 -0
- data/sig/polyrun/worker_ping.rbs +10 -0
- data/sig/polyrun.rbs +11 -0
- metadata +463 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
module Polyrun
|
|
2
|
+
module Database
|
|
3
|
+
# Prepare canonical template DBs with one +bin/rails db:prepare+ (all +DATABASE_URL*+ keys in one process for multi-DB apps), then create per-shard databases in parallel (PostgreSQL +CREATE DATABASE … TEMPLATE …+).
|
|
4
|
+
# Other ActiveRecord adapters (MySQL, SQL Server, SQLite, …) are not automated here—use +polyrun env+ URLs with your own +db:*+ scripts.
|
|
5
|
+
# Replaces shell loops like +dropdb+ / +createdb -T+ when +polyrun.yml databases:+ lists primary + +connections+.
|
|
6
|
+
module CloneShards
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# See +provision!+ on the singleton class for options.
|
|
10
|
+
def provision!(databases_hash, workers:, rails_root:, migrate: true, replace: true, force_drop: false, dry_run: false, silent: true)
|
|
11
|
+
dh = databases_hash.is_a?(Hash) ? databases_hash : {}
|
|
12
|
+
ensure_postgresql!(dh)
|
|
13
|
+
workers = Integer(workers)
|
|
14
|
+
raise Polyrun::Error, "workers must be >= 1" if workers < 1
|
|
15
|
+
|
|
16
|
+
rails_root = File.expand_path(rails_root)
|
|
17
|
+
|
|
18
|
+
migrate_canonical_databases!(dh, rails_root, dry_run, silent) if migrate
|
|
19
|
+
create_shards_from_plan!(dh, workers, replace, force_drop, dry_run)
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def ensure_postgresql!(dh)
|
|
24
|
+
return if UrlBuilder::ConnectionInfer.postgresql?(dh)
|
|
25
|
+
|
|
26
|
+
adapter = UrlBuilder::ConnectionInfer.infer_adapter_name(dh)
|
|
27
|
+
raise Polyrun::Error,
|
|
28
|
+
"CloneShards is PostgreSQL-only (databases adapter is #{adapter}). " \
|
|
29
|
+
"Use polyrun env for URLs and provision shards yourself."
|
|
30
|
+
end
|
|
31
|
+
private_class_method :ensure_postgresql!
|
|
32
|
+
|
|
33
|
+
def migrate_canonical_databases!(dh, rails_root, dry_run, silent)
|
|
34
|
+
pt = (dh["template_db"] || dh[:template_db]).to_s
|
|
35
|
+
if pt.empty?
|
|
36
|
+
raise Polyrun::Error, "CloneShards: set databases.template_db (and optional connections[].template_db)"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if dry_run
|
|
40
|
+
log = UrlBuilder.template_prepare_env_shell_log(dh)
|
|
41
|
+
Polyrun::Log.warn "would: RAILS_ENV=test #{log} bin/rails db:prepare"
|
|
42
|
+
else
|
|
43
|
+
child_env = ENV.to_h.merge(UrlBuilder.template_prepare_env(dh))
|
|
44
|
+
child_env["RAILS_ENV"] ||= ENV["RAILS_ENV"] || "test"
|
|
45
|
+
Provision.prepare_template!(rails_root: rails_root, env: child_env, silent: silent)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
private_class_method :migrate_canonical_databases!
|
|
49
|
+
|
|
50
|
+
def create_shards_from_plan!(dh, workers, replace, force_drop, dry_run)
|
|
51
|
+
conn = UrlBuilder::Connection.resolve_connection(dh)
|
|
52
|
+
if dry_run
|
|
53
|
+
workers.times do |shard_index|
|
|
54
|
+
plan = UrlBuilder.shard_database_plan(dh, shard_index: shard_index)
|
|
55
|
+
if plan.empty?
|
|
56
|
+
raise Polyrun::Error, "CloneShards: empty shard plan for shard_index=#{shard_index}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
plan.each { |row| create_one_shard!(row, replace, force_drop, dry_run, conn) }
|
|
60
|
+
end
|
|
61
|
+
return
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
threads = workers.times.map do |shard_index|
|
|
65
|
+
Thread.new do
|
|
66
|
+
plan = UrlBuilder.shard_database_plan(dh, shard_index: shard_index)
|
|
67
|
+
if plan.empty?
|
|
68
|
+
raise Polyrun::Error, "CloneShards: empty shard plan for shard_index=#{shard_index}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
plan.each { |row| create_one_shard!(row, replace, force_drop, dry_run, conn) }
|
|
72
|
+
rescue => e
|
|
73
|
+
raise Polyrun::Error, "CloneShards shard_index=#{shard_index}: #{e.message}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
threads.each(&:join)
|
|
77
|
+
end
|
|
78
|
+
private_class_method :create_shards_from_plan!
|
|
79
|
+
|
|
80
|
+
def create_one_shard!(row, replace, force_drop, dry_run, conn)
|
|
81
|
+
new_db = row[:new_db].to_s
|
|
82
|
+
tmpl = row[:template_db].to_s
|
|
83
|
+
if dry_run
|
|
84
|
+
Polyrun::Log.warn "would: DROP DATABASE IF EXISTS #{new_db}" if replace
|
|
85
|
+
Polyrun::Log.warn "would: CREATE DATABASE #{new_db} TEMPLATE #{tmpl}"
|
|
86
|
+
return
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
opts = provision_connection_options(conn)
|
|
90
|
+
Provision.drop_database_if_exists!(database: new_db, force: force_drop, **opts) if replace
|
|
91
|
+
Provision.create_database_from_template!(new_db: new_db, template_db: tmpl, **opts)
|
|
92
|
+
end
|
|
93
|
+
private_class_method :create_one_shard!
|
|
94
|
+
|
|
95
|
+
def provision_connection_options(conn)
|
|
96
|
+
{
|
|
97
|
+
host: conn[:host],
|
|
98
|
+
port: conn[:port],
|
|
99
|
+
username: conn[:user],
|
|
100
|
+
password: conn[:password]
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
private_class_method :provision_connection_options
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "open3"
|
|
2
|
+
require "shellwords"
|
|
3
|
+
require_relative "../process_stdio"
|
|
4
|
+
|
|
5
|
+
module Polyrun
|
|
6
|
+
module Database
|
|
7
|
+
# PostgreSQL-only provisioning via +psql+ / +createdb+ (spec2 §5.3). No +pg+ gem.
|
|
8
|
+
# For other adapters, use Rails tasks or vendor CLIs; +Polyrun::Database::UrlBuilder+ still emits +DATABASE_URL+ for supported schemes.
|
|
9
|
+
module Provision
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def quote_ident(name)
|
|
13
|
+
'"' + name.to_s.gsub('"', '""') + '"'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# +DROP DATABASE IF EXISTS name;+ — maintenance DB +postgres+ (or +maintenance_db+).
|
|
17
|
+
def drop_database_if_exists!(database:, host: nil, port: nil, username: nil, password: nil, maintenance_db: "postgres", force: false)
|
|
18
|
+
host ||= ENV["PGHOST"] || "localhost"
|
|
19
|
+
port ||= ENV["PGPORT"] || "5432"
|
|
20
|
+
username ||= ENV["PGUSER"] || "postgres"
|
|
21
|
+
|
|
22
|
+
sql =
|
|
23
|
+
if force
|
|
24
|
+
"DROP DATABASE IF EXISTS #{quote_ident(database)} WITH (FORCE);"
|
|
25
|
+
else
|
|
26
|
+
"DROP DATABASE IF EXISTS #{quote_ident(database)};"
|
|
27
|
+
end
|
|
28
|
+
run_psql!(sql, host: host, port: port, username: username, password: password, maintenance_db: maintenance_db, failure_prefix: "drop database failed")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# CREATE DATABASE new_db TEMPLATE template_db — connects to maintenance DB +postgres+.
|
|
32
|
+
def create_database_from_template!(new_db:, template_db:, host: nil, port: nil, username: nil, password: nil, maintenance_db: "postgres")
|
|
33
|
+
host ||= ENV["PGHOST"] || "localhost"
|
|
34
|
+
port ||= ENV["PGPORT"] || "5432"
|
|
35
|
+
username ||= ENV["PGUSER"] || "postgres"
|
|
36
|
+
|
|
37
|
+
sql = "CREATE DATABASE #{quote_ident(new_db)} TEMPLATE #{quote_ident(template_db)};"
|
|
38
|
+
run_psql!(sql, host: host, port: port, username: username, password: password, maintenance_db: maintenance_db, failure_prefix: "create database failed")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def run_psql!(sql, host:, port:, username:, password:, maintenance_db:, failure_prefix:)
|
|
42
|
+
cmd = ["psql", "-U", username, "-h", host, "-p", port.to_s, "-d", maintenance_db, "-v", "ON_ERROR_STOP=1", "-c", sql]
|
|
43
|
+
_out, err, st =
|
|
44
|
+
if password.nil? || password.to_s.empty?
|
|
45
|
+
Open3.capture3(*cmd)
|
|
46
|
+
else
|
|
47
|
+
Open3.capture3(ENV.to_h.merge("PGPASSWORD" => password.to_s), *cmd)
|
|
48
|
+
end
|
|
49
|
+
raise Polyrun::Error, "#{failure_prefix}: #{err}" unless st.success?
|
|
50
|
+
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
private_class_method :run_psql!
|
|
54
|
+
|
|
55
|
+
# Runs +bin/rails db:prepare+ with merged ENV (+DATABASE_URL+ for primary, +CACHE_DATABASE_URL+, etc.).
|
|
56
|
+
# Multi-DB Rails apps must pass all template URLs in one invocation so each DB uses its own +migrations_paths+.
|
|
57
|
+
# Uses +db:prepare+ (not +db:migrate+ alone) so empty template databases load +schema.rb+ first;
|
|
58
|
+
# apps that squash or archive migrations and keep only incremental files need that path.
|
|
59
|
+
#
|
|
60
|
+
# Streams stdout/stderr to the terminal by default. With +silent: true+, redirects child stdio
|
|
61
|
+
# to +File::NULL+ (no live output; non-interactive).
|
|
62
|
+
def prepare_template!(rails_root:, env:, silent: true)
|
|
63
|
+
exe = File.join(rails_root, "bin", "rails")
|
|
64
|
+
raise Polyrun::Error, "Provision: missing #{exe}" unless File.executable?(exe)
|
|
65
|
+
|
|
66
|
+
child_env = ENV.to_h.merge(env)
|
|
67
|
+
child_env["RAILS_ENV"] ||= ENV["RAILS_ENV"] || "test"
|
|
68
|
+
st, out, err = Polyrun::ProcessStdio.spawn_wait(
|
|
69
|
+
child_env,
|
|
70
|
+
exe,
|
|
71
|
+
"db:prepare",
|
|
72
|
+
chdir: rails_root,
|
|
73
|
+
silent: silent
|
|
74
|
+
)
|
|
75
|
+
unless st.success?
|
|
76
|
+
raise Polyrun::Error, Polyrun::ProcessStdio.format_failure_message("db:prepare", st, out, err)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
true
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Polyrun
|
|
2
|
+
module Database
|
|
3
|
+
# ENV helpers for sharded test databases (parallel_tests–style), stdlib only.
|
|
4
|
+
module Shard
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
# Builds a hash of suggested ENV vars for this shard (strings).
|
|
8
|
+
def env_map(shard_index:, shard_total:, base_database: nil)
|
|
9
|
+
idx = Integer(shard_index)
|
|
10
|
+
tot = Integer(shard_total)
|
|
11
|
+
raise Polyrun::Error, "shard_index out of range" if idx < 0 || idx >= tot
|
|
12
|
+
|
|
13
|
+
out = {
|
|
14
|
+
"POLYRUN_SHARD_INDEX" => idx.to_s,
|
|
15
|
+
"POLYRUN_SHARD_TOTAL" => tot.to_s
|
|
16
|
+
}
|
|
17
|
+
out["TEST_ENV_NUMBER"] = (idx + 1).to_s if tot > 1
|
|
18
|
+
if base_database && !base_database.to_s.empty?
|
|
19
|
+
out["POLYRUN_TEST_DATABASE"] = expand_database_name(base_database.to_s, idx)
|
|
20
|
+
end
|
|
21
|
+
out
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def expand_database_name(template, shard_index)
|
|
25
|
+
template.gsub("%{shard}", Integer(shard_index).to_s).gsub("%<shard>d", format("%d", Integer(shard_index)))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Common URL transform: shard suffix on the database segment (+scheme://host/...+) or +sqlite3:+ path.
|
|
29
|
+
def database_url_with_shard(url, shard_index)
|
|
30
|
+
return url if url.nil? || url.to_s.empty?
|
|
31
|
+
|
|
32
|
+
u = url.to_s
|
|
33
|
+
return u if u.start_with?("http://", "https://", "file://")
|
|
34
|
+
|
|
35
|
+
if u.match?(/\Asqlite3:/i)
|
|
36
|
+
path = u.sub(/\Asqlite3:/i, "")
|
|
37
|
+
if path.match?(%r{([^/]+?)(\.sqlite3)\z}i)
|
|
38
|
+
idx = Integer(shard_index)
|
|
39
|
+
new_path = path.sub(%r{([^/]+?)(\.sqlite3)\z}i) { "#{$1}_#{idx}#{$2}" }
|
|
40
|
+
return "sqlite3:#{new_path}"
|
|
41
|
+
end
|
|
42
|
+
return u
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
return u unless u.match?(%r{\A[a-z][a-z0-9+.-]*://}i)
|
|
46
|
+
|
|
47
|
+
if (m = u.match(%r{\A[a-z][a-z0-9+.-]*://[^/?#]+/([^/?]+)(\?|$)}i))
|
|
48
|
+
base = m[1]
|
|
49
|
+
suffixed = "#{base}_#{Integer(shard_index)}"
|
|
50
|
+
u.sub(%r{/#{Regexp.escape(base)}(\?|$)}, "/#{suffixed}\\1")
|
|
51
|
+
else
|
|
52
|
+
Polyrun::Log.warn "polyrun database: URL has no database segment; shard suffix skipped: #{u}"
|
|
53
|
+
u
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def print_exports(shard_index:, shard_total:, base_database: nil)
|
|
58
|
+
env_map(shard_index: shard_index, shard_total: shard_total, base_database: base_database).each do |k, v|
|
|
59
|
+
Polyrun::Log.puts %(export #{k}=#{v})
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module Polyrun
|
|
2
|
+
module Database
|
|
3
|
+
module UrlBuilder
|
|
4
|
+
# Infer canonical adapter name from +polyrun.yml+ +databases:+ hash.
|
|
5
|
+
module ConnectionInfer
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
INFER_ADAPTER_FROM_NESTED = [
|
|
9
|
+
%w[postgresql postgresql],
|
|
10
|
+
%w[trilogy trilogy],
|
|
11
|
+
%w[mysql2 mysql2],
|
|
12
|
+
%w[mysql mysql2],
|
|
13
|
+
%w[sqlserver sqlserver],
|
|
14
|
+
%w[mssql sqlserver],
|
|
15
|
+
%w[sqlite3 sqlite3],
|
|
16
|
+
%w[sqlite sqlite3],
|
|
17
|
+
%w[mongodb mongodb],
|
|
18
|
+
%w[mongo mongodb]
|
|
19
|
+
].freeze
|
|
20
|
+
|
|
21
|
+
def infer_adapter_name(dh)
|
|
22
|
+
explicit = (dh["adapter"] || dh[:adapter]).to_s.strip.downcase
|
|
23
|
+
explicit = normalize_adapter_alias(explicit)
|
|
24
|
+
return explicit unless explicit.empty?
|
|
25
|
+
|
|
26
|
+
INFER_ADAPTER_FROM_NESTED.each do |key, name|
|
|
27
|
+
return name if nested_hash?(dh, key)
|
|
28
|
+
end
|
|
29
|
+
"postgresql"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def postgresql?(databases_hash)
|
|
33
|
+
infer_adapter_name(databases_hash.is_a?(Hash) ? databases_hash : {}) == "postgresql"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def normalize_adapter_alias(name)
|
|
37
|
+
case name
|
|
38
|
+
when "postgres", "pg" then "postgresql"
|
|
39
|
+
when "mysql" then "mysql2"
|
|
40
|
+
when "mongo" then "mongodb"
|
|
41
|
+
when "mssql" then "sqlserver"
|
|
42
|
+
when "sqlite" then "sqlite3"
|
|
43
|
+
else name
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def nested_hash?(dh, key)
|
|
48
|
+
dh[key].is_a?(Hash) || dh[key.to_sym].is_a?(Hash)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "uri"
|
|
2
|
+
|
|
3
|
+
module Polyrun
|
|
4
|
+
module Database
|
|
5
|
+
module UrlBuilder
|
|
6
|
+
# String construction for +DATABASE_URL+ values.
|
|
7
|
+
module ConnectionUrlBuilders
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def build_sqlite_url(database)
|
|
11
|
+
"sqlite3:#{database}"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def build_url_authority(scheme, host, port, user, password, database)
|
|
15
|
+
if password && !password.to_s.empty?
|
|
16
|
+
u = URI.encode_www_form_component(user.to_s)
|
|
17
|
+
p = URI.encode_www_form_component(password.to_s)
|
|
18
|
+
"#{scheme}://#{u}:#{p}@#{host}:#{port}/#{database}"
|
|
19
|
+
elsif !user.to_s.empty?
|
|
20
|
+
u = URI.encode_www_form_component(user.to_s)
|
|
21
|
+
"#{scheme}://#{u}@#{host}:#{port}/#{database}"
|
|
22
|
+
else
|
|
23
|
+
"#{scheme}://#{host}:#{port}/#{database}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def build_mongodb_url(host, port, user, password, database)
|
|
28
|
+
if user.to_s.empty?
|
|
29
|
+
return "mongodb://#{host}:#{port}/#{database}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
u = URI.encode_www_form_component(user.to_s)
|
|
33
|
+
if password && !password.to_s.empty?
|
|
34
|
+
p = URI.encode_www_form_component(password.to_s)
|
|
35
|
+
"mongodb://#{u}:#{p}@#{host}:#{port}/#{database}"
|
|
36
|
+
else
|
|
37
|
+
"mongodb://#{u}@#{host}:#{port}/#{database}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
require_relative "connection/infer"
|
|
2
|
+
require_relative "connection/url_builders"
|
|
3
|
+
|
|
4
|
+
module Polyrun
|
|
5
|
+
module Database
|
|
6
|
+
module UrlBuilder
|
|
7
|
+
# Adapter detection, ENV fallbacks, and URL string construction (Rails +DATABASE_URL+ conventions).
|
|
8
|
+
module Connection
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def resolve_connection(databases_hash)
|
|
12
|
+
dh = normalize_hash(databases_hash)
|
|
13
|
+
profile = connection_profile(dh)
|
|
14
|
+
blk = merged_connection_block(dh, profile)
|
|
15
|
+
scheme = profile[:scheme]
|
|
16
|
+
host = (blk["host"] || env_first(profile[:env_host]) || profile[:default_host]).to_s
|
|
17
|
+
port = (blk["port"] || env_first(profile[:env_port]) || profile[:default_port]).to_s
|
|
18
|
+
user = (blk["username"] || blk["user"] || env_first(profile[:env_user]) || profile[:default_user]).to_s
|
|
19
|
+
password = blk["password"] || env_first(profile[:env_password])
|
|
20
|
+
|
|
21
|
+
{
|
|
22
|
+
scheme: scheme,
|
|
23
|
+
host: host,
|
|
24
|
+
port: port,
|
|
25
|
+
user: user,
|
|
26
|
+
password: password
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def build_database_url(database, conn)
|
|
31
|
+
scheme = conn[:scheme].to_s
|
|
32
|
+
host = conn[:host].to_s
|
|
33
|
+
port = conn[:port].to_s
|
|
34
|
+
user = conn[:user].to_s
|
|
35
|
+
password = conn[:password]
|
|
36
|
+
db = database.to_s
|
|
37
|
+
|
|
38
|
+
case scheme
|
|
39
|
+
when "postgres"
|
|
40
|
+
ConnectionUrlBuilders.build_url_authority("postgres", host, port, user, password, db)
|
|
41
|
+
when "mysql2", "trilogy"
|
|
42
|
+
ConnectionUrlBuilders.build_url_authority(scheme, host, port, user, password, db)
|
|
43
|
+
when "sqlserver"
|
|
44
|
+
ConnectionUrlBuilders.build_url_authority("sqlserver", host, port, user, password, db)
|
|
45
|
+
when "mongodb"
|
|
46
|
+
ConnectionUrlBuilders.build_mongodb_url(host, port, user, password, db)
|
|
47
|
+
when "sqlite3"
|
|
48
|
+
ConnectionUrlBuilders.build_sqlite_url(db)
|
|
49
|
+
else
|
|
50
|
+
raise Polyrun::Error, "unsupported URL scheme: #{scheme.inspect}"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def connection_profile(dh)
|
|
55
|
+
name = ConnectionInfer.infer_adapter_name(dh)
|
|
56
|
+
case name
|
|
57
|
+
when "postgresql"
|
|
58
|
+
{
|
|
59
|
+
scheme: "postgres",
|
|
60
|
+
nested_key: "postgresql",
|
|
61
|
+
default_host: "localhost",
|
|
62
|
+
default_port: "5432",
|
|
63
|
+
default_user: "postgres",
|
|
64
|
+
env_host: %w[PGHOST],
|
|
65
|
+
env_port: %w[PGPORT],
|
|
66
|
+
env_user: %w[PGUSER],
|
|
67
|
+
env_password: %w[PGPASSWORD]
|
|
68
|
+
}
|
|
69
|
+
when "mysql2"
|
|
70
|
+
nested_mysql_key(dh)
|
|
71
|
+
when "trilogy"
|
|
72
|
+
{
|
|
73
|
+
scheme: "trilogy",
|
|
74
|
+
nested_key: "trilogy",
|
|
75
|
+
default_host: "localhost",
|
|
76
|
+
default_port: "3306",
|
|
77
|
+
default_user: "root",
|
|
78
|
+
env_host: %w[MYSQL_HOST MYSQL_ADDRESS TRILOGY_HOST],
|
|
79
|
+
env_port: %w[MYSQL_PORT MYSQL_TCP_PORT TRILOGY_PORT],
|
|
80
|
+
env_user: %w[MYSQL_USER MYSQL_USERNAME TRILOGY_USER],
|
|
81
|
+
env_password: %w[MYSQL_PASSWORD MYSQL_PWD TRILOGY_PASSWORD]
|
|
82
|
+
}
|
|
83
|
+
when "sqlserver"
|
|
84
|
+
nested =
|
|
85
|
+
if ConnectionInfer.nested_hash?(dh, "sqlserver")
|
|
86
|
+
"sqlserver"
|
|
87
|
+
else
|
|
88
|
+
"mssql"
|
|
89
|
+
end
|
|
90
|
+
{
|
|
91
|
+
scheme: "sqlserver",
|
|
92
|
+
nested_key: nested,
|
|
93
|
+
default_host: "localhost",
|
|
94
|
+
default_port: "1433",
|
|
95
|
+
default_user: "sa",
|
|
96
|
+
env_host: %w[SQLSERVER_HOST MSSQL_HOST TDS_HOST],
|
|
97
|
+
env_port: %w[SQLSERVER_PORT MSSQL_PORT TDS_PORT],
|
|
98
|
+
env_user: %w[SQLSERVER_USER MSSQL_USER SA_USER],
|
|
99
|
+
env_password: %w[SQLSERVER_PASSWORD MSSQL_PASSWORD SA_PASSWORD]
|
|
100
|
+
}
|
|
101
|
+
when "sqlite3"
|
|
102
|
+
nested =
|
|
103
|
+
if ConnectionInfer.nested_hash?(dh, "sqlite3")
|
|
104
|
+
"sqlite3"
|
|
105
|
+
else
|
|
106
|
+
"sqlite"
|
|
107
|
+
end
|
|
108
|
+
{
|
|
109
|
+
scheme: "sqlite3",
|
|
110
|
+
nested_key: nested,
|
|
111
|
+
default_host: "",
|
|
112
|
+
default_port: "",
|
|
113
|
+
default_user: "",
|
|
114
|
+
env_host: [],
|
|
115
|
+
env_port: [],
|
|
116
|
+
env_user: [],
|
|
117
|
+
env_password: []
|
|
118
|
+
}
|
|
119
|
+
when "mongodb"
|
|
120
|
+
nested_mongo_key(dh)
|
|
121
|
+
else
|
|
122
|
+
raise Polyrun::Error, "unsupported databases.adapter: #{name.inspect}"
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def nested_mysql_key(dh)
|
|
127
|
+
nested =
|
|
128
|
+
if ConnectionInfer.nested_hash?(dh, "mysql2")
|
|
129
|
+
"mysql2"
|
|
130
|
+
else
|
|
131
|
+
"mysql"
|
|
132
|
+
end
|
|
133
|
+
{
|
|
134
|
+
scheme: "mysql2",
|
|
135
|
+
nested_key: nested,
|
|
136
|
+
default_host: "localhost",
|
|
137
|
+
default_port: "3306",
|
|
138
|
+
default_user: "root",
|
|
139
|
+
env_host: %w[MYSQL_HOST MYSQL_ADDRESS],
|
|
140
|
+
env_port: %w[MYSQL_PORT MYSQL_TCP_PORT],
|
|
141
|
+
env_user: %w[MYSQL_USER MYSQL_USERNAME],
|
|
142
|
+
env_password: %w[MYSQL_PASSWORD MYSQL_PWD]
|
|
143
|
+
}
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def nested_mongo_key(dh)
|
|
147
|
+
nested =
|
|
148
|
+
if ConnectionInfer.nested_hash?(dh, "mongodb")
|
|
149
|
+
"mongodb"
|
|
150
|
+
else
|
|
151
|
+
"mongo"
|
|
152
|
+
end
|
|
153
|
+
{
|
|
154
|
+
scheme: "mongodb",
|
|
155
|
+
nested_key: nested,
|
|
156
|
+
default_host: "localhost",
|
|
157
|
+
default_port: "27017",
|
|
158
|
+
default_user: "",
|
|
159
|
+
env_host: %w[MONGO_HOST MONGODB_HOST],
|
|
160
|
+
env_port: %w[MONGO_PORT MONGODB_PORT],
|
|
161
|
+
env_user: %w[MONGO_USER MONGODB_USER],
|
|
162
|
+
env_password: %w[MONGO_PASSWORD MONGODB_PASSWORD]
|
|
163
|
+
}
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def merged_connection_block(dh, profile)
|
|
167
|
+
nk = profile[:nested_key]
|
|
168
|
+
nested = dh[nk].is_a?(Hash) ? normalize_hash(dh[nk]) : {}
|
|
169
|
+
top = dh.slice("host", "port", "username", "user", "password").transform_keys(&:to_s)
|
|
170
|
+
nested.merge(top)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def normalize_hash(h)
|
|
174
|
+
h.is_a?(Hash) ? h.transform_keys(&:to_s) : {}
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def env_first(keys)
|
|
178
|
+
Array(keys).each do |k|
|
|
179
|
+
v = ENV[k]
|
|
180
|
+
return v if present?(v)
|
|
181
|
+
end
|
|
182
|
+
nil
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def present?(s)
|
|
186
|
+
!s.nil? && !s.to_s.empty?
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Polyrun
|
|
2
|
+
module Database
|
|
3
|
+
module UrlBuilder
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
def template_prepare_env_fill_connections!(dh, primary_template, out)
|
|
7
|
+
Array(dh["connections"] || dh[:connections]).each do |c|
|
|
8
|
+
nm = (c["name"] || c[:name]).to_s
|
|
9
|
+
key = (c["env_key"] || c[:env_key]).to_s.strip
|
|
10
|
+
key = "DATABASE_URL_#{nm.upcase.tr("-", "_")}" if key.empty? && !nm.empty?
|
|
11
|
+
next if key.empty?
|
|
12
|
+
|
|
13
|
+
tname = (c["template_db"] || c[:template_db]).to_s
|
|
14
|
+
tname = primary_template if tname.empty?
|
|
15
|
+
out[key] = url_for_database_name(dh, tname)
|
|
16
|
+
end
|
|
17
|
+
out
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|