add-comment-column.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Script to add the 'comment' column to the scores table
  2. const sqlite3 = require('sqlite3').verbose();
  3. const path = require('path');
  4. // Path to the database
  5. const DB_PATH = process.env.DB_PATH || path.join(__dirname, '../../data/framed.db');
  6. console.log(`Connecting to database: ${DB_PATH}`);
  7. // Connect to the database
  8. const db = new sqlite3.Database(DB_PATH, (err) => {
  9. if (err) {
  10. console.error('Error connecting to database:', err.message);
  11. process.exit(1);
  12. }
  13. console.log('Connected to the SQLite database.');
  14. // Check if the column already exists
  15. db.get("PRAGMA table_info(scores)", (err, rows) => {
  16. if (err) {
  17. console.error('Error checking table schema:', err.message);
  18. closeAndExit(1);
  19. }
  20. // Parse the result to check if the comment column exists
  21. const hasCommentColumn = rows && rows.some(row => row.name === 'comment');
  22. if (hasCommentColumn) {
  23. console.log('The comment column already exists in the scores table.');
  24. closeAndExit(0);
  25. } else {
  26. // Add the comment column to the scores table
  27. db.run('ALTER TABLE scores ADD COLUMN comment TEXT', (err) => {
  28. if (err) {
  29. console.error('Error adding comment column:', err.message);
  30. closeAndExit(1);
  31. }
  32. console.log('Successfully added comment column to scores table.');
  33. closeAndExit(0);
  34. });
  35. }
  36. });
  37. });
  38. // Function to close the database connection and exit
  39. function closeAndExit(code) {
  40. db.close((err) => {
  41. if (err) {
  42. console.error('Error closing database connection:', err.message);
  43. } else {
  44. console.log('Database connection closed.');
  45. }
  46. process.exit(code);
  47. });
  48. }