Updated the migration files so that any updates to rows are done via ruby rather than raw SQL. With any luck at all, this might fix the various problems people encounter when trying to use rake migrate on different databases. At any rate, it should be a lot more portable between databases.

Also added some ruby code to the fixture files so that boolean columns will be set correctly depending on database adapter, and also the created_at and other date or datetime fields are set dynamically.

Finally, I also changed the names of the old migration files to match the new naming convention (001_filename.rb, 002_filename.rb etc.)

With the new rake syntax, you can run:

rake db:migrate

to create the tables (for MySQL create the db first, but with sqlite/sqlite3 you just have to list the name in database.yml, but you don't need to create the db itself). Then,

rake db:fixtures:load

if you want some example contents.

I'm HOPING that this fixes #261, #268 and even #205 which I closed because I gave up. I've tested rake db:migrate and rake db:fixtures:load by creating new mysql and sqlite3 databases, and that works fine on my system.



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@233 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
bsag 2006-05-02 21:47:30 +00:00
parent 03ff56d703
commit 6dc32aa83b
15 changed files with 169 additions and 104 deletions

View file

@ -0,0 +1,16 @@
class AddUserId < ActiveRecord::Migration
def self.up
add_column :contexts, :user_id, :integer, :default => 1
add_column :projects, :user_id, :integer, :default => 1
add_column :todos, :user_id, :integer, :default => 1
Context.find(:all).each { |context| context.user_id = 1 }
Project.find(:all).each { |project| project.user_id = 1 }
Todo.find(:all).each { |todo| todo.user_id = 1 }
end
def self.down
remove_column :contexts, :user_id
remove_column :projects, :user_id
remove_column :todos, :user_id
end
end

View file

@ -3,10 +3,10 @@ class CreatedAt < ActiveRecord::Migration
# if the column names use symbols instead of strings. # if the column names use symbols instead of strings.
# <http://dev.rubyonrails.org/changeset/2731> # <http://dev.rubyonrails.org/changeset/2731>
def self.up def self.up
rename_column "todos", "created", "created_at" rename_column :todos, :created, :created_at
end end
def self.down def self.down
rename_column "todos", "created_at", "created" rename_column :todos, :created_at, :created
end end
end end

View file

@ -1,6 +1,6 @@
class AddPreferencesToUserTable < ActiveRecord::Migration class AddPreferencesToUserTable < ActiveRecord::Migration
def self.up def self.up
add_column "users", "preferences", :text add_column :users, :preferences, :text
@users = User.find(:all) @users = User.find(:all)
@users.each do |u| @users.each do |u|
u.preferences = { "date_format" => "%d/%m/%Y", "week_starts" => "1", "no_completed" => "5", "staleness_starts" => "7", "due_style" => "1", "admin_email" => "butshesagirl@rousette.org.uk"} u.preferences = { "date_format" => "%d/%m/%Y", "week_starts" => "1", "no_completed" => "5", "staleness_starts" => "7", "due_style" => "1", "admin_email" => "butshesagirl@rousette.org.uk"}
@ -9,6 +9,6 @@ class AddPreferencesToUserTable < ActiveRecord::Migration
end end
def self.down def self.down
remove_column "users", "preferences" remove_column :users, :preferences
end end
end end

View file

@ -1,16 +0,0 @@
class AddUserId < ActiveRecord::Migration
def self.up
add_column "contexts", "user_id", :integer, :default => 1
add_column "projects", "user_id", :integer, :default => 1
add_column "todos", "user_id", :integer, :default => 1
execute "UPDATE 'contexts' SET 'user_id' = 1;"
execute "UPDATE 'projects' SET 'user_id' = 1;"
execute "UPDATE 'todos' SET 'user_id' = 1;"
end
def self.down
remove_column :contexts, :user_id
remove_column :projects, :user_id
remove_column :todos, :user_id
end
end

View file

@ -5,26 +5,26 @@
ActiveRecord::Schema.define(:version => 8) do ActiveRecord::Schema.define(:version => 8) do
create_table "contexts", :force => true do |t| create_table "contexts", :force => true do |t|
t.column "name", :string, :default => "", :null => false t.column "name", :string, :null => false
t.column "hide", :integer, :limit => 4, :default => 0, :null => false t.column "position", :integer, :null => false
t.column "position", :integer, :default => 0, :null => false t.column "hide", :boolean, :default => false
t.column "user_id", :integer, :default => 1 t.column "user_id", :integer, :default => 1
end end
create_table "notes", :force => true do |t| create_table "notes", :force => true do |t|
t.column "user_id", :integer, :default => 0, :null => false t.column "user_id", :integer, :null => false
t.column "project_id", :integer, :default => 0, :null => false t.column "project_id", :integer, :null => false
t.column "body", :text t.column "body", :text
t.column "created_at", :datetime t.column "created_at", :datetime, :default => Sat Jan 01 00:00:00 GMT 2000
t.column "updated_at", :datetime t.column "updated_at", :datetime, :default => Sat Jan 01 00:00:00 GMT 2000
end end
create_table "projects", :force => true do |t| create_table "projects", :force => true do |t|
t.column "name", :string, :default => "", :null => false t.column "name", :string, :null => false
t.column "position", :integer, :default => 0, :null => false t.column "position", :integer, :null => false
t.column "done", :integer, :limit => 4, :default => 0, :null => false t.column "done", :boolean, :default => false
t.column "user_id", :integer, :default => 1 t.column "user_id", :integer, :default => 1
t.column "description", :text t.column "description", :text, :default => ""
end end
create_table "sessions", :force => true do |t| create_table "sessions", :force => true do |t|
@ -36,24 +36,24 @@ ActiveRecord::Schema.define(:version => 8) do
add_index "sessions", ["session_id"], :name => "sessions_session_id_index" add_index "sessions", ["session_id"], :name => "sessions_session_id_index"
create_table "todos", :force => true do |t| create_table "todos", :force => true do |t|
t.column "context_id", :integer, :default => 0, :null => false t.column "context_id", :integer, :null => false
t.column "description", :string, :limit => 100, :default => "", :null => false t.column "project_id", :integer
t.column "description", :string, :null => false
t.column "notes", :text t.column "notes", :text
t.column "done", :integer, :limit => 4, :default => 0, :null => false t.column "done", :boolean, :default => false, :null => false
t.column "created_at", :datetime t.column "created_at", :datetime, :default => Sat Jan 01 00:00:00 GMT 2000
t.column "due", :date t.column "due", :date
t.column "completed", :datetime t.column "completed", :datetime
t.column "project_id", :integer
t.column "user_id", :integer, :default => 1 t.column "user_id", :integer, :default => 1
t.column "type", :string, :default => "Immediate", :null => false t.column "type", :string, :default => "Immediate", :null => false
t.column "show_from", :date t.column "show_from", :date
end end
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.column "login", :string, :limit => 80 t.column "login", :string, :limit => 80, :null => false
t.column "password", :string, :limit => 40 t.column "password", :string, :limit => 40, :null => false
t.column "word", :string t.column "word", :string
t.column "is_admin", :integer, :limit => 4, :default => 0, :null => false t.column "is_admin", :boolean, :default => false, :null => false
t.column "preferences", :text t.column "preferences", :text
end end

View file

@ -1,64 +1,80 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<%
# This writes the fixture boolean columns appropriately
# depending on the database adapter
#
abcs = ActiveRecord::Base.configurations
case abcs[RAILS_ENV]["adapter"]
when "mysql", "oci"
b_true = 1
b_false = 0
when "postgresql", "sqlite", "sqlite3"
b_true = 't'
b_false = 'f'
else
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
%>
agenda: agenda:
id: 1 id: 1
name: agenda name: agenda
position: 1 position: 1
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
call: call:
id: 2 id: 2
name: call name: call
position: 2 position: 2
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
email: email:
id: 3 id: 3
name: email name: email
position: 3 position: 3
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
errand: errand:
id: 4 id: 4
name: errand name: errand
position: 4 position: 4
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
lab: lab:
id: 5 id: 5
name: lab name: lab
position: 5 position: 5
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
library: library:
id: 6 id: 6
name: library name: library
position: 6 position: 6
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
freetime: freetime:
id: 7 id: 7
name: freetime name: freetime
position: 7 position: 7
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
office: office:
id: 8 id: 8
name: office name: office
position: 8 position: 8
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1
waitingfor: waitingfor:
id: 9 id: 9
name: waiting for name: waiting for
position: 9 position: 9
hide: 0 hide: <%= b_false %>
user_id: 1 user_id: 1

View file

@ -1,5 +0,0 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
first:
id: 1
another:
id: 2

View file

@ -1,5 +0,0 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
first:
id: 1
another:
id: 2

View file

@ -1,15 +1,27 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<%
def today
Time.now.to_s(:db)
end
def next_week
1.week.from_now.to_s(:db)
end
def last_week
1.week.ago.to_s(:db)
end
%>
first_notes: first_notes:
id: 1 id: 1
user_id: 1 user_id: 1
project_id: 1 project_id: 1
body: Need to collect a catalogue from Time Machines R Us body: Need to collect a catalogue from Time Machines R Us
created_at: 2006-04-09 12:09:00 created_at: <%= today %>
updated_at: 2006-04-09 12:09:00 updated_at: <%= today %>
another_notes: another_notes:
id: 2 id: 2
user_id: 1 user_id: 1
project_id: 1 project_id: 1
body: Should I go for a swirly effect or a whooshy one? body: Should I go for a swirly effect or a whooshy one?
created_at: 2006-04-08 11:01:05 created_at: <%= today %>
updated_at: 2006-04-08 11:01:05 updated_at: <%= today %>

View file

@ -1,11 +1,26 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<%
# This writes the fixture boolean columns appropriately
# depending on the database adapter
#
abcs = ActiveRecord::Base.configurations
case abcs[RAILS_ENV]["adapter"]
when "mysql", "oci"
b_true = 1
b_false = 0
when "postgresql", "sqlite", "sqlite3"
b_true = 't'
b_false = 'f'
else
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
%>
timemachine: timemachine:
id: 1 id: 1
name: Build a working time machine name: Build a working time machine
description: '' description: ''
position: 1 position: 1
done: 0 done: <%= b_false %>
user_id: 1 user_id: 1
moremoney: moremoney:
@ -13,7 +28,7 @@ moremoney:
name: Make more money than Billy Gates name: Make more money than Billy Gates
description: '' description: ''
position: 2 position: 2
done: 0 done: <%= b_false %>
user_id: 1 user_id: 1
gardenclean: gardenclean:
@ -21,5 +36,5 @@ gardenclean:
name: Evict dinosaurs from the garden name: Evict dinosaurs from the garden
description: '' description: ''
position: 3 position: 3
done: 0 done: <%= b_false %>
user_id: 1 user_id: 1

View file

@ -1,4 +1,36 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<%
def today
Time.now.to_s(:db)
end
def next_week
1.week.from_now.to_s(:db)
end
def last_week
1.week.ago.to_s(:db)
end
def two_weeks_hence
2.week.from_now.to_s(:db)
end
# This writes the fixture boolean columns appropriately
# depending on the database adapter
#
abcs = ActiveRecord::Base.configurations
case abcs[RAILS_ENV]["adapter"]
when "mysql", "oci"
b_true = 1
b_false = 0
when "postgresql", "sqlite", "sqlite3"
b_true = 't'
b_false = 'f'
else
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
%>
1: 1:
id: 1 id: 1
@ -6,9 +38,9 @@
project_id: 2 project_id: 2
description: Call Bill Gates to find out how much he makes per day description: Call Bill Gates to find out how much he makes per day
notes: ~ notes: ~
done: 0 done: <%= b_false %>
created_at: 2004-11-28 16:01:00 created_at: <%= last_week %>
due: 2004-10-30 due: <%= two_weeks_hence %>
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -18,9 +50,9 @@
project_id: 3 project_id: 3
description: Call dinosaur exterminator description: Call dinosaur exterminator
notes: Ask him if I need to hire a skip for the corpses. notes: Ask him if I need to hire a skip for the corpses.
done: 0 done: <%= b_false %>
created_at: 2004-11-28 16:06:08 created_at: <%= today %>
due: 2004-11-30 due: <%= two_weeks_hence %>
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -31,9 +63,9 @@
description: Buy milk description: Buy milk
notes: ~ notes: ~
done: 1 done: 1
created_at: 2004-11-28 16:06:31 created_at: <%= today %>
due: ~ due: ~
completed: 2004-11-28 completed: <%= today %>
user_id: 1 user_id: 1
4: 4:
@ -43,9 +75,9 @@
description: Buy bread description: Buy bread
notes: ~ notes: ~
done: 1 done: 1
created_at: 2004-11-28 16:06:58 created_at: <%= today %>
due: ~ due: ~
completed: 2004-11-28 completed: <%= today %>
user_id: 1 user_id: 1
5: 5:
@ -54,8 +86,8 @@
project_id: 1 project_id: 1
description: Construct time dilation device description: Construct time dilation device
notes: ~ notes: ~
done: 0 done: <%= b_false %>
created_at: 2004-11-28 16:07:33 created_at: <%= today %>
due: ~ due: ~
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -66,9 +98,9 @@
project_id: 1 project_id: 1
description: Phone Grandfather to ask about the paradox description: Phone Grandfather to ask about the paradox
notes: Added some _notes_. notes: Added some _notes_.
done: 0 done: <%= b_false %>
created_at: 2004-11-28 16:08:33 created_at: <%= today %>
due: 2004-12-30 due: <%= last_week %>
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -78,8 +110,8 @@
project_id: 3 project_id: 3
description: Get a book out of the library description: Get a book out of the library
notes: 'Dinosaurs''R' notes: 'Dinosaurs''R'
done: 0 done: <%= b_false %>
created_at: 2004-12-22 14:07:06 created_at: <%= today %>
due: ~ due: ~
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -90,10 +122,10 @@
project_id: ~ project_id: ~
description: Upgrade to Rails 0.9.1 description: Upgrade to Rails 0.9.1
notes: ~ notes: ~
done: 1 done: <%= b_false %>
created_at: 2004-12-20 17:02:52 created_at: <%= today %>
due: 2004-12-21 due: <%= today %>
completed: 2004-12-20 completed: ~
user_id: 1 user_id: 1
9: 9:
@ -102,9 +134,9 @@
project_id: ~ project_id: ~
description: This should be due today description: This should be due today
notes: ~ notes: ~
done: 0 done: <%= b_false %>
created_at: 2004-12-31 17:23:06 created_at: <%= today %>
due: 2004-12-31 due: <%= today %>
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -115,9 +147,9 @@
description: foo description: foo
notes: ~ notes: ~
done: 1 done: 1
created_at: 2004-12-31 18:38:34 created_at: <%= today %>
due: 2005-01-05 due: 2005-01-05
completed: 2005-01-02 12:27:10 completed: <%= today %>
user_id: 1 user_id: 1
11: 11:
@ -126,8 +158,8 @@
project_id: 2 project_id: 2
description: Buy shares description: Buy shares
notes: ~ notes: ~
done: 0 done: <%= b_false %>
created_at: 2005-01-01 12:40:26 created_at: <%= today %>
due: 2005-02-01 due: 2005-02-01
completed: ~ completed: ~
user_id: 1 user_id: 1
@ -138,10 +170,10 @@
project_id: 3 project_id: 3
description: Buy stegosaurus bait description: Buy stegosaurus bait
notes: ~ notes: ~
done: 1 done: <%= b_false %>
created_at: 2005-01-01 12:53:12 created_at: <%= today %>
due: 2005-01-02 due: <%= next_week %>
completed: 2005-01-01 15:44:19 completed: ~
user_id: 1 user_id: 1
13: 13:
@ -150,10 +182,10 @@
project_id: 3 project_id: 3
description: New action in context description: New action in context
notes: Some notes notes: Some notes
done: 1 done: <%= b_false %>
created_at: 2005-01-02 14:52:49 created_at: <%= today %>
due: 2005-03-01 due: <%= next_week %>
completed: 2005-01-02 15:44:19 completed: ~
user_id: 1 user_id: 1
14: 14:
@ -162,8 +194,8 @@
project_id: 2 project_id: 2
description: Call stock broker description: Call stock broker
notes: 'tel: 12345' notes: 'tel: 12345'
done: 0 done: <%= b_false %>
created_at: 2005-01-03 11:38:25 created_at: <%= last_week %>
due: ~ due: ~
completed: ~ completed: ~
user_id: 1 user_id: 1