2017-05-04 10:15:32 -04:00
|
|
|
require 'rails_helper'
|
2018-03-20 03:20:50 -04:00
|
|
|
require_dependency 'migration/column_dropper'
|
2017-05-04 10:15:32 -04:00
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
RSpec.describe Migration::ColumnDropper do
|
2017-05-04 10:15:32 -04:00
|
|
|
def has_column?(table, column)
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(<<~SQL, table: table, column: column) == 1
|
2018-03-26 10:51:27 -04:00
|
|
|
SELECT 1
|
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
|
WHERE
|
|
|
|
table_schema = 'public' AND
|
|
|
|
table_name = :table AND
|
|
|
|
column_name = :column
|
|
|
|
SQL
|
2017-05-04 10:15:32 -04:00
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
describe ".execute_drop" do
|
|
|
|
let(:columns) { %w{junk junk2} }
|
2018-03-26 10:51:27 -04:00
|
|
|
|
|
|
|
before do
|
2018-10-08 03:47:38 -04:00
|
|
|
columns.each do |column|
|
|
|
|
DB.exec("ALTER TABLE topics ADD COLUMN #{column} int")
|
|
|
|
end
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
after do
|
|
|
|
columns.each do |column|
|
|
|
|
DB.exec("ALTER TABLE topics DROP COLUMN IF EXISTS #{column}")
|
|
|
|
end
|
2018-03-26 10:51:27 -04:00
|
|
|
end
|
2017-05-04 10:15:32 -04:00
|
|
|
|
2018-10-08 03:47:38 -04: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 10:51:27 -04:00
|
|
|
end
|
2017-05-04 10:15:32 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 06:32:38 -04:00
|
|
|
describe '.mark_readonly' do
|
|
|
|
let(:table_name) { "table_with_readonly_column" }
|
|
|
|
|
|
|
|
before do
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 06:32:38 -04: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 03:20:50 -04:00
|
|
|
Migration::ColumnDropper.mark_readonly(table_name, 'email')
|
2017-07-24 06:32:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-07-24 20:38:55 -04:00
|
|
|
ActiveRecord::Base.connection.reset!
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 06:32:38 -04:00
|
|
|
DROP TABLE IF EXISTS #{table_name};
|
2018-10-08 03:47:38 -04:00
|
|
|
DROP FUNCTION IF EXISTS #{Migration::BaseDropper.readonly_function_name(table_name, 'email')} CASCADE;
|
2017-07-24 06:32:38 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2017-08-29 11:50:56 -04:00
|
|
|
it 'should be droppable' do
|
2018-10-08 03:47:38 -04: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 11:50:56 -04:00
|
|
|
end
|
2018-08-23 00:51:22 -04:00
|
|
|
|
2017-07-24 06:32:38 -04:00
|
|
|
it 'should prevent updates to the readonly column' do
|
2018-08-23 00:51:22 -04:00
|
|
|
begin
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 06:32:38 -04:00
|
|
|
UPDATE #{table_name}
|
|
|
|
SET email = 'testing@email.com'
|
|
|
|
WHERE topic_id = 1;
|
|
|
|
SQL
|
2018-08-23 00:51:22 -04: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 06:32:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow updates to the other columns' do
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 06:32:38 -04:00
|
|
|
UPDATE #{table_name}
|
|
|
|
SET topic_id = 2
|
|
|
|
WHERE topic_id = 1
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(
|
|
|
|
ActiveRecord::Base.exec_sql("SELECT * FROM #{table_name};").values
|
2017-08-31 00:06:56 -04:00
|
|
|
).to include([2, "something@email.com"])
|
2017-07-24 06:32:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should prevent insertions to the readonly column' do
|
|
|
|
expect do
|
2017-07-25 11:37:25 -04:00
|
|
|
ActiveRecord::Base.connection.raw_connection.exec <<~SQL
|
2017-07-24 06:32:38 -04: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 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
2017-07-24 06:32:38 -04:00
|
|
|
INSERT INTO #{table_name} (topic_id)
|
|
|
|
VALUES (2);
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.query_single("SELECT topic_id FROM #{table_name} WHERE topic_id = 2")
|
|
|
|
).to eq([2])
|
2017-07-24 06:32:38 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-26 14:47:36 -04:00
|
|
|
end
|