Skip to content

Commit

Permalink
Merge pull request #29 from cloudtruth/add-force-option
Browse files Browse the repository at this point in the history
Add :force option to create_enum
  • Loading branch information
bibendi committed Aug 21, 2020
2 parents 916b96e + 195affa commit 11e8556
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/active_record/postgres_enum/postgresql_adapter.rb
Expand Up @@ -42,9 +42,11 @@ def enums
end
end

def create_enum(name, values, if_not_exists: nil)
def create_enum(name, values, force: false, if_not_exists: nil)
return if if_not_exists && enums.include?(name.to_sym)

drop_enum(name, cascade: force == :cascade, if_exists: true) if force

values = values.map { |v| quote v }
execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_record/postgres_enum/schema_dumper.rb
Expand Up @@ -17,10 +17,10 @@ def dump_enums(stream)

@connection.enums.each do |name, values|
values = values.map { |v| " #{v.inspect}," }.join("\n")
statements << " create_enum #{name.inspect}, [\n#{values}\n ]"
statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
end

stream.puts statements.join("\n")
stream.puts statements.join("\n\n")
stream.puts
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/active_record/postgres_enum_spec.rb
Expand Up @@ -24,10 +24,45 @@
expect { connection.create_enum(:foo, %w(a1 a2), if_not_exists: true) }.not_to raise_error
end

it "does not change the enum options if it exists" do
expect { connection.create_enum(:foo, %w(b1 b2), if_not_exists: true) }.not_to raise_error
expect(connection.enums[:foo]).to eq %w(a1 a2)
end

it "fails create an existing enum" do
expect { connection.create_enum(:foo, %w(a1 a2)) }.to raise_error StandardError
end

context "it forces the creation of an enum" do
it "recreates an enum with current set of values" do
expect(connection.enums[:foo]).to eq %w(a1 a2)
expect { connection.create_enum(:foo, %w(b1 b2), force: :cascade) }.not_to raise_error
expect(connection.enums[:foo]).to eq %w(b1 b2)
end

it "does not error out if forcing creation of an enum" do
expect(connection.enums[:bar]).to be_nil
expect { connection.create_enum(:bar, %w(a1 a2), force: :cascade) }.not_to raise_error
expect(connection.enums[:bar]).to eq %w(a1 a2)
end

context "cascading" do
before do
connection.create_table :test_tbl_for_cascade do |t|
t.enum :baz, enum_name: :foo
end
end

it "fails to force create an enum if not cascading" do
expect { connection.create_enum(:foo, %w(a1 a2), force: true) }.to raise_error StandardError
end

it "force creates an enum if cascading" do
expect { connection.create_enum(:foo, %w(a1 a2), force: :cascade) }.not_to raise_error
end
end
end

it "drops an enum" do
expect { connection.drop_enum(:foo) }.to_not raise_error
expect(connection.enums[:foo]).to be_nil
Expand Down

0 comments on commit 11e8556

Please sign in to comment.