Rails Generate Model with out Migration Files - mysql

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

Related

Existing .rb files to create local MySQL database

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

Eror: Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

I'm using:
rails 4.2.1;
ruby 2.1.5p273 (2014-11-13 revision 48405) [i386-mingw32];
spree 3.0.1
database: MySQL
After installing spree success, I let it run, then it appears above error.I've used: bundle exec rake test:prepare, bundle exec rake db:migrate it show
when I run this command: bundle exec rake db:migrate:reset, the error appears:
When I install Spree and use SQLite, it runs fine. But I don't want to use the SQLite database in my web application.
UPDATE
20150522034041_rename_permalink_to_slug_for_products.spree.rb file
# This migration comes from spree (originally 20140106224208)
class RenamePermalinkToSlugForProducts < ActiveRecord::Migration
def change
rename_column :spree_products, :permalink, :slug
end
end
I don't think Migrations file wrong because it worked well on SQLite

rake db:migrate, source and destination

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

How to create a capistrano task to download latest database_backup.tgz and import locally?

I'm trying to do something with a Capistrano task that is similar to the heroku db:pull functionality if you are familiar with it.
I have a remote server. On that server I have a bunch of backups in the /path/db_backups/ folder. And in that folder there's a backup of the database everyday.
All I want to do is
Download the latest backup on the client machine.
Untar it.
Import it into local mysql db.
Anyone know of a good way to handle this? Is there a gem I am unaware of? Is there a script you have handy?
I'm not sure if there is a gem for that. I usually copy/pastle this task on capistrano (config/deploy.rb) to pull a compressed database from the server and store it on my development environment
namespace :utils do
desc 'pull the DB from the server'
task :pull_db, :roles => :db, :only => { :primary => true } do
website = "http://www.my_website.com"
filename = "#{application}.dump.#{Time.now.to_f}.sql"
filename_bz2 = "#{filename}.bz2"
remote_file = "#{current_path}/public/#{filename_bz2}"
text = capture "cat #{deploy_to}/current/config/database.yml"
yaml = YAML::load(text)
on_rollback { run "rm #{remote_file}" }
run "mysqldump -h#{yaml[rails_env]['host']} -u #{yaml[rails_env]['username']} -p #{yaml[rails_env]['database']} | bzip2 -c > #{remote_file}" do |ch, stream, out|
ch.send_data "#{yaml[rails_env]['password']}\n" if out =~ /^Enter password:/
end
local_text = run_locally("cat config/database.yml")
local_yaml = YAML::load(local_text)
run_locally("wget #{website}/#{filename_bz2}")
run_locally("bzip2 -d #{filename_bz2}")
run_locally("bundle exec rake db:drop")
run_locally("bundle exec rake db:create")
if local_yaml['development']['password'] && !local_yaml['development']['password'].blank?
run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} -p#{local_yaml['development']['password']} #{local_yaml['development']['database']} < #{filename}")
else
run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} #{local_yaml['development']['database']} < #{filename}")
end
run_locally("rm #{filename}")
run "rm #{remote_file}"
end
end
The following script should achieve that:
# Find out which file to copy and save its name in a local text file:
# ssh allows you to specify a command that should be executed on the remote
# machine instead of opening a terminal session on it. I use this to get
# a sorted (ls -t sorts by modification date) list of all backups. I then
# truncate this list to one entry using head -1 and save the file name in a
# local file (filename.txt).
# (12.34.56.78 is a placeholder for the ip/hostname of your server)
ssh 12.34.56.78 ls -t /path/to/backups/ | head -1 > filename.txt
# Copy the backup specified in filename.txt to the tmp dir on your local machine.
scp 12.34.56.78:/path/to/backups/`cat filename.txt` /tmp/db_backup.sql.tar
# Untar the backup archive.
cd /tmp && tar -xf db_backup.sql.tar
# Import into database of choice.
mysql -u your_username -p database_to_import_to < /tmp/db_backup.sql
(This assumes that you are on a UNIX system and have scp and tar installed...)

Rails, how to migrate data from development sqlite3 database to production MySQL database?

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