Skip to content

Commit

Permalink
Rename database table
Browse files Browse the repository at this point in the history
First stage in renaming content items to editions is to rename the
underlying database.
  • Loading branch information
thomasleese committed Jan 6, 2017
1 parent e5cde8b commit 7dc077e
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/models/change_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.create_from_content_item(payload, content_item)

def self.join_content_items(content_item_scope)
content_item_scope.joins(
"LEFT JOIN change_notes ON change_notes.content_item_id = content_items.id"
"LEFT JOIN change_notes ON change_notes.content_item_id = editions.id"
)
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ContentItem < ApplicationRecord
self.table_name = "editions"

include DefaultAttributes
include SymbolizeJSON
include DescriptionOverrides
Expand Down
2 changes: 1 addition & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.filter(content_item_scope, base_path:)

def self.join_content_items(content_item_scope)
content_item_scope.joins(
"INNER JOIN locations ON locations.content_item_id = content_items.id"
"INNER JOIN locations ON locations.content_item_id = editions.id"
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/lock_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class LockVersion < ApplicationRecord
def self.join_content_items(content_item_scope)
content_item_scope.joins(
"INNER JOIN lock_versions ON
lock_versions.target_id = content_items.id AND
lock_versions.target_id = editions.id AND
lock_versions.target_type = 'ContentItem'"
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.filter(content_item_scope, name:)

def self.join_content_items(content_item_scope)
content_item_scope.joins(
"INNER JOIN states ON states.content_item_id = content_items.id"
"INNER JOIN states ON states.content_item_id = editions.id"
)
end
end
2 changes: 1 addition & 1 deletion app/models/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.filter(content_item_scope, locale:)

def self.join_content_items(content_item_scope)
content_item_scope.joins(
"INNER JOIN translations ON translations.content_item_id = content_items.id"
"INNER JOIN translations ON translations.content_item_id = editions.id"
)
end
end
2 changes: 1 addition & 1 deletion app/models/user_facing_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.filter(content_item_scope, number:)

def self.join_content_items(content_item_scope)
content_item_scope.joins(
"INNER JOIN user_facing_versions ON user_facing_versions.content_item_id = content_items.id"
"INNER JOIN user_facing_versions ON user_facing_versions.content_item_id = editions.id"
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ def valid_order_fields
end

def disambiguate_field(field)
field == :updated_at ? :"content_items.updated_at" : field
field == :updated_at ? :"editions.updated_at" : field
end
end
12 changes: 6 additions & 6 deletions app/presenters/queries/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.present(content_item, params = {})
def initialize(scope, params = {})
self.scope = scope
self.fields = (params[:fields] || DEFAULT_FIELDS).map(&:to_sym)
self.order = params[:order] || { "content_items.id" => "asc" }
self.order = params[:order] || { "editions.id" => "asc" }
self.limit = params[:limit]
self.offset = params[:offset]
self.search_query = params[:search_query]
Expand Down Expand Up @@ -82,9 +82,9 @@ def select_fields(scope)
fields_to_select = fields.map do |field|
case field
when :publication_state
"content_items.state AS publication_state"
"editions.state AS publication_state"
when :user_facing_version
"content_items.user_facing_version AS user_facing_version"
"editions.user_facing_version AS user_facing_version"
when :lock_version
"lock_versions.number AS lock_version"
when :description
Expand All @@ -100,7 +100,7 @@ def select_fields(scope)
when :change_note
"change_notes.note AS change_note"
when :base_path
"content_items.base_path as base_path"
"editions.base_path as base_path"
when :locale
"documents.locale as locale"
when :content_id
Expand All @@ -117,13 +117,13 @@ def select_fields(scope)

def search(scope)
return scope unless search_query.present?
scope.where("title ilike ? OR content_items.base_path ilike ?", "%#{search_query}%", "%#{search_query}%")
scope.where("title ilike ? OR editions.base_path ilike ?", "%#{search_query}%", "%#{search_query}%")
end

STATE_HISTORY_SQL = <<-SQL.freeze
(
SELECT json_agg((user_facing_version, state))
FROM content_items c
FROM editions c
WHERE c.document_id = documents.id
GROUP BY documents.content_id
)
Expand Down
4 changes: 2 additions & 2 deletions app/queries/get_content_item_ids_with_fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def self.call(content_ids, state_fallback_order:, locale_fallback_order: Content
.where(documents: { locale: locale_fallback_order })
.where.not(document_type: ContentItem::NON_RENDERABLE_FORMATS)
.order("documents.content_id ASC")
.order(order_by_clause("content_items", "state", state_ordering(state_fallback_order)))
.order(order_by_clause("editions", "state", state_ordering(state_fallback_order)))
.order(order_by_clause("documents", "locale", locale_fallback_order))
.pluck("DISTINCT ON (documents.content_id) content_id, content_items.id")
.pluck("DISTINCT ON (documents.content_id) content_id, editions.id")
.map(&:last)
end

Expand Down
2 changes: 1 addition & 1 deletion app/queries/get_latest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def call(content_item_scope)
def inner_scope(content_item_scope)
content_item_scope
.order(:document_id, user_facing_version: :desc)
.select("distinct on(content_items.document_id) content_items.document_id, content_items.id")
.select("distinct on(editions.document_id) editions.document_id, editions.id")
.map(&:id)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/queries/locales_for_content_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.call(
Document.joins(:content_items)
.where(
content_id: content_ids,
content_items: { content_store: content_stores }
editions: { content_store: content_stores }
)
.distinct
.order(:content_id, :locale)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170106103325_rename_content_items_to_editions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameContentItemsToEditions < ActiveRecord::Migration[5.0]
def change
rename_table :content_items, :editions
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.run
.where(state: %w(published unpublished))
.group("documents.content_id", :locale)
.having("count(documents.content_id) > 1")
.pluck("json_agg((content_items.id, user_facing_version))")
.pluck("json_agg((editions.id, user_facing_version))")
.map do |results|
results.map { |row| { content_item_id: row["f1"], version: row["f2"] } }
end
Expand Down
39 changes: 19 additions & 20 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170106100107) do
ActiveRecord::Schema.define(version: 20170106103325) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -49,7 +49,13 @@
t.index ["content_item_id"], name: "index_change_notes_on_content_item_id", using: :btree
end

create_table "content_items", force: :cascade do |t|
create_table "documents", force: :cascade do |t|
t.uuid "content_id", null: false
t.string "locale", null: false
t.index ["content_id", "locale"], name: "index_documents_on_content_id_and_locale", unique: true, using: :btree
end

create_table "editions", force: :cascade do |t|
t.string "title"
t.datetime "public_updated_at"
t.json "details", default: {}
Expand All @@ -72,23 +78,16 @@
t.integer "user_facing_version", default: 1, null: false
t.string "base_path"
t.string "content_store"
t.integer "document_id"
t.integer "document_id", null: false
t.index ["base_path", "content_store"], name: "index_content_items_on_base_path_and_content_store", unique: true, using: :btree
t.index ["document_id", "state"], name: "index_content_items_on_document_id_and_state", using: :btree
t.index ["document_type"], name: "index_content_items_on_document_type", using: :btree
t.index ["last_edited_at"], name: "index_content_items_on_last_edited_at", using: :btree
t.index ["public_updated_at"], name: "index_content_items_on_public_updated_at", using: :btree
t.index ["publishing_app"], name: "index_content_items_on_publishing_app", using: :btree
t.index ["rendering_app"], name: "index_content_items_on_rendering_app", using: :btree
t.index ["state", "base_path"], name: "index_content_items_on_state_and_base_path", using: :btree
t.index ["updated_at"], name: "index_content_items_on_updated_at", using: :btree
end

create_table "documents", force: :cascade do |t|
t.uuid "content_id", null: false
t.string "locale", null: false
t.integer "lock_version", default: 1, null: false
t.index ["content_id", "locale"], name: "index_documents_on_content_id_and_locale", unique: true, using: :btree
t.index ["document_id", "state"], name: "index_editions_on_document_id_and_state", using: :btree
t.index ["document_type"], name: "index_editions_on_document_type", using: :btree
t.index ["last_edited_at"], name: "index_editions_on_last_edited_at", using: :btree
t.index ["public_updated_at"], name: "index_editions_on_public_updated_at", using: :btree
t.index ["publishing_app"], name: "index_editions_on_publishing_app", using: :btree
t.index ["rendering_app"], name: "index_editions_on_rendering_app", using: :btree
t.index ["state", "base_path"], name: "index_editions_on_state_and_base_path", using: :btree
t.index ["updated_at"], name: "index_editions_on_updated_at", using: :btree
end

create_table "events", force: :cascade do |t|
Expand Down Expand Up @@ -202,7 +201,7 @@
t.datetime "updated_at"
end

add_foreign_key "change_notes", "content_items"
add_foreign_key "content_items", "documents"
add_foreign_key "change_notes", "editions", column: "content_item_id"
add_foreign_key "editions", "documents"
add_foreign_key "links", "link_sets"
end
6 changes: 3 additions & 3 deletions lib/tasks/version_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def query
array_agg(state) as states,
array_agg(user_facing_version) as versions,
array_agg(base_path) as base_paths,
array_agg(content_items.id) as content_item_ids
array_agg(editions.id) as content_item_ids
FROM content_items
JOIN documents ON documents.id = content_items.document_id
FROM editions
JOIN documents ON documents.id = editions.document_id
GROUP BY documents.content_id, documents.locale
SQL
end
Expand Down

0 comments on commit 7dc077e

Please sign in to comment.