mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
Feature: Purge cached connections on data source update
This commit is contained in:
parent
72a90f97c6
commit
bb089d8ab3
3 changed files with 57 additions and 25 deletions
22
app/services/concerns/datasource_utils.rb
Normal file
22
app/services/concerns/datasource_utils.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
module DatasourceUtils
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def get_cached_connection(data_source)
|
||||
|
||||
connection = nil
|
||||
if $connections.include? data_source.id
|
||||
data = $connections[data_source.id]
|
||||
if data[:updated_at] === data_source.updated_at
|
||||
connection = $connections[data_source.id][:connection]
|
||||
end
|
||||
end
|
||||
|
||||
connection
|
||||
end
|
||||
|
||||
def cache_connection(data_source, connection)
|
||||
$connections[data_source.id] = { connection: connection, updated_at: data_source.updated_at }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
class MysqlQueryService
|
||||
include DatasourceUtils
|
||||
attr_accessor :data_query, :data_source, :options, :source_options, :current_user
|
||||
|
||||
def initialize(data_query, options, source_options, current_user)
|
||||
|
|
@ -20,9 +21,19 @@ class MysqlQueryService
|
|||
end
|
||||
|
||||
def process
|
||||
if $connections.include? data_source.id
|
||||
connection = $connections[data_source.id][:connection]
|
||||
else
|
||||
|
||||
connection = get_cached_connection(data_source)
|
||||
connection = create_connection unless connection
|
||||
|
||||
query_text = options['query']
|
||||
|
||||
results = connection.query(query_text)
|
||||
|
||||
{ status: 'success', data: results.to_a }
|
||||
end
|
||||
|
||||
private
|
||||
def create_connection
|
||||
connection = Mysql2::Client.new(
|
||||
host: source_options['host'],
|
||||
username: source_options['username'],
|
||||
|
|
@ -31,13 +42,7 @@ class MysqlQueryService
|
|||
database: source_options['database']
|
||||
)
|
||||
|
||||
$connections[data_source.id] = { connection: connection }
|
||||
cache_connection(data_source, connection)
|
||||
connection
|
||||
end
|
||||
|
||||
query_text = options['query']
|
||||
|
||||
results = connection.query(query_text)
|
||||
|
||||
{ status: 'success', data: results.to_a }
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
class PostgresqlQueryService
|
||||
include DatasourceUtils
|
||||
attr_accessor :data_query, :data_source, :options, :source_options, :current_user
|
||||
|
||||
def initialize(data_query, options, source_options, current_user)
|
||||
|
|
@ -20,6 +21,10 @@ class PostgresqlQueryService
|
|||
end
|
||||
|
||||
def process
|
||||
|
||||
connection = get_cached_connection(data_source)
|
||||
connection = create_connection unless connection
|
||||
|
||||
query_text = ''
|
||||
query_text = if options['mode'] === 'gui'
|
||||
send("generate_#{options['operation']}_query", options)
|
||||
|
|
@ -27,20 +32,6 @@ class PostgresqlQueryService
|
|||
options['query']
|
||||
end
|
||||
|
||||
if $connections.include? data_source.id
|
||||
connection = $connections[data_source.id][:connection]
|
||||
else
|
||||
connection = PG.connect(
|
||||
dbname: source_options['database'],
|
||||
user: source_options['username'],
|
||||
password: source_options['password'],
|
||||
host: source_options['host'],
|
||||
port: source_options['port']
|
||||
)
|
||||
|
||||
$connections[data_source.id] = { connection: connection }
|
||||
end
|
||||
|
||||
result = connection.exec(query_text)
|
||||
{ status: 'success', data: result.to_a }
|
||||
end
|
||||
|
|
@ -67,4 +58,18 @@ class PostgresqlQueryService
|
|||
|
||||
query_text
|
||||
end
|
||||
|
||||
def create_connection
|
||||
connection = PG.connect(
|
||||
dbname: source_options['database'],
|
||||
user: source_options['username'],
|
||||
password: source_options['password'],
|
||||
host: source_options['host'],
|
||||
port: source_options['port']
|
||||
)
|
||||
|
||||
cache_connection(data_source, connection)
|
||||
|
||||
connection
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue