Set up a project Rake file

In your project, you’ll need to create a Rakefile. As part of the ActiveRecord gem, it uses rake to make a bunch of helper methods - quick useful commands available from the command line. These commands let us setup, modify and change the database structure easily; and without ever touching the database directly.

Similar to the Gemfile, your Rakefile is saved with that exact name and no file extension. First things first, create a new file called Rakefile in your project folder. And open it in your text editor.

Our Rakefile for this project needs to include a couple of tasks necessary to prepare our database. We just need to tell rake that we want to use these command line utilities that ActiveRecord offers to us. We require them in our Rakefile, along with our main app.

Add the following to the Rakefile:

````# require your app file first require ‘./app’ require ‘sinatra/activerecord/rake’


_Sidebar:_ Rake is simply a command line task runner, meaning that it runs small scripts when prompted to do so from the command line. 

------


## Try out Rake

Now run 

```rake -T````

You should see a list of the commands involved. This is what Rake can help with. These are a bunch of basic commands 

rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_E… rake db:create_migration # Create a migration (parameters: NAME, VERSION) rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV… rake db:environment:set # Set the environment value for the database rake db:fixtures:load # Loads fixtures into the current environment’s database rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) 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:cache:clear # Clears a db/schema_cache.dump file rake db:schema:cache:dump # Creates a db/schema_cache.dump file rake db:schema:dump # Creates a db/schema.rb file that is portable against any DB supported by Active Record rake db:schema:load # Loads a schema.rb file into the database rake db:seed # Loads the seed data from db/seeds.rb rake db:setup # Creates the database, loads the schema, and initializes with the seed data (use db:re… rake db:structure:dump # Dumps the database structure to db/structure.sql rake db:structure:load # Recreates the databases from the structure.sql file rake db:version # Retrieves the current schema version number


But there's only a few that you need to pay real attention to

rake db:create rake db:create_migration rake db:seed
rake db:migrate
rake db:rollback


## Create your DB with Rake

Open a command prompt in your project folder and type 

rake db:create ````

Et voila your database should now be in your projects db folder

×

Subscribe

The latest tutorials sent straight to your inbox.