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

Not working when changing column default #3

Open
felixbuenemann opened this issue Oct 2, 2017 · 1 comment
Open

Not working when changing column default #3

felixbuenemann opened this issue Oct 2, 2017 · 1 comment
Assignees

Comments

@felixbuenemann
Copy link

felixbuenemann commented Oct 2, 2017

Hey, I tried using v0.1.4 with ActiveRecord 4.2.10, but the column default is always dumped as YAML, so it appear the default override is not working, if the default is changed.

The gem works, if the default is set when the column is created, but not when trying to change it.

I also tried various combinations of change_column_default and change_column with the same result.

Sample Migration:

class AddIdentifierToProjects < ActiveRecord::Migration
  def up
    change_table :projects do |t|
      t.string :identifier
    end
    execute "UPDATE projects SET identifier = id"
    change_table :projects do |t|
      t.change :identifier, :string, null: false, default: {
        expr: "currval('projects_id_seq')"
      }
    end
  end

  def down
    change_table :projects do |t|
      t.remove :identifier
    end
  end
end

Unfortunately this migration is impossible to do without changing the column default.

@felixbuenemann
Copy link
Author

felixbuenemann commented Oct 2, 2017

I found a workaround by doing the ALTER column SET DEFAULT in SQL:

class AddIdentifierToProjects < ActiveRecord::Migration
  def up
    change_table :projects do |t|
      t.string :identifier, null: false, default: ''
    end
    execute "UPDATE projects SET identifier = id"
    execute "ALTER TABLE projects ALTER identifier SET DEFAULT currval('projects_id_seq')"
  end

  def down
    change_table :projects do |t|
      t.remove :identifier
    end
  end
end

This properly dumps the column to schema.rb as:

t.string "identifier", :default=>{:expr=>"currval('projects_id_seq'::regclass)"}, :null=>false

@urkle urkle self-assigned this May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants