Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch fully to documents and editions #683

Merged
merged 164 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
b1b9f56
Populate documents from content items
thomasleese Jan 4, 2017
f4a2088
Update documents and content_items models
thomasleese Dec 21, 2016
e09b223
Update model tests
thomasleese Dec 21, 2016
e7cbe57
Update queries for new documents table
thomasleese Dec 21, 2016
596b9e8
Update commands for new documents table
thomasleese Dec 21, 2016
577030f
Update queries to include documents table
thomasleese Dec 22, 2016
1d60bb3
Update tests
thomasleese Dec 22, 2016
83e3c2c
Update queries
thomasleese Dec 23, 2016
f3f1322
Minor code formatting improvements
thomasleese Dec 23, 2016
f08c4c0
Fix unpublish tests
thomasleese Dec 23, 2016
e1dac84
Ensure a document is always created
thomasleese Dec 28, 2016
6f37970
Fix formatting issues
thomasleese Dec 30, 2016
0b71cec
Add a note regarding removal of methods
thomasleese Jan 3, 2017
ddaa74e
Retrieve the document in a retry loop
thomasleese Jan 3, 2017
62cd742
Lock document row when finding a previous draft
dougdroper Dec 30, 2016
5ce016e
Create a query for looking up a document
thomasleese Jan 3, 2017
b9cc2fa
Add 'draft' and 'live' methods to documents
thomasleese Jan 3, 2017
c69e775
Include document fields in content item JSON representation
thomasleese Jan 3, 2017
560dec6
Add content_id to approved fields
thomasleese Jan 3, 2017
f7d86a5
Fix code style issues
thomasleese Jan 3, 2017
272331c
Update tests to account for content ID validation
thomasleese Jan 3, 2017
f229a9a
Add checks for draft and live content items
thomasleese Jan 3, 2017
4c4a770
Update commands to use documents
thomasleese Jan 3, 2017
92d79de
Use content_store to determine live/draft
thomasleese Jan 3, 2017
1f9046f
Use ActiveRecord relations for draft and live
kevindew Jan 3, 2017
86bebbb
Create concern for find_or_create_locked
kevindew Jan 3, 2017
8061aac
Use find_or_create_locked method for locked models
kevindew Jan 3, 2017
1fa4295
Add means of getting previous content item
thomasleese Jan 3, 2017
2e4b39c
Fix field use in state validator
thomasleese Jan 3, 2017
ff62a02
Minor code fixes
thomasleese Jan 3, 2017
386b006
Simplify commands to use document
thomasleese Jan 4, 2017
75d1d22
Simplify query to check published base_path conflict
kevindew Jan 3, 2017
1502d19
ContentItem knows about unpublishing relation
kevindew Jan 4, 2017
c3b0694
Refactored GetContentItemIdsWithFallbacks query
kevindew Jan 4, 2017
00babf1
Simplify LocalesForContentItems query
kevindew Jan 4, 2017
e1d7fa8
Use content_stores rather than states in LocalesForContentItems usage
kevindew Jan 4, 2017
f41f707
Add check for invalid UUIDs
thomasleese Jan 5, 2017
dc3c704
Simplify get latest query
dougdroper Jan 3, 2017
1aa66d1
Rename document spec
thomasleese Jan 5, 2017
2e7727b
Update publish command after changes in master
thomasleese Jan 10, 2017
2995e9b
Memoize content_item and previous_item
thomasleese Jan 10, 2017
d61fe6e
Update code for lock version changes
thomasleese Jan 11, 2017
517b83b
Fix code style error
thomasleese Jan 11, 2017
be5b5de
Fix syntax error
thomasleese Jan 11, 2017
72005b4
Update pact tests
thomasleese Jan 11, 2017
a158f46
Remove unnecessary assignment
thomasleese Jan 11, 2017
88cc0b5
Fix typo
kevindew Jan 16, 2017
49abb15
Store content_id and locale on content_item
kevindew Jan 16, 2017
6504e48
Brief refactoring to discard_draft
kevindew Jan 16, 2017
6601d1e
Make discard draft tests more document centric
kevindew Jan 16, 2017
e1eea7e
Update content_item factory for no lock_version
kevindew Jan 16, 2017
c5791fa
Document centric tests for PatchLinkSet Command
kevindew Jan 16, 2017
9cbd928
Document centric spec for PostAction command
kevindew Jan 16, 2017
d8b8bcf
Remove content_id and locale from TOP_LEVEL_FIELDS
kevindew Jan 16, 2017
e7ff132
Don't reset document_id
kevindew Jan 16, 2017
4882fac
Update content_item_presenter default fields
kevindew Jan 16, 2017
2f09e11
Resolve ambiguous query
kevindew Jan 16, 2017
f8d9c26
More document centric PutContent Command spec
kevindew Jan 16, 2017
6ab0ba8
Create documents in RepresentDownstream spec
kevindew Jan 17, 2017
c07f1eb
published_or_unpublished relation scope on Document
kevindew Jan 17, 2017
427f850
Don't include locale and content_id in with_draft factory
kevindew Jan 17, 2017
b851ae2
Document centric tests for Unpublish command
kevindew Jan 17, 2017
25b9f1a
Use document on BasePathForStateValidator spec
kevindew Jan 17, 2017
5cf4f70
Rename StateForLocaleValidator for Documents
kevindew Jan 17, 2017
092dba4
Rename VersionForLocaleValidator for Documents
kevindew Jan 17, 2017
730badf
Use document in content_item model spec
kevindew Jan 17, 2017
2b8670a
Remove content_id from test
kevindew Jan 17, 2017
7802fdb
Use document in ActionsController spec
kevindew Jan 17, 2017
3669670
Use documents in ContentItemsController spec
kevindew Jan 17, 2017
653494f
Use document in LinkSetsController spec
kevindew Jan 17, 2017
06d17b9
Use document.published_or_unpublished over document.live
kevindew Jan 17, 2017
465f53c
Remove unnecessary migration
thomasleese Jan 18, 2017
31a575b
Update unpublishing_spec to use documents
thomasleese Jan 18, 2017
694da10
Update discard_draft_request_spec to use documents
thomasleese Jan 18, 2017
d7b21aa
Update dependency_resolution_helper to use documents
thomasleese Jan 18, 2017
eb1dd90
Update reallocating_base_path_spec to use documents
thomasleese Jan 18, 2017
727b67d
Update document usage in ChangeHistoryPresenter
kevindew Jan 18, 2017
f1b5a82
Update lookups_spec to use documents
thomasleese Jan 18, 2017
b2f13a7
Further updates to tests to make use of documents
thomasleese Jan 18, 2017
8eb5562
Update message_bus_spec to use documents
thomasleese Jan 18, 2017
24683ce
Add a document method on WebContentItems
thomasleese Jan 18, 2017
a075a86
Update expanded_links_endpoint_spec to use documents
thomasleese Jan 18, 2017
d17384e
Update spec/requests to use documents
thomasleese Jan 18, 2017
9f57160
Correct alphabetical ordering of fields on WebContentItem
kevindew Jan 18, 2017
b77090d
ChangeHistoryPresenter consistently uses WebContentItem
kevindew Jan 18, 2017
fa35f96
Update spec/workers to use documents
thomasleese Jan 18, 2017
7c88ec3
Fix version_validator_spec to use documents
thomasleese Jan 18, 2017
c414a1a
Use document in AvailableTranslations spec
kevindew Jan 18, 2017
a5f717a
Include lock_version in content_item_presenter
kevindew Jan 18, 2017
78bc5a6
Add initial spec for debug presenter
thomasleese Jan 18, 2017
f40399c
Add more tests for debug presenter
thomasleese Jan 18, 2017
45232dd
Specify factory for create_content_item
kevindew Jan 18, 2017
75f2f0c
Use different interface for create_content_item
kevindew Jan 18, 2017
0adbf47
Use documents in GetContentCollection query
kevindew Jan 18, 2017
0c37694
Various coding consistency updates
kevindew Jan 18, 2017
056d2b0
Use document in GetContentItemIdsWithFallbacks spec
kevindew Jan 18, 2017
df587fd
Use documents in GetLatest spec
kevindew Jan 18, 2017
88871f1
Use document in GetLinked spec
kevindew Jan 18, 2017
35c76ea
Use documents in LocalesForContentItems spec
kevindew Jan 18, 2017
30048bf
Fix minor code style issue
thomasleese Jan 19, 2017
3f2f70a
Update pact tests to use documents
thomasleese Jan 19, 2017
8528a21
Fix code style issues
thomasleese Jan 19, 2017
3bfaf50
Various fixes following rebase on master
thomasleese Jan 20, 2017
a03918b
Make sure to backfill LockVersion instances
thomasleese Jan 20, 2017
a42a2fb
Rename 'ContentItem' to 'Edition'
thomasleese Jan 19, 2017
51fd578
Fix minor code formatting issues
thomasleese Jan 19, 2017
1ac430a
Use 'edition' in AccessLimit
thomasleese Jan 19, 2017
a225c14
Use 'edition' in Action
thomasleese Jan 19, 2017
0d552e2
Use 'edition' in ChangeNote
thomasleese Jan 19, 2017
4097515
Rename content_item to edition in UserFacingVersion
kevindew Jan 19, 2017
927c892
Update Document to use editions
thomasleese Jan 19, 2017
30a9274
Update Location to use editions
thomasleese Jan 19, 2017
e9b638f
Update state to use editions
thomasleese Jan 19, 2017
e28149e
Update factories to use edition
kevindew Jan 19, 2017
fc08f10
Use edition in Unpublishing model
kevindew Jan 19, 2017
f2ddc71
Rename factories in specs
kevindew Jan 19, 2017
982409b
Improve code consistency
kevindew Jan 19, 2017
ba89025
Use edition on Translation model
kevindew Jan 19, 2017
7ccb852
Fix typo in variable name
thomasleese Jan 20, 2017
e3fa100
Fix pact tests to use new factories
thomasleese Jan 20, 2017
229b8dc
Rename apperances of 'content_items' to 'editions'
thomasleese Jan 20, 2017
ce575a8
Continue rename of content_item to edition
thomasleese Jan 20, 2017
081847c
Fix minor code style issue
thomasleese Jan 20, 2017
e91415c
Continue to rename content_item to edition
thomasleese Jan 20, 2017
a925bd6
Make content_id and locale private attributes
kevindew Jan 20, 2017
1a3cf6c
Reapproach the as_json additions on Edition
kevindew Jan 20, 2017
bee2c62
No longer access content_id and locale directly on Editions
kevindew Jan 20, 2017
c7e5091
Fix pact tests
thomasleese Jan 20, 2017
4a202ed
Minor code formatting fix
thomasleese Jan 20, 2017
5466907
Rename more ContentItems to Editions
thomasleese Jan 20, 2017
c2f1f8f
Update benchmarking scripts for content item rename
thomasleese Jan 20, 2017
0eb8dd6
Continue renaming content_items to editions
thomasleese Jan 20, 2017
ba143aa
Rename spec variables
thomasleese Jan 20, 2017
d5a0fc8
Rename content item in strings
kevindew Jan 20, 2017
3fc52fb
Move ContentItem definition into own file
thomasleese Jan 23, 2017
1d2eb5e
Rename query for Edition
thomasleese Jan 23, 2017
c90e502
Rename LocalesForContentItems to editions
thomasleese Jan 23, 2017
14a5353
Use reorder so ordering in scope can be acknowledged
kevindew Jan 23, 2017
4fe8e57
Make id non ambiguous
kevindew Jan 23, 2017
f8a052c
Remove state scoping at edition level
kevindew Jan 23, 2017
32cbaf9
Remove state from edition criteria
kevindew Jan 23, 2017
3e9fa65
Update details to conform to placeholder schema
kevindew Jan 23, 2017
265ca5a
Update content item in copy to edition/document
kevindew Jan 23, 2017
2e0daa1
Use "edition" terminology instead of "content item"
kevindew Jan 23, 2017
48ed13e
rename import_content_item_events task
kevindew Jan 23, 2017
eac1ab1
Use editions rather than content_items in govspeak compare task
kevindew Jan 23, 2017
c3f55ec
Use editions rather than content items in descriptions
kevindew Jan 23, 2017
0113dcd
Remove data_cleanup task for Specialist Publisher
kevindew Jan 23, 2017
8601584
Change content_item variable names to edition
kevindew Jan 23, 2017
135890f
Monkey patch ActiveRecord to kill ContentItem class
kevindew Jan 23, 2017
8fb56ac
Use document in put_content refactoring
kevindew Jan 23, 2017
8f3491e
Validate UUID at route level
kevindew Jan 23, 2017
cb03463
Update CheckForContentItemPreventingDraftFromBeingPublished class name
kevindew Jan 24, 2017
77265ed
Make FactoryGirl initialisation format consistent
kevindew Jan 24, 2017
8d1c190
Add missing brackets to method calls
kevindew Jan 24, 2017
abda3c5
Remove memoization for edition
kevindew Jan 24, 2017
3051eec
Raise an error if retries are exhausted
kevindew Jan 24, 2017
e84dd9b
Add a `with_document` scope to Edition
kevindew Jan 24, 2017
f81db55
Make ignoring substitute unpublishings easier to understand
kevindew Jan 24, 2017
a3153d3
Add parenthesis to make precedence clearer
kevindew Jan 24, 2017
e636d9e
Simpler approach to multiline strings
kevindew Jan 24, 2017
045980a
Use exist matchers rather than boolean
kevindew Jan 24, 2017
899016f
Update DeleteContentItem helper
kevindew Jan 24, 2017
159a59f
Remove DefaultAttributes concern
kevindew Jan 26, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ for more information.
Events older then a month are archived to S3, you can import these events back
into your local DB by running the rake tasks in lib/tasks/events.rake, after
you set up the relavent ENV variables. For example if you want to find all the
events that are relavant for a particular content item you can run:
events that are relavant for a particular content_id you can run:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/relavant/relevant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - already got that sorted over here: c877832

