From bb089d8ab30bc3b210d41563872357e65ece0d8b Mon Sep 17 00:00:00 2001 From: navaneeth Date: Fri, 14 May 2021 12:39:38 +0530 Subject: [PATCH] Feature: Purge cached connections on data source update --- app/services/concerns/datasource_utils.rb | 22 +++++++++++++++ app/services/mysql_query_service.rb | 27 +++++++++++-------- app/services/postgresql_query_service.rb | 33 +++++++++++++---------- 3 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 app/services/concerns/datasource_utils.rb diff --git a/app/services/concerns/datasource_utils.rb b/app/services/concerns/datasource_utils.rb new file mode 100644 index 0000000000..1e7fd1ff4e --- /dev/null +++ b/app/services/concerns/datasource_utils.rb @@ -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 + \ No newline at end of file diff --git a/app/services/mysql_query_service.rb b/app/services/mysql_query_service.rb index 25a07ef281..981b54de60 100644 --- a/app/services/mysql_query_service.rb +++ b/app/services/mysql_query_service.rb @@ -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 diff --git a/app/services/postgresql_query_service.rb b/app/services/postgresql_query_service.rb index d04b2250ff..364356e27e 100644 --- a/app/services/postgresql_query_service.rb +++ b/app/services/postgresql_query_service.rb @@ -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