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

2277 UUID #2360

Closed
wants to merge 13 commits into from
100 changes: 100 additions & 0 deletions common/utils/UuidUtils.php
@@ -0,0 +1,100 @@
<?php
/**
* Utility class for Uuid creation.
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Gerhard Brink <gjdbrink@gmail.com>
* @copyright Copyright (c) 2019 Gerhard Brink <gjdbrink@gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

namespace OpenEMR\Common\Utils;

use Ramsey\Uuid\Uuid;

class UuidUtils
{

const MAX_TRIES = 100;

/**
* @param string $table
*
* @return string
* @throws \Exception
*/
public static function createUuid(string $table): string
{
$uuid4 = Uuid::uuid4();

$try = 1;
while (self::findCollision($table, $uuid4)) {
if ($try > self::MAX_TRIES) {
throw new \Exception('Reached maximum amount of tries (max amount: '.self::MAX_TRIES.')');
}

$uuid4 = Uuid::uuid4();
$try++;
}

return $uuid4->toString();
}

/**
* @param $table
* @param $uuid
*
* @return bool
*/
public static function findCollision(string $table, string $uuid): bool
{
$query = "SELECT uuid FROM ".$table." WHERE uuid = ?";

if (sqlQuery($query, $uuid)) {
return true;
}

return false;
}

/**
* @param string $table
*
* @throws \Exception
*/
public static function createMissingUuids(string $table)
{
$query = "SELECT id FROM ".$table." WHERE uuid = ''";

$resultSet = sqlStatement($query);

while ($row = sqlFetchArray($resultSet)) {

$uuid = self::createUuid($table);

$updateQuery = "UPDATE ".$table." SET uuid = ? WHERE id = ?";

sqlQuery($updateQuery, [$uuid, $row['id']]);
}

}

/**
* @param string $table
*
* @return bool
*/
public static function tableNeedsUuidCreation(string $table): bool
{
$query = "SELECT count(id) as total FROM ".$table." WHERE uuid = ''";

$resultSet = sqlQuery($query);

if ($resultSet['total'] > 0) {
return true;
}

return false;
}
}
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -42,7 +42,8 @@
"academe/omnipay-authorizenetapi": "3.1.1",
"omnipay/stripe": "3.0.1",
"waryway/php-traits-library": "1.0.4",
"paragonie/multi-factor": "0.2.2"
"paragonie/multi-factor": "0.2.2",
"ramsey/uuid": "^3.8"
},
"config": {
"platform": {
Expand Down
84 changes: 83 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions library/patient.inc
Expand Up @@ -11,6 +11,7 @@


use OpenEMR\Services\FacilityService;
use OpenEMR\Common\Utils\UuidUtils;

$facilityService = new FacilityService();

Expand Down Expand Up @@ -1149,6 +1150,11 @@ function updatePatientData($pid, $new, $create = false)
$sql .= ", `$key` = " . pdValueOrNull($key, $value);
}

//create uuid for patient
$uuid = UuidUtils::createUuid('patient_data');

$sql .= ", uuid ='".add_escape_custom($uuid)."'";

$db_id = sqlInsert($sql);
} else {
$db_id = $new['id'];
Expand Down
8 changes: 8 additions & 0 deletions library/sql_upgrade_fx.php
Expand Up @@ -866,6 +866,14 @@ function upgradeFromSqlFile($filename)
if ($skipping) {
echo "<font color='green'>Skipping section $line</font><br />\n";
}
} else if (preg_match('/^#IfUuidUpdatePatientsNeeded/', $line)) {

if ( \OpenEMR\Common\Utils\UuidUtils::tableNeedsUuidCreation('patient_data')) {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this here:

$skipping = false;

\OpenEMR\Common\Utils\UuidUtils::createMissingUuids('patient_data');
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this here:

 echo "<font color='green'>Added UUIDs to patient_data table</font><br />\n";

} else {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this here:

$skipping = true;

echo "<font color='green'>Skipping section $line</font><br />\n";
}

} else if (preg_match('/^#ConvertLayoutProperties/', $line)) {
if ($skipping) {
echo "<font color='green'>Skipping section $line</font><br />\n";
Expand Down
7 changes: 6 additions & 1 deletion services/PatientService.php
Expand Up @@ -15,6 +15,7 @@
namespace OpenEMR\Services;

use Particle\Validator\Validator;
use OpenEMR\Common\Utils\UuidUtils;

class PatientService
{
Expand Down Expand Up @@ -114,6 +115,8 @@ public function insert($data)
{
$fresh_pid = $this->getFreshPid();

$uuid = UuidUtils::createUuid('patient_data');

$sql = " INSERT INTO patient_data SET";
$sql .= " pid=?,";
$sql .= " title=?,";
Expand All @@ -130,6 +133,7 @@ public function insert($data)
$sql .= " sex=?,";
$sql .= " race=?,";
$sql .= " ethnicity=?";
$sql .= " uuid=?";

$results = sqlInsert(
$sql,
Expand All @@ -148,7 +152,8 @@ public function insert($data)
$data["dob"],
$data["sex"],
$data["race"],
$data["ethnicity"]
$data["ethnicity"],
$uuid
)
);

Expand Down
4 changes: 4 additions & 0 deletions sql/5_0_1-to-5_0_2_upgrade.sql
Expand Up @@ -85,6 +85,10 @@
-- arguments: none
-- behavior: can take a long time.

-- #IfUuidUpdatePatientsNeeded
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the bottom of this script, add the following lines:

#IfUuidUpdatePatientsNeeded
#EndIf

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And at the bottom of the script above the lines above, create the following block:

#IfMissingColumn patient_data uuid
ALTER TABLE `patient_data` ADD `uuid` varchar(64);
(I was going to also add the unique key here , but realized that this will break since many if the uuids will be blank. Instead, in the sql upgrade custom function, after you are done creating the uuids, then check for existence of the unique key, and if it does not yet exist, then create it)
#EndIf

-- desc: populate patient table with uuids
-- arguments: none

UPDATE `background_services` SET `require_once`='/library/MedEx/MedEx_background.php' WHERE `name`='MedEx';

#IfNotRow2Dx2 list_options list_id proc_type option_id fgp title Custom Favorite Group
Expand Down
2 changes: 2 additions & 0 deletions sql/database.sql
Expand Up @@ -5881,7 +5881,9 @@ CREATE TABLE `patient_data` (
`guardianphone` TEXT,
`guardianworkphone` TEXT,
`guardianemail` TEXT,
`uuid` varchar(64),
UNIQUE KEY `pid` (`pid`),
UNIQUE KEY `uuid` (`uuid`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 ;
-----------------------------------------------------------
Expand Down