decidim-cdtb 0.6.3 → 0.6.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87cfa078ca5fb059a580e029b4319a64d192a0b02e3cfd1136c2dd5d8d30a8e2
4
- data.tar.gz: 2859c2bb9f9b079a672fe10600347bee48652daf70ec6810d412741765f00fdb
3
+ metadata.gz: cb170af8cd1724e42f2651540d6a91f19d4706075bf8cd65d7db7c5b1ac93267
4
+ data.tar.gz: d1b78848891c940476824c5812a5d0dfd5e36e93efa9f615ab8a8835d58919de
5
5
  SHA512:
6
- metadata.gz: 48af433b8ce336f0f1f78a293a0ab129543e556b574f749e8b561e149f3fda247a59a4126cd6e512e35e127e17be921f22a0ad8abc78ad687a52419e4ff74cdd
7
- data.tar.gz: df959a73420d0db727c237ed5c37edbe69e79f224a2bd7bf71f335dafc02d79b3a491718763ab3b2d6ac13716825c5f3cd475e55da2f35268e821c59e9c00ffa
6
+ metadata.gz: 61af8001a2df666fdeb281be62817dd7d4e0727804a243f6dd00fc6b0a360c3225c5edeef2a6e06e6141be5454d05741806d7e61c9f87daa8dd7935734570426
7
+ data.tar.gz: e2ae5dd5539898e812f9a40853d5463e72db5a7ef224e42f4ceedb4b7234b798b30b2b7f1798af65a7e7df9d35aad8bbd327848c34c038a7d1ae50abfa8227df
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.6.4] - 2026-11-17 (minor)
2
+
3
+ - Add `cdtb:upgrades:hero_content_block_settings:export` and `cdtb:upgrades:hero_content_block_settings:restore` tasks to back up hero content block settings before upgrading to Decidim v0.31 and restore the ones lost by the CTA migration.
4
+
1
5
  ## [0.6.3] - 2026-10-16 (minor)
2
6
 
3
7
  - Implement RackAttack feature to block abuse reported IPs.
data/Gemfile.lock CHANGED
@@ -141,7 +141,7 @@ GIT
141
141
  PATH
142
142
  remote: .
143
143
  specs:
144
- decidim-cdtb (0.6.3)
144
+ decidim-cdtb (0.6.4)
145
145
  decidim (>= 0.29.0)
146
146
  rails (>= 6)
147
147
  ruby-progressbar
