Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database and model cleanup #1731

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/stash_engine/funder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module StashEngine
class Funder < ApplicationRecord
self.table_name = 'stash_engine_funders'
belongs_to :ror_org, class_name: 'StashEngine::RorOrg', primary_key: 'ror_id', foreign_key: 'ror_id', optional: true
has_many :roles, class_name: 'StashEngine::Role', as: :role_object
has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy
has_many :users, through: :roles

enum payment_plan: {
Expand Down
24 changes: 0 additions & 24 deletions app/models/stash_engine/funder_role.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/stash_engine/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Journal < ApplicationRecord
# validates :issn, uniqueness: { case_sensitive: false }
has_many :issns, -> { order(created_at: :asc) }, class_name: 'StashEngine::JournalIssn', dependent: :destroy
has_many :alternate_titles, class_name: 'StashEngine::JournalTitle', dependent: :destroy
has_many :roles, class_name: 'StashEngine::Role', as: :role_object
has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy
has_many :users, through: :roles
belongs_to :sponsor, class_name: 'StashEngine::JournalOrganization', optional: true

Expand Down
2 changes: 1 addition & 1 deletion app/models/stash_engine/journal_organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JournalOrganization < ApplicationRecord
self.table_name = 'stash_engine_journal_organizations'
has_many :children, class_name: 'JournalOrganization', primary_key: :id, foreign_key: :parent_org_id, inverse_of: :parent_org
belongs_to :parent_org, class_name: 'JournalOrganization', optional: true, inverse_of: :children
has_many :roles, class_name: 'StashEngine::Role', as: :role_object
has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy
has_many :users, through: :roles

scope :has_children, -> { distinct.joins(:children) }
Expand Down
24 changes: 0 additions & 24 deletions app/models/stash_engine/journal_role.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/stash_engine/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Tenant < ApplicationRecord
has_many :sponsored, class_name: 'Tenant', primary_key: :id, foreign_key: :sponsor_id, inverse_of: :sponsor
has_many :tenant_ror_orgs, class_name: 'StashEngine::TenantRorOrg', dependent: :destroy
has_many :ror_orgs, class_name: 'StashEngine::RorOrg', through: :tenant_ror_orgs
has_many :roles, class_name: 'StashEngine::Role', as: :role_object
has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy
has_many :users, through: :roles

enum payment_plan: {
Expand Down
41 changes: 24 additions & 17 deletions db/migrate/20240425204927_add_roles_table.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
class AddRolesTable < ActiveRecord::Migration[6.1]
JOURNAL_ROLE = <<-SQL.freeze
INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, role, 'StashEngine::Journal', journal_id FROM stash_engine_journal_roles WHERE `stash_engine_journal_roles`.`role` = 'admin'
SQL
JOURNAL_ORG_ROLE = <<-SQL.freeze
INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, 'admin', 'StashEngine::JournalOrganization', journal_organization_id FROM stash_engine_journal_roles WHERE `stash_engine_journal_roles`.`role` = 'org_admin'
SQL
FUNDER_ROLE = <<-SQL.freeze
INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, 'admin', 'StashEngine::Funder', 1 FROM stash_engine_funder_roles
SQL
ROLE_JOURNAL = <<-SQL.freeze
INSERT INTO stash_engine_journal_roles (user_id, role, journal_id) SELECT user_id, 'admin', role_object_id FROM stash_engine_roles where role_object_type = 'StashEngine::Journal'
SQL
ROLE_JOURNAL_ORG = <<-SQL.freeze
INSERT INTO stash_engine_journal_roles (user_id, role, journal_organization_id) SELECT user_id, 'org_admin', role_object_id FROM stash_engine_roles where role_object_type = 'StashEngine::JournalOrganization'
SQL
ROLE_FUNDER = <<-SQL.freeze
INSERT INTO stash_engine_funder_roles (user_id, role, funder_name, funder_id) SELECT user_id, 'admin', 'Chan Zuckerberg Initiative', 'https://ror.org/02qenvm24' FROM stash_engine_roles where role_object_type = 'StashEngine::Funder'
SQL
def change
create_table :stash_engine_funders do |t|
t.string :name
Expand All @@ -22,13 +40,9 @@ def change
add_foreign_key :stash_engine_roles, :stash_engine_users, column: :user_id
reversible do |dir|
dir.up do
StashEngine::JournalRole.all.each do |r|
StashEngine::Role.create(user_id: r.user_id, role: 'admin', role_object: StashEngine::Journal.find(r.journal_id)) if r.journal_id.present? && StashEngine::Journal.exists?(r.journal_id)
StashEngine::Role.create(user_id: r.user_id, role: 'admin', role_object: StashEngine::JournalOrganization.find(r.journal_organization_id)) if r.journal_organization_id.present? && StashEngine::JournalOrganization.exists?(r.journal_organization_id)
end
StashEngine::FunderRole.all.each do |r|
StashEngine::Role.create(user_id: r.user_id, role: r.role, role_object: StashEngine::Funder.find_by(name: r.funder_name)) if StashEngine::Funder.find_by(name: r.funder_name).present?
end
execute JOURNAL_ROLE
execute JOURNAL_ORG_ROLE
execute FUNDER_ROLE
StashEngine::User.where("role != 'user'").each do |u|
case u.role
when 'limited_curator'
Expand All @@ -45,16 +59,9 @@ def change
end
end
dir.down do
StashEngine::Role.journal_roles.each do |r|
StashEngine::JournalRole.create(user_id: r.user_id, role: 'admin', journal_id: r.role_object_id)
end
StashEngine::Role.journal_org_roles.each do |r|
StashEngine::JournalRole.create(user_id: r.user_id, role: 'org_admin', journal_organization_id: r.role_object_id)
end
StashEngine::Role.funder_roles.each do |r|
funder = StashEngine::Funder.find(r.role_object_id)
StashEngine::FunderRole.create(user_id: r.user_id, role: 'admin', funder_name: funder.name, funder_id: funder.ror_id)
end
execute ROLE_JOURNAL
execute ROLE_JOURNAL_ORG
execute ROLE_FUNDER
StashEngine::Role.system_roles.admin.each {|r| StashEngine::User.find(r.user_id).update(role: 'limited_curator') if StashEngine::User.exists?(r.user_id)}
StashEngine::Role.system_roles.curator.each {|r| StashEngine::User.find(r.user_id).update(role: 'curator') if StashEngine::User.exists?(r.user_id)}
StashEngine::Role.superuser.each {|r| StashEngine::User.find(r.user_id).update(role: 'superuser') if StashEngine::User.exists?(r.user_id)}
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240625190621_drop_journal_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DropJournalColumn < ActiveRecord::Migration[7.0]
def change
remove_column :stash_engine_journals, :issn, :string
end
end