Closed francis closed 1 year ago
+1 on that, It would be really helpful to connect with things like bigquery snowflake
(To connect to another database using an active record connection, we had to do some hack workaround)
def connection(port)
# Generate a unique class name based on the database name and host.
# This assumes that the combination of database_name and host is unique for each connection.
random = Random.rand(399...79990)
class_name = "#{database_name}_#{host}_#{random}".gsub(/[^0-9a-zA-Z]/, "_").camelize
# Define the custom connection class dynamically.
custom_connection_class = Class.new(ActiveRecord::Base) do
self.abstract_class = true
def self.establish_custom_connection(datasource, port)
if datasource.host == "localhost" || ENV["SSH_TUNNEL_ENABLED"] == "false"
connection_port = datasource.port
connection_host = "#{datasource.host}"
elsif ENV["SSH_TUNNEL_ENABLED"] == "true"
connection_port = port
connection_host = "127.0.0.1"
end
if datasource.datasource_type == "psql"
db_config_hash = {
adapter: "postgresql",
encoding: "unicode",
database: datasource.database_name,
host: connection_host,
port: connection_port,
username: datasource.database_username,
password: datasource.database_password,
}
establish_connection(db_config_hash)
else
db_config_hash = {
adapter: "mysql2",
database: datasource.database_name,
host: connection_host,
port: connection_port,
username: datasource.database_username,
password: datasource.database_password,
}
establish_connection(db_config_hash)
end
end
end
OK got sequel to work, PR to open
For Ruby users, they might rather use the Sequel gem instead of ActiveRecord.
Boxcars::SQL could look at the connection type, and do the right thing for Sequel or ActiveRecord to support both. Conversely, if it simplifies things, we could make Boxcars::SQL the base class and add Boxcars::SQLSequel and Boxcars::SQLActiveRecord or similar subclasses.