Skip to content

Commit

Permalink
BREAKING CHANGE: the TruncateProcessor will now RESTART IDENTITY when…
Browse files Browse the repository at this point in the history
… using postgres - this behaviour can be changed by passing :options => 'CONTINUE IDENTITY'
  • Loading branch information
thbar committed Jul 2, 2011
1 parent b794e4f commit 06677cf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,4 +1,5 @@
0.9.5 - unreleased
* BREAKING CHANGE: the TruncateProcessor will now RESTART IDENTITY when using postgres - this behaviour can be changed by passing :options => 'CONTINUE IDENTITY' (thbar)
* New EnsureFieldsPresenceProcessor (thbar)
* New CsvDestination based on FasterCSV (lgustafson)
* Fix #41: Allow cache to be turned off on ForeignKeyLookupTransform
Expand Down
7 changes: 6 additions & 1 deletion lib/etl/processor/truncate_processor.rb
Expand Up @@ -14,16 +14,21 @@ class TruncateProcessor < ETL::Processor::Processor
# Options:
# * <tt>:target</tt>: The target connection
# * <tt>:table</tt>: The table name
# * <tt>:options</tt>: Optional truncate options
def initialize(control, configuration)
super
#@file = File.join(File.dirname(control.file), configuration[:file])
@target = configuration[:target] || {}
@table = configuration[:table]
@options = configuration[:options]
end

def process
conn = ETL::Engine.connection(target)
conn.truncate(table_name)
if conn.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
@options ||= 'RESTART IDENTITY'
end
conn.truncate(table_name, @options)
end

private
Expand Down
1 change: 1 addition & 0 deletions test/connection/mysql/schema.sql
Expand Up @@ -29,6 +29,7 @@ create table person_dimension (

drop table if exists truncate_test;
create table truncate_test (
id int not null primary key auto_increment,
x char(4)
);
insert into truncate_test (x) values ('a');
Expand Down
1 change: 1 addition & 0 deletions test/connection/postgresql/schema.sql
Expand Up @@ -32,6 +32,7 @@ create table person_dimension (

drop table truncate_test;
create table truncate_test (
id SERIAL PRIMARY KEY,
x character varying(4)
);
insert into truncate_test (x) values ('a');
Expand Down
37 changes: 37 additions & 0 deletions test/truncate_processor_test.rb
@@ -0,0 +1,37 @@
require File.dirname(__FILE__) + '/test_helper'

include ETL::Processor

class TruncateTest < ActiveRecord::Base
set_table_name 'truncate_test'
end

class TruncateProcessorTest < Test::Unit::TestCase

def create_item!
TruncateTest.create!(:x => 'ABC')
end

def truncate!(options=nil)
TruncateProcessor.new(nil,
:target => :data_warehouse,
:table => TruncateTest.table_name,
:options => options
).process
end

should 'reset ids by default' do
create_item!
truncate!
assert_equal 1, create_item!.id
end

if ETL::Engine.connection(:data_warehouse).class.name =~ /postgres/i
should 'allow disabling id reset for postgres' do
truncate!
create_item!
truncate!('CONTINUE IDENTITY')
assert_equal 2, create_item!.id
end
end
end

0 comments on commit 06677cf

Please sign in to comment.