Skip to content

Commit

Permalink
generated DAO file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Apr 16, 2018
1 parent 6983276 commit c74ea2d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 75 deletions.
20 changes: 10 additions & 10 deletions CRM/Core/DAO/AllCoreTables.data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
'name' => 'Extension',
'class' => 'CRM_Core_DAO_Extension',
'table' => 'civicrm_extension',
) ,
'CRM_Core_DAO_LocationType' => array(
],
'CRM_Core_DAO_LocationType' => [
'name' => 'LocationType',
'class' => 'CRM_Core_DAO_LocationType',
'table' => 'civicrm_location_type',
Expand Down Expand Up @@ -286,13 +286,13 @@
'name' => 'Email',
'class' => 'CRM_Core_DAO_Email',
'table' => 'civicrm_email',
) ,
'CRM_Core_DAO_File' => array(
],
'CRM_Core_DAO_File' => [
'name' => 'File',
'class' => 'CRM_Core_DAO_File',
'table' => 'civicrm_file',
) ,
'CRM_Core_DAO_IM' => array(
],
'CRM_Core_DAO_IM' => [
'name' => 'IM',
'class' => 'CRM_Core_DAO_IM',
'table' => 'civicrm_im',
Expand Down Expand Up @@ -526,13 +526,13 @@
'name' => 'Discount',
'class' => 'CRM_Core_DAO_Discount',
'table' => 'civicrm_discount',
) ,
'CRM_Core_DAO_EntityFile' => array(
],
'CRM_Core_DAO_EntityFile' => [
'name' => 'EntityFile',
'class' => 'CRM_Core_DAO_EntityFile',
'table' => 'civicrm_entity_file',
) ,
'CRM_Core_DAO_EntityTag' => array(
],
'CRM_Core_DAO_EntityTag' => [
'name' => 'EntityTag',
'class' => 'CRM_Core_DAO_EntityTag',
'table' => 'civicrm_entity_tag',
Expand Down
11 changes: 7 additions & 4 deletions CRM/Core/DAO/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/File.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:c0230af2b9234e9f83dca829a0490cf3)
* (GenCodeChecksum:7fd8d2643aa05e33a38983f719c09111)
*/

/**
Expand Down Expand Up @@ -83,6 +83,7 @@ class CRM_Core_DAO_File extends CRM_Core_DAO {
* @var int unsigned
*/
public $created_id;

