2020-08-07 01:14:14 +02:00
|
|
|
class CreateGroups < ActiveRecord::Migration[4.2]
|
2009-01-06 11:49:19 +01:00
|
|
|
GROUP_ADMIN = 'Administrators'
|
|
|
|
GROUP_ORDER = 'Sample Order Group'
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def self.up
|
|
|
|
create_table :groups do |t|
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :type, :string, null: false # inheritance, types: Group, OrderGroup
|
|
|
|
t.column :name, :string, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
t.column :description, :string
|
2021-03-01 15:27:26 +01:00
|
|
|
t.column :actual_size, :integer # OrderGroup column
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :account_balance, :decimal, precision: 8, scale: 2, null: false, default: 0 # OrderGroup column
|
2021-03-01 15:27:26 +01:00
|
|
|
t.column :account_updated, :timestamp # OrderGroup column
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :created_on, :timestamp, null: false
|
|
|
|
t.column :role_admin, :boolean, default: false, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
add_index(:groups, :name, unique: true)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
create_table :memberships do |t|
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :group_id, :integer, null: false
|
|
|
|
t.column :user_id, :integer, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
add_index(:memberships, %i[user_id group_id], unique: true)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Create the default "Administrators" group...
|
|
|
|
puts "Creating group #{GROUP_ADMIN}..."
|
2023-05-12 13:01:12 +02:00
|
|
|
Group.create(name: GROUP_ADMIN, description: 'System administrators.', role_admin: true)
|
2009-01-06 11:49:19 +01:00
|
|
|
raise 'Failed!' unless administrators = Group.find_by_name(GROUP_ADMIN)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Create a sample order group...
|
|
|
|
puts "Creating order group #{GROUP_ORDER}..."
|
2023-05-12 13:01:12 +02:00
|
|
|
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)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
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)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
puts "Joining #{CreateUsers::USER_ADMIN} user to new '#{administrators.name}' group as a group admin..."
|
2023-05-12 13:01:12 +02:00
|
|
|
membership = Membership.create(group: administrators, user: admin)
|
|
|
|
raise 'Failed!' unless admin.memberships.first == membership
|
2009-01-06 11:49:19 +01:00
|
|
|
raise "User #{CreateUsers::USER_ADMIN} has no admin_roles" unless admin.role_admin?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
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)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
puts "Joining #{CreateUsers::USER_TEST} user to new '#{ordergroup.name}' group as a group admin..."
|
2023-05-12 13:01:12 +02:00
|
|
|
membership = Membership.create(group: ordergroup, user: test)
|
|
|
|
raise 'Failed!' unless test.memberships.first == membership
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.down
|
|
|
|
drop_table :groups
|
|
|
|
drop_table :memberships
|
|
|
|
end
|
|
|
|
end
|