简体   繁体   English

schema.rb中的Git数据库更改

[英]Git database changes in schema.rb

So there are 2 relevant branches. 所以有2个相关的分支机构。 notifications and shipping_options . 通知shipping_options

I checked notifications out of master , then made 2 significant database changes: 我检查了master notifications ,然后进行了两次重要的数据库更改:

  • Created GeneralNotice model 创建了GeneralNotice模型
  • Modified existing Notice model, by adding a boolean dismissed 修改现有的 Notice模型,通过添加一个dismissed的布尔值

Then shipping_options was also checked out of master at the same state as notifications , and no database changes were made, only minor code changes. 然后, shipping_options也在与notifications相同的状态下从主服务器中检出,并且没有进行数据库更改,只有少量代码更改。

I ran rake db:migrate the first time switching from notifications to shipping_options and the same vice versa. 我运行rake db:migrate第一次从notifications切换到shipping_options ,反之亦然。

At the moment, when I am in notifications , and run rails c I get the following: 目前,当我在notifications ,并运行rails c我得到以下内容:

2.0.0-p451 :001 > Notice
 => Notice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime, kind: string, general_notice_id: integer, shop_id: integer, dismissed: boolean) 

2.0.0-p451 :002 > GeneralNotice
 => GeneralNotice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime) 
2.0.0-p451 :003 > 
  • Notice has dismissed (as expected) 通知已dismissed (如预期)
  • GeneralNotice exists (as expected) GeneralNotice存在(如预期的那样)

Then i switch to shipping_options and I hope you have been following because here comes the confusing part... 然后我切换到shipping_options ,我希望你一直关注,因为这里有令人困惑的部分......

2.0.0-p451 :001 > Notice
 => Notice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime, kind: string, general_notice_id: integer, shop_id: integer, dismissed: boolean) 

2.0.0-p451 :002 > GeneralNotice
NameError: uninitialized constant GeneralNotice
    from (irb):2
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
2.0.0-p451 :003 > 
  • GeneralNotice doesn't exist, so apparently git is being very nice and helpful by changing the database according to my branch GeneralNotice不存在,所以通过根据我的分支更改数据库显然git是非常好的和有用的
  • Notice still has a boolean dismissed ... Whaaaat???? Notice 仍然有一个布尔dismissed ... Whaaaat ???? Is git being nice or not?? git是好还是不好? HELP!!!!!! 救命!!!!!!
  1. Since you've run the migration in the first branch (notifications), the column was added to the database table, hence it'll show if you inspect. 由于您已在第一个分支(通知)中运行迁移,因此该列已添加到数据库表中,因此它将显示您是否进行检查。 inspect method works with the database table directly, reflecting the columns (attributes). inspect方法直接使用数据库表,反映列(属性)。

  2. GeneralNotice: if you check your database, it would contain the table (general_notices) but since the file general_notice.rb in which GeneralNotice is defined is not present in this branch, it throws uninitialized constant error. GeneralNotice:如果检查数据库,它将包含表(general_notices),但由于定义了GeneralNotice的文件general_notice.rb在此分支中不存在,因此会抛出未初始化的常量错误。

Have a look at this and this for some ideas to put your db under version control. 看看这个以及这个有关将数据库置于版本控制之下的一些想法。

Whenever you switch branch, and branches might contain set of different migrations it is wise to run rake db:reset which reloads database schema from schema.rb file. 每当你切换分支,并且分支可能包含一组不同的迁移时,最好运行rake db:reset ,它从schema.rb文件重新加载数据库模式。

In order to be sure your schema.rb is up-to-date in all branches it is good idea to run rake db:migrate:reset whenever you merge branches with new migrations. 为了确保您的schema.rb在所有分支中都是最新的,每当您将分支与新迁移合并时,最好运行rake db:migrate:reset This rake task runs all your migrations from the very beginning and writes resulting database scheme to a file. 此rake任务从一开始就运行所有迁移,并将生成的数据库方案写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM