From 3e1bc803557ac8a3f2d7ffd9336b6da924c317ee Mon Sep 17 00:00:00 2001 From: Rafael Hovhannisyan Date: Sat, 25 Jul 2020 09:40:58 +0400 Subject: [PATCH 1/2] feat: existance checks for create and drop enums Refs #24 --- .../postgres_enum/postgresql_adapter.rb | 10 +++++++--- spec/active_record/postgres_enum_spec.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/active_record/postgres_enum/postgresql_adapter.rb b/lib/active_record/postgres_enum/postgresql_adapter.rb index 3648f88..93e7b76 100644 --- a/lib/active_record/postgres_enum/postgresql_adapter.rb +++ b/lib/active_record/postgres_enum/postgresql_adapter.rb @@ -42,13 +42,17 @@ def enums end end - def create_enum(name, values) + def create_enum(name, values, if_not_exists: nil) + return if if_not_exists && enums.include?(name.to_sym) + values = values.map { |v| quote v } execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})" end - def drop_enum(name) - execute "DROP TYPE #{name}" + def drop_enum(name, if_exists: nil) + if_exists_statement = 'IF EXISTS' if if_exists + sql = "DROP TYPE #{if_exists_statement} #{name}" + execute sql end def rename_enum(name, new_name) diff --git a/spec/active_record/postgres_enum_spec.rb b/spec/active_record/postgres_enum_spec.rb index 569adf2..4adcc89 100644 --- a/spec/active_record/postgres_enum_spec.rb +++ b/spec/active_record/postgres_enum_spec.rb @@ -20,11 +20,27 @@ expect(connection.enums[:foo]).to eq %w(a1 a2) end + it "creates an enum if not exists" do + expect { connection.create_enum(:foo, %w(a1 a2), if_not_exists: true) }.not_to raise_error + end + + it "fails create an existing enum" do + expect { connection.create_enum(:foo, %w(a1 a2)) }.to raise_error + end + it "drops an enum" do expect { connection.drop_enum(:foo) }.to_not raise_error expect(connection.enums[:foo]).to be_nil end + it "drops an enum if exists" do + expect { connection.drop_enum(:some_unknown_type, if_exists: true) }.to_not raise_error + end + + it "fails drop a non existing enum" do + expect { connection.drop_enum(:some_unknown_type) }.to raise_error + end + it "renames an enum" do expect { connection.rename_enum(:foo, :bar) }.to_not raise_error expect(connection.enums[:bar]).to eq %w(a1 a2) From ac5e7da41db270e66286b0566e82714ee51b7a1a Mon Sep 17 00:00:00 2001 From: Rafael Hovhannisyan Date: Sat, 25 Jul 2020 12:13:56 +0400 Subject: [PATCH 2/2] feat: add cascade option to drop_enum - add cascade option to drop_enum - remove raise_error warnings by adding specific error Refs #24 --- .../postgres_enum/postgresql_adapter.rb | 6 +++-- spec/active_record/postgres_enum_spec.rb | 25 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/active_record/postgres_enum/postgresql_adapter.rb b/lib/active_record/postgres_enum/postgresql_adapter.rb index 93e7b76..e8b83dd 100644 --- a/lib/active_record/postgres_enum/postgresql_adapter.rb +++ b/lib/active_record/postgres_enum/postgresql_adapter.rb @@ -49,9 +49,11 @@ def create_enum(name, values, if_not_exists: nil) execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})" end - def drop_enum(name, if_exists: nil) + def drop_enum(name, cascade: nil, if_exists: nil) if_exists_statement = 'IF EXISTS' if if_exists - sql = "DROP TYPE #{if_exists_statement} #{name}" + cascade_statement = 'CASCADE' if cascade + + sql = "DROP TYPE #{if_exists_statement} #{name} #{cascade_statement}" execute sql end diff --git a/spec/active_record/postgres_enum_spec.rb b/spec/active_record/postgres_enum_spec.rb index 4adcc89..e8d9193 100644 --- a/spec/active_record/postgres_enum_spec.rb +++ b/spec/active_record/postgres_enum_spec.rb @@ -25,7 +25,7 @@ end it "fails create an existing enum" do - expect { connection.create_enum(:foo, %w(a1 a2)) }.to raise_error + expect { connection.create_enum(:foo, %w(a1 a2)) }.to raise_error StandardError end it "drops an enum" do @@ -38,7 +38,24 @@ end it "fails drop a non existing enum" do - expect { connection.drop_enum(:some_unknown_type) }.to raise_error + expect { connection.drop_enum(:some_unknown_type) }.to raise_error StandardError + end + + context 'drops an enum with cascade' do + before do + connection.create_table :test_tbl_for_cascade do |t| + t.enum :baz, enum_name: :foo + end + end + + it "fails drop an enum with cascade" do + expect { connection.drop_enum(:foo) }.to raise_error StandardError + end + + it "drops an enum with cascade" do + expect { connection.drop_enum(:foo, cascade: true) }.to_not raise_error + expect(connection.columns('test_tbl_for_cascade').map(&:name)).to_not include('baz') + end end it "renames an enum" do @@ -56,8 +73,8 @@ end it "fails to add an enum value if exists" do - expect { connection.add_enum_value(:foo, "a1", if_not_exists: false) }.to raise_error - expect { connection.add_enum_value(:foo, "a2") }.to raise_error + expect { connection.add_enum_value(:foo, "a1", if_not_exists: false) }.to raise_error StandardError + expect { connection.add_enum_value(:foo, "a2") }.to raise_error StandardError end it "adds an enum value after a given value" do