Skip to content

Instantly share code, notes, and snippets.

@glowacki-dev
Last active October 25, 2017 14:56
Show Gist options
  • Save glowacki-dev/7b77d4695ec0b04a8f0fe4aa2af371c0 to your computer and use it in GitHub Desktop.
Save glowacki-dev/7b77d4695ec0b04a8f0fe4aa2af371c0 to your computer and use it in GitHub Desktop.
Non-ORM import with SQL file access
file = "/tmp/#{SecureRandom.uuid}"
select = <<~MySQL
SELECT id + #{@user_offset}, email, name, created_at, updated_at
FROM users
INTO OUTFILE '#{file}_users'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
MySQL
copy = <<~PostgreSQL
COPY users (id, email, name, created_at, updated_at)
FROM '#{file}_users'
WITH CSV;
SELECT setval('users_id_seq', (SELECT MAX(id) FROM users));
PostgreSQL
OLD.run(select)
ActiveRecord::Base.connection.execute(copy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment