decidim-apartment 0.2.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 +7 -0
- data/LICENSE-AGPLv3.txt +661 -0
- data/README.md +167 -0
- data/Rakefile +405 -0
- data/app/commands/decidim/apartment/create_tenant_command.rb +94 -0
- data/app/commands/decidim/apartment/noop_settings_tab_command.rb +13 -0
- data/app/commands/decidim/apartment/performance/create_seed_tentant.rb +194 -0
- data/app/controllers/decidim/apartment/tenants_controller.rb +29 -0
- data/app/forms/decidim/apartment/admin/organization_settings_form.rb +44 -0
- data/app/forms/decidim/apartment/tenant_form.rb +35 -0
- data/app/helpers/decidim/apartment/dashboard_helper.rb +17 -0
- data/app/models/decidim/apartment/distribution_host.rb +21 -0
- data/app/models/decidim/apartment/distribution_key.rb +91 -0
- data/app/overrides/apartment_edit_organization.rb +21 -0
- data/app/overrides/apartment_system_asset.rb +10 -0
- data/app/packs/entrypoints/apartment_system_styles.scss +1 -0
- data/app/packs/stylesheets/decidim/apartment/system/_apartment_system_styles.scss +95 -0
- data/app/services/active_storage/service/apartment_disk_storage_service.rb +14 -0
- data/app/services/active_storage/service/apartment_s3_storage_service.rb +24 -0
- data/app/views/decidim/apartment/dashboard/_apartment.html.erb +55 -0
- data/app/views/decidim/apartment/dashboard/show.html.erb +3 -0
- data/app/views/decidim/apartment/organizations/_organizations_list.html.erb +45 -0
- data/app/views/decidim/apartment/organizations/index.html.erb +9 -0
- data/app/views/decidim/apartment/organizations/new.html.erb +11 -0
- data/config/assets.rb +15 -0
- data/config/locales/decidim_apartment.en.yml +58 -0
- data/config/locales/decidim_apartment_toggle.en.yml +15 -0
- data/db/migrate/20250814075441_add_apartment_distribution_key.rb +15 -0
- data/db/migrate/20260907100000_add_apartment_distribution_hosts.rb +32 -0
- data/lib/decidim/apartment/active_storage_wrap.rb +51 -0
- data/lib/decidim/apartment/allowed_hosts.rb +78 -0
- data/lib/decidim/apartment/cache_namespace.rb +38 -0
- data/lib/decidim/apartment/config_registry.rb +102 -0
- data/lib/decidim/apartment/configuration.rb +11 -0
- data/lib/decidim/apartment/convert/attachment_integrity.rb +130 -0
- data/lib/decidim/apartment/convert/constraint_graph_pruner.rb +183 -0
- data/lib/decidim/apartment/convert/errors.rb +9 -0
- data/lib/decidim/apartment/convert/host_gate.rb +76 -0
- data/lib/decidim/apartment/convert/org_distribution_report.rb +106 -0
- data/lib/decidim/apartment/convert/org_metrics.rb +164 -0
- data/lib/decidim/apartment/convert/organization_converter.rb +154 -0
- data/lib/decidim/apartment/convert/organization_pruner.rb +210 -0
- data/lib/decidim/apartment/convert/organization_resolver.rb +386 -0
- data/lib/decidim/apartment/convert/paper_trail_report.rb +127 -0
- data/lib/decidim/apartment/convert/preflight.rb +140 -0
- data/lib/decidim/apartment/convert/schema_cloner.rb +157 -0
- data/lib/decidim/apartment/convert/storage_preconditions.rb +43 -0
- data/lib/decidim/apartment/convert/storage_relocator.rb +118 -0
- data/lib/decidim/apartment/convert/table_registry.rb +109 -0
- data/lib/decidim/apartment/convert.rb +20 -0
- data/lib/decidim/apartment/demo/blob.rb +31 -0
- data/lib/decidim/apartment/demo/city.rb +107 -0
- data/lib/decidim/apartment/demo/education.rb +83 -0
- data/lib/decidim/apartment/demo/foss.rb +153 -0
- data/lib/decidim/apartment/demo/homepage.rb +39 -0
- data/lib/decidim/apartment/demo/localized.rb +27 -0
- data/lib/decidim/apartment/demo/provision.rb +107 -0
- data/lib/decidim/apartment/demo/runner.rb +66 -0
- data/lib/decidim/apartment/demo/system_admin.rb +24 -0
- data/lib/decidim/apartment/demo.rb +19 -0
- data/lib/decidim/apartment/elevator.rb +20 -0
- data/lib/decidim/apartment/engine.rb +140 -0
- data/lib/decidim/apartment/excluded_model_pin.rb +40 -0
- data/lib/decidim/apartment/host_registry.rb +71 -0
- data/lib/decidim/apartment/import/confirm_prompt.rb +46 -0
- data/lib/decidim/apartment/import/database_restorer.rb +105 -0
- data/lib/decidim/apartment/import/dump_host_extractor.rb +67 -0
- data/lib/decidim/apartment/import/dump_preview.rb +96 -0
- data/lib/decidim/apartment/import/dump_transformer.rb +87 -0
- data/lib/decidim/apartment/import/errors.rb +9 -0
- data/lib/decidim/apartment/import/provision_tenant.rb +50 -0
- data/lib/decidim/apartment/import/schema_auditor.rb +118 -0
- data/lib/decidim/apartment/import/storage_migrator.rb +71 -0
- data/lib/decidim/apartment/import/tenant_context.rb +226 -0
- data/lib/decidim/apartment/import/tenant_importer.rb +73 -0
- data/lib/decidim/apartment/import/verifier.rb +58 -0
- data/lib/decidim/apartment/import.rb +15 -0
- data/lib/decidim/apartment/ops/attribute_cryptor.rb +68 -0
- data/lib/decidim/apartment/ops/ciphertext_rewriter.rb +230 -0
- data/lib/decidim/apartment/ops/encrypted_attribute_catalog.rb +106 -0
- data/lib/decidim/apartment/ops/encrypted_attribute_registry.rb +74 -0
- data/lib/decidim/apartment/ops/postgres_permissions.rb +179 -0
- data/lib/decidim/apartment/ops/secret_key_preflight.rb +94 -0
- data/lib/decidim/apartment/ops/secret_key_rotation.rb +226 -0
- data/lib/decidim/apartment/ops/storage_transfer.rb +188 -0
- data/lib/decidim/apartment/overrides/active_job_with_tenant.rb +38 -0
- data/lib/decidim/apartment/overrides/core_seeds.rb +31 -0
- data/lib/decidim/apartment/overrides/create_organization_command.rb +69 -0
- data/lib/decidim/apartment/overrides/dashboard_controller.rb +30 -0
- data/lib/decidim/apartment/overrides/decidim_seed.rb +16 -0
- data/lib/decidim/apartment/overrides/organizations_controller_overrides.rb +75 -0
- data/lib/decidim/apartment/overrides/postgresql_enable_extension.rb +15 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_adapter.rb +34 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_import.rb +35 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_search_path.rb +15 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_statements.rb +17 -0
- data/lib/decidim/apartment/overrides/seeds.rb +52 -0
- data/lib/decidim/apartment/overrides/tenant_init_without_database.rb +19 -0
- data/lib/decidim/apartment/overrides/update_organization_command.rb +38 -0
- data/lib/decidim/apartment/postgres_extensions.rb +198 -0
- data/lib/decidim/apartment/rake_replace.rb +21 -0
- data/lib/decidim/apartment/schema_search_path.rb +43 -0
- data/lib/decidim/apartment/system_organization.rb +67 -0
- data/lib/decidim/apartment/test/factories.rb +11 -0
- data/lib/decidim/apartment/test/seeds/association_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/association_2.jpeg +0 -0
- data/lib/decidim/apartment/test/seeds/association_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/association_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Dusty Grass.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Hyper Blue.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Moonwalker.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Neon Green.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Visual Blue.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_hinehouse.png +0 -0
- data/lib/decidim/apartment/test/seeds/city_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_4.webp +0 -0
- data/lib/decidim/apartment/test/seeds/city_5.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/debian.jpeg +0 -0
- data/lib/decidim/apartment/test/seeds/democratic_school_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/drupal.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/foss.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/immich.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/kid_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/kid_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/montessori_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/montessori_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighboor_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighbor_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighbor_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_55_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00)(1).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_55_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_1_10_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_1_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_5_10_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_5_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_new_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_new_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_new_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_04.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_05.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_06.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_07.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_08.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_09.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_10.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_11.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_12.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_13.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_14.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_15.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_16.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_04.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_05.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_06.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_07.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_08.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_09.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_10.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_11.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_12.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_13.png +0 -0
- data/lib/decidim/apartment/test/seeds/rails.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_5.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/silverstripe.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_1.webp +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/world-map-colors.png +0 -0
- data/lib/decidim/apartment/version.rb +44 -0
- data/lib/decidim/apartment.rb +69 -0
- data/lib/tasks/decidim_apartment_check_install.rake +58 -0
- data/lib/tasks/decidim_apartment_convert.rake +60 -0
- data/lib/tasks/decidim_apartment_gem_manager.rake +62 -0
- data/lib/tasks/decidim_apartment_import.rake +39 -0
- data/lib/tasks/decidim_apartment_install_extension.rake +49 -0
- data/lib/tasks/decidim_apartment_performance_seed.rake +12 -0
- data/lib/tasks/decidim_apartment_rotate.rake +17 -0
- data/lib/tasks/decidim_apartment_storage.rake +27 -0
- data/lib/tasks/decidim_download_your_data_tasks.rake +121 -0
- data/lib/tasks/decidim_mailers.rake +38 -0
- data/lib/tasks/decidim_meetings.rake +30 -0
- data/lib/tasks/decidim_metrics_tasks.rake +29 -0
- data/lib/tasks/decidim_open_data_tasks.rake +24 -0
- data/lib/tasks/decidim_participatory_processes.rake +20 -0
- data/lib/tasks/decidim_reminders_tasks.rake +24 -0
- metadata +361 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Assigns Decidim Awesome / Term Customizer constraint graphs to the keep
|
|
8
|
+
# org before OrganizationPruner's alphabetical walk (parents would be gone).
|
|
9
|
+
class ConstraintGraphPruner
|
|
10
|
+
BATCH_SIZE = 500
|
|
11
|
+
DELETE_SLICE = 1_000
|
|
12
|
+
TERM_GEM = "decidim-term_customizer"
|
|
13
|
+
AWESOME_GEM = "decidim-decidim_awesome"
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def call(keep_org_id:, dry_run:, stats:)
|
|
17
|
+
new(keep_org_id: keep_org_id, dry_run: dry_run, stats: stats).call
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(keep_org_id:, dry_run:, stats:)
|
|
22
|
+
@keep_org_id = keep_org_id.to_i
|
|
23
|
+
@dry_run = dry_run
|
|
24
|
+
@stats = stats
|
|
25
|
+
@handled = []
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call
|
|
29
|
+
prune_term_customizer!
|
|
30
|
+
prune_awesome!
|
|
31
|
+
@handled
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def prune_term_customizer!
|
|
37
|
+
return unless Gem.loaded_specs.has_key?(TERM_GEM)
|
|
38
|
+
|
|
39
|
+
prune_translations!
|
|
40
|
+
prune_translation_sets!
|
|
41
|
+
prune_term_constraints!
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def prune_awesome!
|
|
45
|
+
return unless Gem.loaded_specs.has_key?(AWESOME_GEM)
|
|
46
|
+
|
|
47
|
+
prune_awesome_constraints!
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def prune_translations!
|
|
51
|
+
model = Decidim::TermCustomizer::Translation
|
|
52
|
+
return unless table_ready?(model)
|
|
53
|
+
|
|
54
|
+
walk!(model, :classify_translation)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def prune_translation_sets!
|
|
58
|
+
model = Decidim::TermCustomizer::TranslationSet
|
|
59
|
+
return unless table_ready?(model)
|
|
60
|
+
|
|
61
|
+
walk!(model, :classify_translation_set)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def prune_term_constraints!
|
|
65
|
+
model = Decidim::TermCustomizer::Constraint
|
|
66
|
+
return unless table_ready?(model)
|
|
67
|
+
|
|
68
|
+
walk!(model, :classify_org_column)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def prune_awesome_constraints!
|
|
72
|
+
model = Decidim::DecidimAwesome::ConfigConstraint
|
|
73
|
+
return unless table_ready?(model)
|
|
74
|
+
|
|
75
|
+
walk!(model, :classify_awesome_constraint)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def table_ready?(model)
|
|
79
|
+
model.table_exists?
|
|
80
|
+
rescue StandardError
|
|
81
|
+
false
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def walk!(model, classifier)
|
|
85
|
+
bucket = collect_ids(model, classifier)
|
|
86
|
+
record_table_stats!(model.table_name, bucket)
|
|
87
|
+
delete_records!(model, bucket[:ids])
|
|
88
|
+
@handled << model.table_name
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def collect_ids(model, classifier)
|
|
92
|
+
bucket = { ids: [], own: 0, unknown: 0, samples: [] }
|
|
93
|
+
model.unscoped.find_each(batch_size: BATCH_SIZE) do |record|
|
|
94
|
+
tally!(bucket, send(classifier, record), record.id)
|
|
95
|
+
end
|
|
96
|
+
bucket
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def tally!(bucket, verdict, id)
|
|
100
|
+
case verdict
|
|
101
|
+
when :own then bucket[:own] += 1
|
|
102
|
+
when :unknown
|
|
103
|
+
bucket[:unknown] += 1
|
|
104
|
+
bucket[:samples] << id if bucket[:samples].size < 5
|
|
105
|
+
when :other then bucket[:ids] << id
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def classify_translation(record)
|
|
110
|
+
set = record.translation_set
|
|
111
|
+
return :unknown unless set
|
|
112
|
+
|
|
113
|
+
classify_translation_set(set)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def classify_translation_set(set)
|
|
117
|
+
constraints = set.constraints.to_a
|
|
118
|
+
return :unknown if constraints.empty?
|
|
119
|
+
return :own if constraints.any? { |c| org_id(c) == @keep_org_id }
|
|
120
|
+
|
|
121
|
+
:other
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def classify_org_column(record)
|
|
125
|
+
return :unknown if org_id(record).nil?
|
|
126
|
+
return :own if org_id(record) == @keep_org_id
|
|
127
|
+
|
|
128
|
+
:other
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def classify_awesome_constraint(record)
|
|
132
|
+
config = record.awesome_config
|
|
133
|
+
return :unknown unless config
|
|
134
|
+
return :own if org_id(config) == @keep_org_id
|
|
135
|
+
|
|
136
|
+
:other
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def org_id(record)
|
|
140
|
+
return unless record.respond_to?(:decidim_organization_id)
|
|
141
|
+
return if record.decidim_organization_id.blank?
|
|
142
|
+
|
|
143
|
+
record.decidim_organization_id.to_i
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def record_table_stats!(table, bucket)
|
|
147
|
+
@stats[:kept_unknown] += bucket[:unknown]
|
|
148
|
+
@stats[:kept_own] += bucket[:own]
|
|
149
|
+
@stats[:deleted] += bucket[:ids].size
|
|
150
|
+
@stats[:per_table][table] = {
|
|
151
|
+
own: bucket[:own], other: bucket[:ids].size, unknown: bucket[:unknown],
|
|
152
|
+
keeper: 0, sample_unknown: bucket[:samples]
|
|
153
|
+
}
|
|
154
|
+
log_table_result(
|
|
155
|
+
table, bucket[:ids].size, bucket[:own], bucket[:unknown], bucket[:samples]
|
|
156
|
+
)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def delete_records!(model, ids)
|
|
160
|
+
return if ids.empty? || @dry_run
|
|
161
|
+
|
|
162
|
+
ids.each_slice(DELETE_SLICE) { |slice| model.unscoped.where(id: slice).delete_all }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def log_table_result(table, delete_count, own_count, unknown_count, samples)
|
|
166
|
+
return if delete_count.zero? && own_count.zero? && unknown_count.zero?
|
|
167
|
+
|
|
168
|
+
puts " #{table}: #{table_result_line(delete_count, own_count, unknown_count)}"
|
|
169
|
+
return if samples.empty?
|
|
170
|
+
|
|
171
|
+
puts " sample unknown ids: #{samples.join(", ")}"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def table_result_line(delete_count, own_count, unknown_count)
|
|
175
|
+
prefix = @dry_run ? "would delete" : "deleted"
|
|
176
|
+
return "#{prefix} #{delete_count}, kept #{own_count} own, #{unknown_count} unknown" if delete_count.positive?
|
|
177
|
+
|
|
178
|
+
"kept #{own_count} own, #{unknown_count} unknown"
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Host routing preconditions: primary + secondary hosts must be globally unique.
|
|
8
|
+
class HostGate
|
|
9
|
+
class << self
|
|
10
|
+
def assert_convertible!(org)
|
|
11
|
+
existing = ::Apartment::Tenant.switch("public") do
|
|
12
|
+
DistributionKey.find_by(host: org.host)
|
|
13
|
+
end
|
|
14
|
+
if existing
|
|
15
|
+
resume = ENV["TARGET_TENANT"].to_s
|
|
16
|
+
return if resume.present? && resume == existing.key
|
|
17
|
+
|
|
18
|
+
raise ConvertError,
|
|
19
|
+
"host #{org.host} already has DistributionKey #{existing.key}; " \
|
|
20
|
+
"set TARGET_TENANT=#{existing.key} to resume, or use a free host"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
hosts = [org.host, *Array(org.secondary_hosts)].map { |h| h.to_s.strip }.compact_blank
|
|
24
|
+
conflict = conflicting_hosts(hosts)
|
|
25
|
+
return if conflict.empty?
|
|
26
|
+
|
|
27
|
+
raise ConvertError,
|
|
28
|
+
"host(s) already registered: #{conflict.join(", ")}; " \
|
|
29
|
+
"clear the conflict or set TARGET_TENANT to resume"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def verify!(org, tenant_key)
|
|
33
|
+
distribution_key = DistributionKey.for_host(org.host)
|
|
34
|
+
unless distribution_key&.key == tenant_key
|
|
35
|
+
raise ConvertError,
|
|
36
|
+
"host #{org.host} does not map to tenant #{tenant_key} " \
|
|
37
|
+
"(got #{distribution_key&.key.inspect})"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
Array(org.secondary_hosts).map(&:to_s).map(&:strip).compact_blank.each do |alias_host|
|
|
41
|
+
alias_key = DistributionKey.for_host(alias_host)
|
|
42
|
+
next if alias_key&.key == tenant_key
|
|
43
|
+
|
|
44
|
+
raise ConvertError,
|
|
45
|
+
"secondary host #{alias_host} does not map to tenant #{tenant_key} " \
|
|
46
|
+
"(got #{alias_key&.key.inspect})"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
report_org_settings!(org)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def report_org_settings!(org)
|
|
53
|
+
smtp = org.try(:smtp_settings)
|
|
54
|
+
from = smtp.is_a?(Hash) ? (smtp["from_email"] || smtp[:from_email] || smtp["from"] || smtp[:from]) : nil
|
|
55
|
+
puts "host verify OK: host=#{org.host} tenant mapped"
|
|
56
|
+
puts " smtp from configured: #{from.present? ? "yes (#{from})" : "no"}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def conflicting_hosts(hosts)
|
|
62
|
+
resume = ENV["TARGET_TENANT"].to_s
|
|
63
|
+
::Apartment::Tenant.switch("public") do
|
|
64
|
+
scope = DistributionHost.where(host: hosts)
|
|
65
|
+
if resume.present?
|
|
66
|
+
key = DistributionKey.find_by(key: resume)
|
|
67
|
+
scope = scope.where.not(apartment_distribution_key_id: key.id) if key
|
|
68
|
+
end
|
|
69
|
+
scope.pluck(:host)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Summarizes row counts per organization for tables with decidim_organization_id.
|
|
8
|
+
class OrgDistributionReport
|
|
9
|
+
TABLES = %w(
|
|
10
|
+
decidim_users
|
|
11
|
+
decidim_participatory_processes
|
|
12
|
+
decidim_components
|
|
13
|
+
decidim_action_logs
|
|
14
|
+
).freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def print!(connection:, schema:, keep_org_id: nil)
|
|
18
|
+
new(connection: connection, schema: schema, keep_org_id: keep_org_id&.to_i).print!
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def initialize(connection:, schema:, keep_org_id:)
|
|
23
|
+
@connection = connection
|
|
24
|
+
@schema = schema
|
|
25
|
+
@keep_org_id = keep_org_id
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def print!
|
|
29
|
+
puts "Organization distribution (#{@schema}):"
|
|
30
|
+
TABLES.each { |table| print_table!(table) }
|
|
31
|
+
warn_keep_org_empty!
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def print_table!(table)
|
|
37
|
+
return unless table_exists?(table)
|
|
38
|
+
return unless column_exists?(table, "decidim_organization_id")
|
|
39
|
+
|
|
40
|
+
rows = @connection.select_all(<<~SQL.squish)
|
|
41
|
+
SELECT decidim_organization_id AS org_id, COUNT(*) AS row_count
|
|
42
|
+
FROM #{qualified(table)}
|
|
43
|
+
GROUP BY decidim_organization_id
|
|
44
|
+
ORDER BY row_count DESC, org_id ASC
|
|
45
|
+
SQL
|
|
46
|
+
return if rows.empty?
|
|
47
|
+
|
|
48
|
+
summary = rows.map { |row| "org #{row["org_id"]}=#{row["row_count"]}" }.join(", ")
|
|
49
|
+
puts " #{table}: #{summary}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def warn_keep_org_empty!
|
|
53
|
+
return if @keep_org_id.nil?
|
|
54
|
+
return unless table_exists?("decidim_users")
|
|
55
|
+
return unless column_exists?("decidim_users", "decidim_organization_id")
|
|
56
|
+
|
|
57
|
+
keep_users = @connection.select_value(<<~SQL.squish).to_i
|
|
58
|
+
SELECT COUNT(*) FROM #{qualified("decidim_users")}
|
|
59
|
+
WHERE decidim_organization_id = #{@keep_org_id}
|
|
60
|
+
SQL
|
|
61
|
+
total_users = @connection.select_value(<<~SQL.squish).to_i
|
|
62
|
+
SELECT COUNT(*) FROM #{qualified("decidim_users")}
|
|
63
|
+
SQL
|
|
64
|
+
return if total_users.zero? || keep_users.positive?
|
|
65
|
+
|
|
66
|
+
dominant_org = @connection.select_value(<<~SQL.squish)
|
|
67
|
+
SELECT decidim_organization_id
|
|
68
|
+
FROM #{qualified("decidim_users")}
|
|
69
|
+
GROUP BY decidim_organization_id
|
|
70
|
+
ORDER BY COUNT(*) DESC
|
|
71
|
+
LIMIT 1
|
|
72
|
+
SQL
|
|
73
|
+
|
|
74
|
+
puts ""
|
|
75
|
+
puts "WARNING: keep org #{@keep_org_id} has 0 users (#{total_users} total)."
|
|
76
|
+
puts " Most users belong to org #{dominant_org}. Use ORG_ID=#{dominant_org} for this dump."
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def table_exists?(table)
|
|
80
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
81
|
+
SELECT 1
|
|
82
|
+
FROM pg_tables
|
|
83
|
+
WHERE schemaname = #{@connection.quote(@schema)}
|
|
84
|
+
AND tablename = #{@connection.quote(table)}
|
|
85
|
+
LIMIT 1
|
|
86
|
+
SQL
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def column_exists?(table, column)
|
|
90
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
91
|
+
SELECT 1
|
|
92
|
+
FROM information_schema.columns
|
|
93
|
+
WHERE table_schema = #{@connection.quote(@schema)}
|
|
94
|
+
AND table_name = #{@connection.quote(table)}
|
|
95
|
+
AND column_name = #{@connection.quote(column)}
|
|
96
|
+
LIMIT 1
|
|
97
|
+
SQL
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def qualified(table)
|
|
101
|
+
%("#{@schema}"."#{table}")
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Organization-scoped counts for convert before/after comparison.
|
|
8
|
+
class OrgMetrics
|
|
9
|
+
ORG_SCOPED_TABLES = %w(
|
|
10
|
+
decidim_users
|
|
11
|
+
decidim_participatory_processes
|
|
12
|
+
decidim_components
|
|
13
|
+
decidim_action_logs
|
|
14
|
+
).freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def snapshot(connection:, schema:, org_id:)
|
|
18
|
+
new(connection: connection, schema: schema, org_id: org_id.to_i).snapshot
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def compare!(before:, after:)
|
|
22
|
+
puts "Organization metrics compare:"
|
|
23
|
+
keys = (before.keys | after.keys).sort
|
|
24
|
+
mismatched = []
|
|
25
|
+
keys.each do |key|
|
|
26
|
+
left = before[key].to_i
|
|
27
|
+
right = after[key].to_i
|
|
28
|
+
marker = left == right ? "OK" : "MISMATCH"
|
|
29
|
+
puts " #{key}: before=#{left} after=#{right} [#{marker}]"
|
|
30
|
+
mismatched << key unless left == right
|
|
31
|
+
end
|
|
32
|
+
return if mismatched.empty?
|
|
33
|
+
|
|
34
|
+
raise ConvertError, "organization metrics mismatch: #{mismatched.join(", ")}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(connection:, schema:, org_id:)
|
|
39
|
+
@connection = connection
|
|
40
|
+
@schema = schema
|
|
41
|
+
@org_id = org_id
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def snapshot
|
|
45
|
+
counts = {}
|
|
46
|
+
ORG_SCOPED_TABLES.each do |table|
|
|
47
|
+
next unless table_exists?(table) && column_exists?(table, "decidim_organization_id")
|
|
48
|
+
|
|
49
|
+
counts[table] = count_org_scoped(table)
|
|
50
|
+
end
|
|
51
|
+
counts["decidim_organizations"] = count_organizations
|
|
52
|
+
counts["active_storage_blobs"] = count_resolved_blobs
|
|
53
|
+
counts["active_storage_attachments"] = count_resolved_attachments
|
|
54
|
+
counts["versions"] = count_resolved_versions
|
|
55
|
+
counts["decidim_attachments"] = count_resolved_decidim_attachments
|
|
56
|
+
counts
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def count_org_scoped(table)
|
|
62
|
+
@connection.select_value(<<~SQL.squish).to_i
|
|
63
|
+
SELECT COUNT(*) FROM #{qualified(table)}
|
|
64
|
+
WHERE decidim_organization_id = #{@org_id}
|
|
65
|
+
SQL
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def count_organizations
|
|
69
|
+
return 0 unless table_exists?("decidim_organizations")
|
|
70
|
+
|
|
71
|
+
@connection.select_value(<<~SQL.squish).to_i
|
|
72
|
+
SELECT COUNT(*) FROM #{qualified("decidim_organizations")}
|
|
73
|
+
WHERE id = #{@org_id}
|
|
74
|
+
SQL
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def count_resolved_blobs
|
|
78
|
+
return 0 unless table_exists?("active_storage_blobs")
|
|
79
|
+
|
|
80
|
+
::Apartment::Tenant.switch(@schema) do
|
|
81
|
+
return 0 unless defined?(ActiveStorage::Blob)
|
|
82
|
+
|
|
83
|
+
ActiveStorage::Blob.unscoped.find_each.count do |blob|
|
|
84
|
+
org = OrganizationResolver.resolve(blob)
|
|
85
|
+
org&.id == @org_id
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def count_resolved_attachments
|
|
91
|
+
return 0 unless table_exists?("active_storage_attachments")
|
|
92
|
+
|
|
93
|
+
::Apartment::Tenant.switch(@schema) do
|
|
94
|
+
return 0 unless defined?(ActiveStorage::Attachment)
|
|
95
|
+
|
|
96
|
+
ActiveStorage::Attachment.unscoped.find_each.count do |attachment|
|
|
97
|
+
org = OrganizationResolver.resolve(attachment)
|
|
98
|
+
org&.id == @org_id
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def count_resolved_versions
|
|
104
|
+
return 0 unless table_exists?("versions")
|
|
105
|
+
|
|
106
|
+
::Apartment::Tenant.switch(@schema) do
|
|
107
|
+
model = version_model
|
|
108
|
+
return 0 unless model
|
|
109
|
+
|
|
110
|
+
model.unscoped.find_each.count do |version|
|
|
111
|
+
org = OrganizationResolver.resolve(version)
|
|
112
|
+
org&.id == @org_id
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def count_resolved_decidim_attachments
|
|
118
|
+
return 0 unless table_exists?("decidim_attachments")
|
|
119
|
+
|
|
120
|
+
::Apartment::Tenant.switch(@schema) do
|
|
121
|
+
model = TableRegistry.ar_model_for("decidim_attachments")
|
|
122
|
+
return 0 unless model
|
|
123
|
+
|
|
124
|
+
model.unscoped.find_each.count do |record|
|
|
125
|
+
org = OrganizationResolver.resolve(record)
|
|
126
|
+
org&.id == @org_id
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def version_model
|
|
132
|
+
return PaperTrail::Version if defined?(PaperTrail::Version)
|
|
133
|
+
|
|
134
|
+
TableRegistry.ar_model_for("versions")
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def table_exists?(table)
|
|
138
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
139
|
+
SELECT 1
|
|
140
|
+
FROM pg_tables
|
|
141
|
+
WHERE schemaname = #{@connection.quote(@schema)}
|
|
142
|
+
AND tablename = #{@connection.quote(table)}
|
|
143
|
+
LIMIT 1
|
|
144
|
+
SQL
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def column_exists?(table, column)
|
|
148
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
149
|
+
SELECT 1
|
|
150
|
+
FROM information_schema.columns
|
|
151
|
+
WHERE table_schema = #{@connection.quote(@schema)}
|
|
152
|
+
AND table_name = #{@connection.quote(table)}
|
|
153
|
+
AND column_name = #{@connection.quote(column)}
|
|
154
|
+
LIMIT 1
|
|
155
|
+
SQL
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def qualified(table)
|
|
159
|
+
%("#{@schema}"."#{table}")
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# One organization: Tenant.create → clone public → prune other orgs.
|
|
8
|
+
class OrganizationConverter
|
|
9
|
+
class << self
|
|
10
|
+
def call(org_id:)
|
|
11
|
+
new(org_id: org_id).call
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def provision_tenant_for!(org)
|
|
15
|
+
new(org_id: org.id).send(:provision_tenant!, org)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def clone_for!(org)
|
|
19
|
+
Import::ConfirmPrompt.ask("Clone public into tenant for #{org.host}? [y/N]")
|
|
20
|
+
converter = new(org_id: org.id)
|
|
21
|
+
converter.send(:assert_preconditions!, org)
|
|
22
|
+
return converter.send(:dry_run!, org) unless Import::ConfirmPrompt.write_mode?
|
|
23
|
+
|
|
24
|
+
distribution_key = provision_tenant_for!(org)
|
|
25
|
+
SchemaCloner.call(target_schema: distribution_key.key)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize(org_id:)
|
|
30
|
+
@org_id = org_id.to_i
|
|
31
|
+
@connection = ActiveRecord::Base.connection
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def call
|
|
35
|
+
org = load_organization!
|
|
36
|
+
puts "Convert organization id=#{@org_id} host=#{org.host}"
|
|
37
|
+
assert_preconditions!(org)
|
|
38
|
+
|
|
39
|
+
Import::ConfirmPrompt.ask(
|
|
40
|
+
"Provision tenant, clone public, and prune other orgs for organization #{@org_id}? [y/N]"
|
|
41
|
+
)
|
|
42
|
+
return dry_run!(org) unless Import::ConfirmPrompt.write_mode?
|
|
43
|
+
|
|
44
|
+
before_metrics = OrgMetrics.snapshot(
|
|
45
|
+
connection: @connection,
|
|
46
|
+
schema: "public",
|
|
47
|
+
org_id: @org_id
|
|
48
|
+
)
|
|
49
|
+
puts "Metrics before (public, org #{@org_id}):"
|
|
50
|
+
before_metrics.each { |key, value| puts " #{key}=#{value}" }
|
|
51
|
+
|
|
52
|
+
distribution_key = provision_tenant!(org)
|
|
53
|
+
target_schema = distribution_key.key
|
|
54
|
+
|
|
55
|
+
Import::ConfirmPrompt.ask("Clone public into #{target_schema}? [y/N]")
|
|
56
|
+
clone_result = SchemaCloner.call(target_schema: target_schema)
|
|
57
|
+
|
|
58
|
+
Import::ConfirmPrompt.ask("Prune other organizations from #{target_schema} (keep id=#{@org_id})? [y/N]")
|
|
59
|
+
prune_result = OrganizationPruner.call(target_schema: target_schema, keep_org_id: @org_id)
|
|
60
|
+
|
|
61
|
+
verify_tenant!(target_schema, org)
|
|
62
|
+
AttachmentIntegrity.verify_tenant!(schema: target_schema, keep_org_id: @org_id)
|
|
63
|
+
PaperTrailReport.call(schema: target_schema, keep_org_id: @org_id, abort_on_unresolved: true)
|
|
64
|
+
|
|
65
|
+
Import::ConfirmPrompt.ask("Relocate Active Storage files into #{target_schema}/? [y/N]")
|
|
66
|
+
storage_result = StorageRelocator.call(target_schema: target_schema)
|
|
67
|
+
|
|
68
|
+
after_metrics = OrgMetrics.snapshot(
|
|
69
|
+
connection: @connection,
|
|
70
|
+
schema: target_schema,
|
|
71
|
+
org_id: @org_id
|
|
72
|
+
)
|
|
73
|
+
OrgMetrics.compare!(before: before_metrics, after: after_metrics)
|
|
74
|
+
|
|
75
|
+
{
|
|
76
|
+
organization_id: @org_id,
|
|
77
|
+
host: org.host,
|
|
78
|
+
tenant: target_schema,
|
|
79
|
+
clone: clone_result,
|
|
80
|
+
prune: prune_result,
|
|
81
|
+
storage: storage_result,
|
|
82
|
+
metrics: { before: before_metrics, after: after_metrics }
|
|
83
|
+
}
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def assert_preconditions!(org)
|
|
89
|
+
StoragePreconditions.assert_apartment_disk!
|
|
90
|
+
HostGate.assert_convertible!(org)
|
|
91
|
+
AttachmentIntegrity.assert_no_shared_blobs!(schema: "public")
|
|
92
|
+
PaperTrailReport.call(schema: "public", keep_org_id: @org_id, abort_on_unresolved: true)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def load_organization!
|
|
96
|
+
::Apartment::Tenant.switch("public") do
|
|
97
|
+
Decidim::Organization.find_by(id: @org_id) ||
|
|
98
|
+
raise(ConvertError, "organization #{@org_id} not found in public")
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def provision_tenant!(org)
|
|
103
|
+
::Apartment::Tenant.switch("public") do
|
|
104
|
+
form = TenantForm.new(host: org.host)
|
|
105
|
+
raise ConvertError, "invalid tenant host: #{form.errors.full_messages.join(", ")}" unless form.valid?
|
|
106
|
+
|
|
107
|
+
result = CreateTenantCommand.call(form)
|
|
108
|
+
if result[:ok]
|
|
109
|
+
distribution_key = result[:ok]
|
|
110
|
+
distribution_key.sync_hosts!(
|
|
111
|
+
primary_host: org.host,
|
|
112
|
+
secondary_hosts: org.secondary_hosts
|
|
113
|
+
)
|
|
114
|
+
else
|
|
115
|
+
detail = Array(result[:invalid]).join(", ").presence || "unknown error"
|
|
116
|
+
raise ConvertError, "could not provision tenant for host #{org.host}: #{detail}"
|
|
117
|
+
end
|
|
118
|
+
distribution_key || raise(ConvertError, "could not provision tenant for host #{org.host}")
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def dry_run!(org)
|
|
123
|
+
puts "[dry-run] would CreateTenantCommand for host=#{org.host}"
|
|
124
|
+
tables = TableRegistry.copyable_tables(
|
|
125
|
+
@connection,
|
|
126
|
+
source_schema: "public",
|
|
127
|
+
target_schema: "public"
|
|
128
|
+
)
|
|
129
|
+
tables.each do |table|
|
|
130
|
+
count = @connection.select_value(<<~SQL.squish).to_i
|
|
131
|
+
SELECT COUNT(*) FROM "public"."#{table}"
|
|
132
|
+
SQL
|
|
133
|
+
puts " would copy #{count} rows → <new-tenant>.#{table}" if count.positive?
|
|
134
|
+
end
|
|
135
|
+
OrganizationPruner.call(target_schema: "public", keep_org_id: @org_id)
|
|
136
|
+
{ dry_run: true, organization_id: @org_id, host: org.host }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def verify_tenant!(target_schema, org)
|
|
140
|
+
::Apartment::Tenant.switch(target_schema) do
|
|
141
|
+
org_count = Decidim::Organization.count
|
|
142
|
+
raise ConvertError, "expected 1 organization, got #{org_count}" unless org_count == 1
|
|
143
|
+
|
|
144
|
+
tenant_org = Decidim::Organization.first
|
|
145
|
+
raise ConvertError, "organization id mismatch: expected #{@org_id}, got #{tenant_org.id}" unless tenant_org.id == @org_id
|
|
146
|
+
|
|
147
|
+
puts "Verify OK: tenant #{target_schema} org id=#{tenant_org.id} host=#{tenant_org.host}"
|
|
148
|
+
end
|
|
149
|
+
HostGate.verify!(org, target_schema)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|