Repository: rails/rails
File: activerecord/test/cases/schema_dumper_test.rb (Ruby)

Code before:
      migration.migrate(:down)
      $stdout = original
    end
  end

  def test_schema_dump_keeps_enum_intact_if_it_contains_comma
    original, $stdout = $stdout, StringIO.new

    migration = Class.new(ActiveRecord::Migration::Current) do
      def up
        create_enum "enum_with_comma", ["value1", "value,2", "value3"]
      end

      def down
        drop_enum "enum_with_comma"
      end
    end

    migration.migrate(:up)
    output = dump_all_table_schema

    assert_match %r{create_enum "enum_with_comma", \["value1", "value,2", "value3"\]}, output
  ensure
    migration.migrate(:down) if migration
    $stdout = original
  end if current_adapter?(:PostgreSQLAdapter)
end

class SchemaDumperDefaultsTest < ActiveRecord::TestCase
  include SchemaDumpingHelper

  setup do
    @connection = ActiveRecord::Base.lease_connection
    @connection.create_table :dump_defaults, force: true do |t|
      t.string   :string_with_default,   default: "Hello!"
      t.date     :date_with_default,     default: "2014-06-05"
      t.datetime :datetime_with_default, default: "2014-06-05 07:17:04"
      t.time     :time_with_default,     default: "07:17:04"
      t.decimal  :decimal_with_default,  default: "1234567890.0123456789", precision: 20, scale: 10

      if supports_text_column_with_default?
        t.text :text_with_default, default: "John' Doe"

        if current_adapter?(:PostgreSQLAdapter)
          t.text :uuid, default: -> { "gen_random_uuid()" }
        else
          t.text :uuid, default: -> { "uuid()" }
        end
      end
    end


Review comment (suggestion):
```suggestion
  if current_adapter?(:PostgreSQLAdapter)
    def test_schema_dump_keeps_enum_intact_if_it_contains_comma
      original, $stdout = $stdout, StringIO.new

      migration = Class.new(ActiveRecord::Migration::Current) do
        def up
          create_enum "enum_with_comma", ["value1", "value,2", "value3"]
        end

        def down
          drop_enum "enum_with_comma"
        end
      end

      migration.migrate(:up)
      output = dump_all_table_schema

      assert_includes output, 'create_enum "enum_with_comma", ["value1", "value,2", "value3"]'
    ensure
      migration.migrate(:down)
      $stdout = original
    end
  end
```

Code after:

            add_column :timestamps, :this_should_change_to_timestamp, "datetime"
            add_column :timestamps, :this_should_stay_as_timestamp, "timestamp"
          end
          def down
            drop_table("timestamps")
          end
        end
        migration.migrate(:up)

        output = dump_table_schema("timestamps")
        # Normally we'd write `t.datetime` here. But because you've changed the `datetime_type`
        # to something else, `t.datetime` now means `:timestamptz`. To ensure that old columns
        # are still created as a `:timestamp` we need to change what is written to the schema dump.
        #
        # Typically in Rails we handle this through Migration versioning (`ActiveRecord::Migration::Compatibility`)
        # but that doesn't work here because the schema dumper is not aware of which migration
        # a column was added in.
        assert output.include?('t.timestamp "this_should_change_to_timestamp"')
        assert output.include?('t.timestamp "this_should_stay_as_timestamp"')
      end
    ensure
      migration.migrate(:down)
      $stdout = original
    end
  end
end

class SchemaDumperDefaultsTest < ActiveRecord::TestCase
  include SchemaDumpingHelper

  setup do
    @connection = ActiveRecord::Base.lease_connection
    @connection.create_table :dump_defaults, force: true do |t|
      t.string   :string_with_default,   default: "Hello!"
      t.date     :date_with_default,     default: "2014-06-05"
      t.datetime :datetime_with_default, default: "2014-06-05 07:17:04"
      t.time     :time_with_default,     default: "07:17:04"
      t.decimal  :decimal_with_default,  default: "1234567890.0123456789", precision: 20, scale: 10

      if supports_text_column_with_default?
        t.text :text_with_default, default: "John' Doe"

        if current_adapter?(:PostgreSQLAdapter)
          t.text :uuid, default: -> { "gen_random_uuid()" }
        else
          t.text :uuid, default: -> { "uuid()" }
        end
      end
    end