lionbridge_translation_provider-8.x-2.4/tmgmt_contentapi/tmgmt_contentapi.install
tmgmt_contentapi/tmgmt_contentapi.install
<?php
/**
* @file
* Install file for TMGMT Content API module.
*/
/**
* Install hook to create a table to store the request id of each job.
*/
function tmgmt_contentapi_install() {
// Define the schema for the new table.
\Drupal::database()->schema()->createTable('tmgmt_capi_request_processor', tmgmt_contentapi_defined_schema());
\Drupal::database()->schema()->createTable('tmgmt_capi_response', tmgmt_contentapi_response_defined_schema());
}
/**
* Uninstall hook to drop the table.
*/
function tmgmt_contentapi_uninstall() {
// Drop the table.
\Drupal::database()->schema()->dropTable('tmgmt_capi_request_processor');
\Drupal::database()->schema()->dropTable('tmgmt_capi_response');
}
/**
* Create a table to track each request id of job.
*/
function tmgmt_contentapi_update_9100() {
// Check if the table already exists.
if (\Drupal::database()->schema()->tableExists('tmgmt_capi_request_processor')) {
return;
}
// Define the schema for the new table.
\Drupal::database()->schema()->createTable('tmgmt_capi_request_processor',
tmgmt_contentapi_defined_schema()
);
}
/**
* Add jobs to QUEUE to migrate existng jobs to new table.
*/
function tmgmt_contentapi_update_9101() {
\Drupal::service('tmgmt_contentapi.capi_data_processor')->addJobsToQueueForMigration();
// Clear chache.
\Drupal::cache()->invalidateAll();
}
/**
* Create a table to track each response of a file.
*/
function tmgmt_contentapi_update_9102() {
// Check if the table already exists.
if (\Drupal::database()->schema()->tableExists('tmgmt_capi_response')) {
return;
}
\Drupal::database()->schema()->createTable('tmgmt_capi_response',
tmgmt_contentapi_response_defined_schema()
);
}
/**
* Function to return fields array for install hook as well as for update hook.
*
* To create table tmgmt_capi_request_processor.
*/
function tmgmt_contentapi_defined_schema() {
return [
'description' => 'Table for storing request info.',
'fields' => [
'rid' => [
'description' => 'Primary Key: Unique request ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'tjid' => [
'description' => 'The TMGMT job ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
],
'tjiid' => [
'description' => 'The TMGMT job item ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
],
'updateid' => [
'description' => 'The update id.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
],
'jobid' => [
'description' => 'Job ID.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
],
'providerid' => [
'description' => 'The provider id.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
],
'requestid' => [
'description' => 'The request ID.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'statuscode' => [
'description' => 'The status code - CREATED, SENDING, IN_TRANSLATION, REVIEW_TRANSLATION, TRANSLATION_APPROVED.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
],
'haserror' => [
'description' => 'If this request has any error.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
],
'errormessage' => [
'description' => 'The error message.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
],
'status' => [
'description' => 'NEW, SCANNED, IGNORED, TO_PROCESS, CANCELLED, IMPORTED, COMPLETED.',
'type' => 'varchar',
'length' => 50,
'not null' => FALSE,
'default' => '',
],
'updatedtime' => [
'description' => 'The timestamp when the request was created at capi end.',
'type' => 'datetime',
'not null' => FALSE,
'mysql_type' => 'datetime(6)',
],
'lastupdated' => [
'description' => 'The timestamp when the request was last modified in db.',
'type' => 'timestamp',
'not null' => FALSE,
'mysql_type' => 'timestamp',
],
'file_upload_status' => [
'type' => 'varchar',
'length' => 50,
'not null' => FALSE,
'default' => NULL,
'description' => 'File upload status: PENDING, UPLOADING, UPLOADED, FAILED (Only for CREATED statuscode rows)',
],
'file_upload_attempts' => [
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'default' => NULL,
'description' => 'Number of file upload attempts (Only for CREATED statuscode rows)',
],
'capi_response' => [
'type' => 'text',
'not null' => FALSE,
'default' => NULL,
'description' => 'Response from the CAPI API',
],
],
'primary key' => ['rid'],
];
}
/**
* Function to return fields array for install hook as well as for update hook.
*
* To create table tmgmt_capi_response.
*/
function tmgmt_contentapi_response_defined_schema() {
return [
'description' => 'Stores API response data for submitted files.',
'fields' => [
'respid' => [
'description' => 'Primary Key: Unique ID.',
'type' => 'serial',
'not null' => TRUE,
],
'jobkey' => [
'description' => 'A unique key representing a batch of items.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
],
'item_id' => [
'description' => 'The item ID within the job key.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => '',
],
'apiresponse' => [
'description' => 'Response from API.',
'type' => 'text',
'not null' => TRUE,
],
'created' => [
'description' => 'Timestamp when the data was stored.',
'type' => 'int',
'default' => time(),
],
],
'primary key' => ['respid'],
];
}
/**
* Add robustness columns to prevent race conditions in file upload completion.
* Only CREATED statuscode rows need file_upload_status tracking.
*/
function tmgmt_contentapi_update_9103() {
$database = \Drupal::database();
$schema = $database->schema();
// Add ONLY the essential columns for robustness - Only relevant for CREATED statuscode rows
if (!$schema->fieldExists('tmgmt_capi_request_processor', 'file_upload_status')) {
$schema->addField('tmgmt_capi_request_processor', 'file_upload_status', [
'type' => 'varchar',
'length' => 50,
'not null' => FALSE,
'default' => NULL,
'description' => 'File upload status: PENDING, UPLOADING, UPLOADED, FAILED (Only for CREATED statuscode rows)',
]);
}
// Track number of attempts to upload file - Only relevant for CREATED statuscode rows
if (!$schema->fieldExists('tmgmt_capi_request_processor', 'file_upload_attempts')) {
$schema->addField('tmgmt_capi_request_processor', 'file_upload_attempts', [
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'default' => NULL,
'description' => 'Number of file upload attempts (Only for CREATED statuscode rows)',
]);
}
// Store responses from API in a capi_response column.
if (!$schema->fieldExists('tmgmt_capi_request_processor', 'capi_response')) {
$schema->addField('tmgmt_capi_request_processor', 'capi_response', [
'type' => 'text',
'not null' => FALSE,
'default' => NULL,
'description' => 'Response from the CAPI API',
]);
}
// NOTE: Existing records are left with NULL values in the new tracking columns.
// The file_upload_status and file_upload_attempts columns will only be used
// for new job submissions going forward with the robustness improvements.
\Drupal::logger('tmgmt_contentapi')->info('Update 9103: Added file upload tracking columns for robustness improvements');
}
