lib/wcc/contentful/store/postgres_store.rb in wcc-contentful-1.2.0 vs lib/wcc/contentful/store/postgres_store.rb in wcc-contentful-1.2.1

- old
+ new

@@ -43,16 +43,13 @@ else val = result.getvalue(0, 0) JSON.parse(val) if val end - if views_need_update?(value, previous_value) - # mark dirty - we need to refresh the materialized view - unless mutex.with_read_lock { @dirty } - _instrument 'mark_dirty' - mutex.with_write_lock { @dirty = true } - end + if views_need_update?(value, previous_value) && !mutex.with_read_lock { @dirty } + _instrument 'mark_dirty' + mutex.with_write_lock { @dirty = true } end previous_value end @@ -103,11 +100,11 @@ @connection_pool.with { |conn| conn.exec_prepared('refresh_views_concurrently') } end end end - logger&.debug('[PostgresStore] ' + statement + "\n" + params.inspect) + logger&.debug("[PostgresStore] #{statement}\n#{params.inspect}") _instrument 'exec' do @connection_pool.with { |conn| conn.exec(statement, params) } end end @@ -229,33 +226,29 @@ table = 'contentful_raw_includes' select_statement += ', t.includes' end statement = - select_statement + - " FROM #{table} AS t \n" + - joins.join("\n") + "\n" + - statement + - (limit_statement || '') + "#{select_statement} FROM #{table} AS t \n#{joins.join("\n")}\n#{statement}#{limit_statement || ''}" [statement, params] end - def _eq(path, expected, params) + def _eq(path, expected, params) # rubocop:disable Layout/LineContinuationLeadingSpace return " AND t.id = $#{push_param(expected, params)}" if path == %w[sys id] if path[3] == 'sys' # the path can be either an array or a singular json obj, and we have to dig # into it to detect whether it contains `{ "sys": { "id" => expected } }` expected = { 'sys' => { path[4] => expected } }.to_json return ' AND fn_contentful_jsonb_any_to_jsonb_array(t.data->' \ - "#{quote_parameter_path(path.take(3))}) @> " \ - "jsonb_build_array($#{push_param(expected, params)}::jsonb)" + "#{quote_parameter_path(path.take(3))}) @> " \ + "jsonb_build_array($#{push_param(expected, params)}::jsonb)" end - " AND t.data->#{quote_parameter_path(path)}" \ - " @> to_jsonb($#{push_param(expected, params)})" + " AND t.data->#{quote_parameter_path(path)} " \ + "@> to_jsonb($#{push_param(expected, params)})" end PARAM_TYPES = { String => 'text' @@ -290,11 +283,11 @@ end def push_join(_path, joins) table_alias = "s#{joins.length}" joins << "JOIN contentful_raw AS #{table_alias} ON " \ - "#{table_alias}.id=ANY(t.links)" + "#{table_alias}.id=ANY(t.links)" table_alias end end EXPECTED_VERSION = 2 @@ -324,12 +317,12 @@ end end end def schema_ensured?(conn) - result = conn.exec('SELECT version FROM wcc_contentful_schema_version' \ - ' ORDER BY version DESC LIMIT 1') + result = conn.exec('SELECT version FROM wcc_contentful_schema_version ' \ + 'ORDER BY version DESC LIMIT 1') return false if result.num_tuples == 0 result[0]['version'].to_i >= EXPECTED_VERSION rescue PG::UndefinedTable # need to run v1 schema migration @@ -337,11 +330,11 @@ end def ensure_schema(conn) result = begin - conn.exec('SELECT version FROM wcc_contentful_schema_version' \ - ' ORDER BY version DESC') + conn.exec('SELECT version FROM wcc_contentful_schema_version ' \ + 'ORDER BY version DESC') rescue PG::UndefinedTable [] end 1.upto(EXPECTED_VERSION).each do |version_num| next if result.find { |row| row['version'].to_s == version_num.to_s }