```sh
rake 'events:import_content_item_events[a796ca43-021b-4960-9c99-f41bb8ef2266]'
rake 'events:import_content_id_events[a796ca43-021b-4960-9c99-f41bb8ef2266]'
```
see the rake task for more details.

Expand Down
16 changes: 9 additions & 7 deletions app/commands/base_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ def self.raise_validation_command_error(e)
end
private_class_method :execute_callbacks, :raise_validation_command_error

def check_version_and_raise_if_conflicting(current_versioned_item, previous_version_number)
current_version = LockVersion.find_by(target: current_versioned_item)
def check_version_and_raise_if_conflicting(current_versioned_item, previous_version)
return unless current_versioned_item && previous_version.present?

return unless current_versioned_item && current_version
current_version = current_versioned_item.stale_lock_version

if current_version.conflicts_with?(previous_version_number)
friendly_message = "A lock-version conflict occurred. The `previous_version` you've sent " +
"(#{previous_version_number}) is not the same as the current " +
"lock version of the content item (#{current_version.number})."
if current_version != previous_version.to_i
friendly_message = "A lock-version conflict occurred. The " +
"`previous_version` you've sent (#{previous_version}) is not the " +
"same as the current lock version of the edition " +
"(#{current_version})."

fields = {
fields: {
Expand All @@ -74,6 +75,7 @@ def check_version_and_raise_if_conflicting(current_versioned_item, previous_vers

raise_command_error(409, "Conflict", fields, friendly_message: friendly_message)
end

current_version
end

Expand Down
69 changes: 28 additions & 41 deletions app/commands/v2/discard_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@ class DiscardDraft < BaseCommand
def call
raise_error_if_missing_draft!

check_version_and_raise_if_conflicting(draft, payload[:previous_version])
check_version_and_raise_if_conflicting(document, payload[:previous_version])

delete_supporting_objects
delete_supporting_objects(document.draft)
delete_draft_from_database
increment_live_lock_version if live
increment_lock_version

after_transaction_commit do
downstream_discard_draft(draft.base_path, draft.content_id, locale)
downstream_discard_draft(
document.draft.base_path,
document.content_id,
document.locale
)
end

Action.create_discard_draft_action(draft, locale, event)
Success.new(content_id: content_id)
Action.create_discard_draft_action(document.draft, document.locale, event)
Success.new(content_id: document.content_id)
end

private

def raise_error_if_missing_draft!
return if draft.present?
return if document.draft.present?

code = live.present? ? 422 : 404
message = "There is no draft content item to discard"
code = document.published_or_unpublished.present? ? 422 : 404
message = "There is not a draft edition of this document to discard"

raise CommandError.new(code: code, message: message)
end

def delete_draft_from_database
draft.destroy
document.draft.destroy
end

def downstream_discard_draft(path_used, content_id, locale)
Expand All @@ -46,43 +50,26 @@ def downstream_discard_draft(path_used, content_id, locale)
)
end

def delete_supporting_objects
Location.find_by(content_item: draft).try(:destroy)
State.find_by(content_item: draft).try(:destroy)
Translation.find_by(content_item: draft).try(:destroy)
UserFacingVersion.find_by(content_item: draft).try(:destroy)
LockVersion.find_by(target: draft).try(:destroy)
AccessLimit.find_by(content_item: draft).try(:destroy)
ChangeNote.where(content_item: draft).destroy_all
def delete_supporting_objects(edition)
Location.find_by(edition: edition).try(:destroy)
State.find_by(edition: edition).try(:destroy)
Translation.find_by(edition: edition).try(:destroy)
UserFacingVersion.find_by(edition: edition).try(:destroy)
LockVersion.find_by(target: edition).try(:destroy)
AccessLimit.find_by(edition: edition).try(:destroy)
ChangeNote.where(edition: edition).destroy_all
end

def increment_live_lock_version
LockVersion.find_by!(target: live).increment!
def increment_lock_version
document.increment!(:stale_lock_version)
end

def draft
@draft ||= ContentItem.find_by(
content_id: content_id,
locale: locale,
state: "draft",
def document
@document ||= Document.find_or_create_locked(
content_id: payload[:content_id],
locale: payload.fetch(:locale, Edition::DEFAULT_LOCALE),
)
end

def live
@live ||= ContentItem.find_by(
content_id: content_id,
locale: locale,
state: %w(published unpublished),
)
end

def content_id
payload[:content_id]
end

def locale
payload[:locale] || ContentItem::DEFAULT_LOCALE
end
end
end
end
28 changes: 6 additions & 22 deletions app/commands/v2/patch_link_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class PatchLinkSet < BaseCommand
def call
raise_unless_links_hash_is_provided
validate_schema
link_set = find_or_create_link_set(content_id)
link_set = LinkSet.find_or_create_locked(content_id: content_id)

check_version_and_raise_if_conflicting(link_set, previous_version_number)
LockVersion.find_or_create_by!(target: link_set).increment!

link_set.increment!(:stale_lock_version)

grouped_links.each do |group, payload_content_ids|
# For each set of links in a LinkSet scoped by link_type, this iterator
Expand All @@ -32,23 +33,6 @@ def call

private

def find_or_create_link_set(content_id)
begin
retries ||= 0
LinkSet.transaction(requires_new: true) do
LinkSet.find_or_create_by!(content_id: content_id).lock!
end
rescue ActiveRecord::RecordNotUnique
# This should never need more than 1 retry as the scenario this error
# would occur is: inbetween rails find_or_create SELECT & INSERT
# queries a concurrent request ran an INSERT. Thus on retry the
# SELECT would succeed.
# So if this actually throws an exception here we probably have a
# weird underlying problem.
retry if (retries += 1) == 1
end
end

def content_id
payload.fetch(:content_id)
end
Expand Down Expand Up @@ -82,10 +66,10 @@ def raise_unless_links_hash_is_provided
def send_downstream
return unless downstream

draft_locales = Queries::LocalesForContentItems.call([content_id])
draft_locales = Queries::LocalesForEditions.call([content_id], %w[draft live])
draft_locales.each { |(content_id, locale)| downstream_draft(content_id, locale) }

live_locales = Queries::LocalesForContentItems.call([content_id], %w[published unpublished])
live_locales = Queries::LocalesForEditions.call([content_id], %w[live])
live_locales.each { |(content_id, locale)| downstream_live(content_id, locale) }
end

Expand Down Expand Up @@ -133,7 +117,7 @@ def schema_validator

def schema_name
@schema_name ||= Queries::GetLatest.(
ContentItem.where(content_id: payload[:content_id])
Edition.with_document.where("documents.content_id": content_id)
).pluck(:schema_name).first
end
end
Expand Down
46 changes: 19 additions & 27 deletions app/commands/v2/post_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,51 @@ module Commands
module V2
class PostAction < BaseCommand
def call
check_version_and_raise_if_conflicting(content_item, previous_version_number)
check_version_and_raise_if_conflicting(document, previous_version_number)

Action.create!(
content_id: content_id,
locale: locale,
content_id: document.content_id,
locale: document.locale,
action: action_type,
content_item: content_item,
edition: edition,
user_uid: event.user_uid,
event: event,
)

Success.new(
{ content_id: content_id, locale: locale, action: action_type },
{ content_id: document.content_id, locale: document.locale, action: action_type },
code: 201,
)
end

private

def content_id
payload.fetch(:content_id)
end

def locale
payload.fetch(:locale, ContentItem::DEFAULT_LOCALE)
def document
@document ||= Document.find_or_create_locked(
content_id: payload.fetch(:content_id),
locale: payload.fetch(:locale, Edition::DEFAULT_LOCALE),
)
end

def draft?
payload[:draft].nil? ? true : payload[:draft]
end

def content_item
@content_item ||= find_content_item
def edition
edition = draft? ? document.draft : document.published_or_unpublished

unless edition
message = "Could not find an edition to associate this action with"
raise_command_error(404, message, fields: {})
end

edition
end

def action_type
payload[:action]
end

def find_content_item
content_item = ContentItem.find_by(
content_id: content_id,
locale: locale,
state: draft? ? %w(draft) : %w(published unpublished),
)

unless content_item
message = "Could not find a content item to associate this action with"
raise_command_error(404, message, fields: {})
end
content_item
end

def previous_version_number
payload[:previous_version].to_i if payload[:previous_version]
end
Expand Down
Loading