I am following this question to resolve an invalid input syntax for type timestamp
when I try to copy an empty end_at
column from a csv file into my Postgres table.
The last answer recommended (I am running a 4.2 Rails app) I create temporary table where I copy as a string field and then INSERT INTO the real table using the temporary table data. Because I will be doing this alot I just ran a migration to keep a permanent temp_table (that I truncate after each import). My code is below but I can't figure out how to do the INSERT INTO on the end_at column so that the blank strings come through as null timestamps.
Any hints would be great.
def pg_import data
ActiveRecord::Base.connection.execute("truncate temp_pricelist_price_groups")
ActiveRecord::Base.connection.execute("truncate pricelist_price_groups")
conn = ActiveRecord::Base.connection_pool.checkout
raw = conn.raw_connection
raw.exec("COPY temp_pricelist_price_groups (
pricelist_id,
price_group_id,
uom_id,
quantity,
price,
disc_dollar,
disc_percent,
price_flag,
gross_or_net,
start_at,
end_at
) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', null '', HEADER true)")
# open up your CSV file looping through line by line and getting the line into a format suitable for pg's COPY...
ticker = 0
counter = 0
success_counter = 0
failed_records = []
data = CSV.parse(data)
data.shift
data.each_with_index do |line, index|
line = line.to_csv
raw.put_copy_data line
counter += 1
end
# once all done...
raw.put_copy_end
raw.exec("INSERT INTO pricelist_price_groups
SELECT *
FROM temp_pricelist_price_groups)")
while res = raw.get_result do; end # very important to do this after a copy
postgresql_error_message = raw.error_message
ActiveRecord::Base.connection_pool.checkin(conn)
ActiveRecord::Base.connection.execute('truncate temp_pricelist_price_groups')
return { :csv => false, :item_count => counter, :processed_successfully => counter, :errored_records => failed_records, :error_message => postgresql_error_message }
end