Skip to content

Commit

Permalink
Update queries to include documents table
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasleese committed Dec 22, 2016
1 parent 62c0dc7 commit 43f7c3e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/controllers/lookups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ def by_base_path
base_paths = params.fetch(:base_paths)

base_paths_and_content_ids = ContentItem
.joins(:document)
.where(state: states, base_path: base_paths)
.pluck(:base_path, :content_id)
.pluck(:base_path, 'documents.content_id')
.uniq

response = Hash[base_paths_and_content_ids]
Expand Down
4 changes: 2 additions & 2 deletions app/queries/get_linkables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def call
Rails.cache.fetch ["linkables", document_type, latest_updated_at] do
Queries::GetWebContentItems.(
Queries::GetContentItemIdsWithFallbacks.(
ContentItem.distinct.where(
ContentItem.distinct.joins(:document).where(
document_type: [document_type, "placeholder_#{document_type}"]
).pluck(:content_id),
).pluck('documents.content_id'),
state_fallback_order: [:published, :draft]
),
LinkablePresenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module Helpers
module SupersedePreviousPublishedOrUnpublished
def self.run
state_histories = ContentItem
.joins(:document)
.where(state: %w(published unpublished))
.group(:content_id, :locale)
.having("count(content_id) > 1")
.group("documents.content_id", :locale)
.having("count(documents.content_id) > 1")
.pluck("json_agg((id, user_facing_version))")
.map do |results|
results.map { |row| { content_item_id: row["f1"], version: row["f2"] } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def sql
THEN 'draft'
ELSE 'live'
END AS state_content_store,
ARRAY_AGG(DISTINCT content_items.content_id) as content_ids,
ARRAY_AGG(DISTINCT documents.content_id) as content_ids,
ARRAY_AGG(
ROW(content_items.id, content_items.updated_at)
ORDER BY content_items.updated_at DESC
Expand All @@ -63,6 +63,7 @@ def sql
LEFT JOIN unpublishings
ON content_items.state = 'unpublished'
AND unpublishings.content_item_id = content_items.id
JOIN documents ON documents.id = content_items.document_id
WHERE content_items.base_path IS NOT NULL
AND content_items.state IN ('draft', 'published', 'unpublished')
AND (unpublishings.type IS NULL OR unpublishings.type != 'substitute')
Expand Down
7 changes: 4 additions & 3 deletions lib/data_hygiene/duplicate_content_item/state_for_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def build_results

def sql
<<-SQL
SELECT content_items.content_id,
content_items.locale,
SELECT documents.content_id,
documents.locale,
CASE content_items.state
WHEN 'draft' THEN 'draft' ELSE 'live'
END AS state_content_store,
Expand All @@ -57,8 +57,9 @@ def sql
ORDER BY content_items.updated_at DESC
) as content_items
FROM content_items
JOIN documents ON documents.id = content_items.document_id
WHERE content_items.state IN ('draft', 'published', 'unpublished')
GROUP BY content_items.content_id, content_items.locale, state_content_store
GROUP BY documents.content_id, documents.locale, state_content_store
HAVING COUNT(*) > 1
SQL
end
Expand Down
9 changes: 6 additions & 3 deletions lib/data_hygiene/duplicate_content_item/version_for_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ def build_results

def sql
<<-SQL
SELECT content_items.content_id,
content_items.locale,
SELECT documents.content_id,
documents.locale,
content_items.user_facing_version,
ARRAY_AGG(
ROW(content_items.id, content_items.updated_at)
ORDER BY content_items.updated_at DESC
) as content_items
FROM content_items
GROUP BY content_id, content_items.locale, content_items.user_facing_version
JOIN documents ON content_items.document_id = documents.id
GROUP BY documents.content_id,
documents.locale,
content_items.user_facing_version
HAVING COUNT(*) > 1
SQL
end
Expand Down

0 comments on commit 43f7c3e

Please sign in to comment.