2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Migration::TableDropper do
|
2017-05-12 15:01:43 -04:00
|
|
|
def table_exists?(table_name)
|
2018-10-08 03:47:38 -04:00
|
|
|
DB.exec(<<~SQL) > 0
|
2018-03-21 06:31:05 -04:00
|
|
|
SELECT 1
|
|
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
|
|
WHERE table_schema = 'public' AND
|
|
|
|
table_name = '#{table_name}'
|
2017-05-12 15:01:43 -04:00
|
|
|
SQL
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
|
|
|
|
2019-08-08 10:58:13 -04:00
|
|
|
def function_exists?(function_name, schema_name = "public")
|
|
|
|
DB.exec(<<~SQL) > 0
|
|
|
|
SELECT 1
|
|
|
|
FROM information_schema.routines
|
|
|
|
WHERE routine_type = 'FUNCTION' AND
|
|
|
|
routine_name = '#{function_name}' AND
|
|
|
|
specific_schema = '#{schema_name}'
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
let(:table_name) { "table_with_old_name" }
|
2017-05-12 15:01:43 -04:00
|
|
|
|
2018-03-21 06:31:05 -04:00
|
|
|
before do
|
2018-10-08 03:47:38 -04:00
|
|
|
DB.exec "CREATE TABLE #{table_name} (topic_id INTEGER)"
|
2017-05-12 15:01:43 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
DB.exec <<~SQL
|
|
|
|
INSERT INTO #{table_name} (topic_id) VALUES (1)
|
2018-03-26 10:51:27 -04:00
|
|
|
SQL
|
2018-03-21 06:31:05 -04:00
|
|
|
end
|
2017-05-12 15:01:43 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
describe ".execute_drop" do
|
|
|
|
it "should drop the table" do
|
|
|
|
Migration::TableDropper.execute_drop(table_name)
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(table_exists?(table_name)).to eq(false)
|
2018-03-21 06:31:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
describe ".readonly_only_table" do
|
|
|
|
before { Migration::TableDropper.read_only_table(table_name) }
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
after do
|
|
|
|
ActiveRecord::Base.connection.reset!
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
DB.exec(<<~SQL)
|
|
|
|
DROP TABLE IF EXISTS #{table_name};
|
|
|
|
DROP FUNCTION IF EXISTS #{Migration::BaseDropper.readonly_function_name(table_name)} CASCADE;
|
|
|
|
SQL
|
|
|
|
end
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
it "should be droppable" do
|
|
|
|
Migration::TableDropper.execute_drop(table_name)
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(has_trigger?(Migration::BaseDropper.readonly_trigger_name(table_name))).to eq(false)
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(table_exists?(table_name)).to eq(false)
|
|
|
|
end
|
2018-03-26 10:51:27 -04:00
|
|
|
|
2019-08-08 10:58:13 -04:00
|
|
|
it "should drop the read_only function" do
|
|
|
|
Migration::TableDropper.execute_drop(table_name)
|
|
|
|
|
|
|
|
schema_name, function_name =
|
|
|
|
Migration::BaseDropper.readonly_function_name(table_name).delete_suffix("()").split(".")
|
|
|
|
|
|
|
|
expect(function_exists?(function_name, schema_name)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
it "should prevent insertions to the table" do
|
|
|
|
begin
|
|
|
|
DB.exec <<~SQL
|
|
|
|
INSERT INTO #{table_name} (topic_id) VALUES (2)
|
|
|
|
SQL
|
|
|
|
rescue PG::RaiseException => e
|
|
|
|
[
|
|
|
|
"Discourse: #{table_name} is read only",
|
|
|
|
"discourse_functions.raise_table_with_old_name_readonly()",
|
|
|
|
].each { |message| expect(e.message).to include(message) }
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
2017-05-12 15:01:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|