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.25.0.rc4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.25.0.rc3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.25.0.rc2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.25.0.rc1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.6 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.5 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.0.rc2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.24.0.rc1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.1.rc1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.23.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.22.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.21.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb