rename migrations part 1

This commit is contained in:
zmagod 2017-01-26 16:30:31 +01:00
parent a507bb4d1f
commit c7b03926c0
19 changed files with 123 additions and 123 deletions

View file

@ -1,11 +1,11 @@
class CreateOrganizations < ActiveRecord::Migration class CreateOrganizations < ActiveRecord::Migration
def change def change
create_table :organizations do |t| create_table :teams do |t|
## General info ## General info
t.string :name, null: false t.string :name, null: false
t.timestamps null: false t.timestamps null: false
end end
add_index :organizations, :name, unique: true add_index :teams, :name, unique: true
end end
end end

View file

@ -4,10 +4,10 @@ class CreateProjects < ActiveRecord::Migration
t.string :name, null: false t.string :name, null: false
t.column :visibility, :integer, null: false, default: 0 t.column :visibility, :integer, null: false, default: 0
t.datetime :due_date t.datetime :due_date
t.integer :organization_id, null: false t.integer :team_id, null: false
t.timestamps null: false t.timestamps null: false
end end
add_foreign_key :projects, :organizations add_foreign_key :projects, :teams
end end
end end

View file

@ -1,9 +1,9 @@
class CreateLogs < ActiveRecord::Migration class CreateLogs < ActiveRecord::Migration
def change def change
create_table :logs do |t| create_table :logs do |t|
t.integer :organization_id, null: false t.integer :team_id, null: false
t.string :message, null:false t.string :message, null: false
end end
add_foreign_key :logs, :organizations add_foreign_key :logs, :teams
end end
end end

View file

@ -5,13 +5,13 @@ class CreateSamples < ActiveRecord::Migration
# Foreign keys # Foreign keys
t.integer :user_id, null: false t.integer :user_id, null: false
t.integer :organization_id, null: false t.integer :team_id, null: false
t.timestamps null: false t.timestamps null: false
end end
add_foreign_key :samples, :users add_foreign_key :samples, :users
add_foreign_key :samples, :organizations add_foreign_key :samples, :teams
add_index :samples, :user_id add_index :samples, :user_id
add_index :samples, :organization_id add_index :samples, :team_id
end end
end end

View file

@ -2,12 +2,12 @@ class CreateSampleGroups < ActiveRecord::Migration
def change def change
create_table :sample_groups do |t| create_table :sample_groups do |t|
t.string :name, null: false t.string :name, null: false
t.string :color, null: false, default: "#ff0000" t.string :color, null: false, default: '#ff0000'
t.integer :organization_id, null: false t.integer :team_id, null: false
t.timestamps null: false t.timestamps null: false
end end
add_foreign_key :sample_groups, :organizations add_foreign_key :sample_groups, :teams
add_index :sample_groups, :organization_id add_index :sample_groups, :team_id
end end
end end

View file

@ -4,14 +4,14 @@ class CreateCustomFields < ActiveRecord::Migration
t.string :name, null: false t.string :name, null: false
t.integer :user_id, null: false t.integer :user_id, null: false
t.integer :organization_id, null: false t.integer :team_id, null: false
t.timestamps null: false t.timestamps null: false
end end
add_foreign_key :custom_fields, :users add_foreign_key :custom_fields, :users
add_foreign_key :custom_fields, :organizations add_foreign_key :custom_fields, :teams
add_index :custom_fields, :user_id add_index :custom_fields, :user_id
add_index :custom_fields, :organization_id add_index :custom_fields, :team_id
end end
end end

View file

@ -1,7 +1,7 @@
class AddCreatedByToAssets < ActiveRecord::Migration class AddCreatedByToAssets < ActiveRecord::Migration
def change def change
tables = [:assets, :checklists, :checklist_items, :my_module_groups, tables = [:assets, :checklists, :checklist_items, :my_module_groups,
:my_module_tags, :my_modules, :organizations, :projects, :my_module_tags, :my_modules, :teams, :projects,
:sample_groups, :sample_types, :tables, :tags] :sample_groups, :sample_types, :tables, :tags]
tables.each do |table_name| tables.each do |table_name|
@ -9,8 +9,8 @@ class AddCreatedByToAssets < ActiveRecord::Migration
add_index table_name, :created_by_id add_index table_name, :created_by_id
end end
tables = [:assets, :checklists, :checklist_items, :comments, tables = [:assets, :checklists, :checklist_items, :comments,
:custom_fields, :my_modules, :organizations, :projects, :custom_fields, :my_modules, :teams, :projects,
:reports, :results, :sample_groups, :sample_types, :samples, :reports, :results, :sample_groups, :sample_types, :samples,
:steps, :tables, :tags] :steps, :tables, :tags]
@ -29,8 +29,7 @@ class AddCreatedByToAssets < ActiveRecord::Migration
add_column table_name, :restored_on, :datetime add_column table_name, :restored_on, :datetime
end end
tables = [:sample_my_modules, :user_my_modules, tables = [:sample_my_modules, :user_my_modules, :user_teams, :user_projects]
:user_organizations, :user_projects]
tables.each do |table_name| tables.each do |table_name|
add_column table_name, :assigned_by_id, :integer add_column table_name, :assigned_by_id, :integer
add_index table_name, :assigned_by_id add_index table_name, :assigned_by_id

View file

@ -3,9 +3,9 @@ include DatabaseHelper
class AddSearchQueryIndexes < ActiveRecord::Migration class AddSearchQueryIndexes < ActiveRecord::Migration
def up def up
add_index :projects, :organization_id add_index :projects, :team_id
add_index :user_organizations, :user_id add_index :user_teams, :user_id
add_index :user_organizations, :organization_id add_index :user_teams, :team_id
add_index :user_projects, :user_id add_index :user_projects, :user_id
add_index :user_projects, :project_id add_index :user_projects, :project_id
add_index :tags, :project_id add_index :tags, :project_id
@ -28,9 +28,9 @@ class AddSearchQueryIndexes < ActiveRecord::Migration
end end
def down def down
remove_index :projects, :organization_id remove_index :projects, :team_id
remove_index :user_organizations, :user_id remove_index :user_teams, :user_id
remove_index :user_organizations, :organization_id remove_index :user_teams, :team_id
remove_index :user_projects, :user_id remove_index :user_projects, :user_id
remove_index :user_projects, :project_id remove_index :user_projects, :project_id
remove_index :tags, :project_id remove_index :tags, :project_id

View file

@ -1,11 +1,11 @@
class RemoveUniqueOrganizationNameIndex < ActiveRecord::Migration class RemoveUniqueOrganizationNameIndex < ActiveRecord::Migration
def up def up
remove_index :organizations, :name remove_index :teams, :name
add_index :organizations, :name add_index :teams, :name
end end
def down def down
remove_index :organizations, :name remove_index :teams, :name
add_index :organizations, :name, unique: true add_index :teams, :name, unique: true
end end
end end

View file

@ -3,34 +3,34 @@ include DatabaseHelper
class AddOrganizationManagementSupport < ActiveRecord::Migration class AddOrganizationManagementSupport < ActiveRecord::Migration
def up def up
# Add nullable description to organization # Add nullable description to team
add_column :organizations, :description, :string add_column :teams, :description, :string
# Add estimated file size to asset (in B) # Add estimated file size to asset (in B)
add_column :assets, :estimated_size, :integer, add_column :assets, :estimated_size, :integer,
default: 0 default: 0
change_column_null :assets, :estimated_size, false change_column_null :assets, :estimated_size, false
# Add space taken to organization (in B!) # Add space taken to team (in B!)
add_column :organizations, :space_taken, :integer, limit: 5, add_column :teams, :space_taken, :integer, limit: 5,
default: Constants::MINIMAL_ORGANIZATION_SPACE_TAKEN default: Constants::MINIMAL_TEAM_SPACE_TAKEN
change_column_null :organizations, :space_taken, false change_column_null :teams, :space_taken, false
# Add reference to private user # Add reference to private user
add_column :organizations, :private_user_id, :integer add_column :teams, :private_user_id, :integer
add_index :organizations, :private_user_id add_index :teams, :private_user_id
add_foreign_key :organizations, :users, column: :private_user_id add_foreign_key :teams, :users, column: :private_user_id
end end
def down def down
remove_column :organizations, :description remove_column :teams, :description
remove_column :assets, :estimated_size remove_column :assets, :estimated_size
remove_column :organizations, :space_taken remove_column :teams, :space_taken
remove_foreign_key :organizations, column: :private_user_id remove_foreign_key :teams, column: :private_user_id
remove_index :organizations, :private_user_id remove_index :teams, :private_user_id
remove_column :organizations, :private_user_id remove_column :teams, :private_user_id
end end
end end

View file

