Tag: 迁移

Rails:更改生产数据库的最佳方式

我需要对正在使用的生产数据库进行更改。 只需添加几列。 我已经通过迁移对dev数据库进行了更改。 在保留现有数据的同时更新生产数据库并且不会过多地中断操作的最佳方法是什么? 它是MYSQL,我将需要向列添加数据以及已有的记录。 一列可以有一个默认值(它是布尔值),但另一列是时间戳,应该有一个任意的后退值。 行数并不大。 因此,如果我使用迁移,我如何添加数据?如何才能实现这两个(或三个 – 我在生成数据库上添加数据时最多的迁移,而不是最初通过迁移构建的(我相信他们使用了架构而不是)?

是否存在与Rails迁移相当的PHP?

是否存在与Rails迁移相当的PHP? 寻找一个在本地配置架构更改的好解决方案,然后自动将它们碰到服务器而不会丢失数据。

添加索引:通过生成迁移,在rails上的ruby中的列是唯一的

我知道我可以触摸迁移并添加 add_index :table_name, :column_name, :unique => true 但正确的rails migration命令如何生成呢? rails g migration add_index_to_column_name :column_name, :unique => true 是对的吗? 在我的特例中,我有一个表客户 t.integer :customerID t.string :surname t.string :first_name t.string :phone 我想将customerID设置为唯一。 试着 rails g migration AddIndexToCustomers :customerID, :unique => true 但是,如果我在此之后查看我的迁移文件,它看起来不正确看到这个: def change add_column :customers, :, :customerID, add_column :customers, :, :unique add_column :customers, :=, :string end 有什么想法或建议吗?

Rails:如何将add_index添加到现有表

我已经迁移了一个名为units的表,其中有几列。 我想知道如何使用cmd将独立的“add_index”迁移到此表中。 这段代码是否正确: class AddIndexToUnits < ActiveRecord::Migration def self.up add_index :units, :lesson_id end def self.down remove :units end end 我有一种感觉自我。可能是错的,我不确定。

PostgreSQL模式的Rails迁移

我正在为不同的客户端使用PostgreSQL模式开发多租户rails应用程序。 Rails迁移不适用于开箱即用的多个模式,所以我做了以下rake任务来迁移所有模式,它似乎工作。 我的问题是,如果其他人实施了更好,更优雅的解决方案。 我也非常满意一个很好的教程,包括使用多个模式的PostgreSQL的rails代码示例。 到目前为止,我只找到了关于这个主题的一个很好的演讲http://aac2009.confreaks.com/06-feb-2009-14-30-writing-multi-tenant-applications-in-rails-guy-naor.html和我的目标是tomayko.com/writings/rails-multiple-connections desc ‘Migrates all postgres schemas’ task :schemas do # get all schemas env = “#{RAILS_ENV}” config = YAML::load(File.open(‘config/database.yml’)) ActiveRecord::Base.establish_connection(config[env]) schemas = ActiveRecord::Base.connection.select_values(“select * from pg_namespace where nspname != ‘information_schema’ AND nspname NOT LIKE ‘pg%'”) puts “Migrate schemas: #{schemas.inspect}” # migrate each schema schemas.each do |schema| puts “Migrate schema: #{schema}” config […]

向现有Rails模型添加模型引用

我想知道在Rails 3中添加两个现有类之间关系的“正确”方法。 鉴于现有的模型:小丑和兔子 我想添加一个从Rabbit到Clown的引用(belongs_to)。 我首先尝试生成迁移: rails g migration AddClownToRabbits clown:reference 这给了我一个看起来像这样的迁移: class AddClownToRabbits < ActiveRecord::Migration def self.up add_column :rabbits, :clown, :reference end def self.down remove_column :rabbits, :clown end end 在rake db:migrate迁移之后,我检查SQLite3的development.db并看到一个新列: “clown” reference 我想我期待一个”clown_id” integer列和一个看起来像这样的迁移: class AddClownToRabbits < ActiveRecord::Migration def self.up add_column :rabbits, :clown_id end def self.down remove_column :rabbits, :clown_id end end 我敢肯定:引用应该等同于“t.references:clown”,但我找不到文档(大惊喜)。 API表示add_column: Instantiates […]

PGError:错误:关系“用户”的列“电子邮件”已存在

我一直在开发一个关于localhost的网站,它很好。 今天早上,我尝试使用命令“git push heroku master”将其推送到heroku,然后“heroku run rake db:migrate”。 当我尝试做第二个时,我有一个错误: Connecting to database specified by DATABASE_URL Migrating to DeviseCreateUsers (20130427200347) Migrating to CreateAuthentications (20130427210108) Migrating to AddTokenToAuth (20130427233400) Migrating to AddNotificationToAuth (20130427234836) Migrating to AddNotifToUser (20130428031013) Migrating to AddDeviseToUsers (20130712103048) == AddDeviseToUsers: migrating =============================================== — change_table(:users) rake aborted! An error has occurred, this and all later […]

Rails迁移具有相同的名称

人们如何优雅地应对这个问题? 我有一个创建一个表的迁移,然后另一个删除该表,现在我想重新引入它,问题是我不能,因为他们有相同的名称。

Rails db:migrate关系不存在

我有这样的模特: 学生: class Student < ActiveRecord::Base has_many :participations, dependent: :destroy has_many :subject_item_notes, dependent: :destroy has_many :payments, dependent: :destroy has_many :subject_items, dependent: :destroy, through: :participations has_many :subject_items, dependent: :destroy validates :first_name, :last_name, presence: true accepts_nested_attributes_for :subject_items end 和subject_item: class SubjectItem (teacher) { where(‘teacher_id IS ? or teacher_id = ?’, nil, teacher) } end 和迁移: class AddStudentToSubjectItems […]

我应该展平Rails迁移吗?

可以将db / migrate / *替换为db / schema.rb的内容,这样您只需要一个迁移步骤。 你们有没有这样做过? 为什么?