|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../test_helper" |
| 4 | +require_relative "../../../app/controllers/actual_db_schema/broken_versions_controller" |
| 5 | + |
| 6 | +module ActualDbSchema |
| 7 | + class BrokenVersionsControllerTest < ActionController::TestCase |
| 8 | + def setup |
| 9 | + @utils = TestUtils.new |
| 10 | + @app = Rails.application |
| 11 | + routes_setup |
| 12 | + Rails.logger = Logger.new($stdout) |
| 13 | + ActionController::Base.view_paths = [File.expand_path("../../../app/views/", __dir__)] |
| 14 | + active_record_setup |
| 15 | + @utils.reset_database_yml(TestingState.db_config) |
| 16 | + @utils.cleanup(TestingState.db_config) |
| 17 | + @utils.prepare_phantom_migrations(TestingState.db_config) |
| 18 | + end |
| 19 | + |
| 20 | + def routes_setup |
| 21 | + @routes = @app.routes |
| 22 | + Rails.application.routes.draw do |
| 23 | + get "/rails/broken_versions" => "actual_db_schema/broken_versions#index", as: "broken_versions" |
| 24 | + get "/rails/migrations" => "actual_db_schema/migrations#index", as: "migrations" |
| 25 | + post "/rails/broken_version/:id/delete" => "actual_db_schema/broken_versions#delete", |
| 26 | + as: "delete_broken_version" |
| 27 | + post "/rails/broken_versions/delete_all" => "actual_db_schema/broken_versions#delete_all", |
| 28 | + as: "delete_all_broken_versions" |
| 29 | + end |
| 30 | + ActualDbSchema::BrokenVersionsController.include(@routes.url_helpers) |
| 31 | + end |
| 32 | + |
| 33 | + def active_record_setup |
| 34 | + ActiveRecord::Base.configurations = { "test" => TestingState.db_config } |
| 35 | + ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config } |
| 36 | + end |
| 37 | + |
| 38 | + def delete_migrations_files |
| 39 | + @utils.delete_migrations_files_for("tmp/migrated") |
| 40 | + @utils.delete_migrations_files_for("tmp/migrated_migrate_secondary") |
| 41 | + end |
| 42 | + |
| 43 | + test "GET #index returns a successful response" do |
| 44 | + delete_migrations_files |
| 45 | + get :index |
| 46 | + assert_response :success |
| 47 | + assert_select "table" do |
| 48 | + assert_select "tbody" do |
| 49 | + assert_select "tr" do |
| 50 | + assert_select "td", text: "up" |
| 51 | + assert_select "td", text: "20130906111511" |
| 52 | + assert_select "td", text: @utils.branch_for("20130906111511") |
| 53 | + assert_select "td", text: @utils.primary_database |
| 54 | + end |
| 55 | + assert_select "tr" do |
| 56 | + assert_select "td", text: "up" |
| 57 | + assert_select "td", text: "20130906111512" |
| 58 | + assert_select "td", text: @utils.branch_for("20130906111512") |
| 59 | + assert_select "td", text: @utils.primary_database |
| 60 | + end |
| 61 | + assert_select "tr" do |
| 62 | + assert_select "td", text: "up" |
| 63 | + assert_select "td", text: "20130906111514" |
| 64 | + assert_select "td", text: @utils.branch_for("20130906111514") |
| 65 | + assert_select "td", text: @utils.secondary_database |
| 66 | + end |
| 67 | + assert_select "tr" do |
| 68 | + assert_select "td", text: "up" |
| 69 | + assert_select "td", text: "20130906111515" |
| 70 | + assert_select "td", text: @utils.branch_for("20130906111515") |
| 71 | + assert_select "td", text: @utils.secondary_database |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + test "GET #index when there are no broken versions returns a not found text" do |
| 78 | + get :index |
| 79 | + assert_response :success |
| 80 | + assert_select "p", text: "No broken versions found." |
| 81 | + end |
| 82 | + |
| 83 | + test "POST #delete removes migration entry from the schema_migrations table" do |
| 84 | + delete_migrations_files |
| 85 | + version = "20130906111511" |
| 86 | + sql = "SELECT version FROM schema_migrations WHERE version = '#{version}'" |
| 87 | + ActiveRecord::Base.establish_connection(TestingState.db_config["primary"]) |
| 88 | + assert_not_nil ActiveRecord::Base.connection.select_value(sql) |
| 89 | + |
| 90 | + post :delete, params: { id: "20130906111511", database: @utils.primary_database } |
| 91 | + assert_response :redirect |
| 92 | + get :index |
| 93 | + assert_select "table" do |table| |
| 94 | + assert_no_match "20130906111511", table.text |
| 95 | + end |
| 96 | + assert_select ".flash", text: "Migration 20130906111511 was successfully deleted." |
| 97 | + assert_nil ActiveRecord::Base.connection.select_value(sql) |
| 98 | + end |
| 99 | + |
| 100 | + test "POST #delete_all removes all broken migration entries from the schema_migrations table" do |
| 101 | + delete_migrations_files |
| 102 | + sql = "SELECT COUNT(*) FROM schema_migrations" |
| 103 | + ActiveRecord::Base.establish_connection(TestingState.db_config["primary"]) |
| 104 | + assert_equal 2, ActiveRecord::Base.connection.select_value(sql) |
| 105 | + ActiveRecord::Base.establish_connection(TestingState.db_config["secondary"]) |
| 106 | + assert_equal 2, ActiveRecord::Base.connection.select_value(sql) |
| 107 | + |
| 108 | + post :delete_all |
| 109 | + assert_response :redirect |
| 110 | + get :index |
| 111 | + assert_select "p", text: "No broken versions found." |
| 112 | + ActiveRecord::Base.establish_connection(TestingState.db_config["primary"]) |
| 113 | + assert_equal 0, ActiveRecord::Base.connection.select_value(sql) |
| 114 | + ActiveRecord::Base.establish_connection(TestingState.db_config["secondary"]) |
| 115 | + assert_equal 0, ActiveRecord::Base.connection.select_value(sql) |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments