rails-migrations

What is the best way to resolve Rails orphaned migrations?

◇◆丶佛笑我妖孽 提交于 2019-11-29 20:10:26
I have been switching between branches in a project and each of them have different migrations... This is the scenario: $ rake db:migrate:status Status Migration ID Migration Name -------------------------------------------------- ... up 20130307154128 Change columns in traffic capture up 20130311155109 Remove log settings up 20130311160901 Remove log alarm table up 20130320144219 ********** NO FILE ********** up 20130320161939 ********** NO FILE ********** up 20130320184628 ********** NO FILE ********** up 20130322004817 Add replicate to root settings up 20130403190042 ********** NO FILE ****

Rails migrations: Undo default setting for a column

对着背影说爱祢 提交于 2019-11-29 19:38:16
I have the problem, that I have an migration in Rails that sets up a default setting for a column, like this example: def self.up add_column :column_name, :bought_at, :datetime, :default => Time.now end Suppose, I like to drop that default settings in a later migration, how do I do that with using rails migrations? My current workaround is the execution of a custom sql command in the rails migration, like this: def self.up execute 'alter table column_name alter bought_at drop default' end But I don't like this approach, because I am now dependent on how the underlying database is interpreting

Migrating DATA - not just schema, Rails

南笙酒味 提交于 2019-11-29 11:58:16
问题 Sometimes, data migrations are required. As time passes, code changes and migrations using your domain model are no longer valid and migrations fail. What are the best practices for migrating data? I tried make an example to clarify the problem: Consider this. You have a migration class ChangeFromPartnerAppliedToAppliedAt < ActiveRecord::Migration def up User.all.each do |user| user.applied_at = user.partner_application_at user.save end end this runs perfectly fine, of course. Later, you need

Can I pass default value to rails generate migration?

社会主义新天地 提交于 2019-11-29 10:44:18
问题 I want to know if I can pass a default value to the rails g migration command. Something like: $ rails generate migration add_disabled_to_users disabled:boolean:false #where false is default value for disabled attribute in order to generate: class AddDisabledToUsers < ActiveRecord::Migration def change add_column :users, :disabled, :boolean, default: false end end 回答1: You can't: https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers null and default cannot be specified

Rails: “t.references” not working when creating index

谁都会走 提交于 2019-11-29 09:24:42
class CreateBallots < ActiveRecord::Migration def change create_table :ballots do |t| t.references :user t.references :score t.references :election t.string :key t.timestamps end add_index :ballots, :user add_index :ballots, :score add_index :ballots, :election end end results in: SQLite3::SQLException: table ballots has no column named user: CREATE INDEX "index_ballots_on_user" ON "ballots" ("user")/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change' I thought t.references was supposed to handle that for me? You forgot to add "_id" like this: add_index :ballots,

Heroku run rake db:migrate results in no change in the database, app restarted several times

会有一股神秘感。 提交于 2019-11-28 20:39:29
I have a problem with pushing my migrations to the production database. The issue: I've altered database schema by adding 1 column. I've migrated it to the production database: MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="production" [RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it. == AddLengthColumnToBooks: migrating ========================================= -- add_column(:books, :length, :integer) -> 0.0017s == AddLengthColumnToBooks: migrated (0.0019s) ================================ Thinking that the new DB schema

Rails 4: schema.db shows “Could not dump table ”events“ because of following NoMethodError# undefined method `[]' for nil:NilClass”

谁说我不能喝 提交于 2019-11-28 20:26:50
问题 I've been working on a Rails 4.0 application with sqlite (default for Rails development environment) for events (hackathons) which has a parent model, Event, for which there can be many Press_Blurbs. First I ran some scaffolding generators which created some migrations that I ran seemingly without issue: class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.string :city t.string :theme t.datetime :hackathon_start t.datetime :hackathon_end t.datetime :show_start

How do I add migration with multiple references to the same model in one table? Ruby/Rails

妖精的绣舞 提交于 2019-11-28 20:21:36
How do I create a migration with two fields that reference the same table? I have tables A, and image. A.image1_id will reference image, and A.image2_id will reference image also. There are only 2 images, not many. If I use class AddFields < ActiveRecord::Migration def change change_table(:ticket) do |t| t.references :image1_id t.references :image2_id end end end I don't think that will work because it will add another _id to the end and probably won't know to use the 'image' model. I also thought about change_table(:ticket) do |t| t.references :image But then how do I add two of those? I also

Rails migration does not change schema.rb

大憨熊 提交于 2019-11-28 20:09:44
问题 I have a rails migration that is not being applied to my schema.rb. The migration should create a table: class CreateUserGraphs < ActiveRecord::Migration def change create_table :user_graphs do |t| t.string :name t.string :content t.integer :user_id t.string :type_id t.integer :upload_id t.timestamps end add_index :user_graphs, [:user_id, :created_at] end end I did db:reset. Then I tried rake db:migrate:up VERSION=123123123(this is the migration #). I am in my "dev" environment. Why is the

How do I move a column (with contents) to another table in a Rails migration?

北城以北 提交于 2019-11-28 16:43:05
I need to move some columns from one existing table to another. How do I do it using a rails migration? class AddPropertyToUser < ActiveRecord::Migration def self.up add_column :users, :someprop, :string remove_column :profiles, :someprop end def self.down add_column :profiles, :someprop, :string remove_column :users, :someprop end end The above just creates the new columns, but values are left empty... I want to avoid logging in to the database to manually update the tables. If there is a way to move column values programmatically, what are the performance characteristics? Would it do row-by