-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/observation-multiple-evidences' into staging
- Loading branch information
Showing
14 changed files
with
198 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
db/migrate/20231222154005_create_observation_document_observations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class CreateObservationDocumentObservations < ActiveRecord::Migration[7.0] | ||
def up | ||
create_table :observation_documents_observations do |t| | ||
t.belongs_to :observation_document, foreign_key: {on_delete: :cascade}, index: {name: "observation_documents_observations_doc_index"}, null: false | ||
t.belongs_to :observation, foreign_key: {on_delete: :cascade}, index: {name: "observation_documents_observations_obs_index"}, null: false | ||
t.index [:observation_document_id, :observation_id], unique: true, name: "observation_documents_observations_double_index" | ||
t.timestamps | ||
end | ||
|
||
query = <<-SQL | ||
INSERT INTO observation_documents_observations (observation_document_id, observation_id, created_at, updated_at) | ||
SELECT | ||
od.id, o.id, NOW(), NOW() | ||
FROM | ||
observation_documents od JOIN observations o ON od.observation_id = o.id | ||
SQL | ||
execute(query) | ||
|
||
remove_reference :observation_documents, :observation | ||
end | ||
|
||
def down | ||
add_reference :observation_documents, :observation, foreign_key: true, index: true | ||
|
||
query = <<-SQL | ||
UPDATE observation_documents SET observation_id = odo.observation_id | ||
FROM (SELECT DISTINCT ON (observation_document_id) observation_document_id, observation_id FROM observation_documents_observations) odo | ||
WHERE observation_documents.id = odo.observation_document_id | ||
SQL | ||
execute(query) | ||
|
||
drop_table :observation_documents_observations | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
db/migrate/20231222171303_add_document_type_to_observation_document.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
class AddDocumentTypeToObservationDocument < ActiveRecord::Migration[7.0] | ||
def up | ||
add_column :observation_documents, :document_type, :integer, default: 0, null: false | ||
# 5 is evidence on report in observations table | ||
# changing Maps from 6 to 5 | ||
query = <<~SQL | ||
UPDATE observation_documents SET document_type = odo.evidence_type, updated_at = NOW() | ||
FROM ( | ||
SELECT DISTINCT ON (observation_document_id) observation_document_id, evidence_type | ||
FROM observation_documents_observations | ||
INNER JOIN observations ON observations.id = observation_documents_observations.observation_id | ||
WHERE evidence_type <> 5 AND evidence_type IS NOT NULL | ||
) odo | ||
WHERE observation_documents.id = odo.observation_document_id | ||
SQL | ||
execute(query) | ||
execute("UPDATE observation_documents SET document_type = 5 WHERE document_type = 6") | ||
execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type <> 5") # set no evidence for all except evidence on report | ||
execute("UPDATE observations SET evidence_type = 2 WHERE evidence_type = 5") # update for evidence on report | ||
execute( | ||
<<~SQL | ||
UPDATE observations SET evidence_type = 1 WHERE EXISTS ( | ||
SELECT * FROM observation_documents_observations odo | ||
INNER JOIN observation_documents od ON od.id = odo.observation_document_id | ||
WHERE observation_id = observations.id AND od.deleted_at IS NULL | ||
) | ||
SQL | ||
) # update for linked with non deleted documents | ||
|
||
# Testing data after migration | ||
# I was testing migration correctness with those queries so I will leave them here | ||
result = execute( | ||
"SELECT id FROM observations WHERE evidence_type <> 2 AND coalesce(evidence_on_report, '') <> ''" | ||
) | ||
raise "Mismatched type when evidence_on_report present" if result.count.positive? | ||
result = execute( | ||
<<~SQL | ||
SELECT id FROM observations WHERE evidence_type <> 1 AND EXISTS ( | ||
SELECT * FROM | ||
observation_documents_observations odo | ||
INNER JOIN observation_documents od on od.id = odo.observation_document_id | ||
WHERE odo.observation_id = observations.id AND od.deleted_at IS NULL | ||
) | ||
SQL | ||
) | ||
raise "Documents exists for observations with no linked documents" if result.count.positive? | ||
result = execute( | ||
<<~SQL | ||
SELECT id FROM observations WHERE evidence_type = 1 AND NOT EXISTS ( | ||
SELECT * FROM | ||
observation_documents_observations odo | ||
INNER JOIN observation_documents od on od.id = odo.observation_document_id | ||
WHERE odo.observation_id = observations.id AND od.deleted_at IS NULL | ||
) | ||
SQL | ||
) | ||
raise "No documents when evidence type is 1: #{result.values.flatten}" if result.count.positive? | ||
result = execute( | ||
"SELECT id FROM observations WHERE evidence_type = 0 AND validation_status <> 0" | ||
) | ||
Rails.logger.debug "No evidence for non created (non drafted) observations #{result.values.flatten}" if result.count.positive? | ||
end | ||
|
||
def down | ||
query = <<~SQL | ||
UPDATE observations SET evidence_type = odo.document_type, updated_at = NOW() | ||
FROM ( | ||
SELECT DISTINCT ON (observation_id) observation_id, document_type | ||
FROM observation_documents_observations | ||
INNER JOIN observation_documents ON observation_documents.id = observation_documents_observations.observation_document_id | ||
) odo | ||
WHERE observations.id = odo.observation_id | ||
SQL | ||
execute(query) | ||
execute("UPDATE observations SET evidence_type = 6 WHERE evidence_type = 5") | ||
execute("UPDATE observations SET evidence_type = 5 WHERE evidence_type is null and coalesce(evidence_on_report, '') <> ''") | ||
remove_column :observation_documents, :document_type | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
db/migrate/20240105172847_add_observation_report_to_observation_document.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class AddObservationReportToObservationDocument < ActiveRecord::Migration[7.0] | ||
def change | ||
add_reference :observation_documents, :observation_report, foreign_key: true, index: true | ||
|
||
reversible do |dir| | ||
dir.up do | ||
execute( | ||
<<~SQL | ||
UPDATE observation_documents SET observation_report_id = o.observation_report_id | ||
FROM observations o | ||
INNER JOIN observation_documents_observations odo ON odo.observation_id = o.id | ||
WHERE odo.observation_document_id = observation_documents.id | ||
SQL | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.