Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

sqlite3 adapter drops :decimal columns precision & scale when migration tries to alter them

#2872

example migrations:

class M1 < ActiveRecord::Migration
  def self.up

create_table 'users' do |t|
  t.string 'name'
  t.decimal 'v1', :precision =&gt; 10, :scale =&gt; 3
end



end
def self.down
drop_table 'users'



end end
class M2 < ActiveRecord::Migration def self.up
change_column :users, :v1, :decimal, :precision =&gt; 12, :scale =&gt; 5



end
def self.down end end

expected resulting schema:

ActiveRecord::Schema.define(:version => 20090706090649) do
  create_table "users", :force => true do |t|

t.string  &quot;name&quot;
t.decimal &quot;v1&quot;,   :precision =&gt; 12, :scale =&gt; 5



end end

actual resulting schema:

ActiveRecord::Schema.define(:version => 20090706090649) do
  create_table "users", :force => true do |t|

t.string  &quot;name&quot;
t.decimal &quot;v1&quot;



end end

Reported by Andrey "Zed" Zaikin · July 6th, 2009 @ 10:42 AM

State: incomplete
Milestone: 3.0.6
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: none

Activity

  1. Andrey "Zed" Zaikin
    Andrey "Zed" Zaikin
    • Tag changed from migrations, patch, schema, sqlite3 to activerecord, active_record, migrations, patch, schema, sqlite3

    July 6th, 2009 @ 10:46 AM

  2. Taryn
    Taryn

    I've hit this problem too.
    Looking into the sqlite adaptor code, you can see that it's simply dropping any+all options.

    July 6th, 2009 @ 10:49 AM

  3. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • Milestone cleared.
    • State changed from new to incomplete
    • Assigned user set to Yehuda Katz (wycats)

    Any chance I could get you to include a new test case for this?

    July 8th, 2009 @ 12:12 AM

  4. Paul Hinze
    Paul Hinze

    Here's a test case that fails without the above patch and passes when it is applied. It tests that you can use change_column to alter a column's scale and precision. The attributes are dropped in the code without this patch, and they are properly changed when it is applied.

    July 20th, 2009 @ 07:43 PM

  5. Taryn East
    Taryn East

    Ok, when I click on Paul's patch I get some sort of key error, so here's my own patch that includes a test that fails with the current Rails code, as well as Adnrey's code - which makes it pass.

    Note - I've made the test-case specific to sqlite3 as that's where the issue is located.

    July 22nd, 2009 @ 12:19 PM

  6. Taryn East
    Taryn East

    I meant to add... but technically the test should pass on all adapters. Only I can't test Postgres - because I don't have that installed.
    it runs fine for all the other standard adapters ie it doesn't fail when I remove the adapter-check line except for the postgres section which doesn't run at all.

    July 22nd, 2009 @ 12:27 PM

  7. Taryn East
    Taryn East

    Ok - that's weird. Looks like running a migration with this sets the local values for precision/scale... but doesn't actually change the real db properly.
    You can test that with a migration that changes two columns' precisions:

    class M£ < ActiveRecord::Migration
    def self.up

    change_column :users, :v1, :decimal, :precision => 12, :scale => 5
    change_column :users, :v2, :decimal, :precision => 12, :scale => 5
    

    end

    def self.down end end

    expected resulting schema:

    ActiveRecord::Schema.define(:version => 20090706090649) do
    create_table "users", :force => true do |t|

    t.string  "name"
    t.decimal "v1",   :precision => 12, :scale => 5
    t.decimal "v2",   :precision => 12, :scale => 5
    

    end end

    actual resulting schema:

    ActiveRecord::Schema.define(:version => 20090706090649) do
    create_table "users", :force => true do |t|

    t.string  "name"
    t.decimal "v1"
    t.decimal "v2",   :precision => 12, :scale => 5
    

    end end

    If you reverse the order of two change_column names in the migration... you get:

    ActiveRecord::Schema.define(:version => 20090706090649) do
    create_table "users", :force => true do |t|

    t.string  "name"
    t.decimal "v1",   :precision => 12, :scale => 5
    t.decimal "v2"
    

    end end

    ie - only the last migration seems to actually change the schema...

    July 22nd, 2009 @ 01:27 PM

  8. Taryn East
    Taryn East

    hmmm... and "edit last comment" feature would be appreciated.. ;)

    July 22nd, 2009 @ 01:28 PM

  9. Taryn East
    Taryn East

    Hokay... so after much digging there is one important fact we need to remember.
    SQLite doesn't actually (natively) support :precision/scale.

    The troubles that my company ran into were more based on the fact that in doing the migration... the schema lost the precision/scale - which means that when we uploaded into a NON-sqlite instance of the db... we then ran into issues.

    I'm not sure how to test for this as we don't have Rails tests (that I see) where we dump the schema from one adaptor, then try to schema:load into another.

    In any case, I ran across one other place where the precision/scale are "lost" in the sqlite adaptor... adding the following patch seemed to solve that... but I can't officially test it.

    Any comments/suggestions welcome.

    July 22nd, 2009 @ 05:04 PM

  10. Matt Kern
    Matt Kern

    So if I understand this correctly, the schema should retain the precision and scale, but the sqlite3 adapter should ignore it when creating the columns, correct?

    August 7th, 2009 @ 12:54 AM

  11. Bryce Thornton
    Bryce Thornton

    I've ran into this as well. Yes, it would be nice for the schema to retain the precision and scale for when we actually load it on a production database.

    August 9th, 2009 @ 08:38 PM

  12. Matt Kern
    Matt Kern

    I think it's more than nice, it's required. The recommendation to load from schema rather than running thru the migrations on a production deploy or any new instance deploy makes this pretty high priority.

    August 9th, 2009 @ 08:51 PM

  13. Taryn East
    Taryn East

    Matt - yep - that's the ticket.
    The patches I've submitted do this now.

    We ran into the issue because during testing we used Sqlite - but production (of course) is on a real db... and loading the schema started causing all sorts of truncation errors in our money columns :P

    August 10th, 2009 @ 10:47 AM

  14. Dan Pickett
    Dan Pickett
    • Tag changed from activerecord, active_record, migrations, patch, schema, sqlite3 to activerecord, active_record, bugmash, migrations, patch, schema, sqlite3

    May 9th, 2010 @ 06:23 PM

  15. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • Importance changed from to

    August 30th, 2010 @ 04:10 AM

  16. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:02 PM

  17. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Title changed from [PATCH] sqlite3 adapter drops :decimal columns precision & scale when migration tries to alter them to sqlite3 adapter drops :decimal columns precision & scale when migration tries to alter them
    • Tag set to activerecord, patch, sqlite3

    Using the "patch" tag instead of prefixing the ticket title with "[PATCH]" to make sure patched tickets end up in the open patches bin. :)

    October 10th, 2010 @ 01:04 PM

  18. Jeremy Kemper
  19. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    November 7th, 2010 @ 04:53 PM

  20. Santiago Pastorino
  21. Santiago Pastorino
  22. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.0.5 to 3.0.6

    February 27th, 2011 @ 03:15 AM