2017-05-12 15:01:43 -04:00
|
|
|
require 'rails_helper'
|
2018-03-20 03:20:50 -04:00
|
|
|
require_dependency 'migration/table_dropper'
|
2017-05-12 15:01:43 -04:00
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
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
|
|
|
|
|
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 do
|
|
|
|
Migration::TableDropper.read_only_table(table_name)
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
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 do |message|
|
|
|
|
expect(e.message).to include(message)
|
|
|
|
end
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
2017-05-12 15:01:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|