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,210 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Walks every row in every copyable table and uses OrganizationResolver.
|
|
8
|
+
# - resolved to keep_org_id → keep
|
|
9
|
+
# - resolved to another org → delete
|
|
10
|
+
# - not resolved (nil) → keep (duplicated unknown)
|
|
11
|
+
# Tables with no ActiveRecord model cannot be resolved → all rows kept.
|
|
12
|
+
class OrganizationPruner
|
|
13
|
+
BATCH_SIZE = 500
|
|
14
|
+
DELETE_SLICE = 1_000
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def call(target_schema:, keep_org_id:)
|
|
18
|
+
new(target_schema: target_schema, keep_org_id: keep_org_id).call
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def strict_ownership?
|
|
22
|
+
ENV["STRICT_OWNERSHIP"].to_s == "1"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(target_schema:, keep_org_id:)
|
|
27
|
+
@target_schema = target_schema
|
|
28
|
+
@keep_org_id = keep_org_id.to_i
|
|
29
|
+
@connection = ActiveRecord::Base.connection
|
|
30
|
+
@dry_run = Import::ConfirmPrompt.dry_run?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def call
|
|
34
|
+
resolvable, keeper = tables_for_prune
|
|
35
|
+
print_header!(resolvable, keeper)
|
|
36
|
+
stats = empty_stats
|
|
37
|
+
prune_tenant!(resolvable, keeper, stats)
|
|
38
|
+
finalize!(stats)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def tables_for_prune
|
|
44
|
+
[
|
|
45
|
+
TableRegistry.resolvable_tables(@connection, schema: @target_schema),
|
|
46
|
+
TableRegistry.keeper_tables(@connection, schema: @target_schema)
|
|
47
|
+
]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def empty_stats
|
|
51
|
+
{ deleted: 0, kept_unknown: 0, kept_own: 0, keeper_rows: 0, per_table: {} }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def print_header!(resolvable, keeper)
|
|
55
|
+
puts "OrganizationPruner: keep org #{@keep_org_id} in #{@target_schema}"
|
|
56
|
+
OrgDistributionReport.print!(
|
|
57
|
+
connection: @connection, schema: @target_schema, keep_org_id: @keep_org_id
|
|
58
|
+
)
|
|
59
|
+
puts ""
|
|
60
|
+
puts " record walk (OrganizationResolver) : #{resolvable.size} tables"
|
|
61
|
+
puts " keeper (no AR model) : #{keeper.size} tables"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def prune_tenant!(resolvable, keeper, stats)
|
|
65
|
+
::Apartment::Tenant.switch(@target_schema) do
|
|
66
|
+
@connection.execute("SET session_replication_role = replica") unless @dry_run
|
|
67
|
+
walk_tables!(resolvable, keeper, stats)
|
|
68
|
+
ensure
|
|
69
|
+
@connection.execute("SET session_replication_role = DEFAULT") unless @dry_run
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def walk_tables!(resolvable, keeper, stats)
|
|
74
|
+
handled = ConstraintGraphPruner.call(
|
|
75
|
+
keep_org_id: @keep_org_id, dry_run: @dry_run, stats: stats
|
|
76
|
+
)
|
|
77
|
+
resolvable.each do |table|
|
|
78
|
+
next if handled.include?(table)
|
|
79
|
+
|
|
80
|
+
prune_table!(table, stats)
|
|
81
|
+
end
|
|
82
|
+
report_keeper_tables!(keeper, stats)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def finalize!(stats)
|
|
86
|
+
print_ownership_summary!(stats)
|
|
87
|
+
enforce_strict_ownership!(stats)
|
|
88
|
+
puts "OrganizationPruner complete: deleted=#{stats[:deleted]} " \
|
|
89
|
+
"kept_own=#{stats[:kept_own]} kept_unknown=#{stats[:kept_unknown]} " \
|
|
90
|
+
"keeper_rows=#{stats[:keeper_rows]}"
|
|
91
|
+
stats.merge(dry_run: @dry_run)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def prune_table!(table, stats)
|
|
95
|
+
model = TableRegistry.ar_model_for(table)
|
|
96
|
+
bucket = classify_all(model)
|
|
97
|
+
apply_table_stats!(stats, table, bucket)
|
|
98
|
+
delete_records!(model, bucket[:ids])
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def classify_all(model)
|
|
102
|
+
bucket = { ids: [], own: 0, unknown: 0, samples: [] }
|
|
103
|
+
model.unscoped.find_each(batch_size: BATCH_SIZE) do |record|
|
|
104
|
+
tally!(bucket, classify_record(record), record.id)
|
|
105
|
+
end
|
|
106
|
+
bucket
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def tally!(bucket, verdict, id)
|
|
110
|
+
case verdict
|
|
111
|
+
when :own then bucket[:own] += 1
|
|
112
|
+
when :unknown
|
|
113
|
+
bucket[:unknown] += 1
|
|
114
|
+
bucket[:samples] << id if bucket[:samples].size < 5
|
|
115
|
+
when :other then bucket[:ids] << id
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def apply_table_stats!(stats, table, bucket)
|
|
120
|
+
stats[:kept_unknown] += bucket[:unknown]
|
|
121
|
+
stats[:kept_own] += bucket[:own]
|
|
122
|
+
stats[:deleted] += bucket[:ids].size
|
|
123
|
+
stats[:per_table][table] = {
|
|
124
|
+
own: bucket[:own], other: bucket[:ids].size, unknown: bucket[:unknown],
|
|
125
|
+
keeper: 0, sample_unknown: bucket[:samples]
|
|
126
|
+
}
|
|
127
|
+
log_table_result(table, bucket[:ids].size, bucket[:own], bucket[:unknown], bucket[:samples])
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def classify_record(record)
|
|
131
|
+
if record.respond_to?(:decidim_organization_id) && record.decidim_organization_id.present?
|
|
132
|
+
return :own if record.decidim_organization_id.to_i == @keep_org_id
|
|
133
|
+
|
|
134
|
+
return :other
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
org = OrganizationResolver.resolve(record)
|
|
138
|
+
return :unknown unless org
|
|
139
|
+
return :own if org.id == @keep_org_id
|
|
140
|
+
|
|
141
|
+
:other
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def delete_records!(model, ids)
|
|
145
|
+
return if ids.empty? || @dry_run
|
|
146
|
+
|
|
147
|
+
ids.each_slice(DELETE_SLICE) { |slice| model.unscoped.where(id: slice).delete_all }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def log_table_result(table, delete_count, own_count, unknown_count, sample_unknown)
|
|
151
|
+
return if delete_count.zero? && own_count.zero? && unknown_count.zero?
|
|
152
|
+
|
|
153
|
+
puts " #{table}: #{table_result_line(delete_count, own_count, unknown_count)}"
|
|
154
|
+
return if sample_unknown.empty?
|
|
155
|
+
|
|
156
|
+
puts " sample unknown ids: #{sample_unknown.join(", ")}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def table_result_line(delete_count, own_count, unknown_count)
|
|
160
|
+
prefix = @dry_run ? "would delete" : "deleted"
|
|
161
|
+
return "#{prefix} #{delete_count}, kept #{own_count} own, #{unknown_count} unknown" if delete_count.positive?
|
|
162
|
+
|
|
163
|
+
"kept #{own_count} own, #{unknown_count} unknown"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def report_keeper_tables!(tables, stats)
|
|
167
|
+
tables.each { |table| report_keeper_table!(table, stats) }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def report_keeper_table!(table, stats)
|
|
171
|
+
count = @connection.select_value(<<~SQL.squish).to_i
|
|
172
|
+
SELECT COUNT(*) FROM #{qualified(table)}
|
|
173
|
+
SQL
|
|
174
|
+
stats[:keeper_rows] += count
|
|
175
|
+
stats[:per_table][table] = { own: 0, other: 0, unknown: 0, keeper: count, sample_unknown: [] }
|
|
176
|
+
puts " #{table}: no AR model — #{count} rows kept (cannot resolve org)" if count.positive?
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def print_ownership_summary!(stats)
|
|
180
|
+
puts "Ownership summary (own/other/unknown/keeper):"
|
|
181
|
+
stats[:per_table].sort_by { |table, _| table }.each do |table, row|
|
|
182
|
+
print_ownership_row!(table, row)
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def print_ownership_row!(table, row)
|
|
187
|
+
return if row.values_at(:own, :other, :unknown, :keeper).all?(&:zero?)
|
|
188
|
+
|
|
189
|
+
puts " #{table}: own=#{row[:own]} other=#{row[:other]} " \
|
|
190
|
+
"unknown=#{row[:unknown]} keeper=#{row[:keeper]}"
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def enforce_strict_ownership!(stats)
|
|
194
|
+
return unless self.class.strict_ownership?
|
|
195
|
+
|
|
196
|
+
unknown = stats[:kept_unknown].to_i
|
|
197
|
+
keeper = stats[:keeper_rows].to_i
|
|
198
|
+
return if unknown.zero? && keeper.zero?
|
|
199
|
+
|
|
200
|
+
raise ConvertError,
|
|
201
|
+
"STRICT_OWNERSHIP=1: refusing convert with unknown=#{unknown} keeper_rows=#{keeper}"
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def qualified(table)
|
|
205
|
+
%("#{@target_schema}"."#{table}")
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Resolves Decidim::Organization for any record using the same graph as
|
|
8
|
+
# Decidim::Voca::SyncLocales::LocaleContext. Returns nil when unknown (row is kept).
|
|
9
|
+
class OrganizationResolver
|
|
10
|
+
class << self
|
|
11
|
+
def resolve(record)
|
|
12
|
+
return record if record.is_a?(::Decidim::Organization)
|
|
13
|
+
|
|
14
|
+
resolve_with_resolvers(record)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def resolve_with_resolvers(record)
|
|
18
|
+
resolver_methods.each do |resolver|
|
|
19
|
+
organization = safe_resolve(resolver, record)
|
|
20
|
+
return organization if organization.present?
|
|
21
|
+
end
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def safe_resolve(resolver, record)
|
|
26
|
+
send(resolver, record)
|
|
27
|
+
rescue NameError
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def safe_association(record, name)
|
|
32
|
+
return unless record.respond_to?(name)
|
|
33
|
+
|
|
34
|
+
record.public_send(name)
|
|
35
|
+
rescue NameError
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def resolver_methods
|
|
40
|
+
[
|
|
41
|
+
:try_organization,
|
|
42
|
+
:try_decidim_organization_id,
|
|
43
|
+
:try_record,
|
|
44
|
+
:try_attachments,
|
|
45
|
+
:try_component,
|
|
46
|
+
:try_participatory_space,
|
|
47
|
+
:try_assembly,
|
|
48
|
+
:try_conference,
|
|
49
|
+
:try_flow,
|
|
50
|
+
:try_condition,
|
|
51
|
+
:try_awesome_config,
|
|
52
|
+
:try_author,
|
|
53
|
+
:try_user,
|
|
54
|
+
:try_participant,
|
|
55
|
+
:try_current_user,
|
|
56
|
+
:try_sender,
|
|
57
|
+
:try_recipient,
|
|
58
|
+
:try_amender,
|
|
59
|
+
:try_meeting,
|
|
60
|
+
:try_questionnaire,
|
|
61
|
+
:try_question,
|
|
62
|
+
:try_proposal,
|
|
63
|
+
:try_collaborative_draft,
|
|
64
|
+
:try_project,
|
|
65
|
+
:try_budget,
|
|
66
|
+
:try_category,
|
|
67
|
+
:try_reminder,
|
|
68
|
+
:try_result,
|
|
69
|
+
:try_conference_meeting,
|
|
70
|
+
:try_conference_speakers,
|
|
71
|
+
:try_participants,
|
|
72
|
+
:try_conversation,
|
|
73
|
+
:try_commentable,
|
|
74
|
+
:try_comment_organization,
|
|
75
|
+
:try_resource,
|
|
76
|
+
:try_attachment,
|
|
77
|
+
:try_attached_to,
|
|
78
|
+
:try_privatable_to,
|
|
79
|
+
:try_coauthorable,
|
|
80
|
+
:try_from_to,
|
|
81
|
+
:try_parent,
|
|
82
|
+
:try_item
|
|
83
|
+
]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def try_attachment(record)
|
|
87
|
+
# Decidim::Attachment#collection_for — not ActiveStorage
|
|
88
|
+
collection = safe_association(record, :collection_for)
|
|
89
|
+
return unless collection
|
|
90
|
+
|
|
91
|
+
resolve_with_resolvers(collection)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def try_attached_to(record)
|
|
95
|
+
attached_to = safe_association(record, :attached_to)
|
|
96
|
+
return unless attached_to
|
|
97
|
+
|
|
98
|
+
resolve_with_resolvers(attached_to)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def try_decidim_organization_id(record)
|
|
102
|
+
return unless record.respond_to?(:decidim_organization_id) && record.decidim_organization_id
|
|
103
|
+
|
|
104
|
+
::Decidim::Organization.find_by(id: record.decidim_organization_id)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# ActiveStorage::Attachment#record, PaperTrail rows, etc.
|
|
108
|
+
def try_record(record)
|
|
109
|
+
target = safe_association(record, :record)
|
|
110
|
+
return unless target
|
|
111
|
+
|
|
112
|
+
resolve_with_resolvers(target)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# ActiveStorage::Blob → first attachment's record
|
|
116
|
+
def try_attachments(record)
|
|
117
|
+
return unless record.respond_to?(:attachments)
|
|
118
|
+
|
|
119
|
+
attachments = record.attachments
|
|
120
|
+
return if attachments.blank?
|
|
121
|
+
|
|
122
|
+
try_record(attachments.first)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def try_commentable(record)
|
|
126
|
+
commentable = safe_association(record, :root_commentable)
|
|
127
|
+
return unless commentable
|
|
128
|
+
|
|
129
|
+
resolve_with_resolvers(commentable)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def try_meeting(record)
|
|
133
|
+
return unless record.respond_to?(:meeting) && record.meeting
|
|
134
|
+
|
|
135
|
+
try_component(record.meeting)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def try_awesome_config(record)
|
|
139
|
+
return unless record.respond_to?(:awesome_config) && record.awesome_config
|
|
140
|
+
|
|
141
|
+
try_organization(record.awesome_config)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def try_reminder(record)
|
|
145
|
+
return unless record.respond_to?(:reminder) && record.reminder
|
|
146
|
+
|
|
147
|
+
try_component(record.reminder)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def try_project(record)
|
|
151
|
+
return unless record.respond_to?(:project) && record.project
|
|
152
|
+
|
|
153
|
+
try_component(record.project)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def try_budget(record)
|
|
157
|
+
return unless record.respond_to?(:budget) && record.budget
|
|
158
|
+
|
|
159
|
+
try_component(record.budget)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def try_category(record)
|
|
163
|
+
return unless record.respond_to?(:category) && record.category
|
|
164
|
+
|
|
165
|
+
try_participatory_space(record.category)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def try_flow(record)
|
|
169
|
+
return unless record.respond_to?(:flow) && record.flow
|
|
170
|
+
|
|
171
|
+
try_organization(record.flow)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def try_condition(record)
|
|
175
|
+
return unless record.respond_to?(:condition) && record.condition
|
|
176
|
+
|
|
177
|
+
try_organization(record.condition)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def try_privatable_to(record)
|
|
181
|
+
return unless record.respond_to?(:privatable_to) && record.privatable_to
|
|
182
|
+
|
|
183
|
+
resolve_with_resolvers(record.privatable_to)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def try_coauthorable(record)
|
|
187
|
+
coauthorable = safe_association(record, :coauthorable)
|
|
188
|
+
return unless coauthorable
|
|
189
|
+
|
|
190
|
+
resolve_with_resolvers(coauthorable)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def try_from_to(record)
|
|
194
|
+
from = safe_association(record, :from)
|
|
195
|
+
return resolve_with_resolvers(from) if from
|
|
196
|
+
|
|
197
|
+
to = safe_association(record, :to)
|
|
198
|
+
return resolve_with_resolvers(to) if to
|
|
199
|
+
|
|
200
|
+
nil
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def try_parent(record)
|
|
204
|
+
parent = safe_association(record, :parent)
|
|
205
|
+
return unless parent
|
|
206
|
+
|
|
207
|
+
resolve_with_resolvers(parent)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def try_item(record)
|
|
211
|
+
item = safe_association(record, :item)
|
|
212
|
+
return unless item
|
|
213
|
+
|
|
214
|
+
resolve_with_resolvers(item)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def try_questionnaire(record)
|
|
218
|
+
questionnaire = if record.is_a?(::Decidim::Forms::Questionnaire)
|
|
219
|
+
record
|
|
220
|
+
elsif record.respond_to?(:questionnaire) && record.questionnaire
|
|
221
|
+
record.questionnaire
|
|
222
|
+
end
|
|
223
|
+
return unless questionnaire
|
|
224
|
+
|
|
225
|
+
questionnaire_for = questionnaire.questionnaire_for
|
|
226
|
+
try_component(questionnaire_for) || try_organization(questionnaire_for)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def try_question(record)
|
|
230
|
+
return unless record.respond_to?(:question) && record.question
|
|
231
|
+
|
|
232
|
+
try_questionnaire(record.question)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def try_proposal(record)
|
|
236
|
+
return unless record.respond_to?(:proposal) && record.proposal
|
|
237
|
+
|
|
238
|
+
try_component(record.proposal)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def try_resource(record)
|
|
242
|
+
resource = safe_association(record, :resource)
|
|
243
|
+
return unless resource
|
|
244
|
+
|
|
245
|
+
resolve_with_resolvers(resource)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def try_author(record)
|
|
249
|
+
author = safe_association(record, :author)
|
|
250
|
+
return unless author
|
|
251
|
+
|
|
252
|
+
try_organization(author)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def try_conference(record)
|
|
256
|
+
return unless record.respond_to?(:conference) && record.conference
|
|
257
|
+
|
|
258
|
+
try_organization(record.conference)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def try_conference_speakers(record)
|
|
262
|
+
return unless record.respond_to?(:conference_speakers) && record.conference_speakers
|
|
263
|
+
|
|
264
|
+
speaker = record.conference_speakers.first
|
|
265
|
+
return if speaker.nil?
|
|
266
|
+
|
|
267
|
+
try_organization(speaker)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def try_conference_meeting(record)
|
|
271
|
+
return unless record.respond_to?(:conference_meeting) && record.conference_meeting
|
|
272
|
+
|
|
273
|
+
try_component(record.conference_meeting)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def try_user(record)
|
|
277
|
+
return unless record.respond_to?(:user) && record.user
|
|
278
|
+
|
|
279
|
+
try_organization(record.user)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def try_participant(record)
|
|
283
|
+
return unless record.respond_to?(:participant) && record.participant
|
|
284
|
+
|
|
285
|
+
try_organization(record.participant)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def try_participants(record)
|
|
289
|
+
return unless record.respond_to?(:participants) && record.participants
|
|
290
|
+
|
|
291
|
+
participant = record.participants.first
|
|
292
|
+
return if participant.nil?
|
|
293
|
+
|
|
294
|
+
try_organization(participant)
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def try_sender(record)
|
|
298
|
+
return unless record.respond_to?(:sender) && record.sender
|
|
299
|
+
|
|
300
|
+
try_organization(record.sender)
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def try_current_user(record)
|
|
304
|
+
return unless record.respond_to?(:current_user) && record.current_user
|
|
305
|
+
|
|
306
|
+
try_organization(record.current_user)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def try_recipient(record)
|
|
310
|
+
return unless record.respond_to?(:recipient) && record.recipient
|
|
311
|
+
|
|
312
|
+
try_organization(record.recipient)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def try_conversation(record)
|
|
316
|
+
return unless record.respond_to?(:conversation) && record.conversation
|
|
317
|
+
|
|
318
|
+
participants = record.conversation.participants
|
|
319
|
+
return if participants.empty?
|
|
320
|
+
|
|
321
|
+
try_organization(participants.first)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def try_amender(record)
|
|
325
|
+
return unless record.respond_to?(:amender) && record.amender
|
|
326
|
+
|
|
327
|
+
try_organization(record.amender)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def try_comment_organization(record)
|
|
331
|
+
commentable = safe_association(record, :commentable)
|
|
332
|
+
return unless commentable
|
|
333
|
+
|
|
334
|
+
resolve_with_resolvers(commentable)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def try_collaborative_draft(record)
|
|
338
|
+
return unless record.respond_to?(:collaborative_draft) && record.collaborative_draft
|
|
339
|
+
|
|
340
|
+
try_component(record.collaborative_draft)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def try_result(record)
|
|
344
|
+
return unless record.respond_to?(:result) && record.result
|
|
345
|
+
|
|
346
|
+
try_component(record.result)
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def try_organization(record)
|
|
350
|
+
return unless record.respond_to?(:organization)
|
|
351
|
+
|
|
352
|
+
record.organization
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def try_participatory_space(record)
|
|
356
|
+
participatory_space = safe_association(record, :participatory_space)
|
|
357
|
+
return unless participatory_space
|
|
358
|
+
|
|
359
|
+
try_organization(participatory_space)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def try_assembly(record)
|
|
363
|
+
return unless record.respond_to?(:assembly) && record.assembly
|
|
364
|
+
|
|
365
|
+
try_organization(record.assembly)
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def try_component(record)
|
|
369
|
+
component = try_decidim_component_id(record)
|
|
370
|
+
return component if component.present?
|
|
371
|
+
|
|
372
|
+
return unless record.respond_to?(:component) && record.component
|
|
373
|
+
|
|
374
|
+
try_organization(record.component)
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def try_decidim_component_id(record)
|
|
378
|
+
return unless record.respond_to?(:decidim_component_id) && record.decidim_component_id
|
|
379
|
+
|
|
380
|
+
try_organization(::Decidim::Component.find_by(id: record.decidim_component_id))
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
end
|