Recently I did a mistake (actually foolishness). From phpMyadmin I dropped a table. Now I'm unable to import that table by this command.
mysqldump --user <my_name> --password=<my_pass> deve_lockstate thermostat_histories < thermostat_histories.sql
I'm also unable to generate migration file cause it's already in ActiveRecord.
Please help me.
Thanks.
You can use
rake db:reset
The rake db:reset task will drop the database, recreate it and load the current schema into it.
You can use this command:
rake --describe db
to describe the available rake db tasks.
Related
I have a mariaDB database with a rails application.
I'm planning to setup the rails application normally first, then use its user interface to create the database data first, then export those data using mysqldump.
mysqldump -u username -p database_name > data-dump.sql
My question is, if I do migration later, can I still load the mysql dump without facing a problem?
For example, if my migration removes a column, should I create a new database backup using mysqldump again?
And is there anything I need to be careful about schema?
When you do a mysqldump, by default the table create statements are included. They are only excluded if you pass in the --no-create-info flag. Since your migrations are just alterations to the tables, you can be assured that when you load your data later, it will have all of your migrations applied to it up to the point in time your database was dumped.
Furthermore, when you run migrations, rails keeps track of which have been run in the schema_migrations table. So if you roll back to a point in time where you had more migrations afterwards, you can re-run rake db:migrate and only those new ones will run, since that data was all part of your backup.
Recently I implemented friendly_id on two models on my local rails application. I created two migrations add_slug_to_categories and add_slug_to_services. And did rest the steps and got friendly_id urls working. Then I pushed the same migrations changes to my production server. And then in production rails console I ran Category.find_each(&:save) and Service.find_each(&:save) and got it working on the production too.
Then for some testing purpose on my local machine I took the dump.sql from the production and and dropped the existing db on my local by running rake db:drop and created by rake db:create and ran rake db:migrate. And then I pushed the dump.sql to the local db. Then when I ran rails s I got migrations pending error. So I went ahead and ran rake db:migrate again. Then I got
== 20170411073744 AddSlugToCategories: migrating ==============================
-- add_column(:categories, :slug, :string)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Duplicate column name 'slug': ALTER TABLE `categories` ADD `slug`
I tried dropping the db and again creating many times. But I keep getting the same issue. Could somebody please tell how can I fix this?
To fix the current issue you need to run ALTER TABLE categories DROP COLUMN slug; in dbconsole, in the future you first need to import dump.sql and then migrate rake db:migrate
Sorry if the question seems too simple, but I am quite new to rails. I generated scaffold in development mode. Then I migrated the database and it edited the mysql app_development table but not the mysql app_production table . Is there a specific command to migrate it also to the production table ?
If what you're saying is that you didn't use migrations to perform some changes and now rake db:migrate doesn't produce the database structure that you want (obviously, because it has no idea that you made those changes), you still can use the schema.rb.
rake db:schema:dump
Will read the db and generate a schema.rb for it. Then you can load this schema.rb on production with
rake db:schema:load RAILS_ENV=production
Also, you can delete everything and create the database from scratch using the schema.rb file with
rake db:reset RAILS_ENV=production
For window just write
db:migrate
and for Linux sudo rake db:migrate
I am using Rails v2.3.2 with MySQL v5.1 and mysql2 gem.
I run the following rake tasks in a method like:
def db_operation
Rake::Task['db:drop'].invoke #this one does not work
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
...
end
but Rake::Task['db:drop'].invoke does not drop my database**, and there is no error message which makes me have no clue to find the reason.
Then:
I go to MySQL command-line to execute "DROP DATABASE my_db;" , it raise me the following error message:
ERROR 1010 (HY000): Error dropping database (can't rmdir './my_db/', errno: 17)
After that:
I run above code again, the database surprisingly get dropped...
What was happening?? Why my rake db:drop does not drop database, but after I run drop command on MySQL command-line and run rake db:drop again, it get dropped??? (and I got error when I run on MySQL command-line)
P.S.
rake db:create and rake db:migrate are working without problem.
Refer this LINK
you might be having files in /var/lib/mysql/my_db/ that mysql didn't create.
Try listing those files and see what's there. Try moving anything there to a temporary directory (or deleting if you're really sure you won't need them), then try again.
I'm following a basic tutorial in Linda.
I have been able to install everything properly now, but when I start my Rails server I get this message when I visit localhost:3000:
Unknown database 'simple_cms_development'
and then
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (23.5ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.8ms)
Have you created the database in MySQL? You should be able to run rake db:create and have Rails create it for you.
I had the same error, please run the following command on the Command Prompt:
rake db:create
to solve the problem.
Look for the answers of these questions:
Have you installed the mysql2 gem?
Is it mentioned in your Gemfile?
Did you run the command rake db:create ?
Sometimes creating database with rake causes issues.
You can also create the database inside mysql
Make sure mysql is in the root %PATH% in command prompt type echo %PATH% to check.
If it isn't in your PATH. Then do a quick google search on windows PATH to get instructions
Open command prompt
type mysql -u root -p
type your password that you created for your root
To create database
create database simple_cms_development
done
was getting the same error but caused was different
Mysql2::Error: Unknown database 'rdddd_development'
/Users/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in connect'
/Users/commeasure/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:ininitialize
faced this error due to the created method dynamically, code is here
Role.all.map(&:name).map(&:parameterize).map(&:underscore).each do |name|
define_method("#{name.to_sym}?") do
role.name == name.upcase
end
end
How do I fix this for the temporary purpose just comment it out