Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance usage example? Temp tables #51

Open
nfedyashev opened this issue Feb 8, 2020 · 1 comment
Open

Enhance usage example? Temp tables #51

nfedyashev opened this issue Feb 8, 2020 · 1 comment

Comments

@nfedyashev
Copy link

I'm using postgres-copy for importing CSV files in tempory tables that are later used for parsing line by line(record by record).

That's why such ActiveRecord models & tables could not be created upfront.
I believe that might be not such a rare use case.

That's the workaround that I'm currently using:

    table_name = "upload_#{SecureRandom.hex}"
    ActiveRecord::Base.connection.create_table table_name, id: :integer do |t|
      columns.each do |column_name|
        t.string column_name.to_sym
      end
    end

    klass = Class.new(ActiveRecord::Base) do
      acts_as_copy_target
      self.table_name = table_name
    end
    klass.copy_from file_path.to_s, columns: columns

Please let me know if you may need this as an example in README, so I'll prepare a PR.
HTH

@diogob
Copy link
Owner

diogob commented Feb 8, 2020

Actually this brings to my attention a problem with the interface defined by this gem, a simpler class that takes a database connection and the copy parameters would be a more solid base to build the ActiveRecord behaviour upon.

The PR withe example in the README is a good idea. But I'm also keeping this open to improve on the interface exposed by the gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants