foodsoft/db/migrate/002_create_groups.rb

55 lines
2.6 KiB
Ruby
Raw Normal View History

class CreateGroups < ActiveRecord::Migration[4.2]
2009-01-06 11:49:19 +01:00
GROUP_ADMIN = 'Administrators'
GROUP_ORDER = 'Sample Order Group'
2009-01-06 11:49:19 +01:00
def self.up
create_table :groups do |t|
t.column :type, :string, :null => false # inheritance, types: Group, OrderGroup
t.column :name, :string, :null => false
t.column :description, :string
t.column :actual_size, :integer # OrderGroup column
t.column :account_balance, :decimal, :precision => 8, :scale => 2, :null => false, :default => 0 # OrderGroup column
t.column :account_updated, :timestamp # OrderGroup column
2009-01-06 11:49:19 +01:00
t.column :created_on, :timestamp, :null => false
t.column :role_admin, :boolean, :default => false, :null => false
end
add_index(:groups, :name, :unique => true)
2009-01-06 11:49:19 +01:00
create_table :memberships do |t|
t.column :group_id, :integer, :null => false
t.column :user_id, :integer, :null => false
end
add_index(:memberships, [:user_id, :group_id], :unique => true)
2009-01-06 11:49:19 +01:00
# Create the default "Administrators" group...
puts "Creating group #{GROUP_ADMIN}..."
Group.create(:name => GROUP_ADMIN, :description => "System administrators.", :role_admin => true)
raise 'Failed!' unless administrators = Group.find_by_name(GROUP_ADMIN)
2009-01-06 11:49:19 +01:00
# Create a sample order group...
puts "Creating order group #{GROUP_ORDER}..."
ordergroup = OrderGroup.create!(:name => GROUP_ORDER, :description => "A sample order group created by the migration.", :actual_size => 1, :account_updated => Time.now)
raise "Wrong type created!" unless ordergroup.is_a?(OrderGroup)
2009-01-06 11:49:19 +01:00
# Get the admin user and join the admin group...
raise "User #{CreateUsers::USER_ADMIN} not found, cannot join group '#{administrators.name}'!" unless admin = User.find_by_nick(CreateUsers::USER_ADMIN)
2009-01-06 11:49:19 +01:00
puts "Joining #{CreateUsers::USER_ADMIN} user to new '#{administrators.name}' group as a group admin..."
membership = Membership.create(:group => administrators, :user => admin)
raise "Failed!" unless admin.memberships.first == membership
raise "User #{CreateUsers::USER_ADMIN} has no admin_roles" unless admin.role_admin?
2009-01-06 11:49:19 +01:00
# Get the test user and join the order group...
raise "User #{CreateUsers::USER_TEST} not found, cannot join group '#{ordergroup.name}'!" unless test = User.find_by_nick(CreateUsers::USER_TEST)
2009-01-06 11:49:19 +01:00
puts "Joining #{CreateUsers::USER_TEST} user to new '#{ordergroup.name}' group as a group admin..."
membership = Membership.create(:group => ordergroup, :user => test)
raise "Failed!" unless test.memberships.first == membership
end
def self.down
drop_table :groups
drop_table :memberships
end
end