Agency Bulk Transfer - bcgov/PIMS GitHub Wiki

Agency Bulk Transfer Process

Presently there is no built in process for transferring all properties from one agency to another. Manually a user with the SRES role (which includes the admin-properties claim) can modify each properties owning agency, but obviously with a bulk change this would take too much time. The most efficient method is to create a database migration. To do this follow these steps.

If you are using a Windows OS, you can use the make scripts in the root of the solution.

Generate a new database migration for the next appropriate version (i.e. 01.09.01).

make db-add n={version number}

Add the appropriate folders for this migration to place your SQL scripts.

mkdir backend/dal/Migrations/{version number}/Up/PostUp
mkdir backend/dal/Migrations/{version number}/Down/PostDown

Add your SQL scripts for Up (upgrade) and Down (rollback). The Up script will transfer all properties from the current agency to the appropriate agency. The Down scripts will rollback your change and move the properties back to the original agency (if appropriate).

touch backend/dal/Migrations/{version number}/Up/PostUp/01-PropertyTransfer.sql
touch backend/dal/Migrations/{version number}/Down/PostDown/01-PropertyTransfer.sql

Update the Up script to contain the following. Determine the original Agency Code and the Agency Code that the properties will be transferred to.

Ideally there would be no open projects, however if there are you will need to transfer these to the new agency as well.

declare @fromAgencyId int = (select [Id] from dbo.[Agencies] where [Code] = '{current Agency Code}')
declare @toAgencyId int = (select [Id] from dbo.[Agencies] where [Code] = '{To Agency Code}')

-- Transfer parcels to new agency.
update dbo.[Parcels]
set [AgencyId] = @toAgencyId
where [AgencyId] = @fromAgencyId

-- Transfer buildings to new agency.
update dbo.[Buildings]
set [AgencyId] = @toAgencyId
where [AgencyId] = @fromAgencyId

The rollback script is simply an inverse. Only add a rollback if the state of the solution must return to what it was before the update.

declare @fromAgencyId int = (select [Id] from dbo.[Agencies] where [Code] = '{current Agency Code}')
declare @toAgencyId int = (select [Id] from dbo.[Agencies] where [Code] = '{original Agency Code}')

-- Transfer parcels to original agency.
update dbo.[Parcels]
set [AgencyId] = @toAgencyId
where [AgencyId] = @fromAgencyId

-- Transfer buildings to original agency.
update dbo.[Buildings]
set [AgencyId] = @toAgencyId
where [AgencyId] = @fromAgencyId
⚠️ **GitHub.com Fallback** ⚠️