ROR Database and MIgration .

[Migration, Database Setting, DB Import/Export, Association, Model]
[SQL]

DB DUMP :- (Import/Expert)

Export one DB
$> mysql -u root -p database_name > dump_file {syntax}
$> mysqldump -u root -p gps_whatif > gps_whatif_db.sql
$> Password

Export one table
$> mysql -u root -p database_name table_name > dump_file {syntax}
$> mysqldump -u root -p gps_whatif products > gps_pro.sql
$> Password

Import products table into DB
$> mysql -u root -p database_name < dump_file {syntax}
$> mysql -u root -p gps_whatif < gps_pro.sql
$> Password


ROR Migration :-

What is Migration & Its Advantage ?

Migrations are a convenient way for you to alter your database in a structured and organized manner.
Rails Migration allows you to use Ruby to define changes to your database schema.
Making it possible to use a version control system to keep things synchronized with the actual code.

Adv:-
Teams of developers : If one person makes a schema change, the other developers just need to update, and run "rake migrate".
Production servers : Run "rake migrate" when you roll out a new release to bring the database up to date as well.
Multiple machines: If you develop on both a desktop and a laptop, or in more than one location, migrations can help you keep them all synchronized.

List out what can Rails Migration do?
  • Create table
  • Drop table
  • Rename table
  • Add column
  • Rename column
  • Change column
  • Remove column and so on

What are the different process and syntax of migration ?

$> rails g migration filename {syntax}
$> rails g migration addNameToUser
$> rails g migration addNameToUser name:string
N:- only migration file will generate
$> rails g model modelname {syntax}
$> rails g model user name:string age:string

N:- modelname must be singular, by this command we will get both model & migration file generated. After that we can change migration file asper need create table, add column, rename etc


[Migration file change]

1/Creating table name

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :firstname
t.timestamps null: true
end
end
end

2/ Add a Column to Table

class AddColumnToOrganisation < ActiveRecord::Migration
def change
add_column :table_name, :column_name, :data_type
end
end

3/ Rename column name of a Table

rename_column :table_name, :old_name, :new_name

4/ Change data type of a column

change_column :table_name, :column_name, :new_datatype

5/ Drop a column of a table

remove_column :table_name, :column_name

6/ Drop a table

drop_table :table_name

7/ add new features to column like Uniqueness

add_index :users, :fname, unique: true



Database Setting :-
project/config/database.yml

default: &default
adapter: mysql2
pool: 5
timeout: 5000

development:
<<: *default
database: sample_test
username: root
password: orbio123

production:
<<: *default
database: sample_test
username: root
password: orbio123

N:- In database.yml we need to mension adapter: mysql2 either in default: or development:
and we need to add gem in Gemfile ---- gem 'mysql2'
and bundle install


Multiple database in single application

project/config/database.yml

default: &default
adapter: mysql2
pool: 5
timeout: 5000

development:
<<: *default
database: sample_test
username: root
password: orbio123

development_sec:
adapter: mysql2
database: sample
username: root
password: orbio123
host: localhost

production_sec:
<<: *default
database: sample
username: root
password: orbio123

N :- development: is default env so we do not need any connection explicitly .
But for production_sec: env we need to connection explicitly as below

project/app/model/group.rb (groups table in sample_test database)

class Group < ActiveRecord::Base
establish_connection "#{Rails.env}_sec"
end

[Migration for Second Database]

$> rails g migration subgroup

project/db/migrate/migrationfile.rb

class CreateConections < ActiveRecord::Migration
def connection
ActiveRecord::Base.establish_connection("#{Rails.env}_sec").connection
end

def change
create_table :conections do |t|
t.timestamps null: false
end
end

end
N :- connection action can not be any other action.

Comments

Popular Posts