Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

extract create service associations code

Ivan Kuchincf0eb4c99119
diff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rb
index c7e266f10d0..c2f521e1fee 100644
--- a/app/services/journals/create_service.rb
+++ b/app/services/journals/create_service.rb
@@ -41,11 +41,14 @@
# rubocop:disable Rails/SquishedSQLHeredocs
module Journals
class CreateService
- attr_accessor :journable, :user
+ include Helpers
+
+ attr_reader :journable, :user, :associations
def initialize(journable, user)
- self.user = user
- self.journable = journable
+ @user = user
+ @journable = journable
+ @associations = Association.for(journable)
end
def call(notes: "", cause: CauseOfChange::NoCause.new)
@@ -62,8 +65,6 @@ module Journals
private
- def journable_id = journable.id
-
# If the journalizing happens within the configured aggregation time, is carried out by the same user, has an
# identical cause and only the predecessor or the journal to be created has notes, the changes are aggregated.
# Instead of removing the predecessor, return it here so that it can be stripped in the journal creating
@@ -225,93 +226,26 @@ module Journals
end
def relation_modifications_sql(predecessor)
- relations = []
-
- for_supported_associations do |association|
- relations << association_modifications_sql(association, predecessor)
- end
-
- relations
- end
-
- def for_supported_associations
- associations = {
- agenda_items: :agenda_itemable,
- attachable?: :attachable,
- customizable?: :customizable,
- file_links: :storable,
- life_cycle_steps: :project_life_cycle_step
- }
-
- associations.each do |is_associated, association|
- if journable.respond_to?(is_associated)
- yield association
- end
+ associations.map do |association|
+ association_modifications_sql(association, predecessor)
end
end
def association_modifications_sql(association, predecessor)
<<~SQL
- cleanup_predecessor_#{association} AS (
- #{send(:"cleanup_predecessor_#{association}", predecessor)}
- ), insert_#{association} AS (
- #{send(:"insert_#{association}_sql")}
+ cleanup_predecessor_#{association.name} AS (
+ #{association.cleanup_predecessor(predecessor)}
+ ), insert_#{association.name} AS (
+ #{association.insert_sql}
)
SQL
end
def cleanup_predecessor_data(predecessor)
- cleanup_predecessor(predecessor,
- data_table_name,
- :id,
- :data_id)
- end
-
- def cleanup_predecessor_attachable(predecessor)
- cleanup_predecessor(predecessor,
- "attachable_journals",
- :journal_id,
- :id)
- end
-
- def cleanup_predecessor_customizable(predecessor)
- cleanup_predecessor(predecessor,
- "customizable_journals",
- :journal_id,
- :id)
- end
-
- def cleanup_predecessor_project_life_cycle_step(predecessor)
- cleanup_predecessor(predecessor,
- "project_life_cycle_step_journals",
- :journal_id,
- :id)
- end
-
- def cleanup_predecessor_storable(predecessor)
- cleanup_predecessor(predecessor,
- "storages_file_links_journals",
- :journal_id,
- :id)
- end
-
- def cleanup_predecessor_agenda_itemable(predecessor)
- cleanup_predecessor(predecessor,
- "meeting_agenda_item_journals",
- :journal_id,
- :id)
- end
-
- def cleanup_predecessor(predecessor, table_name, column, referenced_id)
- return "SELECT 1" unless predecessor
-
- sanitize(<<~SQL, column => predecessor.send(referenced_id))
- DELETE
- FROM
- #{table_name}
- WHERE
- #{column} = :#{column}
- SQL
+ cleanup_predecessor_for(predecessor,
+ data_table_name,
+ :id,
+ :data_id)
end
def update_or_insert_journal_sql(predecessor, notes, cause)
@@ -408,132 +342,6 @@ module Journals
SQL
end
- def journable_class_name
- journable.class.base_class.name
- end
-
- def insert_attachable_sql
- sanitize(<<~SQL, journable_id:, journable_class_name:)
- INSERT INTO
- attachable_journals (
- journal_id,
- attachment_id,
- filename
- )
- SELECT
- #{id_from_inserted_journal_sql},
- attachments.id,
- attachments.file
- FROM attachments
- WHERE
- #{only_if_created_sql}
- AND attachments.container_id = :journable_id
- AND attachments.container_type = :journable_class_name
- SQL
- end
-
- def insert_customizable_sql
- sanitize(<<~SQL, journable_id:, journable_class_name:)
- INSERT INTO
- customizable_journals (
- journal_id,
- custom_field_id,
- value
- )
- SELECT
- #{id_from_inserted_journal_sql},
- custom_values.custom_field_id,
- #{normalize_newlines_sql('custom_values.value')}
- FROM custom_values
- WHERE
- #{only_if_created_sql}
- AND custom_values.customized_id = :journable_id
- AND custom_values.customized_type = :journable_class_name
- AND custom_values.value IS NOT NULL
- AND custom_values.value != ''
- SQL
- end
-
- def insert_project_life_cycle_step_sql
- sanitize(<<~SQL, journable_id:)
- INSERT INTO
- project_life_cycle_step_journals (
- journal_id,
- life_cycle_step_id,
- start_date,
- end_date,
- active
- )
- SELECT
- #{id_from_inserted_journal_sql},
- project_life_cycle_steps.id,
- project_life_cycle_steps.start_date,
- project_life_cycle_steps.end_date,
- project_life_cycle_steps.active
- FROM project_life_cycle_steps
- WHERE
- #{only_if_created_sql}
- AND project_life_cycle_steps.project_id = :journable_id
- SQL
- end
-
- def insert_storable_sql
- sanitize(<<~SQL, journable_id:, journable_class_name:)
- INSERT INTO
- storages_file_links_journals (
- journal_id,
- file_link_id,
- link_name,
- storage_name
- )
- SELECT
- #{id_from_inserted_journal_sql},
- file_links.id,
- file_links.origin_name,
- storages.name
- FROM file_links left join storages ON file_links.storage_id = storages.id
- WHERE
- #{only_if_created_sql}
- AND file_links.container_id = :journable_id
- AND file_links.container_type = :journable_class_name
- SQL
- end
-
- def insert_agenda_itemable_sql
- sanitize(<<~SQL, journable_id:)
- INSERT INTO
- meeting_agenda_item_journals (
- journal_id,
- agenda_item_id,
- author_id,
- title,
- notes,
- position,
- duration_in_minutes,
- start_time,
- end_time,
- work_package_id,
- item_type
- )
- SELECT
- #{id_from_inserted_journal_sql},
- agenda_items.id,
- agenda_items.author_id,
- agenda_items.title,
- agenda_items.notes,
- agenda_items.position,
- agenda_items.duration_in_minutes,
- agenda_items.start_time,
- agenda_items.end_time,
- agenda_items.work_package_id,
- agenda_items.item_type
- FROM meeting_agenda_items agenda_items
- WHERE
- #{only_if_created_sql}
- AND agenda_items.meeting_id = :journable_id
- SQL
- end
-
# Updates the updated_at timestamp of the journable.
# That is only carried out if the journable doesn't already have a newer timestamp than the most recent journal or
# hasn't been updated by the `#save` call after which this service runs.
@@ -623,143 +431,22 @@ module Journals
SELECT
*
FROM
- (#{data_changes_sql}) data_changes
+ (#{changes_data_sql}) data_changes
SQL
- for_supported_associations do |association|
+ associations.each do |association|
sql += <<~SQL
FULL JOIN
- (#{send(:"#{association}_changes_sql")}) #{association}_changes
+ (#{association.changes_sql}) #{association.name}_changes
ON
- #{association}_changes.journable_id = data_changes.journable_id
+ #{association.name}_changes.journable_id = data_changes.journable_id
SQL
end
sql
end
- def attachable_changes_sql
- sanitize(<<~SQL, journable_id:, container_type: journable_class_name)
- SELECT
- max_journals.journable_id
- FROM
- max_journals
- LEFT OUTER JOIN
- attachable_journals
- ON
- attachable_journals.journal_id = max_journals.id
- FULL JOIN
- (SELECT *
- FROM attachments
- WHERE attachments.container_id = :journable_id AND attachments.container_type = :container_type) attachments
- ON
- attachments.id = attachable_journals.attachment_id
- WHERE
- (attachments.id IS NULL AND attachable_journals.attachment_id IS NOT NULL)
- OR (attachable_journals.attachment_id IS NULL AND attachments.id IS NOT NULL)
- SQL
- end
-
- def customizable_changes_sql
- sanitize(<<~SQL, journable_id:, customized_type: journable_class_name)
- SELECT
- max_journals.journable_id
- FROM
- max_journals
- LEFT OUTER JOIN
- customizable_journals
- ON
- customizable_journals.journal_id = max_journals.id
- FULL JOIN
- (SELECT *
- FROM custom_values
- WHERE custom_values.customized_id = :journable_id AND custom_values.customized_type = :customized_type) custom_values
- ON
- custom_values.custom_field_id = customizable_journals.custom_field_id
- WHERE
- (custom_values.value IS NULL AND customizable_journals.value IS NOT NULL)
- OR (customizable_journals.value IS NULL AND custom_values.value IS NOT NULL AND custom_values.value != '')
- OR (#{normalize_newlines_sql('customizable_journals.value')} !=
- #{normalize_newlines_sql('custom_values.value')})
- SQL
- end
-
- def project_life_cycle_step_changes_sql
- sanitize(<<~SQL, journable_id:)
- SELECT
- max_journals.journable_id
- FROM
- max_journals
- LEFT OUTER JOIN
- project_life_cycle_step_journals
- ON
- project_life_cycle_step_journals.journal_id = max_journals.id
- FULL JOIN
- (SELECT *
- FROM project_life_cycle_steps
- WHERE project_life_cycle_steps.project_id = :journable_id) life_cycle_steps
- ON
- life_cycle_steps.id = project_life_cycle_step_journals.life_cycle_step_id
- WHERE
- life_cycle_steps.start_date IS DISTINCT FROM project_life_cycle_step_journals.start_date
- OR life_cycle_steps.end_date IS DISTINCT FROM project_life_cycle_step_journals.end_date
- OR life_cycle_steps.active IS DISTINCT FROM project_life_cycle_step_journals.active
- SQL
- end
-
- def storable_changes_sql
- sanitize(<<~SQL, journable_id:, container_type: journable_class_name)
- SELECT
- max_journals.journable_id
- FROM
- max_journals
- LEFT OUTER JOIN
- storages_file_links_journals
- ON
- storages_file_links_journals.journal_id = max_journals.id
- FULL JOIN
- (SELECT *
- FROM file_links
- WHERE file_links.container_id = :journable_id AND file_links.container_type = :container_type) file_links
- ON
- file_links.id = storages_file_links_journals.file_link_id
- WHERE
- (file_links.id IS NULL AND storages_file_links_journals.file_link_id IS NOT NULL)
- OR (storages_file_links_journals.file_link_id IS NULL AND file_links.id IS NOT NULL)
- SQL
- end
-
- def agenda_itemable_changes_sql
- sanitize(<<~SQL, journable_id:)
- SELECT
- max_journals.journable_id
- FROM
- max_journals
- LEFT OUTER JOIN
- meeting_agenda_item_journals
- ON
- meeting_agenda_item_journals.journal_id = max_journals.id
- FULL JOIN
- (SELECT *
- FROM meeting_agenda_items
- WHERE meeting_agenda_items.meeting_id = :journable_id) agenda_items
- ON
- agenda_items.id = meeting_agenda_item_journals.agenda_item_id
- WHERE
- (agenda_items.id IS DISTINCT FROM meeting_agenda_item_journals.agenda_item_id)
- OR (agenda_items.title IS DISTINCT FROM meeting_agenda_item_journals.title)
- OR (#{normalize_newlines_sql('agenda_items.notes')} IS DISTINCT FROM
- #{normalize_newlines_sql('meeting_agenda_item_journals.notes')})
- OR (agenda_items.position IS DISTINCT FROM meeting_agenda_item_journals.position)
- OR (agenda_items.duration_in_minutes IS DISTINCT FROM meeting_agenda_item_journals.duration_in_minutes)
- OR (agenda_items.start_time IS DISTINCT FROM meeting_agenda_item_journals.start_time)
- OR (agenda_items.end_time IS DISTINCT FROM meeting_agenda_item_journals.end_time)
- OR (agenda_items.work_package_id IS DISTINCT FROM meeting_agenda_item_journals.work_package_id)
- OR (agenda_items.item_type IS DISTINCT FROM meeting_agenda_item_journals.item_type)
- SQL
- end
-
- def data_changes_sql
+ def changes_data_sql
sanitize(<<~SQL, journable_id:)
SELECT
#{journable_table_name}.id journable_id
@@ -798,14 +485,6 @@ module Journals
end
end
- def only_if_created_sql
- "EXISTS (SELECT * from inserted_journal)"
- end
-
- def id_from_inserted_journal_sql
- "(SELECT id FROM inserted_journal)"
- end
-
def data_changes_condition_sql
data_table = data_table_name
journable_table = journable_table_name
@@ -874,10 +553,6 @@ module Journals
journable.class.journal_class.table_name
end
- def normalize_newlines_sql(column)
- "REGEXP_REPLACE(COALESCE(#{column},''), '\\r\\n', '\n', 'g')"
- end
-
def cause_sql(cause)
# Using the same encoder mechanism that ActiveRecord uses for json/jsonb columns
ActiveSupport::JSON.encode(cause || {})
@@ -926,9 +601,6 @@ module Journals
Rails.logger.debug { "Inserting new journal for #{journable_type} ##{journable.id}" }
end
end
-
- delegate :sanitize,
- to: ::OpenProject::SqlSanitization
end
end
# rubocop:enable Rails/SquishedSQLHeredocs
diff --git a/app/services/journals/create_service/agenda_itemable.rb b/app/services/journals/create_service/agenda_itemable.rb
new file mode 100644
index 00000000000..7f0f2196136
--- /dev/null
+++ b/app/services/journals/create_service/agenda_itemable.rb
@@ -0,0 +1,109 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class AgendaItemable < Association
+ def associated?
+ journable.respond_to?(:agenda_items)
+ end
+
+ def cleanup_predecessor(predecessor)
+ cleanup_predecessor_for(predecessor,
+ "meeting_agenda_item_journals",
+ :journal_id,
+ :id)
+ end
+
+ def insert_sql
+ sanitize(<<~SQL.squish, journable_id:)
+ INSERT INTO
+ meeting_agenda_item_journals (
+ journal_id,
+ agenda_item_id,
+ author_id,
+ title,
+ notes,
+ position,
+ duration_in_minutes,
+ start_time,
+ end_time,
+ work_package_id,
+ item_type
+ )
+ SELECT
+ #{id_from_inserted_journal_sql},
+ agenda_items.id,
+ agenda_items.author_id,
+ agenda_items.title,
+ agenda_items.notes,
+ agenda_items.position,
+ agenda_items.duration_in_minutes,
+ agenda_items.start_time,
+ agenda_items.end_time,
+ agenda_items.work_package_id,
+ agenda_items.item_type
+ FROM meeting_agenda_items agenda_items
+ WHERE
+ #{only_if_created_sql}
+ AND agenda_items.meeting_id = :journable_id
+ SQL
+ end
+
+ def changes_sql
+ sanitize(<<~SQL.squish, journable_id:)
+ SELECT
+ max_journals.journable_id
+ FROM
+ max_journals
+ LEFT OUTER JOIN
+ meeting_agenda_item_journals
+ ON
+ meeting_agenda_item_journals.journal_id = max_journals.id
+ FULL JOIN
+ (SELECT *
+ FROM meeting_agenda_items
+ WHERE meeting_agenda_items.meeting_id = :journable_id) agenda_items
+ ON
+ agenda_items.id = meeting_agenda_item_journals.agenda_item_id
+ WHERE
+ (agenda_items.id IS DISTINCT FROM meeting_agenda_item_journals.agenda_item_id)
+ OR (agenda_items.title IS DISTINCT FROM meeting_agenda_item_journals.title)
+ OR (#{normalize_newlines_sql('agenda_items.notes')} IS DISTINCT FROM
+ #{normalize_newlines_sql('meeting_agenda_item_journals.notes')})
+ OR (agenda_items.position IS DISTINCT FROM meeting_agenda_item_journals.position)
+ OR (agenda_items.duration_in_minutes IS DISTINCT FROM meeting_agenda_item_journals.duration_in_minutes)
+ OR (agenda_items.start_time IS DISTINCT FROM meeting_agenda_item_journals.start_time)
+ OR (agenda_items.end_time IS DISTINCT FROM meeting_agenda_item_journals.end_time)
+ OR (agenda_items.work_package_id IS DISTINCT FROM meeting_agenda_item_journals.work_package_id)
+ OR (agenda_items.item_type IS DISTINCT FROM meeting_agenda_item_journals.item_type)
+ SQL
+ end
+ end
+end
diff --git a/app/services/journals/create_service/association.rb b/app/services/journals/create_service/association.rb
new file mode 100644
index 00000000000..6c05714f64c
--- /dev/null
+++ b/app/services/journals/create_service/association.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class Association
+ include Helpers
+
+ def self.for(journable)
+ [
+ AgendaItemable,
+ Attachable,
+ Customizable,
+ ProjectLifeCycleStep,
+ Storable
+ ]
+ .map { _1.new(journable) }
+ .select(&:associated?)
+ end
+
+ attr_reader :journable
+
+ def initialize(journable)
+ @journable = journable
+ end
+
+ def name = self.class.name.demodulize.underscore
+ end
+end
diff --git a/app/services/journals/create_service/attachable.rb b/app/services/journals/create_service/attachable.rb
new file mode 100644
index 00000000000..10dc86c729c
--- /dev/null
+++ b/app/services/journals/create_service/attachable.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class Attachable < Association
+ def associated?
+ journable.respond_to?(:attachable?)
+ end
+
+ def cleanup_predecessor(predecessor)
+ cleanup_predecessor_for(predecessor,
+ "attachable_journals",
+ :journal_id,
+ :id)
+ end
+
+ def insert_sql
+ sanitize(<<~SQL.squish, journable_id:, journable_class_name:)
+ INSERT INTO
+ attachable_journals (
+ journal_id,
+ attachment_id,
+ filename
+ )
+ SELECT
+ #{id_from_inserted_journal_sql},
+ attachments.id,
+ attachments.file
+ FROM attachments
+ WHERE
+ #{only_if_created_sql}
+ AND attachments.container_id = :journable_id
+ AND attachments.container_type = :journable_class_name
+ SQL
+ end
+
+ def changes_sql
+ sanitize(<<~SQL.squish, journable_id:, container_type: journable_class_name)
+ SELECT
+ max_journals.journable_id
+ FROM
+ max_journals
+ LEFT OUTER JOIN
+ attachable_journals
+ ON
+ attachable_journals.journal_id = max_journals.id
+ FULL JOIN
+ (SELECT *
+ FROM attachments
+ WHERE attachments.container_id = :journable_id AND attachments.container_type = :container_type) attachments
+ ON
+ attachments.id = attachable_journals.attachment_id
+ WHERE
+ (attachments.id IS NULL AND attachable_journals.attachment_id IS NOT NULL)
+ OR (attachable_journals.attachment_id IS NULL AND attachments.id IS NOT NULL)
+ SQL
+ end
+ end
+end
diff --git a/app/services/journals/create_service/customizable.rb b/app/services/journals/create_service/customizable.rb
new file mode 100644
index 00000000000..ac76db2342f
--- /dev/null
+++ b/app/services/journals/create_service/customizable.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class Customizable < Association
+ def associated?
+ journable.respond_to?(:customizable?)
+ end
+
+ def cleanup_predecessor(predecessor)
+ cleanup_predecessor_for(predecessor,
+ "customizable_journals",
+ :journal_id,
+ :id)
+ end
+
+ def insert_sql
+ sanitize(<<~SQL.squish, journable_id:, journable_class_name:)
+ INSERT INTO
+ customizable_journals (
+ journal_id,
+ custom_field_id,
+ value
+ )
+ SELECT
+ #{id_from_inserted_journal_sql},
+ custom_values.custom_field_id,
+ #{normalize_newlines_sql('custom_values.value')}
+ FROM custom_values
+ WHERE
+ #{only_if_created_sql}
+ AND custom_values.customized_id = :journable_id
+ AND custom_values.customized_type = :journable_class_name
+ AND custom_values.value IS NOT NULL
+ AND custom_values.value != ''
+ SQL
+ end
+
+ def changes_sql
+ sanitize(<<~SQL.squish, journable_id:, customized_type: journable_class_name)
+ SELECT
+ max_journals.journable_id
+ FROM
+ max_journals
+ LEFT OUTER JOIN
+ customizable_journals
+ ON
+ customizable_journals.journal_id = max_journals.id
+ FULL JOIN
+ (SELECT *
+ FROM custom_values
+ WHERE custom_values.customized_id = :journable_id AND custom_values.customized_type = :customized_type) custom_values
+ ON
+ custom_values.custom_field_id = customizable_journals.custom_field_id
+ WHERE
+ (custom_values.value IS NULL AND customizable_journals.value IS NOT NULL)
+ OR (customizable_journals.value IS NULL AND custom_values.value IS NOT NULL AND custom_values.value != '')
+ OR (#{normalize_newlines_sql('customizable_journals.value')} !=
+ #{normalize_newlines_sql('custom_values.value')})
+ SQL
+ end
+ end
+end
diff --git a/app/services/journals/create_service/helpers.rb b/app/services/journals/create_service/helpers.rb
new file mode 100644
index 00000000000..635d8814c11
--- /dev/null
+++ b/app/services/journals/create_service/helpers.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ module Helpers
+ private
+
+ delegate :sanitize,
+ to: ::OpenProject::SqlSanitization
+
+ def journable_id = journable.id
+
+ def journable_class_name = journable.class.base_class.name
+
+ def cleanup_predecessor_for(predecessor, table_name, column, referenced_id)
+ return "SELECT 1" unless predecessor
+
+ sanitize(<<~SQL.squish, column => predecessor.send(referenced_id))
+ DELETE
+ FROM
+ #{table_name}
+ WHERE
+ #{column} = :#{column}
+ SQL
+ end
+
+ def normalize_newlines_sql(column)
+ "REGEXP_REPLACE(COALESCE(#{column},''), '\\r\\n', '\n', 'g')"
+ end
+
+ def only_if_created_sql
+ "EXISTS (SELECT * from inserted_journal)"
+ end
+
+ def id_from_inserted_journal_sql
+ "(SELECT id FROM inserted_journal)"
+ end
+ end
+end
diff --git a/app/services/journals/create_service/project_life_cycle_step.rb b/app/services/journals/create_service/project_life_cycle_step.rb
new file mode 100644
index 00000000000..78be34753af
--- /dev/null
+++ b/app/services/journals/create_service/project_life_cycle_step.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class ProjectLifeCycleStep < Association
+ def associated?
+ journable.respond_to?(:life_cycle_steps)
+ end
+
+ def cleanup_predecessor(predecessor)
+ cleanup_predecessor_for(predecessor,
+ "project_life_cycle_step_journals",
+ :journal_id,
+ :id)
+ end
+
+ def insert_sql
+ sanitize(<<~SQL.squish, journable_id:)
+ INSERT INTO
+ project_life_cycle_step_journals (
+ journal_id,
+ life_cycle_step_id,
+ start_date,
+ end_date,
+ active
+ )
+ SELECT
+ #{id_from_inserted_journal_sql},
+ project_life_cycle_steps.id,
+ project_life_cycle_steps.start_date,
+ project_life_cycle_steps.end_date,
+ project_life_cycle_steps.active
+ FROM project_life_cycle_steps
+ WHERE
+ #{only_if_created_sql}
+ AND project_life_cycle_steps.project_id = :journable_id
+ SQL
+ end
+
+ def changes_sql
+ sanitize(<<~SQL.squish, journable_id:)
+ SELECT
+ max_journals.journable_id
+ FROM
+ max_journals
+ LEFT OUTER JOIN
+ project_life_cycle_step_journals
+ ON
+ project_life_cycle_step_journals.journal_id = max_journals.id
+ FULL JOIN
+ (SELECT *
+ FROM project_life_cycle_steps
+ WHERE project_life_cycle_steps.project_id = :journable_id) life_cycle_steps
+ ON
+ life_cycle_steps.id = project_life_cycle_step_journals.life_cycle_step_id
+ WHERE
+ life_cycle_steps.start_date IS DISTINCT FROM project_life_cycle_step_journals.start_date
+ OR life_cycle_steps.end_date IS DISTINCT FROM project_life_cycle_step_journals.end_date
+ OR life_cycle_steps.active IS DISTINCT FROM project_life_cycle_step_journals.active
+ SQL
+ end
+ end
+end
diff --git a/app/services/journals/create_service/storable.rb b/app/services/journals/create_service/storable.rb
new file mode 100644
index 00000000000..a9e422fee3c
--- /dev/null
+++ b/app/services/journals/create_service/storable.rb
@@ -0,0 +1,88 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Journals::CreateService
+ class Storable < Association
+ def associated?
+ journable.respond_to?(:file_links)
+ end
+
+ def cleanup_predecessor(predecessor)
+ cleanup_predecessor_for(predecessor,
+ "storages_file_links_journals",
+ :journal_id,
+ :id)
+ end
+
+ def insert_sql
+ sanitize(<<~SQL.squish, journable_id:, journable_class_name:)
+ INSERT INTO
+ storages_file_links_journals (
+ journal_id,
+ file_link_id,
+ link_name,
+ storage_name
+ )
+ SELECT
+ #{id_from_inserted_journal_sql},
+ file_links.id,
+ file_links.origin_name,
+ storages.name
+ FROM file_links left join storages ON file_links.storage_id = storages.id
+ WHERE
+ #{only_if_created_sql}
+ AND file_links.container_id = :journable_id
+ AND file_links.container_type = :journable_class_name
+ SQL
+ end
+
+ def changes_sql
+ sanitize(<<~SQL.squish, journable_id:, container_type: journable_class_name)
+ SELECT
+ max_journals.journable_id
+ FROM
+ max_journals
+ LEFT OUTER JOIN
+ storages_file_links_journals
+ ON
+ storages_file_links_journals.journal_id = max_journals.id
+ FULL JOIN
+ (SELECT *
+ FROM file_links
+ WHERE file_links.container_id = :journable_id AND file_links.container_type = :container_type) file_links
+ ON
+ file_links.id = storages_file_links_journals.file_link_id
+ WHERE
+ (file_links.id IS NULL AND storages_file_links_journals.file_link_id IS NOT NULL)
+ OR (storages_file_links_journals.file_link_id IS NULL AND file_links.id IS NOT NULL)
+ SQL
+ end
+ end
+end