Skip to content

Commit

Permalink
fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Jan 5, 2024
1 parent bc813a7 commit 6e2ee51
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,50 @@ def up
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("UPDATE observations SET evidence_type = 1 WHERE EXISTS (SELECT * FROM observation_documents_observations where observation_id = observations.id)") # update for docuements
execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type is null") # for the rest
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
Expand Down

0 comments on commit 6e2ee51

Please sign in to comment.