Skip to content

Commit

Permalink
Merge pull request #616 from mcagov/backup_duplicates
Browse files Browse the repository at this point in the history
Only get the most recent claim when joining
  • Loading branch information
slkendell-mt authored Oct 6, 2023
2 parents c1714da + e50def4 commit c20ade6
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
42 changes: 42 additions & 0 deletions beacons-data/scripts/beacons_bulk_load_legacy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def populateBeacons
:user => 'beacons_service', :password => db_password )

conn.prepare("statement", 'INSERT INTO legacy_beacon (id, hex_id, owner_email, owner_name, created_date, beacon_status, data, last_modified_date, use_activities) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)')
conn.prepare("claim_statement", 'INSERT INTO legacy_beacon_claim_event (id, claim_event_type, legacy_beacon_id, account_holder_id, when_happened, reason) VALUES ($1, $2, $3, $4, $5, $6)')

useLookup = {}
useLookup["MARITIME"] = ["SAILING","MOTOR","ROWING","SMALL_UNPOWERED","FISHING_VESSEL","MERCHANT_VESSEL","FLOATING_PLATFORM","OFFSHORE_WINDFARM","OFFSHORE_RIG_PLATFORM"]
Expand Down Expand Up @@ -83,9 +84,50 @@ def populateBeacons

# run SQL
conn.exec_prepared('statement', [ uuid, hex_id, owner_email, owner_name, created_date, beacon_status, data, last_modified_date, use_activities])


claim_beacon = rand(1..5) == 1

if claim_beacon

claim_date = Faker::Time.between_dates(from: created_date, to: '2022-10-06').iso8601
claim_uuid = SecureRandom.uuid
claim_event_type = "claim"
account_holder_id = getAccountHolderId(conn)
reason = "Test data"

if account_holder_id
conn.exec_prepared('claim_statement', [ claim_uuid, claim_event_type, uuid, account_holder_id, claim_date, reason])

if rand(1..100) == 1 # for 1 in 100, add a second claim
second_claim_date = Faker::Time.between_dates(from: claim_date, to: '2023-10-06').iso8601
conn.exec_prepared('claim_statement', [ SecureRandom.uuid, claim_event_type, uuid, account_holder_id, second_claim_date, "Second claim"])
end

else
puts "No account holder IDs found in the database."
end

end

end
end


def getAccountHolderId(conn)
# Execute a query to retrieve a random account holder ID from the table
result = conn.exec('SELECT id FROM account_holder ORDER BY RANDOM() LIMIT 1')

if result.num_tuples.zero?
return nil # No records found
end

# Extract the ID from the first (and only) row
account_holder_id = result[0]['id']

return account_holder_id
end

def buildBeaconUse(environment, activity, main_use,created_date,last_modified_date)

call_sign = Faker::Artist.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
DROP VIEW beacon_backup;
CREATE OR REPLACE VIEW beacon_backup AS

SELECT *
FROM (SELECT b.id,
b.hex_id,
b.beacon_status,
'MODERN' as category,
b.created_date,
b.last_modified_date,
-- legacy beacons only
NULL as data,
NULL as owner_name,
NULL as owner_email,
NULL as use_activities,
NULL as recovery_email,
NULL as cospas_sarsat_number,
-- modern beacons only
b.manufacturer,
b.model,
b.manufacturer_serial_number,
b.battery_expiry_date,
b.last_serviced_date,
b.chk_code,
b.reference_number,
b.mti,
b.svdr,
b.csta,
b.beacon_type,
b.protocol,
b.coding,
b.account_holder_id
FROM beacon b
UNION ALL
SELECT legacy_beacon.id as id,
legacy_beacon.hex_id as hex_id,
COALESCE(UPPER('DELETED (' || claim_event_type::text || 'ED)'),
UPPER(legacy_beacon.beacon_status)) AS beacon_status,
'LEGACY' as category,
legacy_beacon.created_date,
legacy_beacon.last_modified_date,
-- legacy only
legacy_beacon.data,
legacy_beacon.owner_name,
legacy_beacon.owner_email,
legacy_beacon.use_activities,
legacy_beacon.recovery_email as recovery_email,

-- legacy beacon data fields
(legacy_beacon.data -> 'beacon' ->> 'cospasSarsatNumber') as cospas_sarsat_number,
(legacy_beacon.data -> 'beacon' ->> 'manufacturer') as manufacturer,
(legacy_beacon.data -> 'beacon' ->> 'model') as model,
(legacy_beacon.data -> 'beacon' ->> 'manufacturerSerialNumber') as manufacturer_serial_number,
NULL as battery_expiry_date,
NULL as last_serviced_date,
(legacy_beacon.data -> 'beacon' ->> 'chk_code') as chk_code,
(legacy_beacon.data -> 'beacon' ->> 'reference_number') as reference_number,
(legacy_beacon.data -> 'beacon' ->> 'mti') as mti,
NULL as svdr,
(legacy_beacon.data -> 'beacon' ->> 'csta') as csta,
(legacy_beacon.data -> 'beacon' ->> 'beaconType') as beacon_type,
(legacy_beacon.data -> 'beacon' ->> 'protocol') as protocol,
(legacy_beacon.data -> 'beacon' ->> 'coding') as coding,

-- modern beacons only
NULL AS account_holder_id
FROM legacy_beacon_claim_event c
RIGHT JOIN legacy_beacon ON c.legacy_beacon_id = legacy_beacon.id
WHERE c.when_happened = (SELECT MAX(when_happened)
FROM legacy_beacon_claim_event
WHERE legacy_beacon_id = legacy_beacon.id)) AS beacon_backup
ORDER BY beacon_backup.last_modified_date DESC, beacon_backup.hex_id ASC;

0 comments on commit c20ade6

Please sign in to comment.