diff --git a/db/migrate/20160420152740_create_counter_columns_table.rb b/db/migrate/20160420152740_create_counter_columns_table.rb new file mode 100644 index 0000000..8d6ed07 --- /dev/null +++ b/db/migrate/20160420152740_create_counter_columns_table.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 77f73c7..3e3bfe4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/lib/postgres_upsert/writer.rb b/lib/postgres_upsert/writer.rb index f40bc3a..4d7085b 100644 --- a/lib/postgres_upsert/writer.rb +++ b/lib/postgres_upsert/writer.rb @@ -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 @@ -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] @@ -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 diff --git a/spec/fixtures/comma_with_two_lines.csv b/spec/fixtures/comma_with_two_lines.csv new file mode 100644 index 0000000..738f4dd --- /dev/null +++ b/spec/fixtures/comma_with_two_lines.csv @@ -0,0 +1,3 @@ +id,data +1,test data 1 +2,test data 2 \ No newline at end of file diff --git a/spec/fixtures/counter_column.rb b/spec/fixtures/counter_column.rb new file mode 100644 index 0000000..ae84ebf --- /dev/null +++ b/spec/fixtures/counter_column.rb @@ -0,0 +1,4 @@ +require 'postgres_upsert' + +class CounterColumn < ActiveRecord::Base +end diff --git a/spec/pg_upsert_csv_spec.rb b/spec/pg_upsert_csv_spec.rb index f0cb204..40a849c 100644 --- a/spec/pg_upsert_csv_spec.rb +++ b/spec/pg_upsert_csv_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1112afb..7462e29 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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'