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

Remove :_type_disabled for models in migrations, where type column doesn't exist #7354

Conversation

lpichler
Copy link
Contributor

@lpichler lpichler commented Mar 17, 2016

related #6739
based on comments:
#5554 (comment) or #6739 (comment)

this script helpded me:

# prepare
system("mkdir db/migrate_temp")
print "."
Dir.glob("db/migrate/*").each do |f|
  next if File.directory?(f)
  next if f.include?("20130923182042_collapsed_initial_migration.rb")
  f_with_new_path = f.gsub('migrate', 'migrate_temp')
  system("mv #{f} #{f_with_new_path}")
  print "."
end
system("rake db:migrate &>/dev/null")
puts ".!"
def get_migration_info(f)
  type_column_exists = lambda { |t| ActiveRecord::Base.connection.column_exists?(t, :type) }
  table_exist = lambda { |t| ActiveRecord::Base.connection.data_source_exists?(t) }
  sti_disabled_in_stub = lambda do |content|
    content.include?("self.inheritance_column = :_type_disabled")
  end
  matches = File.read(f).scan(/\s+class (.*) < ActiveRecord::Base([\s\S]*?)end/)
  if matches.present?
    out_array = []
    matches.each do |match|
      klass_stub, klass_content = match
      tbl_exist                 = table_exist.call(klass_stub.tableize.to_sym)
      sti_dis_in_stub           = sti_disabled_in_stub.call(klass_content)
      sti_should_be_disabled    = tbl_exist ? type_column_exists.call(klass_stub.tableize.to_sym) : false

      unless sti_should_be_disabled == sti_dis_in_stub
        out_array.push({:f => f, :klass_stub    => klass_stub,
                        :table_exist            => tbl_exist,
                        :sti_disabled_stub      => sti_dis_in_stub,
                        :sti_should_be_disabled => sti_should_be_disabled})
      end
    end
    out_array
  else
    []
  end
end
i = 1
cnt = Dir.glob("db/migrate_temp/*").count
Dir.glob("db/migrate_temp/*").each do |f|
  next if File.directory?(f)

  f_with_new_path = f.gsub('migrate_temp', 'migrate')
  system("cp #{f} #{f_with_new_path}")
  system("rake db:migrate &>/dev/null")
  reload!
  migration_info = get_migration_info(f_with_new_path)
  if migration_info.present?
    migration_info.map! do |klass_stub|
      klass_stub[:suggestion] = klass_stub[:sti_should_be_disabled] ? "ADD it -> " : "REMOVE it -> "
      klass_stub[:where] = "#{klass_stub[:f]} for #{klass_stub[:klass_stub]}"
      puts klass_stub[:suggestion] + klass_stub[:where]
    end
    puts "."
  end
  print "*" + (cnt - i).to_s
  i = i + 1
end

cc @Fryguy @jrafanie

@miq-bot miq-bot added the wip label Mar 17, 2016
@lpichler lpichler force-pushed the remove_type_disabled_where_it_is_not_needed branch from c72967f to 64629cb Compare March 18, 2016 17:30
@lpichler lpichler changed the title [WIP] Remove :_type_disabled for models in migrations, where type column doesn't exist Remove :_type_disabled for models in migrations, where type column doesn't exist Mar 18, 2016
@miq-bot miq-bot removed the wip label Mar 18, 2016
class OperatingSystemFlavor < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end
class OperatingSystemFlavor < ActiveRecord::Base; end
Copy link
Member

Choose a reason for hiding this comment

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

Really surprised on this one :)

@Fryguy
Copy link
Member

Fryguy commented Mar 18, 2016

@lpichler So does your script there check based on the latest tables, or what the state of the tables are at that time.

@lpichler lpichler force-pushed the remove_type_disabled_where_it_is_not_needed branch from 64629cb to 64af459 Compare April 1, 2016 12:40
@lpichler
Copy link
Contributor Author

lpichler commented Apr 1, 2016

@Fryguy no but now yes, I updated it in way that it based on state of tables at that time.

I also removed self.inheritance_column = :_type_disabled from MigrationStubHelper because only ExtManagementSystem(20131107000917_expand_dialog_field_default_value_size.rb) and DialogField(
20150407144345_add_kerberos_to_ext_management_system.rb) have needed it

@miq-bot
Copy link
Member

miq-bot commented Apr 1, 2016

Checked commit lpichler@64af459 with ruby 2.2.3, rubocop 0.37.2, and haml-lint 0.16.1
60 files checked, 0 offenses detected
Everything looks good. 🏆

end

class FileDepot < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI
Copy link
Member

Choose a reason for hiding this comment

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

We might want to keep this for now...

The migration on the following day adds a type column:

db/migrate/20140410132430_subclass_file_depot_by_protocol.rb-  def up
db/migrate/20140410132430_subclass_file_depot_by_protocol.rb:    add_column :file_depots, :type, :string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but when was this migration created the column was not here, so should I respect state of DB in that time or state of latest DB ?

Copy link
Member

Choose a reason for hiding this comment

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

@lpichler When this migration was created, it would have been possible to run these migrations out of order if you were the author of the other migration. Since we're long past this, I think it's fine to drop this as you have done... 👍

@jrafanie
Copy link
Member

jrafanie commented Apr 5, 2016

@lpichler @matthewd @Fryguy I wonder if the migration specs could monkey patch/module prepend AR::Base's inheritance_column = to check if the table for the stub model already has a type column and warn about it if not (calling super regardless)

@jrafanie jrafanie merged commit ba4a1d5 into ManageIQ:master Apr 5, 2016
@jrafanie jrafanie added this to the Sprint 39 Ending Apr 18, 2016 milestone Apr 5, 2016
@lpichler lpichler deleted the remove_type_disabled_where_it_is_not_needed branch April 5, 2016 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants