前言

同事为了尝试gitlab的新功能,在我不在场的情况下按照官方文档,独自升级了gitlab,按理说正常升级是不会有问题的,但偏偏就出问题了……问题出在rake db:migrate


正文

升级步骤参见官方文档,本文只针对升级中可能出现的问题做解决方案


问题

在执行到rake db:migrate 时会报错,大概意思是说在Gemfile中没有找到mysql2,具体如下

#sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production
rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded.
 Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).


解决方案

报错的同时给出了操作建议,将`gem 'mysql2'`增加到Gemfile文件中。

在Gemfile中查找mysql2的时候发现已经存在此条目,并且gem install mysql2 也是可以安装的,有说需要rails 的版本大于4.0。


这里需要注意,Gemfile 文件已经将需要的依赖包以及版本都写好了,直接使用bundle install就可以,所以应该是不存在版本问题。这里注意到 gem "mysql2", '~> 0.3.16', group: :mysqlgroup 声明,有没有可能是因为group声明的问题,于是将, group: :mysql 注释,在执行果然可以了。

#sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production


问题

不过到这里可能又出新的问题,命令更新数据库的过程中,可能更新到一半就更新不下去了,报类似如下的错误

rake db:migrate

== 20140705105820 CreateStructure: migrating ==================================
-- create_table("users")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "users" already exists

rake db:migrate== 20150616125848 AddFieldsToMovies: migrating ================================-- add_column(:movies, :description, :description)
   -> 0.0006s-- add_column(:movies, :release_date, :datetime)
   -> 0.0003s== 20150616125848 AddFieldsToMovies: migrated (0.0009s) =======================


解决方案

这里面有一个问题,有说rake db:migrate不能多次执行,我没有研究过ruby 可能存在这种情况,但这里出现的原因可能并不是因为rake db:migrate 被执行了多次。


这里需要注意的是,升级gitlab到一个新版本的时候,当前的gitlab也必须处于当前最新版本,如当前gitlab 版本为8.5.1 而8.5.x 这个最新版本为8.5.13 ,这就需要先将gitlab 从8.5.1 升级到 8.5.13 ,然后才能将gitlab 从8.5.x 版本升级到8.6.x的版本,否则就有可能出现上面的问题。


总结

gitlab 升级的过程中好像不能跨版本升级,同时升级的时候要保证gitlab处于当前版本的最新小版本状态。


在更新完数据库后,可以通过以下命令查看更新状态

#sudo -u git -H bundle exec rake db:migrate:status RAILS_ENV=production

显示为up 状态的表明已经更新,如果为down 则表明未更新

database: gitlabhq_production

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20121220064453  Init schema
   up     20130102143055  Rename owner to creator for project
   up     20130110172407  Add public to project
   up     20130123114545  Add issues tracker to project
   up     20130125090214  Add user permissions
   up     20130131070232  Remove private flag from project
   up     20130206084024  Add description to namsespace
   up     20130207104426  Add description to teams
   down     20130211085435  Add issues tracker id to project

对ruby 不熟,对rake db:migrate 具体的解释请参见

http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload/10302357#10302357