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
def change
create_table :organizations do |t|
create_table :teams do |t|
## General info
t.string :name, null: false
t.timestamps null: false
end
add_index :organizations, :name, unique: true
add_index :teams, :name, unique: true
end
end

View file

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

View file

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

View file

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

View file

@ -2,12 +2,12 @@ class CreateSampleGroups < ActiveRecord::Migration
def change
create_table :sample_groups do |t|
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
end
add_foreign_key :sample_groups, :organizations
add_index :sample_groups, :organization_id
add_foreign_key :sample_groups, :teams
add_index :sample_groups, :team_id
end
end

View file

@ -4,14 +4,14 @@ class CreateCustomFields < ActiveRecord::Migration
t.string :name, null: false
t.integer :user_id, null: false
t.integer :organization_id, null: false
t.integer :team_id, null: false
t.timestamps null: false
end
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, :organization_id
add_index :custom_fields, :team_id
end
end

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -7,7 +7,7 @@ class CreateProtocols < ActiveRecord::Migration
t.text :description
t.integer :added_by_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 :parent_id
t.datetime :parent_updated_at
@ -20,7 +20,7 @@ class CreateProtocols < ActiveRecord::Migration
end
add_foreign_key :protocols, :users, column: :added_by_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, :users, column: :archived_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, :added_by_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, :archived_by_id
add_index :protocols, :restored_by_id
@ -59,7 +59,7 @@ class CreateProtocols < ActiveRecord::Migration
MyModule.find_each do |my_module|
protocol = Protocol.new(
my_module_id: my_module.id,
organization_id: my_module.project.organization.id,
team_id: my_module.project.team.id,
protocol_type: 0
)
protocol.save(validate: false)
@ -94,7 +94,7 @@ class CreateProtocols < ActiveRecord::Migration
remove_column :steps, :protocol_id
# 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_keyword_id
@ -110,13 +110,13 @@ class CreateProtocols < ActiveRecord::Migration
remove_index :protocols, column: :description
remove_index :protocols, column: :added_by_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: :archived_by_id
remove_index :protocols, column: :restored_by_id
remove_foreign_key :protocols, column: :added_by_id
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: :archived_by_id
remove_foreign_key :protocols, column: :restored_by_id

View file

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

View file

@ -1,14 +1,13 @@
class RenameDuplicateColumns < ActiveRecord::Migration
require 'set';
require 'set'
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"]
CustomField.where(organization: org).find_each do |column|
CustomField.where(team: team).find_each do |column|
if names.include?(column.name)
name = column.name
i = 0
@ -16,7 +15,7 @@ class RenameDuplicateColumns < ActiveRecord::Migration
name = column.name
i = i+1
suffix = "(" + i.to_s + ")"
if (suffix.length + name.length > 50)
if (suffix.length + name.length > 50)
name = name[0..(49-suffix.length)]
end
name = name + suffix
@ -32,4 +31,4 @@ class RenameDuplicateColumns < ActiveRecord::Migration
def down
# We can't really rollback this change
end
end
end

View file

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

View file

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

View file

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