/**
* Class constructor.
*/
Expand All @@ -97,14 +98,15 @@ public function __construct() {
* @return array
* [CRM_Core_Reference_Interface]
*/
static function getReferenceColumns() {
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static ::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName() , 'created_id', 'civicrm_contact', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'created_id', 'civicrm_contact', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
}

/**
* Returns all the column names of this table
*
Expand Down Expand Up @@ -193,11 +195,12 @@ public static function &fields() {
'created_id' => [
'name' => 'created_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Created By Contact ID') ,
'title' => ts('Created By Contact ID'),
'description' => 'FK to civicrm_contact, who uploaded this file',
'table_name' => 'civicrm_file',
'entity' => 'File',
'bao' => 'CRM_Core_BAO_File',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
],
];
Expand Down
26 changes: 26 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
// }
}

/**
* Upgrade function.
*
* @param string $rev
*/
public function upgrade_5_2_alpha1($rev) {
$this->addTask('CRM-19948 - Add created_id column to civicrm_file', 'addFileCreatedIdColumn');
}

public static function addFileCreatedIdColumn(CRM_Queue_TaskContext $ctx) {
self::addColumn($ctx, 'civicrm_file', 'created_id', "int unsigned COMMENT 'FK to civicrm_contact, who uploaded this file'");

CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_file', 'FK_civicrm_file_created_id');

CRM_Core_DAO::executeQuery("
ALTER TABLE `civicrm_file`
ADD CONSTRAINT `FK_civicrm_file_created_id`
FOREIGN KEY (`created_id`)
REFERENCES `civicrm_contact`(`id`)
ON DELETE SET NULL
ON UPDATE CASCADE;
");

return TRUE;
}

/*
* Important! All upgrade functions MUST add a 'runSql' task.
* Uncomment and use the following template for a new upgrade version
Expand Down
22 changes: 0 additions & 22 deletions CRM/Upgrade/Incremental/php/FourSeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ public function upgrade_4_7_32($rev) {

$this->addTask('CRM-21733: Add status_override_end_date field to civicrm_membership table', 'addColumn', 'civicrm_membership', 'status_override_end_date',
"date DEFAULT NULL COMMENT 'The end date of membership status override if (Override until selected date) override type is selected.'");
$this->addTask('CRM-19948 - Add created_id column to civicrm_file', 'addFileCreatedIdColumn');
}

/*
Expand Down Expand Up @@ -1411,27 +1410,6 @@ public static function civiMailingCreatedDateNull(CRM_Queue_TaskContext $ctx) {
$dataType = 'datetime';
}
CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_mailing CHANGE created_date created_date {$dataType} NULL DEFAULT NULL COMMENT 'Date and time this mailing was created.'");

return TRUE;
}

public static function addFileCreatedIdColumn(CRM_Queue_TaskContext $ctx) {
self::addColumn($ctx, 'civicrm_file', 'created_id', "int unsigned COMMENT 'FK to civicrm_contact, who uploaded this file'");

$fileTable = 'civicrm_file';
$fkName = 'FK_civicrm_file_created_id';

CRM_Core_BAO_SchemaHandler::safeRemoveFK($fileTable, $fkName);

CRM_Core_DAO::executeQuery("
ALTER TABLE `{$fileTable}`
ADD CONSTRAINT `{$fkName}`
FOREIGN KEY (`created_id`)
REFERENCES `civicrm_contact`(`id`)
ON DELETE SET NULL
ON UPDATE CASCADE;
");

return TRUE;
}

Expand Down
7 changes: 6 additions & 1 deletion api/v3/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ function civicrm_api3_attachment_create($params) {
$fileDao->copyValues($file);
if (!$id) {
$fileDao->uri = CRM_Utils_File::makeFileName($name);
$fileDao->created_id = CRM_Core_Session::getLoggedInContactID();
}
$fileDao->save();

Expand Down Expand Up @@ -496,6 +495,12 @@ function _civicrm_api3_attachment_getfields() {
'description' => 'File content (not searchable, not returned by default)',
'type' => CRM_Utils_Type::T_STRING,
);
$spec['created_id'] = array(
'title' => 'Created By Contact ID',
'type' => CRM_Utils_Type::T_INT,
'description' => 'FK to civicrm_contact, who uploaded this file',
'api.default' => 'user_contact_id',
);

return $spec;
}
2 changes: 1 addition & 1 deletion bin/regen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ echo "DROP TABLE IF EXISTS zipcodes" | $MYSQLCMD
$MYSQLCMD < zipcodes.mysql

## For first boot on fresh DB, boot CMS before CRM.
cms_eval 'civicrm_initialize();'
#cms_eval 'civicrm_initialize();'

php GenerateData.php

Expand Down
66 changes: 34 additions & 32 deletions sql/civicrm_generated.mysql

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ public function getStaticCIDRefs() {
'civicrm_website' => array(
0 => 'contact_id',
),
'civicrm_file' => array(
0 => 'created_id',
),
);
}

Expand Down
10 changes: 7 additions & 3 deletions tests/phpunit/api/v3/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ public function testCreateWithWeirdName() {
$this->assertEquals('fa-file-text-o', $createResult['values'][$fileId]['icon']);
}

/**
* If one submits a file, it should be automatically store the logged in contact ID.
* This test ensure that there is always a creator ID of file upload
*/
public function testCreateShouldSetCreatedIdAsTheLoggedInUser() {
$loggedInUser = $this->createLoggedInUser();

Expand Down Expand Up @@ -473,11 +477,11 @@ public function testCreateShouldNotUpdateTheCreatedId() {

$this->callAPISuccess('Attachment', 'create', $attachmentParams);

$updatedAttachment = $this->callAPISuccess('Attachment', 'get', array(
$updatedAttachment = $this->callAPISuccess('Attachment', 'get', [
'id' => $fileId,
'entity_id' => $attachmentParams['entity_id'],
'entity_table' => $attachmentParams['entity_table']
));
'entity_table' => $attachmentParams['entity_table'],
]);

$this->assertNotEmpty($loggedInUser);
$this->assertEmpty($updatedAttachment['values'][$fileId]['created_id']);
Expand Down
4 changes: 2 additions & 2 deletions xml/schema/Core/File.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
<type>int unsigned</type>
<title>Created By Contact ID</title>
<comment>FK to civicrm_contact, who uploaded this file</comment>
<add>4.7</add>
<add>5.2</add>
</field>
<foreignKey>
<name>created_id</name>
<table>civicrm_contact</table>
<key>id</key>
<add>4.7</add>
<add>5.2</add>
<onDelete>SET NULL</onDelete>
</foreignKey>
</table>

0 comments on commit c74ea2d

Please sign in to comment.