LoginSignup
0
0

More than 1 year has passed since last update.

データベースのロールバックやエラー解決法

Last updated at Posted at 2022-02-12

ロールバック

https://konaga-k.hatenablog.com/entry/2021/09/19/103446

バージョン確認

https://qiita.com/akilax/items/7c564590b59908245dd3

$ bundle exec rails db:migrate:status
database: development

 Status   Migration ID    Migration Name
--------------------------------------------------
・・・
      up     20210509061758  Add column editor info to edit histories
   up     20210509072450  Create style histories
   up     20210509090511  Create script histories
   up     20210509094943  Create actual object histories
   up     20210509114438  Create operation histories
   up     20210724120447  ********** NO FILE **********
   up     20210724132142  ********** NO FILE **********
   up     20210724134840  ********** NO FILE **********
   up     20210724141803  ********** NO FILE **********
   up     20210731045833  ********** NO FILE **********
   up     20210731110205  ********** NO FILE **********
   up     20210822092025  Add column page type to pages
   up     20210828114146  ********** NO FILE **********

データベース上には存在してるけどそのmigrationファイルは存在していないものがある

NO FILE のmigrationを削除する

ロールバックではなく、ピンポイントで過去のmigrationをdownにしてmigrationファイルを削除することでデータベースからなくす方法を取る

https://qiita.com/sakatan_1/items/9bf321f81d3b84042694

# ダミーファイルを作成
$ touch db/migrate/20210828114146_hoge.rb
# db/migrate/20210828114146_hoge.rb
class Hoge < ActiveRecord::Migration[5.2]
  def change
  end
end
# migration nameが付与されていることを確認する
$ bundle exec rails db:migrate:status
database: development

 Status   Migration ID    Migration Name
--------------------------------------------------
・・・
   up     20210724120447  ********** NO FILE **********
   up     20210724132142  ********** NO FILE **********
   up     20210724134840  ********** NO FILE **********
   up     20210724141803  ********** NO FILE **********
   up     20210731045833  ********** NO FILE **********
   up     20210731110205  ********** NO FILE **********
   up     20210822092025  Add column page type to pages
   up     20210828114146  Hoge
$ bundle exec rails db:migrate:down VERSION=20210828114146
== 20210828114146 Hoge: reverting =============================================
== 20210828114146 Hoge: reverted (0.0047s) ====================================

# down(rails db:migrate実行前)になっていることを確認する
$ bundle exec rails db:migrate:status
database: development

 Status   Migration ID    Migration Name
--------------------------------------------------
・・・
   up     20210724120447  ********** NO FILE **********
   up     20210724132142  ********** NO FILE **********
   up     20210724134840  ********** NO FILE **********
   up     20210724141803  ********** NO FILE **********
   up     20210731045833  ********** NO FILE **********
   up     20210731110205  ********** NO FILE **********
   up     20210822092025  Add column page type to pages
 down     20210828114146  Hoge

# 必要なくなったので削除する
$ rm db/migrate/20210828114146_hoge.rb
$ bundle exec rails db:migrate:status
database: development

 Status   Migration ID    Migration Name
--------------------------------------------------
・・・
   up     20210724120447  ********** NO FILE **********
   up     20210724132142  ********** NO FILE **********
   up     20210724134840  ********** NO FILE **********
   up     20210724141803  ********** NO FILE **********
   up     20210731045833  ********** NO FILE **********
   up     20210731110205  ********** NO FILE **********
   up     20210822092025  Add column page type to pages

cuctomizable_groupブランチ

https://bokunonikki.net/post/2018/0214_rails_mysql_error/

db:migrateで失敗した。「Mysql2::Error: Table '' already exists」

テーブルを削除することで対応

class CreateCustomizableGroups < ActiveRecord::Migration[5.2]
  def change
    create_table :customizable_groups do |t|
      t.string :name

      t.timestamps
    end
  end
end
$ mysql -u test -p
Enter password: password

> show databases;
> use development;
> show tables;
> drop table customizable_groups;
> exit

「Mysql2::Error: Duplicate column name・・・」エラーの解決法

https://qiita.com/sho-17/items/4cc6f416f9ebd47ef078