@ -1,7 +1,7 @@
class AddForeignKeysToTables < ActiveRecord::Migration class AddForeignKeysToTables < ActiveRecord::Migration
def change def change
tables = [:assets, :checklists, :checklist_items, :my_module_groups, tables = [:assets, :checklists, :checklist_items, :my_module_groups,
:my_module_tags, :my_modules, :organizations, :projects, :my_module_tags, :my_modules, :teams, :projects,
:sample_groups, :sample_types, :tables, :tags] :sample_groups, :sample_types, :tables, :tags]
tables.each do |table_name| tables.each do |table_name|
@ -9,7 +9,7 @@ class AddForeignKeysToTables < ActiveRecord::Migration
end end
tables = [:assets, :checklists, :checklist_items, :comments, tables = [:assets, :checklists, :checklist_items, :comments,
:custom_fields, :my_modules, :organizations, :projects, :custom_fields, :my_modules, :teams, :projects,
:reports, :results, :sample_groups, :sample_types, :samples, :reports, :results, :sample_groups, :sample_types, :samples,
:steps, :tables, :tags] :steps, :tables, :tags]
@ -25,7 +25,7 @@ class AddForeignKeysToTables < ActiveRecord::Migration
end end
tables = [:sample_my_modules, :user_my_modules, tables = [:sample_my_modules, :user_my_modules,
:user_organizations, :user_projects] :user_teams, :user_projects]
tables.each do |table_name| tables.each do |table_name|
add_foreign_key table_name, :users, column: :assigned_by_id add_foreign_key table_name, :users, column: :assigned_by_id
end end

View file

@ -1,13 +1,13 @@
class RemovePrivateOrganizations < ActiveRecord::Migration class RemovePrivateOrganizations < ActiveRecord::Migration
def up def up
remove_foreign_key :organizations, column: :private_user_id remove_foreign_key :teams, column: :private_user_id
remove_index :organizations, :private_user_id remove_index :teams, :private_user_id
remove_column :organizations, :private_user_id remove_column :teams, :private_user_id
end end
def down def down
add_column :organizations, :private_user_id, :integer add_column :teams, :private_user_id, :integer
add_index :organizations, :private_user_id add_index :teams, :private_user_id
add_foreign_key :organizations, :users, column: :private_user_id add_foreign_key :teams, :users, column: :private_user_id
end end
end end

View file

@ -6,39 +6,39 @@ class MigrateOrganizationsStructure < ActiveRecord::Migration
asset.update(file_present: true) asset.update(file_present: true)
end end
# Calculate organizations' taken space # Calculate teams' taken space
Organization.find_each do |org| Team.find_each do |team|
org.calculate_space_taken team.calculate_space_taken
org.save team.save
end end
# Finally, the trickiest task: Re-define organizations # Finally, the trickiest task: Re-define teams
demo_org = Organization.find_by(name: "Demo organization") demo_team = Team.find_by(name: "Demo team")
if demo_org if demo_team
demo_org.user_organizations.each do |uo| demo_team.user_teams.each do |uo|
uo.destroy uo.destroy
end end
end end
Organization.find_each do |org| Team.find_each do |team|
user = org.users.first user = team.users.first
org.update(created_by: user) team.update(created_by: user)
end end
UserOrganization.find_each do |uo| UserTeam.find_each do |uo|
uo.update(role: 2) uo.update(role: 2)
end end
end end
def down def down
# We cannot re-assign users to demo organization or re-update # We cannot re-assign users to demo team or re-update
# their previous user-organization roles # their previous user-team roles
# But we can remove created_by field from organizations # But we can remove created_by field from teams
Organization.find_each do |org| Team.find_each do |team|
org.update(created_by: nil) team.update(created_by: nil)
end end
# Resetting calculated assets & organizations' space # Resetting calculated assets & teams' space
# to 0 doesn't make much sense even when downgrading migration # to 0 doesn't make much sense even when downgrading migration
end end
end end

View file

