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.
Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE-AGPLv3.txt +661 -0
  3. data/README.md +167 -0
  4. data/Rakefile +405 -0
  5. data/app/commands/decidim/apartment/create_tenant_command.rb +94 -0
  6. data/app/commands/decidim/apartment/noop_settings_tab_command.rb +13 -0
  7. data/app/commands/decidim/apartment/performance/create_seed_tentant.rb +194 -0
  8. data/app/controllers/decidim/apartment/tenants_controller.rb +29 -0
  9. data/app/forms/decidim/apartment/admin/organization_settings_form.rb +44 -0
  10. data/app/forms/decidim/apartment/tenant_form.rb +35 -0
  11. data/app/helpers/decidim/apartment/dashboard_helper.rb +17 -0
  12. data/app/models/decidim/apartment/distribution_host.rb +21 -0
  13. data/app/models/decidim/apartment/distribution_key.rb +91 -0
  14. data/app/overrides/apartment_edit_organization.rb +21 -0
  15. data/app/overrides/apartment_system_asset.rb +10 -0
  16. data/app/packs/entrypoints/apartment_system_styles.scss +1 -0
  17. data/app/packs/stylesheets/decidim/apartment/system/_apartment_system_styles.scss +95 -0
  18. data/app/services/active_storage/service/apartment_disk_storage_service.rb +14 -0
  19. data/app/services/active_storage/service/apartment_s3_storage_service.rb +24 -0
  20. data/app/views/decidim/apartment/dashboard/_apartment.html.erb +55 -0
  21. data/app/views/decidim/apartment/dashboard/show.html.erb +3 -0
  22. data/app/views/decidim/apartment/organizations/_organizations_list.html.erb +45 -0
  23. data/app/views/decidim/apartment/organizations/index.html.erb +9 -0
  24. data/app/views/decidim/apartment/organizations/new.html.erb +11 -0
  25. data/config/assets.rb +15 -0
  26. data/config/locales/decidim_apartment.en.yml +58 -0
  27. data/config/locales/decidim_apartment_toggle.en.yml +15 -0
  28. data/db/migrate/20250814075441_add_apartment_distribution_key.rb +15 -0
  29. data/db/migrate/20260907100000_add_apartment_distribution_hosts.rb +32 -0
  30. data/lib/decidim/apartment/active_storage_wrap.rb +51 -0
  31. data/lib/decidim/apartment/allowed_hosts.rb +78 -0
  32. data/lib/decidim/apartment/cache_namespace.rb +38 -0
  33. data/lib/decidim/apartment/config_registry.rb +102 -0
  34. data/lib/decidim/apartment/configuration.rb +11 -0
  35. data/lib/decidim/apartment/convert/attachment_integrity.rb +130 -0
  36. data/lib/decidim/apartment/convert/constraint_graph_pruner.rb +183 -0
  37. data/lib/decidim/apartment/convert/errors.rb +9 -0
  38. data/lib/decidim/apartment/convert/host_gate.rb +76 -0
  39. data/lib/decidim/apartment/convert/org_distribution_report.rb +106 -0
  40. data/lib/decidim/apartment/convert/org_metrics.rb +164 -0
  41. data/lib/decidim/apartment/convert/organization_converter.rb +154 -0
  42. data/lib/decidim/apartment/convert/organization_pruner.rb +210 -0
  43. data/lib/decidim/apartment/convert/organization_resolver.rb +386 -0
  44. data/lib/decidim/apartment/convert/paper_trail_report.rb +127 -0
  45. data/lib/decidim/apartment/convert/preflight.rb +140 -0
  46. data/lib/decidim/apartment/convert/schema_cloner.rb +157 -0
  47. data/lib/decidim/apartment/convert/storage_preconditions.rb +43 -0
  48. data/lib/decidim/apartment/convert/storage_relocator.rb +118 -0
  49. data/lib/decidim/apartment/convert/table_registry.rb +109 -0
  50. data/lib/decidim/apartment/convert.rb +20 -0
  51. data/lib/decidim/apartment/demo/blob.rb +31 -0
  52. data/lib/decidim/apartment/demo/city.rb +107 -0
  53. data/lib/decidim/apartment/demo/education.rb +83 -0
  54. data/lib/decidim/apartment/demo/foss.rb +153 -0
  55. data/lib/decidim/apartment/demo/homepage.rb +39 -0
  56. data/lib/decidim/apartment/demo/localized.rb +27 -0
  57. data/lib/decidim/apartment/demo/provision.rb +107 -0
  58. data/lib/decidim/apartment/demo/runner.rb +66 -0
  59. data/lib/decidim/apartment/demo/system_admin.rb +24 -0
  60. data/lib/decidim/apartment/demo.rb +19 -0
  61. data/lib/decidim/apartment/elevator.rb +20 -0
  62. data/lib/decidim/apartment/engine.rb +140 -0
  63. data/lib/decidim/apartment/excluded_model_pin.rb +40 -0
  64. data/lib/decidim/apartment/host_registry.rb +71 -0
  65. data/lib/decidim/apartment/import/confirm_prompt.rb +46 -0
  66. data/lib/decidim/apartment/import/database_restorer.rb +105 -0
  67. data/lib/decidim/apartment/import/dump_host_extractor.rb +67 -0
  68. data/lib/decidim/apartment/import/dump_preview.rb +96 -0
  69. data/lib/decidim/apartment/import/dump_transformer.rb +87 -0
  70. data/lib/decidim/apartment/import/errors.rb +9 -0
  71. data/lib/decidim/apartment/import/provision_tenant.rb +50 -0
  72. data/lib/decidim/apartment/import/schema_auditor.rb +118 -0
  73. data/lib/decidim/apartment/import/storage_migrator.rb +71 -0
  74. data/lib/decidim/apartment/import/tenant_context.rb +226 -0
  75. data/lib/decidim/apartment/import/tenant_importer.rb +73 -0
  76. data/lib/decidim/apartment/import/verifier.rb +58 -0
  77. data/lib/decidim/apartment/import.rb +15 -0
  78. data/lib/decidim/apartment/ops/attribute_cryptor.rb +68 -0
  79. data/lib/decidim/apartment/ops/ciphertext_rewriter.rb +230 -0
  80. data/lib/decidim/apartment/ops/encrypted_attribute_catalog.rb +106 -0
  81. data/lib/decidim/apartment/ops/encrypted_attribute_registry.rb +74 -0
  82. data/lib/decidim/apartment/ops/postgres_permissions.rb +179 -0
  83. data/lib/decidim/apartment/ops/secret_key_preflight.rb +94 -0
  84. data/lib/decidim/apartment/ops/secret_key_rotation.rb +226 -0
  85. data/lib/decidim/apartment/ops/storage_transfer.rb +188 -0
  86. data/lib/decidim/apartment/overrides/active_job_with_tenant.rb +38 -0
  87. data/lib/decidim/apartment/overrides/core_seeds.rb +31 -0
  88. data/lib/decidim/apartment/overrides/create_organization_command.rb +69 -0
  89. data/lib/decidim/apartment/overrides/dashboard_controller.rb +30 -0
  90. data/lib/decidim/apartment/overrides/decidim_seed.rb +16 -0
  91. data/lib/decidim/apartment/overrides/organizations_controller_overrides.rb +75 -0
  92. data/lib/decidim/apartment/overrides/postgresql_enable_extension.rb +15 -0
  93. data/lib/decidim/apartment/overrides/postgresql_schema_adapter.rb +34 -0
  94. data/lib/decidim/apartment/overrides/postgresql_schema_import.rb +35 -0
  95. data/lib/decidim/apartment/overrides/postgresql_schema_search_path.rb +15 -0
  96. data/lib/decidim/apartment/overrides/postgresql_schema_statements.rb +17 -0
  97. data/lib/decidim/apartment/overrides/seeds.rb +52 -0
  98. data/lib/decidim/apartment/overrides/tenant_init_without_database.rb +19 -0
  99. data/lib/decidim/apartment/overrides/update_organization_command.rb +38 -0
  100. data/lib/decidim/apartment/postgres_extensions.rb +198 -0
  101. data/lib/decidim/apartment/rake_replace.rb +21 -0
  102. data/lib/decidim/apartment/schema_search_path.rb +43 -0
  103. data/lib/decidim/apartment/system_organization.rb +67 -0
  104. data/lib/decidim/apartment/test/factories.rb +11 -0
  105. data/lib/decidim/apartment/test/seeds/association_1.jpg +0 -0
  106. data/lib/decidim/apartment/test/seeds/association_2.jpeg +0 -0
  107. data/lib/decidim/apartment/test/seeds/association_3.jpg +0 -0
  108. data/lib/decidim/apartment/test/seeds/association_4.jpg +0 -0
  109. data/lib/decidim/apartment/test/seeds/bg_Dusty Grass.png +0 -0
  110. data/lib/decidim/apartment/test/seeds/bg_Hyper Blue.png +0 -0
  111. data/lib/decidim/apartment/test/seeds/bg_Moonwalker.png +0 -0
  112. data/lib/decidim/apartment/test/seeds/bg_Neon Green.png +0 -0
  113. data/lib/decidim/apartment/test/seeds/bg_Visual Blue.png +0 -0
  114. data/lib/decidim/apartment/test/seeds/bg_hinehouse.png +0 -0
  115. data/lib/decidim/apartment/test/seeds/city_1.jpg +0 -0
  116. data/lib/decidim/apartment/test/seeds/city_2.jpg +0 -0
  117. data/lib/decidim/apartment/test/seeds/city_3.jpg +0 -0
  118. data/lib/decidim/apartment/test/seeds/city_4.webp +0 -0
  119. data/lib/decidim/apartment/test/seeds/city_5.jpg +0 -0
  120. data/lib/decidim/apartment/test/seeds/debian.jpeg +0 -0
  121. data/lib/decidim/apartment/test/seeds/democratic_school_1.jpg +0 -0
  122. data/lib/decidim/apartment/test/seeds/drupal.jpg +0 -0
  123. data/lib/decidim/apartment/test/seeds/foss.jpg +0 -0
  124. data/lib/decidim/apartment/test/seeds/immich.jpg +0 -0
  125. data/lib/decidim/apartment/test/seeds/kid_1.jpg +0 -0
  126. data/lib/decidim/apartment/test/seeds/kid_2.jpg +0 -0
  127. data/lib/decidim/apartment/test/seeds/montessori_1.jpg +0 -0
  128. data/lib/decidim/apartment/test/seeds/montessori_2.jpg +0 -0
  129. data/lib/decidim/apartment/test/seeds/neighboor_1.jpg +0 -0
  130. data/lib/decidim/apartment/test/seeds/neighbor_2.jpg +0 -0
  131. data/lib/decidim/apartment/test/seeds/neighbor_3.jpg +0 -0
  132. 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
  133. 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
  134. 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
  135. 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
  136. 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
  137. 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
  138. 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
  139. data/lib/decidim/apartment/test/seeds/pattern_new_01.png +0 -0
  140. data/lib/decidim/apartment/test/seeds/pattern_new_02.png +0 -0
  141. data/lib/decidim/apartment/test/seeds/pattern_new_03.png +0 -0
  142. data/lib/decidim/apartment/test/seeds/pattern_squares_01.png +0 -0
  143. data/lib/decidim/apartment/test/seeds/pattern_squares_02.png +0 -0
  144. data/lib/decidim/apartment/test/seeds/pattern_squares_03.png +0 -0
  145. data/lib/decidim/apartment/test/seeds/pattern_squares_04.png +0 -0
  146. data/lib/decidim/apartment/test/seeds/pattern_squares_05.png +0 -0
  147. data/lib/decidim/apartment/test/seeds/pattern_squares_06.png +0 -0
  148. data/lib/decidim/apartment/test/seeds/pattern_squares_07.png +0 -0
  149. data/lib/decidim/apartment/test/seeds/pattern_squares_08.png +0 -0
  150. data/lib/decidim/apartment/test/seeds/pattern_squares_09.png +0 -0
  151. data/lib/decidim/apartment/test/seeds/pattern_squares_10.png +0 -0
  152. data/lib/decidim/apartment/test/seeds/pattern_squares_11.png +0 -0
  153. data/lib/decidim/apartment/test/seeds/pattern_squares_12.png +0 -0
  154. data/lib/decidim/apartment/test/seeds/pattern_squares_13.png +0 -0
  155. data/lib/decidim/apartment/test/seeds/pattern_squares_14.png +0 -0
  156. data/lib/decidim/apartment/test/seeds/pattern_squares_15.png +0 -0
  157. data/lib/decidim/apartment/test/seeds/pattern_squares_16.png +0 -0
  158. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_01.png +0 -0
  159. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_02.png +0 -0
  160. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_03.png +0 -0
  161. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_04.png +0 -0
  162. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_05.png +0 -0
  163. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_06.png +0 -0
  164. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_07.png +0 -0
  165. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_08.png +0 -0
  166. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_09.png +0 -0
  167. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_10.png +0 -0
  168. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_11.png +0 -0
  169. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_12.png +0 -0
  170. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_13.png +0 -0
  171. data/lib/decidim/apartment/test/seeds/rails.jpg +0 -0
  172. data/lib/decidim/apartment/test/seeds/school_1.jpg +0 -0
  173. data/lib/decidim/apartment/test/seeds/school_3.jpg +0 -0
  174. data/lib/decidim/apartment/test/seeds/school_4.jpg +0 -0
  175. data/lib/decidim/apartment/test/seeds/school_5.jpg +0 -0
  176. data/lib/decidim/apartment/test/seeds/silverstripe.jpg +0 -0
  177. data/lib/decidim/apartment/test/seeds/street_1.jpg +0 -0
  178. data/lib/decidim/apartment/test/seeds/street_2.jpg +0 -0
  179. data/lib/decidim/apartment/test/seeds/street_3.jpg +0 -0
  180. data/lib/decidim/apartment/test/seeds/urban_agri_1.webp +0 -0
  181. data/lib/decidim/apartment/test/seeds/urban_agri_2.jpg +0 -0
  182. data/lib/decidim/apartment/test/seeds/urban_agri_3.jpg +0 -0
  183. data/lib/decidim/apartment/test/seeds/urban_agri_4.jpg +0 -0
  184. data/lib/decidim/apartment/test/seeds/world-map-colors.png +0 -0
  185. data/lib/decidim/apartment/version.rb +44 -0
  186. data/lib/decidim/apartment.rb +69 -0
  187. data/lib/tasks/decidim_apartment_check_install.rake +58 -0
  188. data/lib/tasks/decidim_apartment_convert.rake +60 -0
  189. data/lib/tasks/decidim_apartment_gem_manager.rake +62 -0
  190. data/lib/tasks/decidim_apartment_import.rake +39 -0
  191. data/lib/tasks/decidim_apartment_install_extension.rake +49 -0
  192. data/lib/tasks/decidim_apartment_performance_seed.rake +12 -0
  193. data/lib/tasks/decidim_apartment_rotate.rake +17 -0
  194. data/lib/tasks/decidim_apartment_storage.rake +27 -0
  195. data/lib/tasks/decidim_download_your_data_tasks.rake +121 -0
  196. data/lib/tasks/decidim_mailers.rake +38 -0
  197. data/lib/tasks/decidim_meetings.rake +30 -0
  198. data/lib/tasks/decidim_metrics_tasks.rake +29 -0
  199. data/lib/tasks/decidim_open_data_tasks.rake +24 -0
  200. data/lib/tasks/decidim_participatory_processes.rake +20 -0
  201. data/lib/tasks/decidim_reminders_tasks.rake +24 -0
  202. metadata +361 -0
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Elevator to be used in Rails middleware stack.
7
+ # This elevator will parse the host and return the distribution key
8
+ # And will ensure /system administration stays in "public"
9
+ class Elevator < ::Apartment::Elevators::Generic
10
+ def parse_tenant_name(request)
11
+ return "public" if request.path.start_with?("/system")
12
+
13
+ distribution_key = Decidim::Apartment::DistributionKey.for_host(request.host)
14
+ return "public" unless distribution_key
15
+
16
+ distribution_key.key
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "decidim/core"
5
+ require "decidim/toggle"
6
+
7
+ module Decidim
8
+ module Apartment
9
+ # Rails engine: elevator, system UI, zero-config boot hooks.
10
+ class Engine < ::Rails::Engine
11
+ isolate_namespace Decidim::Apartment
12
+
13
+ initializer "decidim_apartment.ignore_deface_overrides" do
14
+ overrides_path = root.join("app/overrides").to_s
15
+ Rails.autoloaders.main.ignore(overrides_path) if Dir.exist?(overrides_path)
16
+ end
17
+
18
+ routes do
19
+ ::Decidim::System::Engine.routes.draw do
20
+ resources :apartment_tenants, only: [:create], controller: "/decidim/apartment/tenants"
21
+ end
22
+ end
23
+
24
+ config.to_prepare do
25
+ ::Apartment.use_sql = Decidim::Apartment.use_sql?
26
+ Decidim::Apartment.config_registry.apply!
27
+ Decidim::Apartment.encrypted_attribute_registry.register_defaults!
28
+ Decidim::Core::Seeds.include(Decidim::Apartment::Overrides::CoreSeeds)
29
+ Decidim::Seeds.include(Decidim::Apartment::Overrides::Seeds)
30
+ Decidim.singleton_class.prepend(Decidim::Apartment::Overrides::DecidimSeed)
31
+ require "apartment/adapters/postgresql_adapter"
32
+ ::Apartment::Adapters::PostgresqlSchemaAdapter.prepend(
33
+ Decidim::Apartment::Overrides::PostgresqlSchemaImport
34
+ )
35
+ ::Apartment::Adapters::PostgresqlSchemaFromSqlAdapter.prepend(
36
+ Decidim::Apartment::Overrides::PostgresqlSchemaAdapter
37
+ )
38
+ Decidim::System::CreateOrganization.include(Decidim::Apartment::Overrides::CreateOrganizationCommand)
39
+ Decidim::System::UpdateOrganization.include(Decidim::Apartment::Overrides::UpdateOrganizationCommand)
40
+ Decidim::System::OrganizationsController.include(
41
+ Decidim::Apartment::Overrides::OrganizationsControllerOverrides
42
+ )
43
+ Decidim::System::DashboardController.include(Decidim::Apartment::Overrides::DashboardController)
44
+ ActiveSupport.on_load(:active_job) do
45
+ include Decidim::Apartment::Overrides::ActiveJobWithTenant
46
+ singleton_class.prepend Decidim::Apartment::Overrides::ActiveJobWithTenant::Execute
47
+ end
48
+ end
49
+
50
+ initializer "decidim_apartment.tenant_init_without_database" do
51
+ ::Apartment::Tenant.singleton_class.prepend(
52
+ Decidim::Apartment::Overrides::TenantInitWithoutDatabase
53
+ )
54
+ end
55
+
56
+ initializer "decidim_apartment.strip_schema_table_length" do
57
+ ActiveSupport.on_load(:active_record) do
58
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.prepend(
59
+ Decidim::Apartment::Overrides::PostgresqlSchemaStatements
60
+ )
61
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.prepend(
62
+ Decidim::Apartment::Overrides::PostgresqlSchemaSearchPath
63
+ )
64
+ end
65
+ ActiveSupport.on_load(:active_record_postgresqladapter) do
66
+ prepend Decidim::Apartment::Overrides::PostgresqlEnableExtension
67
+ prepend Decidim::Apartment::Overrides::PostgresqlDropTableTenantScoped
68
+ end
69
+ end
70
+
71
+ initializer "decidim_apartment.configure_apartment" do
72
+ raise "Add ros-apartment to your Gemfile" unless Gem.loaded_specs.has_key?("ros-apartment")
73
+
74
+ ::Apartment.configure do |apartment_config|
75
+ apartment_config.tenant_names = -> { Decidim::Apartment::DistributionKey.tenant_keys }
76
+ apartment_config.default_tenant = "public"
77
+ apartment_config.use_sql = Decidim::Apartment.use_sql?
78
+ apartment_config.active_record_log = Rails.env.development?
79
+ Decidim::Apartment.config_registry.apply!(apartment_config)
80
+ end
81
+ Rails.application.config.middleware.insert_before Decidim::Middleware::CurrentOrganization,
82
+ Decidim::Apartment::Elevator
83
+ Rails.application.config.middleware.insert_after Decidim::Middleware::CurrentOrganization,
84
+ Decidim::Apartment::SystemOrganization
85
+ Decidim::Api::Engine.config.middleware.use Decidim::Apartment::Elevator if defined?(Decidim::Api::Engine)
86
+ end
87
+
88
+ initializer "decidim_apartment.schema_search_path" do
89
+ config.after_initialize do
90
+ Decidim::Apartment::SchemaSearchPath.ensure!
91
+ end
92
+ end
93
+
94
+ initializer "decidim_apartment.pin_excluded_models" do
95
+ config.after_initialize do
96
+ require "good_job" if Decidim::Apartment.good_job_available?
97
+ Decidim::Apartment.config_registry.apply!
98
+ end
99
+ end
100
+
101
+ initializer "decidim_apartment.cache_namespace" do
102
+ config.after_initialize do
103
+ Decidim::Apartment::CacheNamespace.wrap!
104
+ end
105
+ end
106
+
107
+ initializer "decidim_apartment.active_storage" do
108
+ config.after_initialize do
109
+ Decidim::Apartment::ActiveStorageWrap.wrap!
110
+ end
111
+ end
112
+
113
+ initializer "decidim_apartment.allowed_hosts", after: :load_environment_config do |app|
114
+ app.config.after_initialize do
115
+ Decidim::Apartment::AllowedHosts.apply!(app)
116
+ end
117
+ end
118
+
119
+ initializer "decidim_apartment.add_cells_view_paths" do
120
+ Cell::ViewModel.view_paths << File.expand_path("#{Decidim::Apartment::Engine.root}/app/cells")
121
+ end
122
+
123
+ initializer "decidim_apartment.webpacker.assets_path" do
124
+ Decidim.register_assets_path File.expand_path("#{Decidim::Apartment::Engine.root}/app/packs")
125
+ end
126
+
127
+ initializer "decidim_apartment.organization_settings_tab",
128
+ after: "decidim_toggle.organization_settings_tabs" do
129
+ Decidim::Toggle.settings_tabs :organization_settings do |tabs|
130
+ tabs.add_tab :decidim_apartment,
131
+ I18n.t("decidim_toggle.system.#{Decidim::Apartment::MODULE_NAME}.tab"),
132
+ form: Decidim::Apartment::Admin::OrganizationSettingsForm,
133
+ command: Decidim::Apartment::NoopSettingsTabCommand,
134
+ module_name: Decidim::Apartment::MODULE_NAME,
135
+ position: 5
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ # ros-apartment only prefixes excluded table_name during Tenant.init.
6
+ # apply! can run again after models load; pin so writes stay in public.
7
+ class ExcludedModelPin
8
+ def self.call(names, schema: "public")
9
+ names.each { |name| new(name, schema).pin }
10
+ end
11
+
12
+ def initialize(name, schema)
13
+ @name = name
14
+ @schema = schema
15
+ end
16
+
17
+ def pin
18
+ return if abstract?
19
+
20
+ klass.table_name = "#{@schema}.#{bare_table}"
21
+ rescue NameError
22
+ nil
23
+ end
24
+
25
+ private
26
+
27
+ def klass
28
+ @klass ||= @name.constantize
29
+ end
30
+
31
+ def abstract?
32
+ klass.respond_to?(:abstract_class?) && klass.abstract_class?
33
+ end
34
+
35
+ def bare_table
36
+ klass.table_name.to_s.split(".", 2).last
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Syncs primary + secondary hosts into +apartment_distribution_hosts+ (public).
7
+ class HostRegistry
8
+ class << self
9
+ def sync!(distribution_key, primary_host:, secondary_hosts: [])
10
+ ::Apartment::Tenant.switch("public") do
11
+ new(distribution_key).sync!(
12
+ primary_host: primary_host,
13
+ secondary_hosts: secondary_hosts
14
+ )
15
+ end
16
+ end
17
+ end
18
+
19
+ def initialize(distribution_key)
20
+ @distribution_key = distribution_key
21
+ end
22
+
23
+ def sync!(primary_host:, secondary_hosts: [])
24
+ hosts = normalize_hosts(primary_host, secondary_hosts)
25
+ assert_global_uniqueness!(hosts)
26
+
27
+ @distribution_key.update!(host: hosts[:primary])
28
+ replace_rows!(hosts)
29
+ @distribution_key
30
+ end
31
+
32
+ private
33
+
34
+ def normalize_hosts(primary_host, secondary_hosts)
35
+ primary = primary_host.to_s.strip
36
+ raise ArgumentError, "primary host blank" if primary.blank?
37
+
38
+ secondaries = Array(secondary_hosts).map { |h| h.to_s.strip }.compact_blank.uniq
39
+ secondaries -= [primary]
40
+ { primary: primary, secondaries: secondaries }
41
+ end
42
+
43
+ def assert_global_uniqueness!(hosts)
44
+ all = [hosts[:primary], *hosts[:secondaries]]
45
+ conflict = DistributionHost
46
+ .where(host: all)
47
+ .where.not(apartment_distribution_key_id: @distribution_key.id)
48
+ .pluck(:host)
49
+ return if conflict.empty?
50
+
51
+ raise ArgumentError, "host(s) already registered: #{conflict.join(", ")}"
52
+ end
53
+
54
+ def replace_rows!(hosts)
55
+ @distribution_key.distribution_hosts.delete_all
56
+ DistributionHost.create!(
57
+ distribution_key: @distribution_key,
58
+ host: hosts[:primary],
59
+ kind: "primary"
60
+ )
61
+ hosts[:secondaries].each do |host|
62
+ DistributionHost.create!(
63
+ distribution_key: @distribution_key,
64
+ host: host,
65
+ kind: "secondary"
66
+ )
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ ##
7
+ # Interactive y/n confirmation gates before write phases.
8
+ class ConfirmPrompt
9
+ class << self
10
+ def ask(message)
11
+ if dry_run?
12
+ puts "#{message} [dry-run: skipped]"
13
+ return true
14
+ end
15
+
16
+ if auto_yes?
17
+ puts "#{message} [auto: yes]"
18
+ return true
19
+ end
20
+
21
+ raise ImportError, "interactive confirmation required (use YES=1 or DRY_RUN=1)" unless $stdin.tty?
22
+
23
+ print "#{message} "
24
+ answer = $stdin.gets&.strip&.downcase
25
+ return true if %w(y yes).include?(answer)
26
+
27
+ puts "Aborted."
28
+ exit 1
29
+ end
30
+
31
+ def dry_run?
32
+ ENV["DRY_RUN"].to_s == "1"
33
+ end
34
+
35
+ def auto_yes?
36
+ ENV["YES"].to_s == "1"
37
+ end
38
+
39
+ def write_mode?
40
+ !dry_run?
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ ##
7
+ # Restores a transformed tenant dump into the target PostgreSQL schema.
8
+ class DatabaseRestorer
9
+ class << self
10
+ def call(context:)
11
+ new(context: context).call
12
+ end
13
+ end
14
+
15
+ def initialize(context:)
16
+ @context = context
17
+ end
18
+
19
+ def call
20
+ transformed = DumpTransformer.call(
21
+ dump_file: @context.dump_file,
22
+ source_schema: @context.source_schema,
23
+ target_schema: @context.target_tenant
24
+ )
25
+
26
+ SchemaAuditor.audit_transformed!(transformed[:path], target_schema: @context.target_tenant)
27
+
28
+ sql_file = build_psql_script(transformed[:path])
29
+ run_psql!(sql_file)
30
+ reset_sequences!
31
+ update_blob_service_names!
32
+
33
+ puts "Database import complete (#{transformed[:lines]} SQL lines)"
34
+ end
35
+
36
+ private
37
+
38
+ def build_psql_script(transformed_path)
39
+ script = Tempfile.new(["decidim_apartment_import_exec", ".sql"])
40
+ script.write(<<~SQL.squish)
41
+ SET search_path TO "#{@context.target_tenant}", public;
42
+ SET session_replication_role = replica;
43
+ SQL
44
+ script.write(File.read(transformed_path))
45
+ script.write("\nSET session_replication_role = DEFAULT;\n")
46
+ script.flush
47
+ script.path
48
+ end
49
+
50
+ def run_psql!(sql_file)
51
+ config = ActiveRecord::Base.connection_db_config.configuration_hash
52
+ env = {
53
+ "PGPASSWORD" => config[:password].to_s,
54
+ "PGHOST" => config[:host].to_s,
55
+ "PGPORT" => config[:port].to_s,
56
+ "PGUSER" => config[:username].to_s,
57
+ "PGDATABASE" => config[:database].to_s
58
+ }
59
+
60
+ success = system(env, "psql", "-v", "ON_ERROR_STOP=1", "-f", sql_file)
61
+ raise ImportError, "psql restore failed" unless success
62
+ end
63
+
64
+ def reset_sequences!
65
+ distribution_key.switch do
66
+ tables = ActiveRecord::Base.connection.select_values(<<~SQL.squish)
67
+ SELECT tablename
68
+ FROM pg_tables
69
+ WHERE schemaname = '#{@context.target_tenant}'
70
+ SQL
71
+
72
+ tables.each do |table|
73
+ quoted_table = ActiveRecord::Base.connection.quote_table_name(table)
74
+ ActiveRecord::Base.connection.execute(<<~SQL.squish)
75
+ SELECT setval(
76
+ pg_get_serial_sequence('#{@context.target_tenant}.#{table}', 'id'),
77
+ COALESCE((SELECT MAX(id) FROM #{quoted_table}), 1)
78
+ )
79
+ SQL
80
+ rescue ActiveRecord::StatementInvalid
81
+ next
82
+ end
83
+ end
84
+ end
85
+
86
+ def update_blob_service_names!
87
+ service_name = Rails.application.config.active_storage.service.to_s
88
+ distribution_key.switch do
89
+ next unless ActiveRecord::Base.connection.table_exists?(:active_storage_blobs)
90
+
91
+ ActiveRecord::Base.connection.execute(<<~SQL.squish)
92
+ UPDATE active_storage_blobs SET service_name = #{ActiveRecord::Base.connection.quote(service_name)}
93
+ SQL
94
+ end
95
+ end
96
+
97
+ def distribution_key
98
+ @distribution_key ||= ::Apartment::Tenant.switch("public") do
99
+ Decidim::Apartment::DistributionKey.find_by!(key: @context.target_tenant)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ ##
7
+ # Extracts organization host(s) from a tenant SQL dump.
8
+ class DumpHostExtractor
9
+ ORG_TABLE = "decidim_organizations"
10
+
11
+ class << self
12
+ def call(dump_file, source_schema)
13
+ new(dump_file, source_schema).call
14
+ end
15
+ end
16
+
17
+ def initialize(dump_file, source_schema)
18
+ @dump_file = dump_file
19
+ @source_schema = source_schema
20
+ end
21
+
22
+ def call
23
+ hosts = []
24
+ org_names = []
25
+ in_copy = false
26
+
27
+ File.foreach(@dump_file) do |line|
28
+ if copy_start?(line)
29
+ in_copy = true
30
+ next
31
+ end
32
+
33
+ if in_copy
34
+ break if line.strip == "\\."
35
+
36
+ fields = line.chomp.split("\t")
37
+ next if fields.size < 3
38
+
39
+ org_names << fields[1]
40
+ hosts << normalize_host(fields[2])
41
+ end
42
+ end
43
+
44
+ distinct_hosts = hosts.uniq
45
+ raise ImportError, "dump has no organization host" if distinct_hosts.empty?
46
+ raise ImportError, "dump has multiple organizations (#{distinct_hosts.join(", ")})" if distinct_hosts.size > 1
47
+
48
+ { host: distinct_hosts.first, org_name: org_names.first }
49
+ end
50
+
51
+ private
52
+
53
+ def copy_start?(line)
54
+ patterns = [
55
+ /^COPY\s+#{Regexp.escape(@source_schema)}\.#{ORG_TABLE}\s/i,
56
+ /^COPY\s+"#{Regexp.escape(@source_schema)}"\.#{ORG_TABLE}\s/i
57
+ ]
58
+ patterns.any? { |pattern| line.match?(pattern) }
59
+ end
60
+
61
+ def normalize_host(host)
62
+ host.to_s.strip.downcase.delete_suffix(".")
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ ##
7
+ # Read-only row counts from COPY blocks in a tenant dump.
8
+ class DumpPreview
9
+ SPACE_TABLES = %w(
10
+ decidim_participatory_processes
11
+ decidim_assemblies
12
+ decidim_conferences
13
+ decidim_initiatives
14
+ ).freeze
15
+
16
+ COUNTERS = {
17
+ organizations: "decidim_organizations",
18
+ users: "decidim_users",
19
+ components: "decidim_components",
20
+ blobs: "active_storage_blobs"
21
+ }.freeze
22
+
23
+ Stats = Struct.new(
24
+ :organizations,
25
+ :users,
26
+ :spaces,
27
+ :components,
28
+ :blobs,
29
+ keyword_init: true
30
+ )
31
+
32
+ class << self
33
+ def call(dump_file, source_schema)
34
+ new(dump_file, source_schema).call
35
+ end
36
+
37
+ def detect_source_schema(dump_file)
38
+ File.foreach(dump_file) do |line|
39
+ match = line.match(/CREATE\s+SCHEMA\s+(?:IF\s+NOT\s+EXISTS\s+)?["']?([0-9a-f-]{36})/i)
40
+ return match[1].downcase if match
41
+
42
+ match = line.match(/SET\s+search_path\s*=\s*["']?([0-9a-f-]{36})/i)
43
+ return match[1].downcase if match
44
+ end
45
+ raise ImportError, "could not detect SOURCE_SCHEMA from dump; set SOURCE_SCHEMA explicitly"
46
+ end
47
+ end
48
+
49
+ def initialize(dump_file, source_schema)
50
+ @dump_file = dump_file
51
+ @source_schema = source_schema
52
+ @counts = Hash.new(0)
53
+ @current_table = nil
54
+ end
55
+
56
+ def call
57
+ File.foreach(@dump_file) do |line|
58
+ if copy_start?(line)
59
+ @current_table = table_from_copy(line)
60
+ next
61
+ end
62
+
63
+ if @current_table
64
+ if line.strip == "\\."
65
+ @current_table = nil
66
+ else
67
+ @counts[@current_table] += 1
68
+ end
69
+ end
70
+ end
71
+
72
+ Stats.new(
73
+ organizations: @counts[COUNTERS[:organizations]],
74
+ users: @counts[COUNTERS[:users]],
75
+ spaces: SPACE_TABLES.sum { |table| @counts[table] },
76
+ components: @counts[COUNTERS[:components]],
77
+ blobs: @counts[COUNTERS[:blobs]]
78
+ )
79
+ end
80
+
81
+ private
82
+
83
+ def copy_start?(line)
84
+ return false unless line.start_with?("COPY ")
85
+
86
+ table_from_copy(line).present?
87
+ end
88
+
89
+ def table_from_copy(line)
90
+ match = line.match(/^COPY\s+(?:"?#{Regexp.escape(@source_schema)}"?)\.([^\s(]+)/i)
91
+ match&.[](1)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ ##
7
+ # Rewrites a tenant-scoped SQL dump for import into a target schema.
8
+ class DumpTransformer
9
+ STRIP_PUBLIC_TABLES = /
10
+ \A
11
+ COPY\s+public\.
12
+ (?:
13
+ apartment_distribution_keys |
14
+ apartment_distribution_hosts |
15
+ schema_migrations |
16
+ ar_internal_metadata |
17
+ decidim_.* |
18
+ active_storage_.*
19
+ )
20
+ /ix
21
+
22
+ REJECT_LINE = /
23
+ \A\s*
24
+ (?:
25
+ DROP\s+SCHEMA\s+(?:IF\s+EXISTS\s+)?(?:public|shared_extensions)\b |
26
+ ALTER\s+DATABASE\b |
27
+ TRUNCATE\s+public\.
28
+ )
29
+ /ix
30
+
31
+ class << self
32
+ def call(dump_file:, source_schema:, target_schema:)
33
+ new(dump_file: dump_file, source_schema: source_schema, target_schema: target_schema).call
34
+ end
35
+ end
36
+
37
+ def initialize(dump_file:, source_schema:, target_schema:)
38
+ @dump_file = dump_file
39
+ @source_schema = source_schema.downcase
40
+ @target_schema = target_schema.downcase
41
+ end
42
+
43
+ def call
44
+ output = Tempfile.new(["decidim_apartment_import", ".sql"])
45
+ output_path = output.path
46
+ skipping = false
47
+ lines_out = 0
48
+
49
+ File.foreach(@dump_file) do |line|
50
+ if copy_strip?(line)
51
+ skipping = true
52
+ next
53
+ end
54
+
55
+ if skipping
56
+ skipping = false if line.strip == "\\."
57
+ next
58
+ end
59
+
60
+ next if line.match?(REJECT_LINE)
61
+
62
+ transformed = rewrite_line(line)
63
+ output.write(transformed)
64
+ lines_out += 1
65
+ end
66
+
67
+ output.flush
68
+ SchemaAuditor.audit_transformed!(output_path, target_schema: @target_schema)
69
+
70
+ { path: output_path, lines: lines_out }
71
+ end
72
+
73
+ private
74
+
75
+ def copy_strip?(line)
76
+ line.match?(STRIP_PUBLIC_TABLES)
77
+ end
78
+
79
+ def rewrite_line(line)
80
+ line
81
+ .gsub(@source_schema, @target_schema)
82
+ .gsub(@source_schema.upcase, @target_schema)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Import
6
+ class ImportError < StandardError; end
7
+ end
8
+ end
9
+ end