Skip to content

Instantly share code, notes, and snippets.

@muneebaahmad
Last active January 25, 2019 01:21
Show Gist options
  • Save muneebaahmad/26bf679884b14fc3375bb9074530a18c to your computer and use it in GitHub Desktop.
Save muneebaahmad/26bf679884b14fc3375bb9074530a18c to your computer and use it in GitHub Desktop.
Parses a csv to look for email addresses. Then checks the sql table to see which emails exist and then returns those users ids.
require 'mysql2'
require 'csv'
# Returns the user ids for emails that already exist
class Users
CSV_FILENAME = 'emails.csv'.freeze
EMAIL_COLUMN_NUMBER = 0
def user_ids
ids = []
results = client.query(
'SELECT id, first_name, last_name, email
FROM users WHERE email IN (' + emails.join(', ') + ')'
)
results.each do |row|
ids.push(row['id'])
end
puts ids.join(', ') # return comma separated ids
end
def emails
@emails = []
CSV.foreach(CSV_FILENAME) do |row|
@emails.push(row[EMAIL_COLUMN_NUMBER])
end
@emails # return the array
end
def client
@client ||= Mysql2::Client.new(
host: 'localhost',
username: 'root',
database: 'test_database'
)
end
end
Users.new.user_ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment