Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions db/migrate/20160420152740_create_counter_columns_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateCounterColumnsTable < ActiveRecord::Migration
def change
create_table :counter_columns do |t|
t.string :data
t.integer :update_count, default: 0
end
end
end
7 changes: 6 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150710162236) do
ActiveRecord::Schema.define(version: 20160420152740) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -22,6 +22,11 @@
t.string "data"
end

create_table "counter_columns", force: :cascade do |t|
t.string "data"
t.integer "update_count", default: 0
end

create_table "reserved_word_models", force: :cascade do |t|
t.string "select", limit: 255
t.string "group", limit: 255
Expand Down
8 changes: 7 additions & 1 deletion lib/postgres_upsert/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def initialize(klass, source, options = {})
:delimiter => ",",
:header => true,
:unique_key => [primary_key],
:update_only => false})
:update_only => false,
:counter_column => nil})
@options[:unique_key] = Array.wrap(@options[:unique_key])
@source = source.instance_of?(String) ? File.open(source, 'r') : source
@columns_list = get_columns
Expand Down Expand Up @@ -68,6 +69,10 @@ def quoted_table_name
@klass.quoted_table_name
end

def counter_column
@options[:counter_column]
end

def get_columns
columns_list = @options[:columns] ? @options[:columns].map(&:to_s) : []
if @options[:header]
Expand Down Expand Up @@ -145,6 +150,7 @@ def update_set_clause
"\"#{col}\" = t.\"#{col}\""
end
command << "\"updated_at\" = '#{DateTime.now.utc}'" if column_names.include?("updated_at")
command << "\"#{counter_column}\" = \"#{counter_column}\" + 1" if counter_column
"SET #{command.join(',')}"
end

Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/comma_with_two_lines.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,data
1,test data 1
2,test data 2
4 changes: 4 additions & 0 deletions spec/fixtures/counter_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'postgres_upsert'

class CounterColumn < ActiveRecord::Base
end
20 changes: 20 additions & 0 deletions spec/pg_upsert_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,25 @@ def timestamp
end

end

context 'counter column update' do
before(:each) do
ActiveRecord::Base.connection.execute %{
TRUNCATE TABLE counter_columns;
}
end
it "increments the counter column on updated rows" do
CounterColumn.create(id: 1, data: "test data 1")
PostgresUpsert.write CounterColumn, File.expand_path('spec/fixtures/comma_with_two_lines.csv'), :counter_column => :update_count

expect(
CounterColumn.find(1).update_count
).to eq(1)

expect(
CounterColumn.find(2).update_count
).to eq(0)
end
end
end

1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'fixtures/three_column'
require 'fixtures/reserved_word_model'
require 'fixtures/composite_key_model'
require 'fixtures/counter_column'
require 'rspec'
require 'rspec/rails'
require 'rspec/autorun'
Expand Down