We often use rake db:reset command for recreating the database, instead of conventional 3-steps for doing the same.
- rake db:drop (dropping the Database, wipe away all your data)
- rake db:create (Create the Database)
- rake db:migrate (Runs all the migrations, present in your rails project)
while what rake db:reset do, is drop your database, create your database and then runs the schema.rb file.
rake db:reset actually performs same thing, which all the three statements will do, but behind the curtains.
Main difference between both is reset uses schema.rb file to built table in database while the other runs all the migrations.
It differs in some cases, when we are using methods of databases(like SQL, POSTGRES) in our migrations
for eg:
class Durations < ActiveRecord::Migration
def change
create_table :durations do |t|
t.string :car_name
t.column :time_taken, :interval
t.timestamps
end
end
end
here, we have used interval(literal of Database, not ROR's) and when this migration runs it will create a field named time_taken of type interval
CREATE TABLE durations
(
id serial NOT NULL,
car_name character varying,
time_taken interval,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
CONSTRAINT durations_pkey PRIMARY KEY (id)
)
but in schema.rb file, we have time_taken as a string field.
ActiveRecord::Schema.define(version: 20160428123153) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "durations", force: :cascade do |t|
t.string "car_name"
t.string "time_taken"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Now, when we reset out database
then in durations table, we got
CREATE TABLE durations
(
id serial NOT NULL,
car_name character varying,
time_taken character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
CONSTRAINT durations_pkey PRIMARY KEY (id)
)
0 Comment(s)