|
32 | 32 | ActiveRecord::ConnectionAdapters::CockroachDB.initial_setup |
33 | 33 |
|
34 | 34 | module ActiveRecord |
| 35 | + # TODO: once in rails 7.2, remove this and replace with a `#register` call. |
| 36 | + # See: https://github.com/rails/rails/commit/22a26d7f74ea8f0d5f7c4169531ae38441cfd5e5#diff-2468c670eb10c24bd2823e42708489a336d6f21c6efc7e3c4a574166fa77bb22 |
35 | 37 | module ConnectionHandling |
| 38 | + def cockroachdb_adapter_class |
| 39 | + ConnectionAdapters::CockroachDBAdapter |
| 40 | + end |
| 41 | + |
36 | 42 | def cockroachdb_connection(config) |
37 | | - # This is copied from the PostgreSQL adapter. |
38 | | - conn_params = config.symbolize_keys.compact |
39 | | - |
40 | | - # Map ActiveRecords param names to PGs. |
41 | | - conn_params[:user] = conn_params.delete(:username) if conn_params[:username] |
42 | | - conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database] |
43 | | - |
44 | | - # Forward only valid config params to PG::Connection.connect. |
45 | | - valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl] |
46 | | - conn_params.slice!(*valid_conn_param_keys) |
47 | | - |
48 | | - ConnectionAdapters::CockroachDBAdapter.new( |
49 | | - ConnectionAdapters::CockroachDBAdapter.new_client(conn_params), |
50 | | - logger, |
51 | | - conn_params, |
52 | | - config |
53 | | - ) |
54 | | - # This rescue flow appears in new_client, but it is needed here as well |
55 | | - # since Cockroach will sometimes not raise until a query is made. |
56 | | - rescue ActiveRecord::StatementInvalid => error |
57 | | - no_db_err_check1 = conn_params && conn_params[:dbname] && error.cause.message.include?(conn_params[:dbname]) |
58 | | - no_db_err_check2 = conn_params && conn_params[:dbname] && error.cause.message.include?("pg_type") |
59 | | - if no_db_err_check1 || no_db_err_check2 |
60 | | - raise ActiveRecord::NoDatabaseError |
61 | | - else |
62 | | - raise ActiveRecord::ConnectionNotEstablished, error.message |
63 | | - end |
| 43 | + cockroachdb_adapter_class.new(config) |
64 | 44 | end |
65 | 45 | end |
66 | 46 | end |
@@ -255,6 +235,21 @@ def self.database_exists?(config) |
255 | 235 | false |
256 | 236 | end |
257 | 237 |
|
| 238 | + def initialize(...) |
| 239 | + super |
| 240 | + |
| 241 | + # This rescue flow appears in new_client, but it is needed here as well |
| 242 | + # since Cockroach will sometimes not raise until a query is made. |
| 243 | + rescue ActiveRecord::StatementInvalid => error |
| 244 | + no_db_err_check1 = @connection_parameters && @connection_parameters[:dbname] && error.cause.message.include?(@connection_parameters[:dbname]) |
| 245 | + no_db_err_check2 = @connection_parameters && @connection_parameters[:dbname] && error.cause.message.include?("pg_type") |
| 246 | + if no_db_err_check1 || no_db_err_check2 |
| 247 | + raise ActiveRecord::NoDatabaseError |
| 248 | + else |
| 249 | + raise ActiveRecord::ConnectionNotEstablished, error.message |
| 250 | + end |
| 251 | + end |
| 252 | + |
258 | 253 | # override |
259 | 254 | # The PostgreSQLAdapter uses syntax for an anonymous function |
260 | 255 | # (DO $$) that CockroachDB does not support. |
|
0 commit comments