Sha256: 855e48b0450e631ea3201091b72b5814700750be3d8edbc1fee8a4ea0dd95ae3

Contents?: true

Size: 1.58 KB

Versions: 9

Compression:

Stored size: 1.58 KB

Contents

require "spec_helper"

describe "Reverting scenic schema statements", :db do
  around do |example|
    with_view_definition :greetings, 1, "SELECT text 'hola' AS greeting" do
      example.run
    end
  end

  it "reverts dropped view to specified version" do
    run_migration(migration_for_create, :up)
    run_migration(migration_for_drop, :up)
    run_migration(migration_for_drop, :down)

    expect { execute("SELECT * from greetings") }
      .not_to raise_error
  end

  it "reverts updated view to specified version" do
    with_view_definition :greetings, 2, "SELECT text 'good day' AS greeting" do
      run_migration(migration_for_create, :up)
      run_migration(migration_for_update, :up)
      run_migration(migration_for_update, :down)

      greeting = execute("SELECT * from greetings")[0]["greeting"]

      expect(greeting).to eq "hola"
    end
  end

  def migration_for_create
    Class.new(::ActiveRecord::Migration) do
      def change
        create_view :greetings
      end
    end
  end

  def migration_for_drop
    Class.new(::ActiveRecord::Migration) do
      def change
        drop_view :greetings, revert_to_version: 1
      end
    end
  end

  def migration_for_update
    Class.new(::ActiveRecord::Migration) do
      def change
        update_view :greetings, version: 2, revert_to_version: 1
      end
    end
  end

  def run_migration(migration, directions)
    silence_stream(STDOUT) do
      Array.wrap(directions).each do |direction|
        migration.migrate(direction)
      end
    end
  end

  def execute(sql)
    ActiveRecord::Base.connection.execute(sql)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scenic-1.3.0 spec/integration/revert_spec.rb
scenic-1.2.0 spec/integration/revert_spec.rb
scenic-1.1.1 spec/integration/revert_spec.rb
scenic-1.1.0 spec/integration/revert_spec.rb
scenic-1.0.0 spec/integration/revert_spec.rb
scenic-0.3.0 spec/integration/revert_spec.rb
scenic-0.2.1 spec/integration/revert_spec.rb
scenic-0.2.0 spec/integration/revert_spec.rb
scenic-0.1.0 spec/integration/revert_spec.rb