Sha256: 9318ac541549bd9c7968f6335868d849208423dbf8eaffe8b038c40397673766

Contents?: true

Size: 980 Bytes

Versions: 99

Compression:

Stored size: 980 Bytes

Contents

# frozen_string_literal: true

class MigrateUserRolesToParticipatoryProcessRoles < ActiveRecord::Migration[5.1]
  class ParticipatoryProcess < ApplicationRecord
    self.table_name = :decidim_participatory_processes
  end

  class User < ApplicationRecord
    self.table_name = :decidim_users
  end

  def up
    User.find_each do |user|
      next if user.roles.empty? || user.roles.include?("admin")

      values = processes(user).flat_map do |process|
        user.roles.map do |role|
          "(#{user.id}, #{process.id}, '#{role}', NOW(), NOW())"
        end
      end

      execute("
        INSERT INTO decidim_admin_participatory_process_user_roles
        (decidim_user_id, decidim_participatory_process_id, role, created_at, updated_at)
        VALUES #{values.join(", ")}
      ")
    end
    remove_column :decidim_users, :roles
  end

  private

  def processes(user)
    ParticipatoryProcess.where(decidim_organization_id: user.decidim_organization_id)
  end
end

Version data entries

99 entries across 99 versions & 1 rubygems

Version Path
decidim-core-0.29.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.27.9 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.29.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.27.8 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.29.0.rc4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.29.0.rc3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.29.0.rc2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.29.0.rc1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.27.7 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.27.6 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.26.10 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.26.9 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.27.5 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.0.rc5 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.28.0.rc4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb