/
chapter03code.txt
99 lines (73 loc) · 2 KB
/
chapter03code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'rubygems'
require_gem 'activerecord'
if ARGV[0] =~ /VERSION=\d+/
version = ARGV[0].split('=')[1].to_i
else
version = nil
end
@logger = Logger.new $stderr
ActiveRecord::Base.logger = @logger
ActiveRecord::Base.colorize_logging = false
@config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
ActiveRecord::Base.establish_connection(@config["development"])
ActiveRecord::Migrator.migrate("", version)
class CreateUsersTable < ActiveRecord::Migration
def self.up
end
def self.down
end
end
# Script to create the first version of a Cows table
def self.up
create_table :cows do |t|
t.column :name, :string
t.column :breed, :string
t.column :born_on, :datetime
t.column :milkable, :boolean
end
end
mysql> show tables;
Empty set (0.12 sec)
mysql> describe cows;
mysql> show tables;
mysql> select * from schema_info;
def self.down
drop_table :cows
end
mysql> show tables;
def self.up
add_column :cows, :farmer_id, :integer
end
def self.down
remove_column :cows, :farmer_id
end
mysql> describe cows;
rename_column :cows, :born_on, :created_on
change_column :address, :postal_code, :string
add_index :cows, :farmer_id, :name => "index_on_cows_for_farmers"
add_index :ownerships, [:farmer_id, :tractor_id], :unique => true
remove_index :ownerships, :column => [:farmer_id, :tractor_id]
remove_index :cows, :name => "index_on_cows_for_farmers"
def self.up
add_column :orders, :total, :decimal
orders = Order.find(:all)
orders.each do |order|
subtotal = 0
order.line_items.each do |line_item|
subtotal += line_item.quantity * line_item.price
end
order.total = subtotal
end
end
def self.down
remove_column :orders, :total
end
def self.up
add_column :orders, :total, :decimal
ActiveRecord.execute("UPDATE orders LEFT OUTER JOIN line_items
SET orders.total=SUM(line_items.quantity * line_items.price) ON
orders.id=line_items.order_id")
end
def self.down
remove_column :orders, :total
end