I am using PostgreSQL with ruby and rails app and have Postgres_copy gem installed
https://github.com/diogob/postgres-copy
I have hotels table and want to import text file using function pg_copy_from the gem. Data in the file is separated by the "|" symbol.
So I have created rake task
namespace :db do
namespace :import do
desc "Copy hotels records to the database"
task :hotels => :environment do
Hotel.pg_copy_from 'db/ActivePropertyList.txt', :delimiter => '|', :map => {
'HotelID' => 'ean_hotel_id',
'SequenceNumber' => 'sequence_number',
'Name' => 'name',
'Address' => 'address'}
end
end
end
If i run rake db:import:hotels nothing happens. What could be the problem?
UPDATE
I noticed that when I run psql and try to see all realtions by \d command, it says "No relations found."
However, I ran rake:db:create:all and rake db:migrate commands and can see the schema file. Also, \l command in psql console shows my database.
Maybe it would be easier just to run
c = Hotel.connection.raw_connection
c.exec(%q{\copy hotels (hotel_id, name) FROM 'db/ActivePropertyList.txt' DELIMITER '|'})
, but I suggest nothing happens because of the missing relations.
My import failed because of the upper-case issue that is common for the PostgreSQL. Renaming all header fields and database column names to lower case did the trick.
Related
Following the instructions in https://stackoverflow.com/a/24496452/102675 I wound up with the following:
namespace :db do
desc 'Drop, create, migrate, seed and populate sample data'
task seed_sample_data: [:drop, :create, :migrate, :seed, :populate_sample_data] do
puts 'Sample Data Populated. Ready to go!'
end
desc 'Populate the database with sample data'
task populate_sample_data: :environment do
puts Inspector.column_names.include?('how_to_fix')
# create my sample data
end
end
As you would expect, I get true if I run bundle exec rake db:populate_sample_data
BUT if I run bundle exec rake db:seed_sample_data I get all the migration output and then false. In other words I can't see the Inspector attribute how_to_fix even though it definitely exists as proved by the other rake run. Where did my attribute go?
My guess is that this is a "caching" problem. Can you try the following?
task populate_sample_data: :environment do
Inspector.reset_column_information
# ...
end
P.S. We used to have a similar problem working with different databases having the exact same schema (only except some columns here and there)
Is there any conventions of rails or a right way to create/manipulate a table that will contain just one row?
If not, what is the best way to do that?
I need a way to store configurations of the system.
Thanks.
Edited:
The rake db:seed command, basically execute whatever code you write in db/seeds.rb file of your application. Though can write any code in this file, by convention you should write code which populate your database with the basic data,
for example: when ever your deploy your application somewhere, and create a new database for it, you want that user with admin credential must be present there. So you will write the code which create that user in this file. Below is the sample code which will create a user and assign admin role to him.
puts "********Seeding Data Start************"
admin = User.create(:first_name => 'System', :last_name => 'Admin',
:email => 'systemadmin#sunpower.com', :password => 'sunpoweradmin',
:password_confirmation => 'sunpoweradmin', :source_system_id => 'systemadmin',
:source_system => 'LP',:entity_type => "Customer", :target_system => "OPENAM")
if admin.errors.blank?
puts "***User #{admin.first_name} #{admin.last_name} created ***"
admin.add_role :admin # add_role is method defined by rolify gem
puts "***admin role assigned to #{admin.first_name} #{admin.last_name}***"
else
puts "admin user failed to create due to below reasons:"
admin.errors.each do |x, y|
puts"#{x} #{y}" # x will be the field name and y will be the error on it
end
end
puts "********Seeding Data End************"
Now whenever you recreate your database, you just need to run below command to populate the database, with the basic data
$ rake db:seed RAILS_ENV=production
The correct order to setup database in production, with all the rake task available within db namespace is as below
$rake db:create RAILS_ENV=production
$rake db:migrate RAILS_ENV=production
$ rake db:seed RAILS_ENV=production
NOTE: You can replace the first two commands with $rake db:setup RAILS_ENV=production , it will run both create and migrate internally
OR
You could use the rails-settings-cached gem which is a fork of the rails-settings gem
Once setup, you'll be able to do things such as:
Setting.foo = 123
Setting.foo # returns 123
Hope this may help you or what you are looking for..
I'm trying to create some seed data and got this code from Railcasts. I've modified it slightly but doesn't seem to be working when i run bundle exec rake db:seed from the terminal. I get the following error in the terminal...
wrong number of arguments (0 for 1)
Below is my code in the seeds.rb file to populate the table. Is there a silly mistake in there somewhere?
require 'open-uri'
International.delete.all
open("international.txt") do |countries|
countries.read.each_line do |data|
code, country, currency = data.chomp.split("|")
International.create!(:code => code, :country => country, :currency => currency)
end
end
and my text file (stored in the same directory as the seeds.rb file is...
AU|Australia|AUD
CA|Canada|CAD
GB|United Kingdom|GBP
US|United States|USD
You need to pass an id to delete.
I assume you want to delete_all
International.delete_all
Project is the model name and I want to do something like:
Project.create(:name => 'projectname', :identifier => 'projectidentifier')
This should be done in the terminal through a ruby script. I am not going to use rails console to create it nor use seeds.rb in a db file to migrate this as rake db:seed.
Can someone help. Thanks
The easiest way would be using rails runner (which essentially loads rails):
rails runner your_script.rb
The line of code would be a content of that script.
What about a rake task?
on lib/tasks, create a file named data.rake, and the content:
namespace :data
desc "Create project data"
task create_project_data: :environment do
Project.create(name: 'projectname', identifier: 'projectidentifier')
end
end
And you can run it as any rake task
rake data:create_project_data
And it will also appear when you list your rake tasks
rake -T
hi i m new to ROR i need to how to import following data to table City in Database
This file is in libs>>tasks>>
desc "Import City to ActiveRecord table"
task :city_import, [:argument] => :environment do
puts "Importing City ***"
City.create(:name=>'Balaghat',:state_id=>State.find_by_name('Madhya Pradesh'));
City.create(:name=>'Belgaum',:state_id=>State.find_by_name('Karnataka'));
end
i try to using rake in terminal but not working
In terminal go to your app and type
rake city_import