redmine_plugins_helper 0.10.0 → 0.12.1

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: 747dc2f64ca0a3fdcbec5d50be57450f63a7c725654ee6dabee7e856c39ebac3
4
- data.tar.gz: c7a1f9c07f653fec428625f5ab0486a313751b8940a322c98a6e14d34bcbafd8
3
+ metadata.gz: 0f7d3b510c4a42d1c78bf2d850648ee579a8a3c24adff6a8ba6ba84b91bce6be
4
+ data.tar.gz: bf26964cb5d67e16accf2631477d1f89373c79781d23bff661a85762d3f99a50
5
5
  SHA512:
6
- metadata.gz: 8dde27c9f58ac5cdf5cc87c231b374f9d68896ea9a85c3fdf370c73ccfbd37b3deaf8e0575622c941b10f3c6d32372c632329b1e0d784d8bdc82ec639edce174
7
- data.tar.gz: 74ce1473a20c49d9910de3496a6eb07a92c5a9f26f0c4d6da6d4c9ada1fb511040079473898ea519b4a8574b946ace508c0dd49fa02c7d3af74df3a08b8f031a
6
+ metadata.gz: cf2e3febe40f0d8246a905e0a1583d8cf68354309aa949dde4d6fdeced11a5b96dbdd65a85c9ede5ed1d9b381061f53455a2d4e116c91867fffa8528796311f7
7
+ data.tar.gz: c153f16bb325b0ab9356d110662727ab4e8f84e8d919b6acd4f53cf74d097ec3d7b57e12e159583dbe05681e42f1370f52a7605984d2cd694aafc9df5d84dec7
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Reference: https://stackoverflow.com/questions/62570662/rails-4-ruby-2-7-1-schema-rb-shows-could-not-dump-table-because-of-following-f
4
+
5
+ return unless RUBY_VERSION >= '2.7' && ::Rails.version < '5'
6
+
7
+ module ActiveRecord
8
+ module ConnectionAdapters
9
+ module ColumnDumper
10
+ def prepare_column_options(column, types) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
11
+ spec = {}
12
+ spec[:name] = column.name.inspect
13
+ spec[:type] = column.type.to_s
14
+ spec[:null] = 'false' unless column.null
15
+
16
+ limit = column.limit || types[column.type][:limit]
17
+ spec[:limit] = limit.inspect if limit
18
+ spec[:precision] = column.precision.inspect if column.precision
19
+ spec[:scale] = column.scale.inspect if column.scale
20
+
21
+ default = schema_default(column).dup if column.has_default?
22
+ spec[:default] = default unless default.nil?
23
+
24
+ spec
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/enumerator.rb ADDED
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ # Required by "sass/utils", it fails if RUBY_VERSION == 2.7.6.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RedminePluginsHelper
2
4
  module Available
3
5
  class << self
@@ -9,12 +11,18 @@ module RedminePluginsHelper
9
11
  true
10
12
  end
11
13
 
12
- def model?(model_class)
13
- table?(model_class.table_name)
14
+ def database_schema?
15
+ database? && ::RedminePluginsHelper::Migration.from_code.all?(&:applied?)
16
+ end
17
+
18
+ def model?(*model_classes)
19
+ table?(*model_classes.map(&:table_name))
14
20
  end
15
21
 
16
- def table?(table_name)
17
- database? && ::ActiveRecord::Base.connection.table_exists?(table_name)
22
+ def table?(*table_names)
23
+ return false unless database?
24
+
25
+ table_names.all? { |table_name| ::ActiveRecord::Base.connection.table_exists?(table_name) }
18
26
  end
19
27
 
20
28
  def settings?
@@ -2,13 +2,7 @@
2
2
 
3
3
  module RedminePluginsHelper
4
4
  class FixMigrations
5
- def initialize
6
- run
7
- end
8
-
9
- private
10
-
11
- def run
5
+ def perform
12
6
  database_plugins_versions.each do |dbv|
13
7
  check_database_version(dbv)
14
8
  end
@@ -16,6 +10,8 @@ module RedminePluginsHelper
16
10
  Rails.logger.info("Local versions found: #{local_versions.count}")
17
11
  end
18
12
 
13
+ private
14
+
19
15
  def check_database_version(dbv)
20
16
  lv = local_version(dbv[:timestamp])
21
17
  return unless lv && lv.count == 1 && dbv[:plugin] != lv.first[:plugin]
@@ -43,8 +39,8 @@ module RedminePluginsHelper
43
39
 
44
40
  def move_plugin_version(source_version, target_version)
