I'm new to ruby and mysql.
I was told to execute two commands below:
mysql -u root; then CREATE DATABASE sd
rake db:migrate.
The sd database was empty when created. After I ran the 2nd command, sd is full of items.
I'm wondering how rake knows that the destination is sd and what the source is.
I know that there are some scripts under db/migrate folder, so I guess rake knows who the destination is from such newly-created(I assume, 'cause I'm new to ruby) scripts. But how about the source?
Thanks!
The source of the data will normally be controlled by the db/migrate/*.rb files as you are aware of.
But they may have hooked another task onto db:migrate via the Rakefile or lib/tasks/*.rake files so "rake db:mirgrate" may also runs some extra tasks. A common task that adds seed information is the rake db:seed task, which normally runs db/seeds.rb .
When I use db/seeds.rb I typically put my seed data in db/fixtures/*.yml, but others may have different places.
on newer rails you can also use rake db:create to do the database creation (assuming the user in database.yml has sufficient privileges). rake -T db will tell you wnat tasks have been named with db in them, eg:
$ rake -T db
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:seed_fu # Loads seed data for the current environment.
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql
rake db:test:seed # seed the test database
rake db:version # Retrieves the current schema version number
It looks like you are trying to work with a Rails application. I would suggest looking at config/database.yml. That file holds configuration data for test, development, and production environments. By default, rails uses the development environment. You will likely find the answer in config/database.yml under development section.
Duck
All information about your database is on the file config/database.yml. This doc can help you: http://guides.rubyonrails.org/configuring.html#configuring-a-database
And you can change this:
mysql -u root; then CREATE DATABASE sd
for that:
rake db:create
Related
I downloaded source code from BitBucket and I am trying to set up a local MySQL database with an existing .rb files in RubyMine. Is there a direct way to do so or do I need to set up all the tables manually?
You can create the database for a Rails project in one command:
$ rails db:create
(for Rails 5 only; if you're using Rails <= 4, s/rails/rake/)
You'll also need to load the schema in, which is one more command:
$ rails db:schema:load
If you have seeding data in seeds.rb, you can load that with one further command:
$ rails db:seed
I just cloned an application from my developer and I want to set it up on my computer. I get this error when I go to localhost:3000, and I don't really understand what it means.
Error:
ActiveRecord::StatementInvalid - Mysql2::Error: Table
'goacquire_development.sessions' doesn't exist: SHOW FULL FIELDS FROM `sessions`:
I think it could be my installation process, I'll appreciate if someone could explain this error to me.
A few ideas:
Make sure MySQL is running on your local machine.
Make sure you have run rake db:create and rake db:migrate.
Make sure you have a database.yml file configured with MySQL settings.
Hope this helps.
Install MySQL server on your machine
Config database.yml (Create new if your app don't have.)
Run commands:
# cd /path/to/your/app
# bundle install
# rake db:create
# rake db:migrate
# rails s
I have created the data modeling and imported the sql file and generated the schema for all my models as below. Now i want to generate the models for all my tables in the MySQl with out the migration files.
Also i have two namescopes and want to use the same model for all the controllers with different namescope.
# Create a new Rails Project
rails new <project-name> -d mysql
# Run Bundler
bundle install
# Database Initiation
rake db:create
# Dump SQL file into MySQL
mysql -u hmv -p <database-name> < <database-file>
# Generate SQL Schema from the MySQL tables
rake db:schema:dump
# Creating a Git Repo for Versioning and Collaboration
git init
git remote add origin https://<Username>:<Password>#bitbucket.org/harshamv/<Project-Name>.git
git add .
git commit -m 'Initial commit'
git push -u origin master
# Create the Initial Migration File
rails generate migration initial_schema_dump
# Copy the Content from the Schema file to the migration file and run the following command
rake db:migrate
You can use the --skip_migration option
rails g model MyModel --skip_migration
invoke active_record
create app/models/my_model.rb
invoke test_unit
create test/models/my_model_test.rb
create test/fixtures/my_models.yml
With Rails, how do you migrate data from development sqlite3 database to production MySQL database?
How to make it easier?
You should use a gem like YamlDB. Install the Gem and then use the following rake tasks
rake db:data:dump
RAILS_ENV=production rake db:data:load
The first command dumps the contents of dev database to a file called db/data.yml
Also, please remember that this must be used in addition to rake db:schema:dump|load tasks as this only ports the data assuming the schema is already in place
Assuming your database configurations are properly set up in config/database.yml, the following should get the database structure set up in production for you.
Runs against development database by default:
rake db:schema:dump
Run this against your production database by virtue of the RAILS_ENV environment variable:
rake RAILS_ENV=production db:schema:load
How to do?
I tried something like:
RAILS_ENV=production rake db:create db:load
in file /lib/tasks/load_tasks.rake and this file I tried in terminal as rake db:migrate, but I am getting errors about syntax etc.
I entered into terminal this command (I saw it in tutorial):
rails generate scaffold Account user_name:string description:text premium:boolean \
income:integer ranking:float fee:decimal birthday:date login_time:time
And this made me file 20110518181941_create_accounts.rb
How can I create database table - I thought the command above will create me database in mysql... I am now a bit confusing, what to do?
Which rule is playing here rake db:migrate?
I think you are getting this error because your syntax is wrong, please put && between sentences:
RAILS_ENV=production rake db:create && db:schema:load
or do it in separated lines
RAILS_ENV=production rake db:create
RAILS_ENV=production rake db:schema:load
the first command will create the database, the second command will Load the db/schema.rb file into the database
And finally you need to run your migration:
RAILS_ENV=production rake db:migrate
to create your Accounts table.
BTW if you run:
rake -T
you can see the list of rake tasks and their descriptions.
Hope this helps.