@ -7,7 +7,7 @@ class CreateProtocols < ActiveRecord::Migration
t.text :description t.text :description
t.integer :added_by_id t.integer :added_by_id
t.integer :my_module_id t.integer :my_module_id
t.integer :organization_id, null: false t.integer :team_id, null: false
t.integer :protocol_type, null: false, default: 0 t.integer :protocol_type, null: false, default: 0
t.integer :parent_id t.integer :parent_id
t.datetime :parent_updated_at t.datetime :parent_updated_at
@ -20,7 +20,7 @@ class CreateProtocols < ActiveRecord::Migration
end end
add_foreign_key :protocols, :users, column: :added_by_id add_foreign_key :protocols, :users, column: :added_by_id
add_foreign_key :protocols, :my_modules, column: :my_module_id add_foreign_key :protocols, :my_modules, column: :my_module_id
add_foreign_key :protocols, :organizations, column: :organization_id add_foreign_key :protocols, :teams, column: :team_id
add_foreign_key :protocols, :protocols, column: :parent_id add_foreign_key :protocols, :protocols, column: :parent_id
add_foreign_key :protocols, :users, column: :archived_by_id add_foreign_key :protocols, :users, column: :archived_by_id
add_foreign_key :protocols, :users, column: :restored_by_id add_foreign_key :protocols, :users, column: :restored_by_id
@ -29,7 +29,7 @@ class CreateProtocols < ActiveRecord::Migration
add_index :protocols, :description add_index :protocols, :description
add_index :protocols, :added_by_id add_index :protocols, :added_by_id
add_index :protocols, :my_module_id add_index :protocols, :my_module_id
add_index :protocols, :organization_id add_index :protocols, :team_id
add_index :protocols, :parent_id add_index :protocols, :parent_id
add_index :protocols, :archived_by_id add_index :protocols, :archived_by_id
add_index :protocols, :restored_by_id add_index :protocols, :restored_by_id
@ -59,7 +59,7 @@ class CreateProtocols < ActiveRecord::Migration
MyModule.find_each do |my_module| MyModule.find_each do |my_module|
protocol = Protocol.new( protocol = Protocol.new(
my_module_id: my_module.id, my_module_id: my_module.id,
organization_id: my_module.project.organization.id, team_id: my_module.project.team.id,
protocol_type: 0 protocol_type: 0
) )
protocol.save(validate: false) protocol.save(validate: false)
@ -94,7 +94,7 @@ class CreateProtocols < ActiveRecord::Migration
remove_column :steps, :protocol_id remove_column :steps, :protocol_id
# Simply drop the rest of the protocols # Simply drop the rest of the protocols
# (the ones that are organization-wide) # (the ones that are team-wide)
remove_index :protocol_protocol_keywords, column: :protocol_id remove_index :protocol_protocol_keywords, column: :protocol_id
remove_index :protocol_protocol_keywords, column: :protocol_keyword_id remove_index :protocol_protocol_keywords, column: :protocol_keyword_id
@ -110,13 +110,13 @@ class CreateProtocols < ActiveRecord::Migration
remove_index :protocols, column: :description remove_index :protocols, column: :description
remove_index :protocols, column: :added_by_id remove_index :protocols, column: :added_by_id
remove_index :protocols, column: :my_module_id remove_index :protocols, column: :my_module_id
remove_index :protocols, column: :organization_id remove_index :protocols, column: :team_id
remove_index :protocols, column: :parent_id remove_index :protocols, column: :parent_id
remove_index :protocols, column: :archived_by_id remove_index :protocols, column: :archived_by_id
remove_index :protocols, column: :restored_by_id remove_index :protocols, column: :restored_by_id
remove_foreign_key :protocols, column: :added_by_id remove_foreign_key :protocols, column: :added_by_id
remove_foreign_key :protocols, :my_modules remove_foreign_key :protocols, :my_modules
remove_foreign_key :protocols, :organizations remove_foreign_key :protocols, :teams
remove_foreign_key :protocols, column: :parent_id remove_foreign_key :protocols, column: :parent_id
remove_foreign_key :protocols, column: :archived_by_id remove_foreign_key :protocols, column: :archived_by_id
remove_foreign_key :protocols, column: :restored_by_id remove_foreign_key :protocols, column: :restored_by_id

View file

@ -1,25 +1,25 @@
class AddOrganizationIdToProtocolKeywords < ActiveRecord::Migration class AddOrganizationIdToProtocolKeywords < ActiveRecord::Migration
def up def up
add_column :protocol_keywords, :organization_id, :integer add_column :protocol_keywords, :team_id, :integer
add_foreign_key :protocol_keywords, :organizations, column: :organization_id add_foreign_key :protocol_keywords, :teams, column: :team_id
add_index :protocol_keywords, :organization_id add_index :protocol_keywords, :team_id
# Set organization to all protocol keywords # Set team to all protocol keywords
ProtocolKeyword.find_each do |kw| ProtocolKeyword.find_each do |kw|
if kw.protocols.count == 0 if kw.protocols.count == 0
kw.destroy kw.destroy
else else
kw.update(organization_id: kw.protocols.first.organization_id) kw.update(team_id: kw.protocols.first.team_id)
end end
end end
# Finally, set the column to null = false # Finally, set the column to null = false
change_column_null :protocol_keywords, :organization_id, false change_column_null :protocol_keywords, :team_id, false
end end
def down def down
remove_index :protocol_keywords, column: :organization_id remove_index :protocol_keywords, column: :team_id
remove_foreign_key :protocol_keywords, column: :organization_id remove_foreign_key :protocol_keywords, column: :team_id
remove_column :protocol_keywords, :organization_id remove_column :protocol_keywords, :team_id
end end
end end

View file

