rails-migrations

Database Specific Migration Code [duplicate]

流过昼夜 提交于 2019-11-30 20:45:33
问题 This question already has answers here : How do I check the Database type in a Rails Migration? (5 answers) Closed 4 years ago . I'm creating an application that needs to run under multiple databases. I currently have some code in a migration that I only want run under specific databases (postgresql and mysql). Any way of setting this up? Thanks. 回答1: Your migration has access to a database connection in connection and the connection has an adapter_name method so you can just ask it what sort

'Connect' a rails app to an already existing MySQL DB?

有些话、适合烂在心里 提交于 2019-11-30 20:36:25
问题 So in my company we are slowly moving to Rails instead of PHP(Code Igniter to be precise). So, our actual PHP App is using a Mysql DB and I'd like to connect a new Rails app to this DB but meanwhile our PHP is still running, so I can't change the DB. I don't really know where I should start to use all the rails features (Or at least as much as possible). 回答1: There shouldn't be any harm in connecting your rails app to an existing database. You will need to watch for anything that goes against

How to add sequences to a migration and use them in a model?

限于喜欢 提交于 2019-11-30 11:09:53
I want to have a " Customer " Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use PostgreSQL. Have a look at my migration: class CreateAccountsCustomers < ActiveRecord::Migration def up say "Creating sequenze for customer number starting at 1002" execute 'CREATE SEQUENCE customer_no_seq START 1002;' create_table :accounts_customers do |t| t.string :type t.integer :customer_no, :unique => true t.integer :salutation, :limit => 1 t

Migrating DATA - not just schema, Rails

会有一股神秘感。 提交于 2019-11-30 08:29:56
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 a schema change class AddAcceptanceConfirmedAt < ActiveRecord::Migration def change add_column :users,

Can I pass default value to rails generate migration?

一世执手 提交于 2019-11-30 08:09:48
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 You can't: https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers null and default cannot be specified via command line. Rails migration generator does not handle default values, but after generation of

has_many, belongs_to relation in active record migration rails 4

泄露秘密 提交于 2019-11-30 06:13:37
问题 I have created a User model and later a Task model. I have not mentioned any relation between them while creating. I understand that User has_many Tasks and a Task belongs_to User . I need to establish this relation between them through migration. My question is, what would be the migration generation command for establishing that relation? Any help would be highly appreciated. 回答1: You could call: rails g model task user:references which will generates an user_id column in the tasks table

How to drop columns using Rails migration

廉价感情. 提交于 2019-11-30 06:09:43
问题 What's the syntax for dropping a database table column through a Rails migration? 回答1: remove_column :table_name, :column_name For instance: remove_column :users, :hobby would remove the hobby Column from the users table. 回答2: For older versions of Rails ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype For Rails 3 and up rails generate migration RemoveFieldNameFromTableName field_name:datatype 回答3: Rails 4 has been updated, so the change method can be used in

Why can't I create an array as a column in a table in Rails?

吃可爱长大的小学妹 提交于 2019-11-30 05:40:40
问题 Why can't I do something like this: class CreateModels < ActiveRecord::Migration def self.up create_table :fruit do |t| t.array :apples end end end Is there some other way to make an array ("apples) be an attribute of an instance of the Fruit class? 回答1: Check out the Rails guide on associations (pay particular attention to has_many). You can use any column type supported by your database (use t.column instead of t.type ), although if portability across DBs is a concern, I believe it's

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

本秂侑毒 提交于 2019-11-29 23:35:08
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 t.datetime :show_end t.text :about t.string :hack_rsvp_url t.string :show_rsvp_url t.timestamps end

Rails migration does not change schema.rb

荒凉一梦 提交于 2019-11-29 23:03:10
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 migration not affecting schema.rb? From the documentation : The rake db:reset task will drop the database