Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :force option to create_enum #29

Merged
merged 4 commits into from Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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