data/README.md CHANGED
@@ -303,6 +303,24 @@ See the [Installation](#installation) chapter to install a GitHub Action on your
303
303
 
304
304
  TO-DO also check that all migrations have been executed and the schema.rb does not change
305
305
 
306
+ #### Back up and restore hero content block settings
307
+
308
+ When upgrading to Decidim v0.31, the `MoveCtaToHeroContentBlock` migration overwrites the settings of the hero content blocks with the CTA values, losing every other setting.
309
+
310
+ Before upgrading (before running the migrations), export the raw settings of every hero content block to a new JSON file (the export will not overwrite an existing file):
311
+
312
+ ```
313
+ bin/rake 'cdtb:upgrades:hero_content_block_settings:export[/secure/path/hero-settings.json]'
314
+ ```
315
+
316
+ Keep the file securely outside the application repository. After upgrading, restore the missing keys with:
317
+
318
+ ```
319
+ bin/rake 'cdtb:upgrades:hero_content_block_settings:restore[/secure/path/hero-settings.json]'
320
+ ```
321
+
322
+ The restore uses the original content block and organization IDs and scope, so run it against the same database. It preserves any current setting, including the migrated CTA values, and only adds keys absent from the current settings. Each block is restored in its own transaction; a missing or mismatched block is reported and skipped, and the restore continues with the rest. An invalid backup aborts the restore. A backup from before the affected migration is required to recover settings that were lost.
323
+
306
324
  ## Development
307
325
 
308
326
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -13,3 +13,4 @@ require "decidim/cdtb/spam/spam_users_detector"
13
13
  require "decidim/cdtb/storage/local_sharding"
14
14
  require "decidim/cdtb/storage/set_local_on_blobs"
15
15
  require "decidim/cdtb/upgrades/validate_migrations_task"
16
+ require "decidim/cdtb/upgrades/hero_content_block_settings"
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Decidim
6
+ module Cdtb
7
+ module Upgrades
8
+ # Exports and restores the raw settings of every hero content block.
9
+ #
10
+ # The `MoveCtaToHeroContentBlock` migration from Decidim v0.31 overwrites the hero
11
+ # content block settings with only the CTA values, losing every other setting.
12
+ # Export the settings before migrating and restore them afterwards.
13
+ module HeroContentBlockSettings
14
+ VERSION = 1
15
+
16
+ # Writes a versioned JSON file with the settings of all hero content blocks.
17
+ # Refuses to overwrite an existing file and makes it readable only by the owner.
18
+ #
19
+ # Returns the number of exported content blocks.
20
+ def self.export(path)
21
+ records = Decidim::ContentBlock.where(manifest_name: "hero").order(:id)
22
+ .pluck(:id, :decidim_organization_id, :scope_name, :settings)
23
+ .map do |id, organization_id, scope_name, settings|
24
+ { id:, organization_id:, scope_name:, settings: }
25
+ end
26
+
27
+ File.open(path, "wx", 0o600) do |file|
28
+ file.write(JSON.pretty_generate(version: VERSION, content_blocks: records))
29
+ file.write("\n")
30
+ end
31
+ records.size
32
+ end
33
+
34
+ # Restores the keys missing from the current settings of each hero content block.
35
+ # Current values (including the migrated CTA values) are always preserved.
36
+ # Each block is restored in its own transaction: a failing block is reported and
37
+ # the restore continues with the rest. An invalid backup aborts the whole restore.
38
+ #
39
+ # Returns the number of successfully processed content blocks.
40
+ def self.restore(path)
41
+ backup = JSON.parse(File.read(path))
42
+ records = validate_backup!(backup)
43
+
44
+ records.count do |record|
45
+ Decidim::ContentBlock.transaction { restore_record!(record) }
46
+ true
47
+ rescue StandardError => e
48
+ puts "Block [#{record["id"]}] failed due to: #{e.message} - #{e.backtrace&.first(5)&.join("\n")}"
49
+ false
50
+ end
51
+ end
52
+
53
+ def self.restore_record!(record)
54
+ block = Decidim::ContentBlock.lock.find_by!(
55
+ id: record.fetch("id"),
56
+ decidim_organization_id: record.fetch("organization_id"),
57
+ scope_name: record.fetch("scope_name"),
58
+ manifest_name: "hero"
59
+ )
60
+ current = block[:settings]
61
+ raise ArgumentError, "Invalid current settings for hero block #{block.id}" unless current.nil? || current.is_a?(Hash)
62
+
63
+ current ||= {}
64
+ restored = (record.fetch("settings") || {}).merge(current)
65
+ return if restored == current
66
+
67
+ block.update_columns(settings: restored, updated_at: Time.current)
68
+ end
69
+
70
+ def self.validate_backup!(backup)
71
+ raise ArgumentError, "Invalid hero settings backup format or version" unless valid_format?(backup)
72
+
73
+ records = backup.fetch("content_blocks")
74
+ raise ArgumentError, "Invalid hero settings backup record" unless records.all? { |record| valid_record?(record) }
75
+
76
+ ids = records.map { |record| record.fetch("id") }
77
+ raise ArgumentError, "Duplicate hero content block IDs in backup" unless ids.uniq.size == ids.size
78
+
79
+ records
80
+ end
81
+
82
+ def self.valid_format?(backup)
83
+ backup.is_a?(Hash) && backup["version"] == VERSION && backup["content_blocks"].is_a?(Array)
84
+ end
85
+
86
+ def self.valid_record?(record)
87
+ record.is_a?(Hash) &&
88
+ positive_integer?(record["id"]) &&
89
+ positive_integer?(record["organization_id"]) &&
90
+ record["scope_name"].is_a?(String) && !record["scope_name"].empty? &&
91
+ valid_settings?(record)
92
+ end
93
+
94
+ def self.valid_settings?(record)
95
+ record.key?("settings") && (record["settings"].nil? || record["settings"].is_a?(Hash))
96
+ end
97
+
98
+ def self.positive_integer?(value)
99
+ value.is_a?(Integer) && value.positive?
100
+ end
101
+ private_class_method :restore_record!, :validate_backup!, :valid_format?, :valid_record?, :valid_settings?, :positive_integer?
102
+ end
103
+ end
104
+ end
105
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Decidim
4
4
  module Cdtb
5
- VERSION = "0.6.3"
5
+ VERSION = "0.6.4"
6
6
  DECIDIM_MIN_VERSION = ">= 0.29.0"
7
7
  end
8
8
  end
@@ -16,5 +16,29 @@ namespace :cdtb do
16
16
  task.execute!
17
17
  raise("There are pending migrations") if task.pending_migrations?
18
18
  end
19
+
20
+ namespace :hero_content_block_settings do
21
+ require "decidim/cdtb/upgrades/hero_content_block_settings"
22
+
23
+ desc <<~EODESC
24
+ Export all hero content block settings to a new JSON file. Run it before upgrading to Decidim v0.31.
25
+ EODESC
26
+ task :export, [:path] => :environment do |_task, args|
27
+ raise ArgumentError, "Provide a JSON file path" if args.path.blank?
28
+
29
+ count = Decidim::Cdtb::Upgrades::HeroContentBlockSettings.export(args.path)
30
+ puts "Exported settings for #{count} hero content blocks to #{args.path}"
31
+ end
32
+
33
+ desc <<~EODESC
34
+ Restore missing hero content block settings from a JSON backup generated with the export task.
35
+ EODESC
36
+ task :restore, [:path] => :environment do |_task, args|
37
+ raise ArgumentError, "Provide a JSON file path" if args.path.blank?
38
+
39
+ count = Decidim::Cdtb::Upgrades::HeroContentBlockSettings.restore(args.path)
40
+ puts "Successfully processed #{count} hero content blocks from #{args.path}"
41
+ end
42
+ end
19
43
  end
20
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decidim-cdtb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Valls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-21 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: decidim
@@ -105,6 +105,7 @@ files:
105
105
  - lib/decidim/cdtb/task.rb
106
106
  - lib/decidim/cdtb/tasks.rb
107
107
  - lib/decidim/cdtb/tasks_utils.rb
108
+ - lib/decidim/cdtb/upgrades/hero_content_block_settings.rb
108
109
  - lib/decidim/cdtb/upgrades/install_gem_migrations_step.rb
109
110
  - lib/decidim/cdtb/upgrades/upgrade_modules_task.rb
110
111
  - lib/decidim/cdtb/upgrades/validate_migrations_task.rb