migrate_learning_analytics_data.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * LearningAnalytics数据迁移脚本
  4. * 将PostgreSQL中的数据迁移到MySQL
  5. */
  6. require __DIR__ . '/../vendor/autoload.php';
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. // 设置Laravel应用
  10. $app = require_once __DIR__ . '/../bootstrap/app.php';
  11. $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
  12. $kernel->bootstrap();
  13. echo "=== LearningAnalytics数据迁移开始 ===\n\n";
  14. try {
  15. // 1. 迁移知识点数据
  16. echo "1. 迁移知识点数据...\n";
  17. $knowledgePoints = [
  18. ['kp_code' => 'KP001', 'name' => '因式分解基础', 'parent_kp_code' => null, 'level' => 1],
  19. ['kp_code' => 'KP002', 'name' => '提公因式法', 'parent_kp_code' => 'KP001', 'level' => 2],
  20. ['kp_code' => 'KP003', 'name' => '分组分解法', 'parent_kp_code' => 'KP001', 'level' => 2],
  21. ['kp_code' => 'KP004', 'name' => '完全平方公式', 'parent_kp_code' => 'KP001', 'level' => 2],
  22. ['kp_code' => 'KP005', 'name' => '平方差公式', 'parent_kp_code' => 'KP001', 'level' => 2],
  23. ];
  24. foreach ($knowledgePoints as $kp) {
  25. DB::table('knowledge_points')->updateOrInsert(
  26. ['kp_code' => $kp['kp_code']],
  27. [
  28. 'name' => $kp['name'],
  29. 'parent_kp_code' => $kp['parent_kp_code'],
  30. 'created_at' => now(),
  31. 'updated_at' => now(),
  32. ]
  33. );
  34. }
  35. echo "✓ 知识点数据迁移完成\n\n";
  36. // 2. 从PostgreSQL导出student_attempts数据
  37. echo "2. 从PostgreSQL导出student_attempts数据...\n";
  38. echo "注意:需要手动执行以下命令导出数据:\n";
  39. echo "docker exec learning_analytics_postgres pg_dump -U rag_user -d learning_analytics -t student_attempts --data-only --no-owner --no-privileges > /tmp/student_attempts.sql\n\n";
  40. // 3. 转换PostgreSQL数据为MySQL格式
  41. echo "3. 数据转换说明:\n";
  42. echo " - PostgreSQL的timestamp with time zone -> MySQL的timestamp\n";
  43. echo " - PostgreSQL的boolean (t/f) -> MySQL的boolean (1/0)\n";
  44. echo " - PostgreSQL的numeric -> MySQL的decimal\n";
  45. echo " - PostgreSQL的jsonb -> MySQL的json\n";
  46. echo " - PostgreSQL的inet -> MySQL的varchar\n\n";
  47. // 4. 导入示例数据(如果PostgreSQL无法访问)
  48. echo "4. 导入示例数据到student_attempts表...\n";
  49. $sampleAttempts = [
  50. [
  51. 'student_id' => 'stu_1762395159_4',
  52. 'session_id' => 'session_stu_1762395159_4_1763471539',
  53. 'attempt_number' => 1,
  54. 'question_id' => 'Q_BATCH_stu_1762395159_4_1763471524_6680',
  55. 'question_code' => 'Q_BATCH_stu_1762395159_4_1763471524_6680',
  56. 'kp_code' => 'KP1001',
  57. 'skill_tags' => null,
  58. 'is_correct' => false,
  59. 'student_answer' => '',
  60. 'correct_answer' => '解题步骤:使用立方差公式a³ - b³ = (a - b)(a² + ab + b²),其中a=x, b=2',
  61. 'partial_score' => 0.0000,
  62. 'attempt_time_seconds' => 88.00,
  63. 'time_per_question' => 88.00,
  64. 'started_at' => '2025-11-18 13:12:19',
  65. 'completed_at' => '2025-11-18 13:12:19',
  66. 'time_spent_seconds' => 88,
  67. 'question_difficulty' => 0.08,
  68. 'question_type' => '数学题',
  69. 'max_score' => 100,
  70. 'device_type' => null,
  71. 'browser_info' => null,
  72. 'ip_address' => null,
  73. 'location_info' => null,
  74. 'status' => 'completed',
  75. 'reviewed' => false,
  76. 'feedback_provided' => null,
  77. 'created_at' => '2025-11-18 13:12:19',
  78. 'updated_at' => '2025-11-18 13:12:19',
  79. ],
  80. [
  81. 'student_id' => 'stu_1762395159_4',
  82. 'session_id' => 'session_stu_1762395159_4_1763471539',
  83. 'attempt_number' => 1,
  84. 'question_id' => 'Q_BATCH_stu_1762395159_4_1763471524_7908',
  85. 'question_code' => 'Q_BATCH_stu_1762395159_4_1763471524_7908',
  86. 'kp_code' => 'KP1001',
  87. 'skill_tags' => null,
  88. 'is_correct' => true,
  89. 'student_answer' => '',
  90. 'correct_answer' => '应用解题过程:首先识别需要十字相乘的数学对象,然后应用相应的分解方法...',
  91. 'partial_score' => 1.0000,
  92. 'attempt_time_seconds' => 137.00,
  93. 'time_per_question' => 137.00,
  94. 'started_at' => '2025-11-18 13:12:19',
  95. 'completed_at' => '2025-11-18 13:12:19',
  96. 'time_spent_seconds' => 137,
  97. 'question_difficulty' => 0.08,
  98. 'question_type' => '数学题',
  99. 'max_score' => 100,
  100. 'device_type' => null,
  101. 'browser_info' => null,
  102. 'ip_address' => null,
  103. 'location_info' => null,
  104. 'status' => 'completed',
  105. 'reviewed' => false,
  106. 'feedback_provided' => null,
  107. 'created_at' => '2025-11-18 13:12:19',
  108. 'updated_at' => '2025-11-18 13:12:19',
  109. ],
  110. ];
  111. foreach ($sampleAttempts as $attempt) {
  112. DB::table('student_attempts')->insert($attempt);
  113. }
  114. echo "✓ 示例数据导入完成\n\n";
  115. // 5. 验证迁移结果
  116. echo "5. 验证迁移结果...\n";
  117. $kpCount = DB::table('knowledge_points')->count();
  118. $attemptCount = DB::table('student_attempts')->count();
  119. echo "知识点数量: {$kpCount}\n";
  120. echo "答题记录数量: {$attemptCount}\n\n";
  121. echo "=== LearningAnalytics数据迁移完成 ===\n";
  122. } catch (Exception $e) {
  123. echo "❌ 迁移失败: " . $e->getMessage() . "\n";
  124. echo "堆栈跟踪: " . $e->getTraceAsString() . "\n";
  125. exit(1);
  126. }