Class: ThemeSettingsMigrationsRunner

Inherits:
Object
  • Object
show all
Defined in:
app/services/theme_settings_migrations_runner.rb

Defined Under Namespace

Modules: Helpers Classes: Migration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme, limit: 100, timeout: 100, memory: 2.megabytes) ⇒ ThemeSettingsMigrationsRunner

Returns a new instance of ThemeSettingsMigrationsRunner.



68
69
70
71
72
73
# File 'app/services/theme_settings_migrations_runner.rb', line 68

def initialize(theme, limit: 100, timeout: 100, memory: 2.megabytes)
  @theme = theme
  @limit = limit
  @timeout = timeout
  @memory = memory
end

Class Method Details

.loader_js_lib_contentObject



58
59
60
61
62
63
64
65
66
# File 'app/services/theme_settings_migrations_runner.rb', line 58

def self.loader_js_lib_content
  @loader_js_lib_content ||=
    File.read(
      File.join(
        Rails.root,
        "app/assets/javascripts/discourse/node_modules/loader.js/dist/loader/loader.js",
      ),
    )
end

Instance Method Details

#run(fields: nil, raise_error_on_out_of_sequence: true) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/services/theme_settings_migrations_runner.rb', line 75

def run(fields: nil, raise_error_on_out_of_sequence: true)
  fields ||= lookup_pending_migrations_fields

  count = fields.count
  return [] if count == 0

  raise_error("themes.import_error.migrations.too_many_pending_migrations") if count > @limit

  migrations = convert_fields_to_migrations(fields)
  migrations.sort_by!(&:version)

  current_migration_version =
    @theme.theme_settings_migrations.order(version: :desc).pick(:version)

  current_migration_version ||= -Float::INFINITY

  current_settings = lookup_overriden_settings

  migrations.map do |migration|
    if migration.version <= current_migration_version && raise_error_on_out_of_sequence
      raise_error(
        "themes.import_error.migrations.out_of_sequence",
        name: migration.original_name,
        current: current_migration_version,
      )
    end

    migrated_settings = execute(migration, current_settings)

    results = {
      version: migration.version,
      name: migration.name,
      original_name: migration.original_name,
      theme_field_id: migration.theme_field_id,
      settings_before: current_settings,
      settings_after: migrated_settings,
    }
    current_settings = migrated_settings
    current_migration_version = migration.version
    results
  rescue DiscourseJsProcessor::TranspileError => error
    raise_error(
      "themes.import_error.migrations.syntax_error",
      name: migration.original_name,
      error: error.message,
    )
  rescue MiniRacer::V8OutOfMemoryError
    raise_error(
      "themes.import_error.migrations.exceeded_memory_limit",
      name: migration.original_name,
    )
  rescue MiniRacer::ScriptTerminatedError
    raise_error("themes.import_error.migrations.timed_out", name: migration.original_name)
  rescue MiniRacer::RuntimeError => error
    message = error.message
    if message.include?("no_exported_migration_function")
      raise_error(
        "themes.import_error.migrations.no_exported_function",
        name: migration.original_name,
      )
    elsif message.include?("default_export_is_not_a_function")
      raise_error(
        "themes.import_error.migrations.default_export_not_a_function",
        name: migration.original_name,
      )
    elsif message.include?("migration_function_no_returned_value")
      raise_error(
        "themes.import_error.migrations.no_returned_value",
        name: migration.original_name,
      )
    elsif message.include?("migration_function_wrong_return_type")
      raise_error(
        "themes.import_error.migrations.wrong_return_type",
        name: migration.original_name,
      )
    else
      raise_error(
        "themes.import_error.migrations.runtime_error",
        name: migration.original_name,
        error: message,
      )
    end
  end
end