2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
RSpec.describe Migration::ColumnDropper do
|
2017-05-04 22:15:32 +08:00
|
|
|
def has_column?(table, column)
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(<<~SQL, table: table, column: column) == 1
|
2018-03-26 22:51:27 +08:00
|
|
|
SELECT 1
|
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
|
WHERE
|
|
|
|
table_schema = 'public' AND
|
|
|
|
table_name = :table AND
|
|
|
|
column_name = :column
|
|
|
|
SQL
|
2017-05-04 22:15:32 +08:00
|
|
|
end
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
describe ".execute_drop" do
|
|
|
|
let(:columns) { %w{junk junk2} }
|
2018-03-26 22:51:27 +08:00
|
|
|
|
|
|
|
before do
|
2018-10-08 15:47:38 +08:00
|
|
|
columns.each do |column|
|
|
|
|
DB.exec("ALTER TABLE topics ADD COLUMN #{column} int")
|
|
|
|
end
|
2018-03-26 22:51:27 +08:00
|
|
|
end
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
after do
|
|
|
|
columns.each do |column|
|
|
|
|
DB.exec("ALTER TABLE topics DROP COLUMN IF EXISTS #{column}")
|
|
|
|
end
|
2018-03-26 22:51:27 +08:00
|
|
|
end
|
2017-05-04 22:15:32 +08:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
it "drops the columns" do
|
|
|
|
Migration::ColumnDropper.execute_drop("topics", columns)
|
|
|
|
|
|
|
|
columns.each do |column|
|
|
|
|
expect(has_column?('topics', column)).to eq(false)
|
|
|
|
end
|
2018-03-26 22:51:27 +08:00
|
|
|
end
|
2017-05-04 22:15:32 +08:00
|
|
|
end
|
|
|
|
|
2017-07-24 18:32:38 +08:00
|
|
|
describe '.mark_readonly' do
|
|
|
|
let(:table_name) { "table_with_readonly_column" }
|
|
|
|
|
|
|
|
before do
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
CREATE TABLE #{table_name} (topic_id INTEGER, email TEXT);
|
|
|
|
|
|
|
|
INSERT INTO #{table_name} (topic_id, email)
|
|
|
|
VALUES (1, 'something@email.com');
|
|
|
|
SQL
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
Migration::ColumnDropper.mark_readonly(table_name, 'email')
|
2017-07-24 18:32:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-07-25 08:38:55 +08:00
|
|
|
ActiveRecord::Base.connection.reset!
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
DROP TABLE IF EXISTS #{table_name};
|
2018-10-08 15:47:38 +08:00
|
|
|
DROP FUNCTION IF EXISTS #{Migration::BaseDropper.readonly_function_name(table_name, 'email')} CASCADE;
|
2017-07-24 18:32:38 +08:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2017-08-29 23:50:56 +08:00
|
|
|
it 'should be droppable' do
|
2018-10-08 15:47:38 +08:00
|
|
|
Migration::ColumnDropper.execute_drop(table_name, ['email'])
|
|
|
|
|
|
|
|
expect(has_trigger?(Migration::BaseDropper.readonly_trigger_name(
|
|
|
|
table_name, 'email'
|
|
|
|
))).to eq(false)
|
|
|
|
|
|
|
|
expect(has_column?(table_name, 'email')).to eq(false)
|
2017-08-29 23:50:56 +08:00
|
|
|
end
|
2018-08-23 12:51:22 +08:00
|
|
|
|
2017-07-24 18:32:38 +08:00
|
|
|
it 'should prevent updates to the readonly column' do
|
2018-08-23 12:51:22 +08:00
|
|
|
begin
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
UPDATE #{table_name}
|
|
|
|
SET email = 'testing@email.com'
|
|
|
|
WHERE topic_id = 1;
|
|
|
|
SQL
|
2018-08-23 12:51:22 +08:00
|
|
|
rescue PG::RaiseException => e
|
|
|
|
[
|
|
|
|
"Discourse: email in #{table_name} is readonly",
|
|
|
|
'discourse_functions.raise_table_with_readonly_column_email_readonly()'
|
|
|
|
].each do |message|
|
|
|
|
expect(e.message).to include(message)
|
|
|
|
end
|
|
|
|
end
|
2017-07-24 18:32:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow updates to the other columns' do
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
UPDATE #{table_name}
|
|
|
|
SET topic_id = 2
|
|
|
|
WHERE topic_id = 1
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(
|
2022-04-05 18:08:28 +08:00
|
|
|
DB.query("SELECT * FROM #{table_name};").first.values
|
|
|
|
).to include 2, "something@email.com"
|
2017-07-24 18:32:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should prevent insertions to the readonly column' do
|
|
|
|
expect do
|
2017-07-25 23:37:25 +08:00
|
|
|
ActiveRecord::Base.connection.raw_connection.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
INSERT INTO #{table_name} (topic_id, email)
|
|
|
|
VALUES (2, 'something@email.com');
|
|
|
|
SQL
|
|
|
|
end.to raise_error(
|
|
|
|
PG::RaiseException,
|
|
|
|
/Discourse: email in table_with_readonly_column is readonly/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow insertions to the other columns' do
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 18:32:38 +08:00
|
|
|
INSERT INTO #{table_name} (topic_id)
|
|
|
|
VALUES (2);
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.query_single("SELECT topic_id FROM #{table_name} WHERE topic_id = 2")
|
|
|
|
).to eq([2])
|
2017-07-24 18:32:38 +08:00
|
|
|
end
|
|
|
|
end
|
2017-04-27 02:47:36 +08:00
|
|
|
end
|