45
41
  Rails.logger.info("Moving #{source_version} to plugin \"#{target_version}\"")
46
- ::ActiveRecord::SchemaMigration.find_by(version: source_version)
47
- .update!(version: target_version)
42
+ ::ActiveRecord::SchemaMigration.where(version: source_version)
43
+ .update_all(version: target_version) # rubocop:disable Rails/SkipsModelValidations
48
44
  end
49
45
 
50
46
  def local_version(timestamp)
@@ -54,7 +50,7 @@ module RedminePluginsHelper
54
50
  end
55
51
 
56
52
  def local_versions
57
- @local_versions = begin
53
+ @local_versions ||= begin
58
54
  r = {}
59
55
  Redmine::Plugin.registered_plugins.each_value do |p|
60
56
  p.migrations.each do |m|
@@ -67,24 +63,12 @@ module RedminePluginsHelper
67
63
  end
68
64
 
69
65
  def database_plugins_versions
70
- @database_plugins_versions = begin
71
- r = []
72
- ::RedminePluginsHelper::Migrations.db_all_versions.each do |v|
73
- pv = parse_plugin_version(v)
74
- next unless pv
75
-
76
- r << pv
77
- end
78
- r
66
+ @database_plugins_versions ||= ::RedminePluginsHelper::Migration
67
+ .from_database.select(&:plugin?).map do |m|
68
+ { plugin: m.plugin_id, timestamp: m.version, version: m.database_version }
79
69
  end
80
70
  end
81
71
 
82
- def parse_plugin_version(version)
83
- h = ::RedminePluginsHelper::Migrations.parse_plugin_version(version)
84
- h[:version] = version if h.is_a?(Hash)
85
- h
86
- end
87
-
88
72
  def plugin_version(plugin_id, timestamp)
89
73
  "#{timestamp}-#{plugin_id}"
90
74
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedminePluginsHelper
4
+ class Migration
5
+ module Code
6
+ common_concern
7
+
8
+ module ClassMethods
9
+ # @return [Enumerable<RedminePluginsHelper::Migration>]
10
+ def from_code
11
+ from_core_code + from_plugins_code
12
+ end
13
+
14
+ # @return [Enumerable<RedminePluginsHelper::Migration>]
15
+ def from_core_code
16
+ ::Rails.application.paths['db/migrate'].flat_map do |path|
17
+ from_path_code(path)
18
+ end
19
+ end
20
+
21
+ # @return [Enumerable<RedminePluginsHelper::Migration>]
22
+ def from_path_code(path)
23
+ ::Dir["#{path}/*.rb"].map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
24
+ .map { |version| new(PLUGIN_ID_CORE_VALUE, version) }
25
+ end
26
+
27
+ # @return [Enumerable<RedminePluginsHelper::Migration>]
28
+ def from_plugins_code
29
+ ::Redmine::Plugin.registered_plugins.values.flat_map do |plugin|
30
+ plugin.migrations.map { |version| new(plugin.id, version) }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedminePluginsHelper
4
+ class Migration
5
+ module Database
6
+ common_concern
7
+
8
+ DATABASE_CORE_VERSION_PARSER = /^(\d+)$/.to_parser { |m| [PLUGIN_ID_CORE_VALUE, m[1]] }
9
+ DATABASE_PLUGIN_VERSION_PARSER = /^(\d+)\-(\S+)$/.to_parser { |m| [m[2], m[1]] }
10
+ DATABASE_VERSION_PARSERS = [DATABASE_PLUGIN_VERSION_PARSER, DATABASE_CORE_VERSION_PARSER]
11
+ .freeze
12
+
13
+ module ClassMethods
14
+ # @return [Enumerable<RedminePluginsHelper::Migration>]
15
+ def from_database
16
+ ::ActiveRecord::SchemaMigration.create_table
17
+ ::ActiveRecord::SchemaMigration.all.pluck(:version).map do |version|
18
+ from_database_version(version)
19
+ end
20
+ end
21
+
22
+ # @return [RedminePluginsHelper::Migration]
23
+ def from_database_version(version)
24
+ DATABASE_VERSION_PARSERS
25
+ .lazy
26
+ .map { |parser| parser.parse(version).if_present { |args| new(*args) } }
27
+ .find(&:present?) || raise("None parser parsed \"#{version}\"")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedminePluginsHelper
4
+ class Migration
5
+ require_sub __FILE__, include_modules: true
6
+
7
+ PLUGIN_ID_CORE_VALUE = :_core_
8
+
9
+ common_constructor :plugin_id, :version do
10
+ self.plugin_id = plugin_id.to_sym
11
+ self.version = version.to_i
12
+ end
13
+
14
+ # @return [Boolean]
15
+ def applied?
16
+ ::ActiveRecord::SchemaMigration.create_table
17
+ ::ActiveRecord::SchemaMigration.where(version: database_version).any?
18
+ end
19
+
20
+ # @return [String]
21
+ def database_version
22
+ core? ? version.to_s : "#{version}-#{plugin_id}"
23
+ end
24
+
25
+ # @return [Boolean]
26
+ def core?
27
+ plugin_id == PLUGIN_ID_CORE_VALUE
28
+ end
29
+
30
+ # @return [Boolean]
31
+ def plugin?
32
+ !core?
33
+ end
34
+ end
35
+ end
@@ -5,38 +5,21 @@ module RedminePluginsHelper
5
5
  class << self