@ -1,14 +1,13 @@
class RenameDuplicateColumns < ActiveRecord::Migration class RenameDuplicateColumns < ActiveRecord::Migration
require 'set'
require 'set';
def up def up
# Update duplicate or prohibited column names with extensions # Update duplicate or prohibited column names with extensions
Organization.find_each do |org| Team.find_each do |team|
names = Set.new ["Assigned", "Sample name", "Sample type", "Sample group", "Added on", "Added by"] names = Set.new ["Assigned", "Sample name", "Sample type", "Sample group", "Added on", "Added by"]
CustomField.where(organization: org).find_each do |column| CustomField.where(team: team).find_each do |column|
if names.include?(column.name) if names.include?(column.name)
name = column.name name = column.name
i = 0 i = 0
@ -16,7 +15,7 @@ class RenameDuplicateColumns < ActiveRecord::Migration
name = column.name name = column.name
i = i+1 i = i+1
suffix = "(" + i.to_s + ")" suffix = "(" + i.to_s + ")"
if (suffix.length + name.length > 50) if (suffix.length + name.length > 50)
name = name[0..(49-suffix.length)] name = name[0..(49-suffix.length)]
end end
name = name + suffix name = name + suffix
@ -32,4 +31,4 @@ class RenameDuplicateColumns < ActiveRecord::Migration
def down def down
# We can't really rollback this change # We can't really rollback this change
end end
end end

View file

@ -1,16 +1,16 @@
class AddCurrentOrganizationToUser < ActiveRecord::Migration class AddCurrentOrganizationToUser < ActiveRecord::Migration
def up def up
add_column :users, :current_organization_id, :integer add_column :users, :current_team_id, :integer
add_foreign_key :users, :organizations, column: :current_organization_id add_foreign_key :users, :teams, column: :current_team_id
User.find_each do |user| User.find_each do |user|
if user.organizations.count > 0 if user.teams.count > 0
user.update(current_organization_id: user.organizations.first.id) user.update(current_team_id: user.teams.first.id)
end end
end end
end end
def down def down
remove_column :users, :current_organization_id remove_column :users, :current_team_id
end end
end end

View file

@ -5,28 +5,30 @@ class CreateSamplesTables < ActiveRecord::Migration
default: SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE default: SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE
# Foreign keys # Foreign keys
t.references :user, null: false t.references :user, null: false
t.references :organization, null: false t.references :team, null: false
t.timestamps null: false t.timestamps null: false
end end
add_index :samples_tables, :user_id add_index :samples_tables, :user_id
add_index :samples_tables, :organization_id add_index :samples_tables, :team_id
User.find_each do |user| User.find_each do |user|
next unless user.organizations next unless user.teams
user.organizations.find_each do |org| user.teams.find_each do |team|
org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup team_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup
next unless org.custom_fields next unless team.custom_fields
org.custom_fields.each_with_index do |_, index| team.custom_fields.each_with_index do |_, index|
org_status['columns'] << { 'visible' => true, team_status['columns'] << { 'visible' => true,
'search' => { 'search' => '', 'search' => {
'smart' => true, 'search' => '',
'regex' => false, 'smart' => true,
'caseInsensitive' => true } } 'regex' => false,
org_status['ColReorder'] << (7 + index) 'caseInsensitive' => true
} }
team_status['ColReorder'] << (7 + index)
end end
SamplesTable.create(user: user, organization: org, status: org_status) SamplesTable.create(user: user, team: team, status: team_status)
end end
end end
end end

View file

@ -1,9 +1,9 @@
class RenameNonUniqueSampleTypesGroups < ActiveRecord::Migration class RenameNonUniqueSampleTypesGroups < ActiveRecord::Migration
def up def up
Organization.find_each do |org| Team.find_each do |team|
st_ids = [] st_ids = []
org.sample_types.find_each do |st| team.sample_types.find_each do |st|
next unless SampleType.where(organization_id: org.id) next unless SampleType.where(team_id: team.id)
.where(name: st.name) .where(name: st.name)
.count > 1 .count > 1
st_ids << st.id st_ids << st.id
@ -20,10 +20,10 @@ class RenameNonUniqueSampleTypesGroups < ActiveRecord::Migration
end end
end end
Organization.find_each do |org| Team.find_each do |team|
sg_ids = [] sg_ids = []
org.sample_groups.find_each do |sg| team.sample_groups.find_each do |sg|
next unless SampleGroup.where(organization_id: org.id) next unless SampleGroup.where(team_id: team.id)
.where(name: sg.name) .where(name: sg.name)
.count > 1 .count > 1
sg_ids << sg.id sg_ids << sg.id