fix-datetime-precision.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { AppDataSource } from '../models/index.js';
  2. import { logger } from '../utils/logger.js';
  3. async function fixDatetimePrecision() {
  4. try {
  5. await AppDataSource.initialize();
  6. logger.info('[Fix] Database connected');
  7. // 设置时区
  8. await AppDataSource.query("SET time_zone = '+08:00'");
  9. logger.info('[Fix] Timezone set to +08:00');
  10. // 检查当前精度
  11. const currentStructure = await AppDataSource.query(`
  12. SELECT
  13. COLUMN_NAME,
  14. COLUMN_TYPE
  15. FROM INFORMATION_SCHEMA.COLUMNS
  16. WHERE TABLE_SCHEMA = DATABASE()
  17. AND TABLE_NAME = 'user_day_statistics'
  18. AND COLUMN_NAME IN ('created_at', 'updated_at')
  19. `);
  20. logger.info('[Fix] Current column types:');
  21. for (const col of currentStructure) {
  22. logger.info(`[Fix] ${col.COLUMN_NAME}: ${col.COLUMN_TYPE}`);
  23. }
  24. // 修改 created_at 从 datetime(6) 改为 datetime(去掉微秒精度)
  25. logger.info('[Fix] Modifying created_at precision...');
  26. await AppDataSource.query(`
  27. ALTER TABLE user_day_statistics
  28. MODIFY COLUMN created_at DATETIME NULL
  29. `);
  30. await AppDataSource.query(`
  31. ALTER TABLE user_day_statistics
  32. MODIFY COLUMN created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  33. `);
  34. logger.info('[Fix] created_at precision fixed');
  35. // 修改 updated_at 从 datetime(6) 改为 datetime(去掉微秒精度)
  36. logger.info('[Fix] Modifying updated_at precision...');
  37. await AppDataSource.query(`
  38. ALTER TABLE user_day_statistics
  39. MODIFY COLUMN updated_at DATETIME NULL
  40. `);
  41. await AppDataSource.query(`
  42. ALTER TABLE user_day_statistics
  43. MODIFY COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  44. `);
  45. logger.info('[Fix] updated_at precision fixed');
  46. // 更新历史数据:确保时间格式正确(去掉微秒部分)
  47. logger.info('[Fix] Updating historical data to remove microseconds...');
  48. await AppDataSource.query(`
  49. UPDATE user_day_statistics
  50. SET created_at = DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s')
  51. WHERE created_at IS NOT NULL
  52. `);
  53. await AppDataSource.query(`
  54. UPDATE user_day_statistics
  55. SET updated_at = DATE_FORMAT(updated_at, '%Y-%m-%d %H:%i:%s')
  56. WHERE updated_at IS NOT NULL
  57. `);
  58. logger.info('[Fix] Historical data updated');
  59. // 验证结果
  60. logger.info('[Fix] Verifying results...');
  61. const newStructure = await AppDataSource.query(`
  62. SELECT
  63. COLUMN_NAME,
  64. COLUMN_TYPE,
  65. DATA_TYPE
  66. FROM INFORMATION_SCHEMA.COLUMNS
  67. WHERE TABLE_SCHEMA = DATABASE()
  68. AND TABLE_NAME = 'user_day_statistics'
  69. AND COLUMN_NAME IN ('created_at', 'updated_at')
  70. ORDER BY COLUMN_NAME
  71. `);
  72. logger.info('[Fix] New column types:');
  73. for (const col of newStructure) {
  74. logger.info(`[Fix] ${col.COLUMN_NAME}: ${col.COLUMN_TYPE} (${col.DATA_TYPE})`);
  75. }
  76. // 查看样本数据
  77. const sampleData = await AppDataSource.query(`
  78. SELECT
  79. id,
  80. DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') AS created_at_formatted,
  81. DATE_FORMAT(updated_at, '%Y-%m-%d %H:%i:%s') AS updated_at_formatted,
  82. created_at,
  83. updated_at
  84. FROM user_day_statistics
  85. ORDER BY id DESC
  86. LIMIT 5
  87. `);
  88. logger.info('[Fix] Sample data after fix:');
  89. for (const row of sampleData) {
  90. logger.info(`[Fix] ID ${row.id}:`);
  91. logger.info(`[Fix] created_at: ${row.created_at} (formatted: ${row.created_at_formatted})`);
  92. logger.info(`[Fix] updated_at: ${row.updated_at} (formatted: ${row.updated_at_formatted})`);
  93. }
  94. logger.info('[Fix] Fix completed successfully!');
  95. process.exit(0);
  96. } catch (error) {
  97. logger.error('[Fix] Fix failed:', error);
  98. process.exit(1);
  99. } finally {
  100. if (AppDataSource.isInitialized) {
  101. await AppDataSource.destroy();
  102. }
  103. }
  104. }
  105. void fixDatetimePrecision();