describe 'Ridgepole::Client#diff -> migrate' do context 'when rename table' do let(:actual_dsl) { erbh(<<-EOS) create_table "clubs", force: :cascade do |t| t.string "name", default: "", null: false t.index ["name"], name: "idx_name", unique: true, <%= i cond(5.0, using: :btree) %> end create_table "departments", primary_key: "dept_no", force: :cascade do |t| t.string "dept_name", limit: 40, null: false t.index ["dept_name"], name: "dept_name", unique: true, <%= i cond(5.0, using: :btree) %> end create_table "dept_emp", id: false, force: :cascade do |t| t.integer "emp_no", null: false t.string "dept_no", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["dept_no"], name: "dept_no", <%= i cond(5.0, using: :btree) %> t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "dept_manager", id: false, force: :cascade do |t| t.string "dept_no", null: false t.integer "emp_no", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["dept_no"], name: "dept_no", <%= i cond(5.0, using: :btree) %> t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "employee_clubs", force: :cascade do |t| t.integer "emp_no", null: false t.integer "club_id", null: false t.index ["emp_no", "club_id"], name: "idx_emp_no_club_id", <%= i cond(5.0, using: :btree) %> end create_table "employees", primary_key: "emp_no", force: :cascade do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false end create_table "salaries", id: false, force: :cascade do |t| t.integer "emp_no", null: false t.integer "salary", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "titles", id: false, force: :cascade do |t| t.integer "emp_no", null: false t.string "title", limit: 50, null: false t.date "from_date", null: false t.date "to_date" t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end EOS } let(:expected_dsl) { erbh(<<-EOS) create_table "clubs", force: :cascade do |t| t.string "name", default: "", null: false t.index ["name"], name: "idx_name", unique: true, <%= i cond(5.0, using: :btree) %> end create_table "departments", primary_key: "dept_no", force: :cascade do |t| t.string "dept_name", limit: 40, null: false t.index ["dept_name"], name: "dept_name", unique: true, <%= i cond(5.0, using: :btree) %> end create_table "dept_emp", id: false, force: :cascade do |t| t.integer "emp_no", null: false t.string "dept_no", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["dept_no"], name: "dept_no", <%= i cond(5.0, using: :btree) %> t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "dept_manager", id: false, force: :cascade do |t| t.string "dept_no", null: false t.integer "emp_no", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["dept_no"], name: "dept_no", <%= i cond(5.0, using: :btree) %> t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "employee_clubs", force: :cascade do |t| t.integer "emp_no", null: false t.integer "club_id", null: false t.index ["emp_no", "club_id"], name: "idx_emp_no_club_id", <%= i cond(5.0, using: :btree) %> end create_table "employees2", primary_key: "emp_no", force: :cascade, renamed_from: 'employees' do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false end create_table "salaries", id: false, force: :cascade do |t| t.integer "emp_no", null: false t.integer "salary", null: false t.date "from_date", null: false t.date "to_date", null: false t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end create_table "titles2", id: false, force: :cascade, renamed_from: :titles do |t| t.integer "emp_no", null: false t.string "title", limit: 50, null: false t.date "from_date", null: false t.date "to_date" t.index ["emp_no"], name: "emp_no", <%= i cond(5.0, using: :btree) %> end EOS } before { subject.diff(actual_dsl).migrate } subject { client } it { delta = subject.diff(expected_dsl) expect(delta.differ?).to be_truthy expect(subject.dump).to match_fuzzy actual_dsl delta.migrate expect(subject.dump).to match_fuzzy expected_dsl.gsub(/, renamed_from: ('employees'|:titles)/, '') } it { delta = Ridgepole::Client.diff(actual_dsl, expected_dsl, reverse: true) expect(delta.differ?).to be_truthy expect(delta.script).to match_fuzzy <<-EOS rename_table("employees2", "employees") rename_table("titles2", "titles") EOS } end context 'when rename table (dry-run)' do let(:actual_dsl) { erbh(<<-EOS) create_table "employees", primary_key: "emp_no", force: :cascade do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false t.index ["first_name"], name: "first_name", <%= i cond(5.0, using: :btree) %> end create_table "employees3", primary_key: "emp_no", force: :cascade do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false t.index ["first_name"], name: "first_name", <%= i cond(5.0, using: :btree) %> end EOS } let(:expected_dsl) { erbh(<<-EOS) create_table "employees2", primary_key: "emp_no", force: :cascade, renamed_from: 'employees' do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false t.index ["first_name"], name: "first_name", <%= i cond(5.0, using: :btree) %> end create_table "employees4", primary_key: "emp_no", force: :cascade, renamed_from: :employees3 do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false t.index ["first_name"], name: "first_name", <%= i cond(5.0, using: :btree) %> end EOS } before { subject.diff(actual_dsl).migrate } subject { client } it { delta = subject.diff(expected_dsl) expect(delta.differ?).to be_truthy expect(subject.dump).to match_fuzzy actual_dsl delta.migrate(noop: true) expect(subject.dump).to match_fuzzy actual_dsl } end context 'when rename table (not found)' do before { restore_tables } subject { client } let(:dsl) { erbh(<<-EOS) create_table "employees2", primary_key: "emp_no", force: :cascade, renamed_from: 'not_employees' do |t| t.date "birth_date", null: false t.string "first_name", limit: 14, null: false t.string "last_name", limit: 16, null: false t.string "gender", limit: 1, null: false t.date "hire_date", null: false end EOS } it { # No existence checking because there is that the table to be read is limited expect { subject.diff(dsl) }.to_not raise_error } end end