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,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Reports PaperTrail versions by item_type and org resolution.
|
|
8
|
+
class PaperTrailReport
|
|
9
|
+
Result = Struct.new(:by_type, :unresolved, :other_org, :own, keyword_init: true)
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def call(schema:, keep_org_id:, abort_on_unresolved: true)
|
|
13
|
+
new(schema: schema, keep_org_id: keep_org_id, abort_on_unresolved: abort_on_unresolved).call
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def allow_unknown_versions?
|
|
17
|
+
ENV["ALLOW_UNKNOWN_VERSIONS"].to_s == "1"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(schema:, keep_org_id:, abort_on_unresolved: true)
|
|
22
|
+
@schema = schema
|
|
23
|
+
@keep_org_id = keep_org_id.to_i
|
|
24
|
+
@abort_on_unresolved = abort_on_unresolved
|
|
25
|
+
@connection = ActiveRecord::Base.connection
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call
|
|
29
|
+
return empty_result unless table_exists?
|
|
30
|
+
|
|
31
|
+
result = scan!
|
|
32
|
+
print_report!(result)
|
|
33
|
+
enforce!(result)
|
|
34
|
+
result
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def empty_result
|
|
40
|
+
Result.new(by_type: {}, unresolved: 0, other_org: 0, own: 0)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def scan!
|
|
44
|
+
by_type = Hash.new { |h, k| h[k] = { total: 0, own: 0, other: 0, unresolved: 0, sample_ids: [] } }
|
|
45
|
+
unresolved = 0
|
|
46
|
+
other_org = 0
|
|
47
|
+
own = 0
|
|
48
|
+
|
|
49
|
+
::Apartment::Tenant.switch(@schema) do
|
|
50
|
+
model = version_model
|
|
51
|
+
return empty_result unless model
|
|
52
|
+
|
|
53
|
+
model.unscoped.find_each do |version|
|
|
54
|
+
item_type = version.try(:item_type).to_s.presence || "(blank)"
|
|
55
|
+
entry = by_type[item_type]
|
|
56
|
+
entry[:total] += 1
|
|
57
|
+
|
|
58
|
+
org = OrganizationResolver.resolve(version)
|
|
59
|
+
if org.nil?
|
|
60
|
+
entry[:unresolved] += 1
|
|
61
|
+
unresolved += 1
|
|
62
|
+
entry[:sample_ids] << version.id if entry[:sample_ids].size < 5
|
|
63
|
+
elsif org.id == @keep_org_id
|
|
64
|
+
entry[:own] += 1
|
|
65
|
+
own += 1
|
|
66
|
+
else
|
|
67
|
+
entry[:other] += 1
|
|
68
|
+
other_org += 1
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
Result.new(by_type: by_type, unresolved: unresolved, other_org: other_org, own: own)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def print_report!(result)
|
|
77
|
+
puts "PaperTrail versions (#{@schema}, keep_org=#{@keep_org_id}):"
|
|
78
|
+
if result.by_type.empty?
|
|
79
|
+
puts " (no versions table or empty)"
|
|
80
|
+
return
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
result.by_type.sort_by { |type, _| type }.each do |item_type, stats|
|
|
84
|
+
puts " #{item_type}: total=#{stats[:total]} own=#{stats[:own]} " \
|
|
85
|
+
"other=#{stats[:other]} unresolved=#{stats[:unresolved]}"
|
|
86
|
+
next if stats[:sample_ids].blank?
|
|
87
|
+
|
|
88
|
+
puts " sample unresolved ids: #{stats[:sample_ids].join(", ")}"
|
|
89
|
+
end
|
|
90
|
+
puts " totals: own=#{result.own} other=#{result.other_org} unresolved=#{result.unresolved}"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def enforce!(result)
|
|
94
|
+
if result.unresolved.positive? && @abort_on_unresolved && !self.class.allow_unknown_versions?
|
|
95
|
+
raise ConvertError,
|
|
96
|
+
"PaperTrail has #{result.unresolved} unresolved version(s); " \
|
|
97
|
+
"set ALLOW_UNKNOWN_VERSIONS=1 to keep them, or clean legacy item_type rows"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
return unless result.other_org.positive? && @abort_on_unresolved
|
|
101
|
+
|
|
102
|
+
# After prune, other_org must be zero. During preflight, other orgs are expected.
|
|
103
|
+
return if @schema == "public"
|
|
104
|
+
|
|
105
|
+
raise ConvertError,
|
|
106
|
+
"PaperTrail still has #{result.other_org} version(s) for other organizations after prune"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def version_model
|
|
110
|
+
return PaperTrail::Version if defined?(PaperTrail::Version)
|
|
111
|
+
|
|
112
|
+
TableRegistry.ar_model_for("versions")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def table_exists?
|
|
116
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
117
|
+
SELECT 1
|
|
118
|
+
FROM pg_tables
|
|
119
|
+
WHERE schemaname = #{@connection.quote(@schema)}
|
|
120
|
+
AND tablename = 'versions'
|
|
121
|
+
LIMIT 1
|
|
122
|
+
SQL
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Read-only report before converting multi-org public into tenant schemas.
|
|
8
|
+
class Preflight
|
|
9
|
+
OUTBOUND_TABLES = %w(
|
|
10
|
+
solid_queue_jobs
|
|
11
|
+
good_jobs
|
|
12
|
+
delayed_jobs
|
|
13
|
+
que_jobs
|
|
14
|
+
).freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def call(source_schema: "public", keep_org_id: nil)
|
|
18
|
+
new(source_schema: source_schema, keep_org_id: keep_org_id).call
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def initialize(source_schema: "public", keep_org_id: nil)
|
|
23
|
+
@source_schema = source_schema
|
|
24
|
+
@keep_org_id = keep_org_id&.to_i
|
|
25
|
+
@connection = ActiveRecord::Base.connection
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call
|
|
29
|
+
organizations = load_organizations
|
|
30
|
+
raise ConvertError, "no organizations in #{@source_schema}" if organizations.empty?
|
|
31
|
+
|
|
32
|
+
StoragePreconditions.report!
|
|
33
|
+
puts "preflight timestamp: #{Time.now.utc.iso8601}"
|
|
34
|
+
|
|
35
|
+
permission_checks = Ops::PostgresPermissions.call(
|
|
36
|
+
connection: @connection,
|
|
37
|
+
schema: @source_schema
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
puts "=== Convert preflight (#{@source_schema}) ==="
|
|
41
|
+
Ops::PostgresPermissions.print_report!(permission_checks)
|
|
42
|
+
Ops::PostgresPermissions.raise_if_failed!(
|
|
43
|
+
permission_checks,
|
|
44
|
+
error_class: ConvertError,
|
|
45
|
+
connection: @connection
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
resolvable = TableRegistry.resolvable_tables(@connection, schema: @source_schema)
|
|
49
|
+
keeper = TableRegistry.keeper_tables(@connection, schema: @source_schema)
|
|
50
|
+
|
|
51
|
+
puts "organizations : #{organizations.size}"
|
|
52
|
+
organizations.each do |org|
|
|
53
|
+
secondary = Array(org.secondary_hosts).compact_blank
|
|
54
|
+
suffix = secondary.any? ? " secondary_hosts=#{secondary.join(",")}" : ""
|
|
55
|
+
puts " - id=#{org.id} host=#{org.host}#{suffix}"
|
|
56
|
+
end
|
|
57
|
+
puts ""
|
|
58
|
+
OrgDistributionReport.print!(
|
|
59
|
+
connection: @connection,
|
|
60
|
+
schema: @source_schema
|
|
61
|
+
)
|
|
62
|
+
puts ""
|
|
63
|
+
puts "prune via OrganizationResolver : #{resolvable.size} tables"
|
|
64
|
+
puts "keeper (no AR model, all rows N×) : #{keeper.size} tables"
|
|
65
|
+
if keeper.any?
|
|
66
|
+
puts " keeper tables:"
|
|
67
|
+
keeper.each { |table| puts " - #{table}" }
|
|
68
|
+
end
|
|
69
|
+
puts " note: unresolvable rows inside AR tables are also kept N×"
|
|
70
|
+
puts ""
|
|
71
|
+
|
|
72
|
+
AttachmentIntegrity.assert_no_shared_blobs!(schema: @source_schema)
|
|
73
|
+
warn_outbound_queues!
|
|
74
|
+
report_paper_trail!(organizations)
|
|
75
|
+
|
|
76
|
+
{
|
|
77
|
+
organizations: organizations,
|
|
78
|
+
resolvable_tables: resolvable,
|
|
79
|
+
keeper_tables: keeper,
|
|
80
|
+
postgres_permissions: permission_checks
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def load_organizations
|
|
87
|
+
::Apartment::Tenant.switch(@source_schema) do
|
|
88
|
+
Decidim::Organization.order(:id).to_a
|
|
89
|
+
end
|
|
90
|
+
rescue ::Apartment::TenantNotFound
|
|
91
|
+
::Apartment::Tenant.switch("public") do
|
|
92
|
+
Decidim::Organization.order(:id).to_a
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def report_paper_trail!(organizations)
|
|
97
|
+
org_id = @keep_org_id || organizations.first&.id
|
|
98
|
+
return unless org_id
|
|
99
|
+
|
|
100
|
+
PaperTrailReport.call(
|
|
101
|
+
schema: @source_schema,
|
|
102
|
+
keep_org_id: org_id,
|
|
103
|
+
abort_on_unresolved: @source_schema != "public" || ENV["CONVERT_PREFLIGHT_STRICT"].to_s == "1"
|
|
104
|
+
)
|
|
105
|
+
rescue ConvertError => e
|
|
106
|
+
# Preflight on public: warn on unresolved unless strict; still print.
|
|
107
|
+
puts "WARNING: #{e.message}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def warn_outbound_queues!
|
|
111
|
+
OUTBOUND_TABLES.each do |table|
|
|
112
|
+
next unless table_exists?(table)
|
|
113
|
+
|
|
114
|
+
count = @connection.select_value(<<~SQL.squish).to_i
|
|
115
|
+
SELECT COUNT(*) FROM #{qualified(table)}
|
|
116
|
+
SQL
|
|
117
|
+
next if count.zero?
|
|
118
|
+
|
|
119
|
+
puts "WARNING: outbound queue table #{table} has #{count} row(s); " \
|
|
120
|
+
"pause/drain jobs before convert"
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def table_exists?(table)
|
|
125
|
+
@connection.select_value(<<~SQL.squish).present?
|
|
126
|
+
SELECT 1
|
|
127
|
+
FROM pg_tables
|
|
128
|
+
WHERE schemaname = #{@connection.quote(@source_schema)}
|
|
129
|
+
AND tablename = #{@connection.quote(table)}
|
|
130
|
+
LIMIT 1
|
|
131
|
+
SQL
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def qualified(table)
|
|
135
|
+
%("#{@source_schema}"."#{table}")
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Copies row data from public (or source schema) into an empty tenant schema.
|
|
8
|
+
# Structure is assumed to already exist (Apartment::Tenant.create).
|
|
9
|
+
class SchemaCloner
|
|
10
|
+
class << self
|
|
11
|
+
def call(target_schema:, source_schema: "public")
|
|
12
|
+
new(target_schema: target_schema, source_schema: source_schema).call
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(target_schema:, source_schema: "public")
|
|
17
|
+
@target_schema = target_schema
|
|
18
|
+
@source_schema = source_schema
|
|
19
|
+
@connection = ActiveRecord::Base.connection
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call
|
|
23
|
+
assert_tenant_empty!
|
|
24
|
+
tables = copyable_tables
|
|
25
|
+
raise ConvertError, "no copyable tables between #{@source_schema} and #{@target_schema}" if tables.empty?
|
|
26
|
+
|
|
27
|
+
puts "SchemaCloner: #{tables.size} tables from #{@source_schema} → #{@target_schema}"
|
|
28
|
+
|
|
29
|
+
if Import::ConfirmPrompt.dry_run?
|
|
30
|
+
dry_run!(tables)
|
|
31
|
+
return { tables: tables.size, rows: 0, dry_run: true }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
rows = 0
|
|
35
|
+
@connection.execute("SET session_replication_role = replica")
|
|
36
|
+
begin
|
|
37
|
+
rows = copy_tables!(tables)
|
|
38
|
+
assert_row_counts!(tables)
|
|
39
|
+
reset_sequences!
|
|
40
|
+
update_blob_service_names!
|
|
41
|
+
ensure
|
|
42
|
+
@connection.execute("SET session_replication_role = DEFAULT")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
puts "SchemaCloner complete: #{rows} rows copied across #{tables.size} tables"
|
|
46
|
+
{ tables: tables.size, rows: rows, dry_run: false }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def assert_tenant_empty!
|
|
52
|
+
::Apartment::Tenant.switch(@target_schema) do
|
|
53
|
+
return unless Decidim::Organization.exists?
|
|
54
|
+
|
|
55
|
+
raise ConvertError,
|
|
56
|
+
"tenant #{@target_schema} already has organizations; drop tenant before re-cloning"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def dry_run!(tables)
|
|
61
|
+
tables.each do |table|
|
|
62
|
+
count = @connection.select_value(<<~SQL.squish).to_i
|
|
63
|
+
SELECT COUNT(*) FROM #{qualified(@source_schema, table)}
|
|
64
|
+
SQL
|
|
65
|
+
puts " would copy #{count} rows → #{@target_schema}.#{table}"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def copy_tables!(tables)
|
|
70
|
+
total = 0
|
|
71
|
+
tables.each do |table|
|
|
72
|
+
count = @connection.select_value(<<~SQL.squish).to_i
|
|
73
|
+
SELECT COUNT(*) FROM #{qualified(@source_schema, table)}
|
|
74
|
+
SQL
|
|
75
|
+
next if count.zero?
|
|
76
|
+
|
|
77
|
+
begin
|
|
78
|
+
@connection.transaction(requires_new: true) do
|
|
79
|
+
@connection.execute(<<~SQL.squish)
|
|
80
|
+
INSERT INTO #{qualified(@target_schema, table)}
|
|
81
|
+
SELECT * FROM #{qualified(@source_schema, table)}
|
|
82
|
+
SQL
|
|
83
|
+
end
|
|
84
|
+
total += count
|
|
85
|
+
puts " copied #{count} rows → #{table}"
|
|
86
|
+
rescue ActiveRecord::StatementInvalid => e
|
|
87
|
+
message = e.cause&.message || e.message
|
|
88
|
+
raise ConvertError, "failed to copy non-empty table #{table} (#{count} rows): #{message}"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
total
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def assert_row_counts!(tables)
|
|
95
|
+
mismatches = []
|
|
96
|
+
tables.each do |table|
|
|
97
|
+
source_count = @connection.select_value(<<~SQL.squish).to_i
|
|
98
|
+
SELECT COUNT(*) FROM #{qualified(@source_schema, table)}
|
|
99
|
+
SQL
|
|
100
|
+
target_count = @connection.select_value(<<~SQL.squish).to_i
|
|
101
|
+
SELECT COUNT(*) FROM #{qualified(@target_schema, table)}
|
|
102
|
+
SQL
|
|
103
|
+
next if source_count == target_count
|
|
104
|
+
|
|
105
|
+
mismatches << "#{table}: source=#{source_count} target=#{target_count}"
|
|
106
|
+
end
|
|
107
|
+
return if mismatches.empty?
|
|
108
|
+
|
|
109
|
+
raise ConvertError, "clone row count mismatch: #{mismatches.join("; ")}"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def reset_sequences!
|
|
113
|
+
::Apartment::Tenant.switch(@target_schema) do
|
|
114
|
+
copyable_tables.each do |table|
|
|
115
|
+
@connection.transaction(requires_new: true) { reset_sequence_for_table!(table) }
|
|
116
|
+
rescue ActiveRecord::StatementInvalid
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def reset_sequence_for_table!(table)
|
|
123
|
+
@connection.execute(<<~SQL.squish)
|
|
124
|
+
SELECT setval(
|
|
125
|
+
pg_get_serial_sequence('#{@target_schema}.#{table}', 'id'),
|
|
126
|
+
COALESCE((SELECT MAX(id) FROM #{qualified(@target_schema, table)}), 1)
|
|
127
|
+
)
|
|
128
|
+
SQL
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def update_blob_service_names!
|
|
132
|
+
service_name = Rails.application.config.active_storage.service.to_s
|
|
133
|
+
::Apartment::Tenant.switch(@target_schema) do
|
|
134
|
+
next unless @connection.table_exists?(:active_storage_blobs)
|
|
135
|
+
|
|
136
|
+
@connection.execute(<<~SQL.squish)
|
|
137
|
+
UPDATE active_storage_blobs
|
|
138
|
+
SET service_name = #{@connection.quote(service_name)}
|
|
139
|
+
SQL
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def copyable_tables
|
|
144
|
+
TableRegistry.copyable_tables(
|
|
145
|
+
@connection,
|
|
146
|
+
source_schema: @source_schema,
|
|
147
|
+
target_schema: @target_schema
|
|
148
|
+
).reject { |table| TableRegistry::SKIP_TABLES.include?(table) }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def qualified(schema, table)
|
|
152
|
+
%("#{schema}"."#{table}")
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Convert must run against ApartmentDiskStorage so blobs can be relocated and extracted.
|
|
8
|
+
class StoragePreconditions
|
|
9
|
+
class << self
|
|
10
|
+
def assert_apartment_disk!
|
|
11
|
+
service = ActiveStorage::Blob.service
|
|
12
|
+
return service if apartment_disk?(service)
|
|
13
|
+
|
|
14
|
+
hint = if s3_service?(service)
|
|
15
|
+
"Run decidim_apartment:storage:to_local first, then set Active Storage to ApartmentDiskStorage."
|
|
16
|
+
else
|
|
17
|
+
"Configure Active Storage service ApartmentDiskStorage (see active_storage docs)."
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
raise ConvertError,
|
|
21
|
+
"convert requires ApartmentDiskStorage (got #{service.class.name}). #{hint}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def report!
|
|
25
|
+
service = assert_apartment_disk!
|
|
26
|
+
root = service.respond_to?(:root) ? service.root : "(unknown)"
|
|
27
|
+
puts "storage: #{service.class.name} root=#{root}"
|
|
28
|
+
service
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def apartment_disk?(service)
|
|
32
|
+
service.is_a?(ActiveStorage::Service::ApartmentDiskStorageService) &&
|
|
33
|
+
!s3_service?(service)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def s3_service?(service)
|
|
37
|
+
service.is_a?(ActiveStorage::Service::ApartmentS3StorageService)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
module Decidim
|
|
7
|
+
module Apartment
|
|
8
|
+
module Convert
|
|
9
|
+
##
|
|
10
|
+
# Copies blob files from legacy Disk / public-tenant paths into storage/<tenant>/.
|
|
11
|
+
class StorageRelocator
|
|
12
|
+
Result = Struct.new(:copied, :already_present, :missing, :checksum_mismatch, keyword_init: true)
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def call(target_schema:)
|
|
16
|
+
new(target_schema: target_schema).call
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(target_schema:)
|
|
21
|
+
@target_schema = target_schema
|
|
22
|
+
@dry_run = Import::ConfirmPrompt.dry_run?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call
|
|
26
|
+
service = StoragePreconditions.assert_apartment_disk!
|
|
27
|
+
root = service.root
|
|
28
|
+
result = Result.new(copied: 0, already_present: 0, missing: 0, checksum_mismatch: 0)
|
|
29
|
+
|
|
30
|
+
::Apartment::Tenant.switch(@target_schema) do
|
|
31
|
+
next result unless defined?(ActiveStorage::Blob) && ActiveRecord::Base.connection.table_exists?(:active_storage_blobs)
|
|
32
|
+
|
|
33
|
+
ActiveStorage::Blob.unscoped.find_each do |blob|
|
|
34
|
+
process_blob!(blob, root, result)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
puts "StorageRelocator: copied=#{result.copied} already_present=#{result.already_present} " \
|
|
39
|
+
"missing=#{result.missing} checksum_mismatch=#{result.checksum_mismatch}"
|
|
40
|
+
|
|
41
|
+
raise ConvertError, "StorageRelocator: #{result.missing} blob file(s) missing on disk" if result.missing.positive?
|
|
42
|
+
raise ConvertError, "StorageRelocator: #{result.checksum_mismatch} blob checksum mismatch(es)" if result.checksum_mismatch.positive?
|
|
43
|
+
|
|
44
|
+
result
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def process_blob!(blob, root, result)
|
|
50
|
+
key = blob.key
|
|
51
|
+
dest = tenant_path(root, @target_schema, key)
|
|
52
|
+
|
|
53
|
+
if File.exist?(dest)
|
|
54
|
+
verify_file!(dest, blob, result)
|
|
55
|
+
result.already_present += 1
|
|
56
|
+
return
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
source = find_source(root, key)
|
|
60
|
+
unless source
|
|
61
|
+
result.missing += 1
|
|
62
|
+
puts " missing blob key=#{key}"
|
|
63
|
+
return
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
if @dry_run
|
|
67
|
+
puts " [dry-run] would copy #{source} → #{dest}"
|
|
68
|
+
result.copied += 1
|
|
69
|
+
return
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
|
73
|
+
FileUtils.cp(source, dest)
|
|
74
|
+
verify_file!(dest, blob, result)
|
|
75
|
+
result.copied += 1
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def find_source(root, key)
|
|
79
|
+
candidates = [
|
|
80
|
+
legacy_path(root, key),
|
|
81
|
+
tenant_path(root, "public", key)
|
|
82
|
+
]
|
|
83
|
+
candidates.find { |path| File.exist?(path) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def legacy_path(root, key)
|
|
87
|
+
File.join(root, folder_for(key), key)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def tenant_path(root, tenant, key)
|
|
91
|
+
File.join(root, tenant, folder_for(key), key)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def folder_for(key)
|
|
95
|
+
[key[0..1], key[2..3]].join("/")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def verify_file!(path, blob, result)
|
|
99
|
+
actual_size = File.size(path)
|
|
100
|
+
if blob.byte_size.present? && actual_size != blob.byte_size.to_i
|
|
101
|
+
result.checksum_mismatch += 1
|
|
102
|
+
puts " byte_size mismatch key=#{blob.key} expected=#{blob.byte_size} actual=#{actual_size}"
|
|
103
|
+
return
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
expected = blob.checksum
|
|
107
|
+
return if expected.blank?
|
|
108
|
+
|
|
109
|
+
actual = Digest::MD5.base64digest(File.binread(path))
|
|
110
|
+
return if actual == expected
|
|
111
|
+
|
|
112
|
+
result.checksum_mismatch += 1
|
|
113
|
+
puts " checksum mismatch key=#{blob.key}"
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Convert
|
|
6
|
+
##
|
|
7
|
+
# Tenant tables: what to clone, and how to load rows for OrganizationResolver.
|
|
8
|
+
class TableRegistry
|
|
9
|
+
SKIP_TABLES = %w(
|
|
10
|
+
apartment_distribution_keys
|
|
11
|
+
apartment_distribution_hosts
|
|
12
|
+
ar_internal_metadata
|
|
13
|
+
schema_migrations
|
|
14
|
+
spatial_ref_sys
|
|
15
|
+
).freeze
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
def copyable_tables(connection, source_schema:, target_schema:)
|
|
19
|
+
source = tables_in_schema(connection, source_schema)
|
|
20
|
+
target = tables_in_schema(connection, target_schema).to_set
|
|
21
|
+
|
|
22
|
+
source.select do |table|
|
|
23
|
+
SKIP_TABLES.exclude?(table) && target.include?(table) && columns_compatible?(
|
|
24
|
+
connection, source_schema, target_schema, table
|
|
25
|
+
)
|
|
26
|
+
end.sort
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Tables we can walk record-by-record with ActiveRecord + OrganizationResolver.
|
|
30
|
+
def resolvable_tables(connection, schema:)
|
|
31
|
+
copyable_tables(
|
|
32
|
+
connection,
|
|
33
|
+
source_schema: schema,
|
|
34
|
+
target_schema: schema
|
|
35
|
+
).select { |table| ar_model_for(table) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Clone copies these; pruner cannot assign org → every row stays (duplicated N×).
|
|
39
|
+
def keeper_tables(connection, schema:)
|
|
40
|
+
copyable_tables(
|
|
41
|
+
connection,
|
|
42
|
+
source_schema: schema,
|
|
43
|
+
target_schema: schema
|
|
44
|
+
).reject { |table| ar_model_for(table) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def ar_model_for(table_name)
|
|
48
|
+
ar_models_by_table[table_name]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def reset_ar_model_cache!
|
|
52
|
+
@ar_models_by_table = nil
|
|
53
|
+
@models_loaded = false
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def ar_models_by_table
|
|
59
|
+
@ar_models_by_table ||= begin
|
|
60
|
+
ensure_models_loaded!
|
|
61
|
+
ActiveRecord::Base.descendants.each_with_object({}) do |model, hash|
|
|
62
|
+
next if model.abstract_class?
|
|
63
|
+
next if model.table_name.blank?
|
|
64
|
+
next unless model.name.to_s.start_with?("Decidim::")
|
|
65
|
+
|
|
66
|
+
current = hash[model.table_name]
|
|
67
|
+
hash[model.table_name] = model if current.nil? || current < model
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def ensure_models_loaded!
|
|
73
|
+
return if @models_loaded
|
|
74
|
+
|
|
75
|
+
Rails.application.eager_load! unless Rails.application.config.eager_load
|
|
76
|
+
@models_loaded = true
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def tables_in_schema(connection, schema)
|
|
80
|
+
connection.select_values(<<~SQL.squish)
|
|
81
|
+
SELECT tablename
|
|
82
|
+
FROM pg_tables
|
|
83
|
+
WHERE schemaname = #{connection.quote(schema)}
|
|
84
|
+
ORDER BY tablename
|
|
85
|
+
SQL
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def column_names(connection, schema, table)
|
|
89
|
+
connection.select_values(<<~SQL.squish)
|
|
90
|
+
SELECT column_name
|
|
91
|
+
FROM information_schema.columns
|
|
92
|
+
WHERE table_schema = #{connection.quote(schema)}
|
|
93
|
+
AND table_name = #{connection.quote(table)}
|
|
94
|
+
ORDER BY ordinal_position
|
|
95
|
+
SQL
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def columns_compatible?(connection, source_schema, target_schema, table)
|
|
99
|
+
source_cols = column_names(connection, source_schema, table)
|
|
100
|
+
target_cols = column_names(connection, target_schema, table)
|
|
101
|
+
return false if source_cols.empty? || target_cols.empty?
|
|
102
|
+
|
|
103
|
+
source_cols == target_cols
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|