|
| 1 | +require 'migrations/helpers/migration_shared_context' |
| 2 | + |
| 3 | +RSpec.shared_context 'bigint migration step1' do |
| 4 | + subject(:run_migration) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) } |
| 5 | + |
| 6 | + include_context 'migration' |
| 7 | + |
| 8 | + let(:skip_bigint_id_migration) { nil } |
| 9 | + |
| 10 | + before do |
| 11 | + fake_config = double |
| 12 | + allow(fake_config).to receive(:get).with(:skip_bigint_id_migration).and_return(skip_bigint_id_migration) |
| 13 | + allow(RakeConfig).to receive(:config).and_return(fake_config) |
| 14 | + end |
| 15 | + |
| 16 | + describe 'up' do |
| 17 | + context 'when skip_bigint_id_migration is false' do |
| 18 | + let(:skip_bigint_id_migration) { false } |
| 19 | + |
| 20 | + context 'when the table is empty' do |
| 21 | + before do |
| 22 | + db[table].delete |
| 23 | + end |
| 24 | + |
| 25 | + it "changes the id column's type to bigint" do |
| 26 | + expect(db).to have_table_with_column_and_type(table, :id, 'integer') |
| 27 | + |
| 28 | + run_migration |
| 29 | + |
| 30 | + expect(db).to have_table_with_column_and_type(table, :id, 'bigint') |
| 31 | + end |
| 32 | + |
| 33 | + it 'does not add the id_bigint column' do |
| 34 | + expect(db).not_to have_table_with_column(table, :id_bigint) |
| 35 | + |
| 36 | + run_migration |
| 37 | + |
| 38 | + expect(db).not_to have_table_with_column(table, :id_bigint) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'when the table is not empty' do |
| 43 | + before do |
| 44 | + db[table].insert(insert_hash) |
| 45 | + end |
| 46 | + |
| 47 | + it "does not change the id column's type" do |
| 48 | + expect(db).to have_table_with_column_and_type(table, :id, 'integer') |
| 49 | + |
| 50 | + run_migration |
| 51 | + |
| 52 | + expect(db).to have_table_with_column_and_type(table, :id, 'integer') |
| 53 | + end |
| 54 | + |
| 55 | + it 'adds the id_bigint column' do |
| 56 | + expect(db).not_to have_table_with_column(table, :id_bigint) |
| 57 | + |
| 58 | + run_migration |
| 59 | + |
| 60 | + expect(db).to have_table_with_column_and_type(table, :id_bigint, 'bigint') |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'when skip_bigint_id_migration is true' do |
| 66 | + let(:skip_bigint_id_migration) { true } |
| 67 | + |
| 68 | + it "neither changes the id column's type, nor adds the id_bigint column" do |
| 69 | + expect(db).to have_table_with_column_and_type(table, :id, 'integer') |
| 70 | + expect(db).not_to have_table_with_column(table, :id_bigint) |
| 71 | + |
| 72 | + run_migration |
| 73 | + |
| 74 | + expect(db).to have_table_with_column_and_type(table, :id, 'integer') |
| 75 | + expect(db).not_to have_table_with_column(table, :id_bigint) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # down |
| 81 | +end |
0 commit comments