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
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.
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.
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.