mini_record
mini_record copied to clipboard
Auto changing column type don't work on PostgreSQL
Hello. It's look like changing column type is not working at PostgreSQL. For example:
class Some < ActiveRecord::Base
field :super_field, as: :integer
auto_upgrade!
end
Works well. Then I change :integer to :text, reload app and Some class... And nothing happend. :( Is it for me only or it's a bug?
Any other changings (adding, removing columns etc.) works perfectly.
I can reproduce this issue. I'm using MySQL though with the mysql2 adapter.
Reproduction Steps I created a dummy class
class Foobar < ActiveRecord::Base
field :name, as: :string
field :data, as: :integer
end
I ran auto_upgrade!
irb(main):001:0> Foobar => Foobar(Table doesn't exist) irb(main):002:0> Foobar.auto_upgrade! [MiniRecord] Creating Table foobars (542.8ms) CREATE TABLE `foobars` (`id` int(11) auto_increment PRIMARY KEY) ENGINE=InnoDB [MiniRecord] Adding column foobars.name (514.5ms) ALTER TABLE `foobars` ADD `name` varchar(255) [MiniRecord] Adding column foobars.data (444.6ms) ALTER TABLE `foobars` ADD `data` int(11) => nil
I changed the type to text
class Foobar < ActiveRecord::Base
field :name, as: :string
field :data, as: :text
end
I ran auto_upgrade! again
irb(main):003:0> Foobar.auto_upgrade! => nil
I double check column type in MySQL and it is still an integer.
MariaDB [test_app_development]> describe foobars; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | data | int(11) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+
I found the issue and I am able to get it to change the column type. I will submit a PR soon.
You can ignore my puts statements prefaced with "[JAMES]".
irb(main):024:0> Foobar.auto_upgrade!
[JAMES] ---- TEST
[JAMES] id
[JAMES] name
[JAMES] new_attr: {}, changed: false
[JAMES] new_type: string
[JAMES] data
[MiniRecord] Detected schema change for foobars.data#type from :integer to :text
[JAMES] new_attr: {:type=>:text}, changed: true
[JAMES] new_type: text
[MiniRecord] Changing column foobars.data to new type text
(721.5ms) ALTER TABLE `foobars` CHANGE `data` `data` text DEFAULT NULL
=> nil
Now I look at my DB and the column is text now!
MariaDB [test_app_development]> describe foobars; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | data | text | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+
@burnt43, thank you very much! It's amazing responce speed! I'll wait for a new release with your PR! :)
@Xanders, if you need this quickly you can update your Gemfile and point it to my github burnt43/mini_record until the PR is fix and accepted.
@burnt43, thank you for a tip! :) I don't need it so quickly. :)