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
Related
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
In my current application i am using PostgreSQL Data base,
but I want to change the PostgreSQL database into MYSQL DB.
if it's impossible ?
Step 1
Make a backup copy of your data
For Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db
For Rails 2.x install the YAML DB plugin:
script/plugin install git://github.com/adamwiggins/yaml_db.git
Run the dump task
rake db:dump
Step 2
Update your config/database.yml file.
Step 3 :
gem install mysql
Have rake create your database
rake db:create
rake db:schema:load
Step 4
Use YamlDb to reload your data into MySql
rake db:load
this is a duplicate
Migrate database from Postgres to MySQL
dont forget to change the gems and your database config file to something like this:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
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
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