-
Notifications
You must be signed in to change notification settings - Fork 125
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
[WIP] Add type column to service_order table #328
Conversation
|
||
say_with_time("Set service_order type") do | ||
ServiceOrder.all.each do |service_order| | ||
service_order.update_attribute(:type, service_order.miq_requests.pluck(:type)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So 2 things
-
Type is reserved for STI to just taking the type from MiqRequest is going to be an issue. If you're going to set a
:type
column it should match a sub-class ofServiceOrder
so something like if MiqRequest isServiceTemplateProvisionRequest
then ServiceOrder would becomeServiceProvisionOrder
andServiceTemplateTransformationPlanRequest
=>ServiceTransformationOrder
-
Updating for every service_order isn't very efficient, it'd be much better to group_by miq_request type and do an update_all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service_order.miq_requests.pluck(:type)
That yields an array of all the types, right? (When there are multiple requests.)
Possibly something like
types = service_order.miq_requests.pluck(:type).sort.uniq
service_order.update_attribute(:type, types.first) if types.count == 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think..
SELECT string_agg(DISTINCT type, ', ') AS type, service_order_id
FROM miq_requests
WHERE service_order_id IS NOT NULL
GROUP BY service_order_id;
gives you the right list, so you can transform it into a single update, and it should mostly work.
(No idea how to say that in rails-speak though :).)
We may want to clear any types with a comma after though. (And I guess not use type
but something like request_type
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agrare Ok, we don't want STI here, so I've changed the column to request_type
as per @himdel's suggestion. It's now a sorted and comma-joined string in the event there is more than one request type.
If there's a more efficient way of doing this update I think I will need a little help putting it together.
@himdel Do we still need this since ManageIQ/manageiq#18368 was merged? |
@djberg96 Either you linked the wrong PR, or the two are completely unrelated. But yes, I think we do need this, |
@agrare Ok, clarification, do you think we should be using STI here? Or use a different column altogether (as I've now done)? |
@djberg96 I'm not familiar enough with service orders and requests to say, but is it actually possible for a service order to have multiple types of requests? If not then using STI for the service order makes sense. |
@bzwei @tinaafitz can you help here? |
@djberg96 I assume v2v is creating a service order for their transformation requests and that is why you need this? |
Almost sure the v2v requests are For details, see https://bugzilla.redhat.com/show_bug.cgi?id=1565049 please :) |
@himdel Thanks for providing the additional information. :-) |
Well we wouldn't want a comma joined list of types for a single order right? If an order can only be of a single type then STI would seem to be the way to go. I'll post some examples of doing this more efficiently in a bit. |
@djberg96 I forget what you were trying to do here. From what I gather:
Is this correct? We would also have to handle updating this list when we create new service order requests right? |
Frankly, at this point anything that works is fine :). For the purposes of UI, if this were exposed as a virtual attribute on ServiceOrder, it would be enough, anything we can filter on in the API is fine really. That said, virtual attribute would mean the API has to run that for every single service order, just to determine whether to show it or not, which sounds like an N+1 to me, hence the preference to cache it in the service order table. The "let's just join the types" approach is the simplest that works without assuming anything about what ServiceOrder can or cannot contain. (And yes, it requires adding/removing requests to update that field.) An STI-based approach of having 2 kinds of ServiceOrder and let each only accept specific request types does sound better, but possibly not at the cost of making this take another release to spec and finish. |
@tinaafitz, @agrare, @djberg96 : we would actually need you to move forward with this. Is that anything we can do? /cc: @himdel |
(Alternatively, if the decision was to abandon the effort completely, close this and ManageIQ/manageiq-ui-service#1463.) |
Alright, trying to dust off the cobwebs on this one. I don't think we want a So, let's leave it at |
Well, we actually need a way to guarantee that this column gets set automatically when a new ServiceOrder is created, which would cover it. I think that means a |
Created a simple "just add a .v2v? virtual attribute on ServiceOrder" in ManageIQ/manageiq#19332 for your consideration :) |
I don't love the comma-separated list of child types on a service_order. I don't think I ever got an answer to "Can a ServiceOrder have both v2v child request types and non-v2v child request types?" According to @himdel's PR If that is the case, lets make ServiceOrder STI and create a ServiceTransformationOrder subclass. |
I have no idea there either. I don't think anybody knows, IMO it was a mistake to reuse ServiceOrder for v2v without an explicit plan, but we're stuck with that. (That's pretty much the only reason I proposed the comma separated list - nobody knows, but this shouldn't break anything.) The |
(Definitely +1 on STI, if it can be done :)). |
Okay let me try to find out, |
ServiceOrder is transparent in the current use of v2v, one service order per v2v request. Therefore STI should work. |
Any progress here? The migration seems ready to me.. EDIT: except it says |
Checked commits https://github.com/djberg96/manageiq-schema/compare/19bb4ceae42c4a332570647f9394e3eac3a84149~...da0823d6e75f6b9d02a3c757da7af29003407d3c with ruby 2.5.5, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0 db/migrate/20190123145942_add_type_column_to_service_order.rb
|
say_with_time("Set service_order type") do | ||
ServiceOrder.all.each do |service_order| | ||
type = service_order.miq_requests.pluck(:type).any? { |t| t === 'ServiceTemplateTransformationPlanRequest' } ? ServiceOrderV2V : ServiceOrderCart | ||
service_order.update_attribute(:type, type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few thoughts on these changes.
add_colum
andremove_column
referenceservice_order
instead ofservice_orders
(plural)- Use
update
instead ofupdate_attribute
- Since you are manipulating the
type
column directly I would recommend disabling STI at the top of the migration withself.inheritance_column = :_type_disabled
in the class. - I recall we try to avoid active_record relationships in migration and use direct queries. In this case that would mean avoiding the
service_order.miq_requests
call. - There are references to new class types
ServiceOrderV2V : ServiceOrderCart
which are not merged yet and the migration will fail. (See [WIP] Use STI with ServiceOrder model manageiq#19766) - PR is missing migration tests.
Then I really started thinking about the changes and wanted to suggest the following migration replacement.
The basic premise is we get all the v2v Requests and do a single call to update the related ServiceOrders. Any ServiceOrders remaining will be cart orders, so we can update those by finding ones that have type => nil
.
Here's what I think the migration should look like: (
class AddTypeColumnToServiceOrder < ActiveRecord::Migration[5.0]
class ServiceOrder < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end
def up
add_column :service_orders, :type, :string
say_with_time("Set service_orders type") do
v2v_order_ids = ServiceTemplateTransformationPlanRequest.pluck(:service_order_id)
ServiceOrder.where(:id => v2v_order_ids).update_all(:type => 'ServiceOrderV2V')
ServiceOrder.where(:type => nil).update_all(:type => 'ServiceOrderCart')
end
end
def down
remove_column :service_orders, :type
end
end
This still requires that ManageIQ/manageiq#19766 gets merged and I highly recommend tests be added to this PR before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to update ManageIQ/manageiq#19766 to address the comment above (#328 (comment))
Well, we actually need a way to guarantee that this column gets set automatically when a new ServiceOrder is created, which would cover it. I think that means a before_save hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will also need some logic to make sure the v2v paths create the right kind
Closing in favor of #452 |
This adds a
type
column to theservice_order
table. The associated type would be plucked from its associated requests e.g.ServiceTemplateProvisionRequest
orServiceTemplateTransformationPlanRequest
.Attempts to solve ManageIQ/manageiq-ui-service#1463
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1565049
WIP until I can confirm how the migration should work.