ai_upgrade_assistant-0.2.0-alpha2/ai_upgrade_assistant.install
ai_upgrade_assistant.install
<?php
/**
* @file
* Install, update and uninstall functions for the AI Upgrade Assistant module.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*/
function ai_upgrade_assistant_schema() {
$schema = [];
$schema['ai_upgrade_patterns'] = [
'description' => 'Stores upgrade patterns for AI training.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Pattern ID.',
],
'type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'The type of upgrade pattern.',
],
'project' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The name of the project being upgraded.',
],
'from_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The version being upgraded from.',
],
'to_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The version being upgraded to.',
],
'pattern_data' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'The serialized pattern data.',
],
'context_data' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'The serialized context data.',
],
'success' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'description' => 'Whether the upgrade was successful.',
],
'sync_status' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 'pending',
'description' => 'Status of synchronization with central training database.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the pattern was created.',
],
'changed' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the pattern was last updated.',
],
],
'primary key' => ['id'],
'indexes' => [
'project' => ['project'],
'type' => ['type'],
'sync_status' => ['sync_status'],
'created' => ['created'],
],
];
$schema['ai_upgrade_feedback'] = [
'description' => 'Stores feedback on upgrade suggestions for AI training.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Feedback ID.',
],
'pattern_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Foreign Key: The related pattern ID.',
],
'feedback_type' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The type of feedback (success, failure, partial).',
],
'feedback_data' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'The serialized feedback data.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the feedback was created.',
],
],
'primary key' => ['id'],
'foreign keys' => [
'pattern' => [
'table' => 'ai_upgrade_patterns',
'columns' => ['pattern_id' => 'id'],
],
],
'indexes' => [
'pattern_feedback' => ['pattern_id', 'feedback_type'],
'created' => ['created'],
],
];
$schema['ai_upgrade_index'] = [
'description' => 'Stores upgrade pattern indexes for faster lookups.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique pattern index ID.',
],
'pattern_hash' => [
'type' => 'varchar_ascii',
'length' => 64,
'not null' => TRUE,
'description' => 'Hash of the upgrade pattern.',
],
'pattern_type' => [
'type' => 'varchar_ascii',
'length' => 32,
'not null' => TRUE,
'description' => 'Type of upgrade pattern.',
],
'weight' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of the pattern for ordering.',
],
],
'primary key' => ['id'],
'indexes' => [
'pattern' => ['pattern_hash', 'pattern_type'],
'weight' => ['weight'],
],
];
$schema['ai_upgrade_achievements'] = [
'description' => 'Stores user achievements and progress',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key',
],
'uid' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'User ID',
],
'achievement_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Type of achievement (upgrade, pattern, community)',
],
'level' => [
'type' => 'int',
'not null' => TRUE,
'default' => 1,
'description' => 'Current level (1-20)',
],
'experience' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Experience points',
],
'specialization' => [
'type' => 'varchar',
'length' => 32,
'description' => 'User specialization (warrior, paladin, etc)',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of creation',
],
'updated' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of last update',
],
],
'primary key' => ['id'],
'indexes' => [
'uid' => ['uid'],
'achievement_type' => ['achievement_type'],
'specialization' => ['specialization'],
],
];
$schema['ai_upgrade_stats'] = [
'description' => 'Stores user upgrade statistics',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key',
],
'uid' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'User ID',
],
'upgrades_completed' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Total successful upgrades',
],
'patterns_contributed' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Number of patterns contributed',
],
'community_points' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Points earned from community participation',
],
'battle_points' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Points earned from critical fixes',
],
'guild_id' => [
'type' => 'int',
'description' => 'Associated guild ID',
],
'rank_title' => [
'type' => 'varchar',
'length' => 64,
'description' => 'Current rank title',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of creation',
],
'updated' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of last update',
],
],
'primary key' => ['id'],
'indexes' => [
'uid' => ['uid'],
'guild_id' => ['guild_id'],
],
];
$schema['ai_upgrade_guilds'] = [
'description' => 'Stores guild information',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key',
],
'name' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Guild name',
],
'description' => [
'type' => 'text',
'description' => 'Guild description',
],
'leader_uid' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Guild leader User ID',
],
'level' => [
'type' => 'int',
'not null' => TRUE,
'default' => 1,
'description' => 'Guild level',
],
'experience' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Guild experience points',
],
'specialization' => [
'type' => 'varchar',
'length' => 32,
'description' => 'Guild specialization',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of creation',
],
'updated' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp of last update',
],
],
'primary key' => ['id'],
'indexes' => [
'leader_uid' => ['leader_uid'],
'specialization' => ['specialization'],
],
];
$schema['ai_upgrade_assistant_forum_topics'] = [
'description' => 'Stores forum topics for the AI Upgrade Assistant.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique topic ID.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Topic title.',
],
'content' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
'description' => 'Topic content.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The {users}.uid that created the topic.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the topic was created.',
],
'tags' => [
'type' => 'text',
'size' => 'normal',
'description' => 'JSON encoded array of tags.',
],
'attachments' => [
'type' => 'text',
'size' => 'normal',
'description' => 'JSON encoded array of file attachments.',
],
],
'primary key' => ['id'],
'indexes' => [
'created' => ['created'],
'uid' => ['uid'],
],
'foreign keys' => [
'topic_author' => [
'table' => 'users',
'columns' => ['uid' => 'uid'],
],
],
];
$schema['ai_upgrade_assistant_forum_replies'] = [
'description' => 'Stores forum replies for the AI Upgrade Assistant.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique reply ID.',
],
'topic_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The topic ID this reply belongs to.',
],
'content' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
'description' => 'Reply content.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The {users}.uid that created the reply.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the reply was created.',
],
'attachments' => [
'type' => 'text',
'size' => 'normal',
'description' => 'JSON encoded array of file attachments.',
],
],
'primary key' => ['id'],
'indexes' => [
'topic_id' => ['topic_id'],
'created' => ['created'],
'uid' => ['uid'],
],
'foreign keys' => [
'reply_topic' => [
'table' => 'ai_upgrade_assistant_forum_topics',
'columns' => ['topic_id' => 'id'],
],
'reply_author' => [
'table' => 'users',
'columns' => ['uid' => 'uid'],
],
],
];
$schema['ai_upgrade_assistant_patterns'] = [
'description' => 'Stores upgrade patterns for the AI Upgrade Assistant.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Pattern ID.',
],
'module_name' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The name of the module.',
],
'task_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'code_analysis',
'description' => 'The type of task this pattern is for.',
],
'pattern_data' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'The serialized pattern data.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the pattern was created.',
],
'changed' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when the pattern was last updated.',
],
],
'primary key' => ['id'],
'indexes' => [
'module_name' => ['module_name'],
'task_type' => ['task_type'],
],
];
$schema['ai_upgrade_assistant_success_stories'] = [
'description' => 'Stores success stories for the AI Upgrade Assistant.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique story ID.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Story title.',
],
'content' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
'description' => 'Story content.',
],
'metadata' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
'description' => 'JSON encoded metadata about the success story.',
],
'screenshots' => [
'type' => 'text',
'size' => 'normal',
'description' => 'JSON encoded array of screenshot file IDs.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The {users}.uid that created the story.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the story was created.',
],
],
'primary key' => ['id'],
'indexes' => [
'created' => ['created'],
'uid' => ['uid'],
],
'foreign keys' => [
'story_author' => [
'table' => 'users',
'columns' => ['uid' => 'uid'],
],
],
];
$schema['ai_upgrade_assistant_update_history'] = [
'description' => 'Stores history of module updates.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Update ID.',
],
'project' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The name of the project being updated.',
],
'from_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The version being updated from.',
],
'to_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The version being updated to.',
],
'status' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The status of the update.',
],
'start_time' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the update started.',
],
'end_time' => [
'type' => 'int',
'not null' => FALSE,
'description' => 'Timestamp when the update completed.',
],
'metadata' => [
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'description' => 'Serialized metadata about the update.',
],
'results' => [
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'description' => 'Serialized results of the update.',
],
],
'primary key' => ['id'],
'indexes' => [
'project' => ['project'],
'status' => ['status'],
'start_time' => ['start_time'],
],
];
$schema['ai_upgrade_assistant_update_patches'] = [
'description' => 'Stores patches generated during updates.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Patch ID.',
],
'update_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Foreign Key: The associated update ID.',
],
'file_path' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The path of the file being patched.',
],
'patch_content' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'The content of the patch.',
],
'ai_confidence' => [
'type' => 'float',
'not null' => FALSE,
'description' => 'AI confidence score for the patch.',
],
'status' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'The status of the patch.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the patch was created.',
],
],
'primary key' => ['id'],
'foreign keys' => [
'update_id' => [
'table' => 'ai_upgrade_assistant_update_history',
'columns' => ['update_id' => 'id'],
],
],
'indexes' => [
'update_id' => ['update_id'],
'status' => ['status'],
],
];
$schema['ai_upgrade_assistant_update_errors'] = [
'description' => 'Stores errors encountered during updates.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Error ID.',
],
'update_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Foreign Key: The associated update ID.',
],
'error_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'The type of error.',
],
'message' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
'description' => 'The error message.',
],
'stack_trace' => [
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
'description' => 'The error stack trace.',
],
'timestamp' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the error occurred.',
],
],
'primary key' => ['id'],
'foreign keys' => [
'update_id' => [
'table' => 'ai_upgrade_assistant_update_history',
'columns' => ['update_id' => 'id'],
],
],
'indexes' => [
'update_id' => ['update_id'],
'error_type' => ['error_type'],
'timestamp' => ['timestamp'],
],
];
$schema['upgrade_analysis'] = [
'description' => 'Stores module upgrade analysis results.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Analysis ID.',
],
'uuid' => [
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'description' => 'Unique identifier for this entity.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The user ID of the author.',
],
'project' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The name of the project being analyzed.',
],
'current_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'Current version of the project.',
],
'target_version' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'Target version for upgrade.',
],
'analysis_data' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'description' => 'Serialized analysis results.',
],
'status' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 'pending',
'description' => 'Status of the analysis.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the analysis was created.',
'default' => 0,
],
'changed' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Timestamp when the analysis was last updated.',
'default' => 0,
],
'langcode' => [
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => 'en',
'description' => 'Language code for this item.',
],
],
'primary key' => ['id'],
'unique keys' => [
'uuid' => ['uuid'],
],
'indexes' => [
'project' => ['project'],
'status' => ['status'],
'created' => ['created'],
'uid' => ['uid'],
],
'foreign keys' => [
'author' => [
'table' => 'users_field_data',
'columns' => ['uid' => 'uid'],
],
],
];
return $schema;
}
/**
* Implements hook_install().
*/
function ai_upgrade_assistant_install() {
// Only try to set weight if system table exists
$schema = \Drupal::database()->schema();
if ($schema->tableExists('system')) {
try {
$weight = _ai_upgrade_assistant_calculate_weight();
\Drupal::database()
->update('system')
->fields(['weight' => $weight])
->condition('name', 'ai_upgrade_assistant')
->condition('type', 'module')
->execute();
}
catch (\Exception $e) {
\Drupal::logger('ai_upgrade_assistant')->warning('Could not set module weight during installation: @error', ['@error' => $e->getMessage()]);
}
}
}
/**
* Create initial database tables.
*/
function ai_upgrade_assistant_update_9001() {
$schema = ai_upgrade_assistant_schema();
$database = Database::getConnection();
// Create all required tables
$tables = [
'ai_upgrade_patterns',
'ai_upgrade_feedback',
'ai_upgrade_achievements',
'ai_upgrade_assistant_forum_topics',
'ai_upgrade_assistant_forum_replies',
'ai_upgrade_assistant_patterns',
'ai_upgrade_assistant_success_stories',
'ai_upgrade_assistant_update_history',
'upgrade_analysis'
];
foreach ($tables as $table) {
if (!$database->schema()->tableExists($table) && isset($schema[$table])) {
$database->schema()->createTable($table, $schema[$table]);
}
}
}
/**
* Add achievement system tables.
*/
function ai_upgrade_assistant_update_9002() {
$schema = \Drupal::database()->schema();
// Create achievement tables if they don't exist
$achievement_tables = [
'ai_upgrade_achievements',
'ai_upgrade_stats',
'ai_upgrade_guilds'
];
foreach ($achievement_tables as $table) {
if (!$schema->tableExists($table) && isset(ai_upgrade_assistant_schema()[$table])) {
$schema->createTable($table, ai_upgrade_assistant_schema()[$table]);
}
}
}
/**
* Move API keys from config to state storage.
*/
function ai_upgrade_assistant_update_9003() {
$config = \Drupal::configFactory()->getEditable('ai_upgrade_assistant.settings');
$state = \Drupal::state();
// Move API keys from config to state
$api_keys = [
'openai_api_key',
'github_token',
'drupal_api_key'
];
foreach ($api_keys as $key) {
if ($value = $config->get($key)) {
$state->set('ai_upgrade_assistant.' . $key, $value);
$config->clear($key);
}
}
$config->save();
}
/**
* Calculate optimal module weight based on dependencies.
*/
function _ai_upgrade_assistant_calculate_weight() {
$moduleHandler = \Drupal::service('module_handler');
$moduleList = $moduleHandler->getModuleList();
// Get weights of modules we depend on
$dependencyWeights = [];
$dependencies = ['system', 'update', 'migrate', 'migrate_drupal'];
foreach ($dependencies as $dependency) {
if ($moduleHandler->moduleExists($dependency)) {
$dependencyWeights[] = $moduleList[$dependency]->weight;
}
}
// Set our weight higher than our highest dependency
// Add 10 to ensure we run after all core modules
return empty($dependencyWeights) ? 10 : max($dependencyWeights) + 10;
}
/**
* Add community learning tables.
*/
function ai_upgrade_assistant_update_9004() {
$schema = ai_upgrade_assistant_schema();
$database = Database::getConnection();
// Create community learning tables if they don't exist
$tables = [
'ai_upgrade_assistant_patterns',
'ai_upgrade_assistant_forum_topics',
'ai_upgrade_assistant_forum_replies',
'ai_upgrade_assistant_success_stories',
];
foreach ($tables as $table) {
if (!$database->schema()->tableExists($table) && isset($schema[$table])) {
$database->schema()->createTable($table, $schema[$table]);
}
}
}
/**
* Install update history tracking tables.
*/
function ai_upgrade_assistant_update_9005() {
$schema = Database::getConnection()->schema();
$tables = [
'ai_upgrade_assistant_update_history',
'ai_upgrade_assistant_update_patches',
'ai_upgrade_assistant_update_errors',
];
$update_schema = ai_upgrade_assistant_schema();
foreach ($tables as $table) {
if (!$schema->tableExists($table)) {
$schema->createTable($table, $update_schema[$table]);
}
}
}
/**
* Add task_type column to ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9006() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table) && !$schema->fieldExists($table, 'task_type')) {
$spec = [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'code_analysis',
'description' => 'The type of task this pattern is for.',
];
$schema->addField($table, 'task_type', $spec);
$schema->addIndex($table, 'task_type', ['task_type'], ['task_type_index']);
// Update existing records to have the default task_type
Database::getConnection()
->update($table)
->fields(['task_type' => 'code_analysis'])
->isNull('task_type')
->execute();
}
}
/**
* Add context column to ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9013() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table) && !$schema->fieldExists($table, 'context')) {
$spec = [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'code_analysis',
'description' => 'The context of the pattern.',
];
$schema->addField($table, 'context', $spec);
// Set default context for existing patterns
Database::getConnection()
->update($table)
->fields(['context' => 'code_analysis'])
->isNull('context')
->execute();
}
}
/**
* Rename module_name column to project in ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9008() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table)) {
$spec = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The project name.',
];
// Add new column
$schema->addField($table, 'project', $spec);
// Copy data from old column to new
Database::getConnection()->query("UPDATE {" . $table . "} SET project = module_name");
// Drop old column
$schema->dropField($table, 'module_name');
return t('Renamed module_name column to project in ai_upgrade_assistant_patterns table.');
}
}
/**
* Update module weight to ensure proper template loading.
*/
function ai_upgrade_assistant_update_9009() {
try {
$weight = _ai_upgrade_assistant_calculate_weight();
\Drupal::database()
->update('system')
->fields(['weight' => $weight])
->condition('name', 'ai_upgrade_assistant')
->condition('type', 'module')
->execute();
}
catch (\Exception $e) {
\Drupal::logger('ai_upgrade_assistant')->error('Could not update module weight: @error', ['@error' => $e->getMessage()]);
}
}
/**
* Calculate optimal module weight based on dependencies.
*
* Ensures our module runs after core upgrade modules and any other
* modules that might be providing upgrade paths we need to analyze.
*
* @return int
* The calculated module weight
*/
function _ai_upgrade_assistant_calculate_module_weight() {
$moduleHandler = \Drupal::service('module_handler');
$moduleList = $moduleHandler->getModuleList();
// Get weights of modules we depend on
$dependencyWeights = [];
$dependencies = ['update', 'migrate', 'migrate_drupal'];
foreach ($dependencies as $dependency) {
if ($moduleHandler->moduleExists($dependency)) {
$dependencyWeights[] = $moduleList[$dependency]->weight;
}
}
// Set our weight higher than our highest dependency
// Add 5 to ensure we run after any potential sub-modules
$weight = empty($dependencyWeights) ? 10 : max($dependencyWeights) + 5;
return $weight;
}
/**
* Update module weight to ensure optimal execution order.
*/
function ai_upgrade_assistant_update_9010() {
$weight = _ai_upgrade_assistant_calculate_module_weight();
module_set_weight('ai_upgrade_assistant', $weight);
return t('Updated AI Upgrade Assistant module weight to @weight for optimal execution order.', [
'@weight' => $weight,
]);
}
/**
* Set module weight and create cache tables.
*/
function ai_upgrade_assistant_update_9011() {
module_set_weight('ai_upgrade_assistant', 10);
ai_upgrade_assistant_install();
}
/**
* Add task_type and module_name columns to ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9012() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table)) {
if (!$schema->fieldExists($table, 'task_type')) {
$spec = [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'code_analysis',
'description' => 'The type of task this pattern is for.',
];
$schema->addField($table, 'task_type', $spec);
}
if (!$schema->fieldExists($table, 'module_name')) {
$spec = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The name of the module this pattern is for.',
];
$schema->addField($table, 'module_name', $spec);
// Copy data from project column to module_name if it exists
if ($schema->fieldExists($table, 'project')) {
Database::getConnection()->query("UPDATE {ai_upgrade_assistant_patterns} SET module_name = project");
$schema->dropField($table, 'project');
}
}
}
}
/**
* Implements hook_uninstall().
*/
function ai_upgrade_assistant_uninstall() {
// Clean up configuration
\Drupal::configFactory()->getEditable('ai_upgrade_assistant.settings')->delete();
// Clean up state
\Drupal::state()->deleteMultiple([
'ai_upgrade_assistant.openai_api_key',
'ai_upgrade_assistant.github_token',
'ai_upgrade_assistant.huggingface_token',
'ai_upgrade_assistant.last_sync',
'ai_upgrade_assistant.sync_token',
'ai_upgrade_assistant.is_production',
]);
}
/**
* Add task_type column to ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9014() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table) && !$schema->fieldExists($table, 'task_type')) {
$spec = [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'code_analysis',
'description' => 'The type of task this pattern is for.',
];
$schema->addField($table, 'task_type', $spec);
// Update existing records to have the default value
Database::getConnection()->update($table)
->fields(['task_type' => 'code_analysis'])
->execute();
}
}
/**
* Add module_name column to ai_upgrade_assistant_patterns table.
*/
function ai_upgrade_assistant_update_9015() {
$schema = Database::getConnection()->schema();
$table = 'ai_upgrade_assistant_patterns';
if ($schema->tableExists($table) && !$schema->fieldExists($table, 'module_name')) {
$spec = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The name of the module.',
];
$schema->addField($table, 'module_name', $spec);
// Create an index for better performance
$schema->addIndex($table, 'module_name', ['module_name'], [
'fields' => ['module_name' => 255],
]);
// Try to populate module_name from existing data if possible
try {
$query = Database::getConnection()->select($table, 'p');
$query->fields('p', ['id', 'pattern_data']);
$results = $query->execute()->fetchAll();
foreach ($results as $result) {
$pattern_data = unserialize($result->pattern_data);
if (isset($pattern_data['module'])) {
Database::getConnection()->update($table)
->fields(['module_name' => $pattern_data['module']])
->condition('id', $result->id)
->execute();
}
}
}
catch (\Exception $e) {
\Drupal::logger('ai_upgrade_assistant')->error(
'Error populating module_name data: @error',
['@error' => $e->getMessage()]
);
}
}
}
