Posts Tagged “migration”

I ran into an issue earlier where adding a sub project to an already existing Redmine project produced an error and after a little bit of troubleshooting determined that upgrading Redmine would resolve the issue. During the upgrade process I ran into some other issues including a specific migration error using rake that is noted below. The upgrade process involved upgrading Redmine 0.8.3 to Redmine 0.9.3.

Read the rest of this entry »

Tags: , , , , , , , , , , , , , , , , , , ,

Comments 2 Comments »

The other day I did some optimization work on a PostgreSQL database and at the end needed to create migrations so the changes I was going to make could be deployed on multiple servers. It was my first shot at migrations so I wanted to note for myself and anyone else that may find it useful how to create Ruby migrations for dropping and adding Indexes on PostgreSQL tables. Below you will find the basic concept for creating a migration that adds a Postgres table index and information on removing a table index which is just the reverse of adding.

Read the rest of this entry »

Tags: , , , , , , , , , , , , ,

Comments No Comments »

The regular scaffold generator (e.g. ruby script/generate scaffold model controller) requires you to create your model and controller first. The scaffold_resource generator, on the other hand, generates the (RESTful) model and controller for you. For example:

ruby script/generate scaffold_resource Caregiver first_name:string last_name:string

In Rails 2.0, use resource instead:

ruby script/generate resource Caregiver first_name:string last_name:string

Here’s everything you get:

Views for all the CRUD operations and a layout

create app/views/caregivers/index.rhtml

create app/views/caregivers/show.rhtml

create app/views/caregivers/new.rhtml

create app/views/caregivers/edit.rhtml

create app/views/layouts/caregivers.rhtml


create app/models/caregiver.rb

Controller with methods for CRUD operations

create app/controllers/caregivers_controller.rb


create app/helpers/caregivers_helper.rb

Looks like this initially

module CaregiversHelper


Migration to create the table in the database

create db/migrate/003_create_caregivers.rb

Looks like this:

class CreateCaregivers < ActiveRecord::Migration

  def self.up

    create_table :caregivers do |t|

      t.column :first_name, :string

      t.column :last_name, :string

      t.column :address, :string

      t.column :city, :string

      t.column :state, :string

      t.column :home_phone, :string

      t.column :work_phone, :string

      t.column :cell_phone, :string

      t.column :relationship, :string

      t.column :email, :string


  end  def self.down

    drop_table :caregivers



Sets up route in config/routes.rb for REST

map.resources :caregivers

Tags: , , , , , , , ,

Comments 1 Comment »

PostgreSQL 8.2.5 error

== CreateHeartrates: migrating ================================================

– create_table(:heartrates)

NOTICE: CREATE TABLE will create implicit sequence “heartrates_id_seq” for serial column “”

rake aborted!

PGError: ERROR: multiple default values specified for column “id” of table “heartrates”

: CREATE TABLE heartrates (“id” serial primary key DEFAULT NULL, “user_id” integer DEFAULT NULL, “timestamp” timestamp with time zone DEFAULT NULL, “heartrate” smallint NOT NULL)


Read the rest of this entry »

Tags: , , , , , , ,

Comments 1 Comment »


After installing PostgreSQL on Windows, I converted my Rails application from MySQL to PostgreSQL with the following steps:

  1. Create a new database in PostgreSQL
  2. Installed postgres-pr gem (Ruby driver) because the postgres gem (native driver) didn’t work. Will figure out why later on.
  3. Changed the appropriate Rails config stuff in database.yml
      adapter: postgresql
      database: database_name
      username: postgres
      password: password
      host: localhost
  4. I ran only my first Rails migration to test that it worked using this command: rake db:migrate VERSION=1. My first migration (001_create_heartrates.rb) creates a table in the database so it will be easy to test.

Robby explains a quick way to migrate the data as well


You can test that the migration worked via 2 methods (GUI or command line)

  1. Using following commands with the psql command line utility (Start Menu -> PostgreSQL -> psql to postgres):
    psql commmand Description
    \? help
    \c database_name connect to a database (many other commands won’t work until you execute this)
    \dt show tables (execute \c first)
    \d table_name describes a particular table (e.g. table_name)
  2. Using the pgAdmin III GUI:
    • The database table is deeply nested in the tree, I missed it at first. Go to PostgreSQL Database Server 8.2 -> Databases -> database_name -> Schemas -> public -> Tables -> table_name
Tags: , , , , , , , , , , ,

Comments No Comments »