以下のmigrationファイルのdef changeの中身を一旦空にしてからdb:migrateすると通る。その後元に戻しておく。今後は通るそうです。

# 20210724132142_add_column_to_customizable_group.rb
class AddColumnToCustomizableGroup < ActiveRecord::Migration[5.2]
  def change
    add_column :customizable_groups, :tenant_id, :integer
  end
end
# 20210724134840_add_column_to_customizable_group2.rb
class AddColumnToCustomizableGroup2 < ActiveRecord::Migration[5.2]
  def change
    add_column :customizable_groups, :sort, :integer
  end
end
# 20210724141803_add_column_to_customizable_object2.rb
class AddColumnToCustomizableObject2 < ActiveRecord::Migration[5.2]
  def change
    add_column :customizable_objects, :customizable_group_id, :integer
  end
end
# 20210731110205_change_column_default_customizable_objects.rb
class ChangeColumnDefaultCustomizableObjects < ActiveRecord::Migration[5.2]
  def change
    change_column_default :customizable_objects, :customizable_group_id, from: 0, to: nil
  end
end

rollbackをしてdownにしてからブランチを切り替える

# migrationのstatusを見ると以下のようになる
      up     20210509114438  Create operation histories
   up     20210724120447  Create customizable groups
   up     20210724132142  Add column to customizable group
   up     20210724134840  Add column to customizable group2
   up     20210724141803  Add column to customizable object2
   up     20210731110205  Change column default customizable objects
   up     20210822092025  ********** NO FILE **********

NO FILEになっているmigrationがどのブランチのものなのかわからない

とりあえず削除しておく

$ touch db/migrate/20210822092025_hoge.rb
# ファイルの中身をダーミーとして作成しておく
$ bundle exec rails db:migrate:down VERSION=20210822092025
$ rm db/migrate/20210822092025_hoge.rb
$ bundle exec rails db:migrate:status
・・・
   up     20210509114438  Create operation histories
   up     20210724120447  Create customizable groups
   up     20210724132142  Add column to customizable group
   up     20210724134840  Add column to customizable group2
   up     20210724141803  Add column to customizable object2
   up     20210731110205  Change column default customizable objects

ロールバックしてテーブルを元に戻しておく

$ bundle exec rails db:rollback STEP=5
== 20210731110205 ChangeColumnDefaultCustomizableObjects: reverting ===========
-- change_column_default(:customizable_objects, :customizable_group_id, {:from=>nil, :to=>0})
   -> 0.0363s
== 20210731110205 ChangeColumnDefaultCustomizableObjects: reverted (0.0393s) ==

== 20210724141803 AddColumnToCustomizableObject2: reverting ===================
-- remove_column(:customizable_objects, :customizable_group_id, :integer)
   -> 0.0640s
== 20210724141803 AddColumnToCustomizableObject2: reverted (0.0644s) ==========

== 20210724134840 AddColumnToCustomizableGroup2: reverting ====================
-- remove_column(:customizable_groups, :sort, :integer)
   -> 0.0425s
== 20210724134840 AddColumnToCustomizableGroup2: reverted (0.0428s) ===========

== 20210724132142 AddColumnToCustomizableGroup: reverting =====================
-- remove_column(:customizable_groups, :tenant_id, :integer)
   -> 0.0625s
== 20210724132142 AddColumnToCustomizableGroup: reverted (0.0629s) ============

== 20210724120447 CreateCustomizableGroups: reverting =========================
-- drop_table(:customizable_groups)
   -> 0.0233s
== 20210724120447 CreateCustomizableGroups: reverted (0.0234s) ================

これでテーブルが元に戻ったことを確認した

v5/load_positionブランチ

schema.rb

create_table "pages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
    t.integer "tenant_id", null: false
    t.integer "directory_id"
    t.integer "approval_route_id"
    t.integer "page_type", default: 1, null: false
    t.string "name"
    t.string "source_url"
    t.string "page_code"
    t.string "title"
    t.text "description"
    t.text "keywords"
    t.string "canonical"
    t.text "domains"
    t.string "access_url"
    t.integer "require_login", default: 0, null: false
    t.integer "is_smp_page", default: 0
    t.integer "is_made_of_automatic", default: 0
    t.integer "version", default: 0
    t.integer "public_status", default: 0
    t.integer "conversion", default: 0
    t.boolean "is_ec_product_detail", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "view_js_position"
    t.integer "view_css_position"
    t.index ["tenant_id", "page_type"], name: "index_page_type_on_pages"
  end