6
6
  def local_versions
7
7
  r = {}
8
- Redmine::Plugin.registered_plugins.each_value do |p|
9
- p.migrations.each do |ts|
10
- r[p.id] ||= []
11
- r[p.id] << ts
12
- end
8
+ ::RedminePluginsHelper::Migration.from_code.select(&:plugin?).each do |migration|
9
+ r[migration.plugin_id] ||= []
10
+ r[migration.plugin_id] << migration.version
13
11
  end
14
12
  r
15
13
  end
16
14
 
17
15
  def db_versions
18
16
  r = {}
19
- db_all_versions.each do |v|
20
- pv = parse_plugin_version(v)
21
- next unless pv
22
-
23
- r[pv[:plugin]] ||= []
24
- r[pv[:plugin]] << pv[:timestamp]
17
+ ::RedminePluginsHelper::Migration.from_database.select(&:plugin?).each do |migration|
18
+ r[migration.plugin_id] ||= []
19
+ r[migration.plugin_id] << migration.version
25
20
  end
26
21
  r
27
22
  end
28
-
29
- def db_all_versions
30
- ::ActiveRecord::SchemaMigration.create_table
31
- ::ActiveRecord::SchemaMigration.all.pluck(:version)
32
- end
33
-
34
- def parse_plugin_version(version)
35
- m = version.match(/^(\d+)\-(\S+)$/)
36
- return nil unless m
37
-
38
- { plugin: m[2].to_sym, timestamp: m[1].to_i }
39
- end
40
23
  end
41
24
  end
42
25
  end
@@ -3,5 +3,5 @@
3
3
  module RedminePluginsHelper
4
4
  AUTHOR = 'Eduardo Henrique Bogoni'
5
5
  SUMMARY = 'Helper for Redmine plugins'
6
- VERSION = '0.10.0'
6
+ VERSION = '0.12.1'
7
7
  end
@@ -42,7 +42,7 @@ namespace :redmine do
42
42
  namespace :migrate do
43
43
  desc 'Fix migrations moved from a plugin to another'
44
44
  task fix: :environment do
45
- RedminePluginsHelper::FixMigrations.new
45
+ ::RedminePluginsHelper::FixMigrations.new.perform
46
46
  end
47
47
 
48
48
  desc 'Show migrations status of all plugins'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redmine_plugins_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
- - 0.10.0
7
+ - 0.12.1
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-06 00:00:00.000000000 Z
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bigdecimal
@@ -110,12 +110,17 @@ files:
110
110
  - app/assets/stylesheets/plugins_autoload.scss.erb
111
111
  - config/initializers/assets.rb
112
112
  - config/initializers/backport_pg_10_support_to_rails_4.rb
113
+ - config/initializers/ruby_2_7_rails_4_column_dumper.rb
113
114
  - init.rb
115
+ - lib/enumerator.rb
114
116
  - lib/redmine_plugins_helper.rb
115
117
  - lib/redmine_plugins_helper/available.rb
116
118
  - lib/redmine_plugins_helper/fix_migrations.rb
117
119
  - lib/redmine_plugins_helper/hooks/add_assets.rb
118
120
  - lib/redmine_plugins_helper/migrate.rb
121
+ - lib/redmine_plugins_helper/migration.rb
122
+ - lib/redmine_plugins_helper/migration/code.rb
123
+ - lib/redmine_plugins_helper/migration/database.rb
119
124
  - lib/redmine_plugins_helper/migrations.rb
120
125
  - lib/redmine_plugins_helper/patches/redmine/plugin_migration_context.rb
121
126
  - lib/redmine_plugins_helper/patches/redmine/plugin_patch.rb