Rake db:migrate aborted! Ghost development database using MySQL2? - mysql

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.

Related

rake aborted! Mysql2::Error: Duplicate column name 'slug': ALTER TABLE `categories`

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

What is the best approach to change mysql into postgresql in a ruby Project

Sorry, I am a new ruby programmer. I have a project that is using MySql database structure, and I want to change the MysqlDB to PostgreSQL. Is it possible to do this, if so how..?
What I this is, if I have to change the database.yml and change this specific file, when I run rake db:create and rake db:migrate this will work without problems..
I have changed to PostgreSQL, already. When I run rake db:create I don't get any errors, but when I run rake db:migrate I get the following error :
rake db:migrate
== 20151021060955 CreatePackages: migrating ===================================
-- create_table(:packages)
-> 0.0079s
-- execute("ALTER TABLE packages AUTO_INCREMENT = 100000")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
"PG::SyntaxError: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 1: ALTER TABLE packages AUTO_INCREMENT = 100000"
The problem being, AUTO_INCREMENT is in mysql, I want to do AUTO_INCREMENT in postgresql as well. Here is the migrate package class I'm using:
class CreatePackages < ActiveRecord::Migration
def change
create_table :packages do |t|
t.string :name
t.decimal :total
t.attachment :avatar
t.timestamps
end
execute("ALTER TABLE packages AUTO_INCREMENT = 100000")
end
end
How can I do the same thing using PostgreSQL?
Seem the problem with your migration with AUTO_INCREMENT that use only by mysql not using with pg alternative of AUTO_INCREMENT is SERIAL in pg so could your replace execute("ALTER TABLE packages AUTO_INCREMENT = 100000") to execute("SELECT setval('packages _id_seq', 100000)") in your migration file should that work.

rake db:drop and db:create do the tasks with failure

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.

migrating database from development to production - rails

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

'rake db:drop' does not work

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.