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.
Related
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
rake db:drop complains that database does not exist, but actually drops it.
rake db:create complains that database already exists, but actually creates it.
DBMS is MySQL. This behaviour occurs only on one machine. On other servers the same application creates and drops database without any issues.
When you run rake db:migrate or db:create without mentioning any environment then by default rails try to drop/create dev/test databases;
it could occur when your one of db either test or dev is not present when you are trying to drop and one of db is present when you are trying to create.
drop the both db (test/dev) and then rake db:create, it won't throw error.and after this run rake db:drop, will work fine.
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.
OK, I just re-installed MySQL Servers 5.5(32bit to 64bit) on my Windows 7 64-bit machine. I then took the steps to use mysql2 for rails:
gem 'mysql2', '0.2.6'
gem 'rails', '3.0.7'
Step 1:
rake db:drop
Couldn't drop store_development : #<Mysql2::Error: Unknown database 'store_development'>
Strange because in the MySQL Workbench I only see the test database up.
Step 2:
$ rake db:migrate
rake aborted!
Unknown database 'store_development'
No idea why I get this error so I try to reset the database now.
Step 3:
rake db:reset
(in C:/store)
Couldn't drop store_development : #<Mysql2::Error: Unknown database 'store_development'>
store_test already exists
-- create_table("business_online_stores", {:force=>true})
-> 0.1110s
-- create_table("business_retail_stores", {:force=>true})
-> 0.0740s
-- create_table("businesses", {:force=>true})
-> 0.0900s
-- create_table("products", {:force=>true})
-> 0.0810s
-- create_table("user_prices", {:force=>true})
-> 0.0720s
-- create_table("users", {:force=>true})
-> 0.1810s
-- initialize_schema_migrations_table()
-> 0.2800s
-- assume_migrated_upto_version(0, "db/migrate")
-> 0.0320s
You have 6 pending migrations:
20110909175650 DeviseCreateUsers
20110929161120 CreateUserPrices
20111004023516 CreateProducts
20111007023521 CreateBusinesses
20111009215120 CreateBusinessRetailStores
20111009215419 CreateBusinessOnlineStores
Run "rake db:migrate" to update your database then try again.
Yes, the Test database does already exist but where is this Ghost development database at? How does it reset the migrations, yet tell me to migrate them again. Ok, ill do as it says now:
Step 4:
$ rake db:migrate
(in C:/store)
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'users' already exists: CREATE TABLE `users`
Step 5:
$ rake db:migrate
rake aborted!
Unknown database 'store_development'
Impossible, I went inside of my MySQL Workbench and dropped the test and development databases by right-clicking them and going through the necessary steps so nothing exist even when I refresh it, yet I get this error about a database that doesn't exist.
How do I fix this problem?
Note: 0.2.7 Gives me the same issues.
Note: These are my first 2 migration files just encase anyone wanted to see them: https://gist.github.com/1341682
Just a thought, but does Users already have entries in the email column? I wonder if an existing duplicate entry would prevent the addition of the validation :unique=>true
And just to check, did you want to do?:
add_column :users, :email, :string
instead of add_index?
The only way to solve this that I know of was to recreate my whole application.
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