|
| 1 | +How to migrate from 1.x.x |
| 2 | +========================= |
| 3 | + |
| 4 | +Changes in the namespace |
| 5 | +======================== |
| 6 | + |
| 7 | +First off, start by renaming your namespaces, what used to be |
| 8 | + |
| 9 | +```ruby |
| 10 | +class MyClass < Enumeration::Base |
| 11 | +``` |
| 12 | + |
| 13 | +will now be |
| 14 | + |
| 15 | +```ruby |
| 16 | +class MyClass < Enumerations::Base |
| 17 | +``` |
| 18 | + |
| 19 | +Configuration |
| 20 | +============= |
| 21 | + |
| 22 | +Since v2 has a breaking change of default values in the database columns, you have 2 options: |
| 23 | + |
| 24 | +1. Create an initializer that you will configure to have the old naming conventions |
| 25 | +2. Create data migrations and convert your data to be alligned with the new Enumerations |
| 26 | + |
| 27 | +First option might be tempting, but you need to weigh in the advantages of the new enumerations. |
| 28 | +With the second option, you can get enumerations in the database instead of ID's, which may prove quite usefull. |
| 29 | + |
| 30 | +First way |
| 31 | +========= |
| 32 | + |
| 33 | +You need to create the following initializer |
| 34 | + |
| 35 | +```ruby |
| 36 | +# config/initializers/enumerations.rb |
| 37 | + |
| 38 | +Enumerations.configure do |config| |
| 39 | + config.primary_key = :id |
| 40 | + config.foreign_key_suffix = :id |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +And voilà! You can carry on with work knowing your enumerations are up to date. |
| 45 | + |
| 46 | +Second way |
| 47 | +========== |
| 48 | + |
| 49 | +You can use a gem for the migration, or just write raw SQL. |
| 50 | + |
| 51 | +Your migrations will look something like this: |
| 52 | + |
| 53 | +First to change the column types |
| 54 | +```ruby |
| 55 | +class MoveMyEnumToNewEnumerationsColumns |
| 56 | + def up |
| 57 | + rename_column :my_table, :my_enum_id, :my_enum |
| 58 | + change_column :my_table, :my_enum, :string |
| 59 | + end |
| 60 | + |
| 61 | + def down |
| 62 | + change_column :my_table, :my_enum, 'integer USING CAST(my_enum AS integer)' |
| 63 | + rename_column :my_table, :my_enum, :my_enum_id |
| 64 | + end |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +And now for the actual data |
| 69 | + |
| 70 | +```ruby |
| 71 | +class MoveMyEnumToNewEnumerations < ActiveRecord::Migration |
| 72 | + def up |
| 73 | + execute(<<-SQL |
| 74 | + UPDATE my_table |
| 75 | + SET my_enum = |
| 76 | + CASE my_enum |
| 77 | + WHEN '1' THEN 'first' |
| 78 | + WHEN '2' THEN 'second' |
| 79 | + WHEN '3' THEN 'third' |
| 80 | + END |
| 81 | + SQL |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + def down |
| 86 | + execute(<<-SQL |
| 87 | + UPDATE my_table |
| 88 | + SET my_enum = |
| 89 | + CASE my_enum |
| 90 | + WHEN 'first' THEN '1' |
| 91 | + WHEN 'second' THEN '2' |
| 92 | + WHEN 'third' THEN '3' |
| 93 | + END |
| 94 | + SQL |
| 95 | + ) |
| 96 | +end |
| 97 | +``` |
| 98 | + |
| 99 | +And that's it, you won't need an initializer for this to work anymore, because these are the default options Enumeration gem ships with. |
0 commit comments