-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.rb
63 lines (57 loc) · 3.29 KB
/
database_setup.rb
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
require 'pg'
puts "Establishing connection to database ..."
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
encoding: 'unicode',
pool: 5,
database: '##############',
username: '##############',
password: '##############',
host: '#########################################',
port: 5432,
min_messages: 'error'
)
puts "Now connected to database\n"
ActiveRecord::Schema.define do
unless ActiveRecord::Base.connection.table_exists?(:contacts) &&
ActiveRecord::Base.connection.table_exists?(:phone_numbers)
puts "Setting up Database (creating tables) ..."
create_table :contacts do |table|
table.column :firstname, :string
table.column :lastname, :string
table.column :email, :string
table.timestamps
end
create_table :phone_numbers do |table|
table.column :phonenumber, :string
table.column :numbertype, :string
table.references :contact
table.timestamps
end
Contact.create(firstname: 'Alan', lastname: 'Hodges', email: '[email protected]')
dylan = Contact.create(firstname: 'Dylan', lastname: 'May', email: '[email protected]')
Contact.create(firstname: 'Jennifer', lastname: 'Churchill', email: '[email protected]')
warren = Contact.create(firstname: 'Warren', lastname: 'Pullman', email: '[email protected]')
colin = Contact.create(firstname: 'Colin', lastname: 'Jackson', email: '[email protected]')
Contact.create(firstname: 'Colin', lastname: 'Chapman', email: '[email protected]')
Contact.create(firstname: 'Michael', lastname: 'Clarkson', email: '[email protected]')
lily = Contact.create(firstname: 'Lily', lastname: 'Slater', email: '[email protected]')
Contact.create(firstname: 'Sam', lastname: 'Hudson', email: '[email protected]')
Contact.create(firstname: 'Sonia', lastname: 'Harris', email: '[email protected]')
Contact.create(firstname: 'Emma', lastname: 'Paterson', email: '[email protected]')
Contact.create(firstname: 'Sue', lastname: 'Jackson', email: '[email protected]')
Contact.create(firstname: 'Connor', lastname: 'Hughes', email: '[email protected]')
Contact.create(firstname: 'Theresa', lastname: 'North', email: '[email protected]')
Contact.create(firstname: 'Stephen', lastname: 'Gibson', email: '[email protected]')
Contact.create(firstname: 'Zoe', lastname: 'Harris', email: '[email protected]')
Contact.create(firstname: 'Steven', lastname: 'Short', email: '[email protected]')
Contact.create(firstname: 'Dorothy', lastname: 'Walsh', email: '[email protected]')
Contact.create(firstname: 'Boris', lastname: 'Bell', email: '[email protected]')
Contact.create(firstname: 'Victor', lastname: 'Mitchell', email: '[email protected]')
PhoneNumber.create(phonenumber: '604-555-0001', numbertype: 'Mobile', contact_id: dylan.id)
PhoneNumber.create(phonenumber: '604-555-0002', numbertype: 'Office', contact_id: warren.id)
PhoneNumber.create(phonenumber: '604-555-0003', numbertype: 'Fax', contact_id: lily.id)
PhoneNumber.create(phonenumber: '604-555-1000', numbertype: 'Mobile', contact_id: colin.id)
PhoneNumber.create(phonenumber: '604-555-1001', numbertype: 'Home', contact_id: colin.id)
end
end