追加したmigrationファイル

db/migrate/20200811080315_add_viewjs_position_to_pages.rb
db/migrate/20200903053736_add_viewcss_position_to_pages.rb

customizable_object_values_limitブランチのrollback

change_columnを使用しているときはupとdownのメソッドを使用してからrollbackする

https://qiita.com/s_n_o_w/items/5aa2d5d14d650b1955cf

class ChangeDatatypeObjectComponentValues < ActiveRecord::Migration[5.2]
  def up
    change_column :object_component_values, :value, :text, :limit => 4294967295
  end
  def down
    change_column :object_component_values, :value, :text
  end
end
$ bundle exec rails db:rollback
== 20210731045833 ChangeDatatypeObjectComponentValues: reverting ==============
-- change_column(:object_component_values, :value, :text)
   -> 0.0820s
== 20210731045833 ChangeDatatypeObjectComponentValues: reverted (0.0821s) =====

Index name 'index_page_type_on_pages' on table 'pages' already existsの解決方法

v5/load_positonブランチでのこと

db:migrateで失敗していたようでローカルが立ち上がらない

$ bundle exec rails db:migrate
== 20210822092025 AddColumnPageTypeToPages: migrating =========================
-- change_table(:pages, {:bulk=>true})
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Index name 'index_page_type_on_pages' on table 'pages' already exists

ステータスを確認する

$ bundle exec rails db:migrate:status
・・・
   up     20200907060913  Add detailsto object components
   up     20200907065535  Add recruit infoto object components
   up     20210509061758  Add column editor info to edit histories
   up     20210509072450  Create style histories
   up     20210509090511  Create script histories
   up     20210509094943  Create actual object histories
   up     20210509114438  Create operation histories
  down    20210822092025  Add column page type to pages
# カラムは存在しているのにdownになっている状態

migrationファイル

# 20210822092025_add_column_page_type_to_pages.rb
class AddColumnPageTypeToPages < ActiveRecord::Migration[5.2]
  def change
    change_table :pages, bulk: true do |t|
      t.column :page_type,
               :integer,
               after: :approval_route_id,
               null: false,
               default: 1

      t.index [:tenant_id, :page_type],
              name: 'index_page_type_on_pages'
    end
  end
end

まずはインデックスを削除する

> drop index index_page_type_on_pages on pages;
Query OK, 0 rows affected (0.036 sec)
Records: 0  Duplicates: 0  Warnings: 0

次にカラムを削除する

> alter table pages drop column page_type;
Query OK, 0 rows affected (0.074 sec)
Records: 0  Duplicates: 0  Warnings: 0

再度db:migrateする

$ bundle exec rails db:migrate
== 20210822092025 AddColumnPageTypeToPages: migrating =========================
-- change_table(:pages, {:bulk=>true})
   -> 0.0771s
== 20210822092025 AddColumnPageTypeToPages: migrated (0.0774s) ================

成功

https://www.y-hakopro.com/entry/2020/05/22/091648

https://teratail.com/questions/91983

http://taustation.com/mysql-field-manipulation/

無事にエラーは解消されたが、次に別ブランチで作業するのでちゃんとロールバックしてデータベースを元に戻しておく

(おそらくだが、rebaseしたブランチと元のブランチを分けておいて、rebaseした方はdb:migrateしないでバックアップ用としておいておいた方が良いかも)

$ bundle exec rails db:rollback STEP=10
$ bundle exec rails db:migrate:status
・・・
  down    20200811080315  Add viewjs position to pages
  down    20200903053736  Add viewcss position to pages
  down    20200907060913  Add detailsto object components
  down    20200907065535  Add recruit infoto object components
  down    20210509061758  Add column editor info to edit histories
  down    20210509072450  Create style histories
  down    20210509090511  Create script histories
  down    20210509094943  Create actual object histories
  down    20210509114438  Create operation histories
  down    20210822092025  Add column page type to pages
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0