|
4 | 4 | let(:blocked_tables) { ["sessions", "authentication_tokens"] } |
5 | 5 | let(:all_tables) { ["users", "posts", "sessions", "authentication_tokens"] } |
6 | 6 | let(:connection) do |
7 | | - double("connection", tables: all_tables) |
| 7 | + double("connection", tables: all_tables, quote_table_name: "`mysql_genius_identifier_probe`") |
8 | 8 | end |
9 | 9 |
|
10 | 10 | def validate(sql) |
@@ -116,6 +116,32 @@ def validate(sql) |
116 | 116 | end |
117 | 117 | end |
118 | 118 |
|
| 119 | + describe ".normalize_identifier_quotes" do |
| 120 | + it "leaves backticks intact for backtick-quoting adapters" do |
| 121 | + sql = "SELECT `id` FROM `users` WHERE `name` = 'has `backtick` text'" |
| 122 | + |
| 123 | + expect(described_class.normalize_identifier_quotes(sql, connection)).to(eq(sql)) |
| 124 | + end |
| 125 | + |
| 126 | + it "rewrites backtick identifiers for double-quote adapters" do |
| 127 | + pg_connection = double("connection") |
| 128 | + allow(pg_connection).to(receive(:quote_table_name) { |name| %("#{name.gsub('"', '""')}") }) |
| 129 | + |
| 130 | + sql = "SELECT `id`, `title` FROM `users` WHERE `name` = 'has `backtick` text'" |
| 131 | + |
| 132 | + expect(described_class.normalize_identifier_quotes(sql, pg_connection)) |
| 133 | + .to(eq(%(SELECT "id", "title" FROM "users" WHERE "name" = 'has `backtick` text'))) |
| 134 | + end |
| 135 | + |
| 136 | + it "unescapes doubled backticks before adapter quoting" do |
| 137 | + pg_connection = double("connection") |
| 138 | + allow(pg_connection).to(receive(:quote_table_name) { |name| %("#{name.gsub('"', '""')}") }) |
| 139 | + |
| 140 | + expect(described_class.normalize_identifier_quotes("SELECT `weird``name` FROM `users`", pg_connection)) |
| 141 | + .to(eq(%(SELECT "weird`name" FROM "users"))) |
| 142 | + end |
| 143 | + end |
| 144 | + |
119 | 145 | describe ".masked_column?" do |
120 | 146 | let(:patterns) { ["password", "secret", "digest", "token"] } |
121 | 147 |
|
|
0 commit comments