migration.rb.tt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
  2. <%- if migration_action == 'add' -%>
  3. def change
  4. <%# Begin Zammad customization -%>
  5. # return if it's a new setup
  6. return if !Setting.exists?(name: 'system_init_done')
  7. <%# End Zammad customization -%>
  8. <% attributes.each do |attribute| -%>
  9. <%- if attribute.reference? -%>
  10. add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
  11. <%- elsif attribute.token? -%>
  12. add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
  13. add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
  14. <%- elsif !attribute.virtual? -%>
  15. add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
  16. <%- if attribute.has_index? -%>
  17. add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
  18. <%- end -%>
  19. <%- end -%>
  20. <%- end -%>
  21. end
  22. <%- elsif migration_action == 'join' -%>
  23. def change
  24. <%# Begin Zammad customization -%>
  25. # return if it's a new setup
  26. return if !Setting.exists?(name: 'system_init_done')
  27. <%# End Zammad customization -%>
  28. create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t|
  29. <%- attributes.each do |attribute| -%>
  30. <%- if attribute.reference? -%>
  31. t.references :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
  32. <%- elsif !attribute.virtual? -%>
  33. <%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %>
  34. <%- end -%>
  35. <%- end -%>
  36. end
  37. end
  38. <%- else -%>
  39. def change
  40. <%# Begin Zammad customization -%>
  41. # return if it's a new setup
  42. return if !Setting.exists?(name: 'system_init_done')
  43. <%# End Zammad customization -%>
  44. <% attributes.each do |attribute| -%>
  45. <%- if migration_action -%>
  46. <%- if attribute.reference? -%>
  47. remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %>
  48. <%- else -%>
  49. <%- if attribute.has_index? -%>
  50. remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
  51. <%- end -%>
  52. <%- if !attribute.virtual? -%>
  53. remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
  54. <%- end -%>
  55. <%- end -%>
  56. <%- end -%>
  57. <%- end -%>
  58. end
  59. <%- end -%>
  60. end