Last active
October 25, 2017 14:56
-
-
Save glowacki-dev/7b77d4695ec0b04a8f0fe4aa2af371c0 to your computer and use it in GitHub Desktop.
Non-ORM import with SQL file access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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