feat: refactor skills API and database schema
- Replaced file-based skill category loading with API calls in the GET and POST methods of the skills route. - Added new `SkillsService` for handling skill category operations. - Updated SQL initialization script to create `skill_categories`, `skills`, and `skill_links` tables with appropriate relationships. - Enhanced `ApiClient` with methods for loading skill categories and creating new skills, improving API interaction. - Introduced error handling for skill category creation and loading processes.
This commit is contained in:
@@ -10,6 +10,34 @@ CREATE TABLE teams (
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Skill categories table
|
||||
CREATE TABLE skill_categories (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
icon VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Skills table
|
||||
CREATE TABLE skills (
|
||||
id VARCHAR(100) PRIMARY KEY,
|
||||
name VARCHAR(200) NOT NULL,
|
||||
description TEXT,
|
||||
icon VARCHAR(100),
|
||||
category_id VARCHAR(50) REFERENCES skill_categories(id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Skill links table
|
||||
CREATE TABLE skill_links (
|
||||
id SERIAL PRIMARY KEY,
|
||||
skill_id VARCHAR(100) REFERENCES skills(id) ON DELETE CASCADE,
|
||||
url TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Users table
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
@@ -73,6 +101,8 @@ INSERT INTO teams (id, name, direction) VALUES
|
||||
|
||||
-- Indexes for performance
|
||||
CREATE INDEX idx_teams_direction ON teams(direction);
|
||||
CREATE INDEX idx_skills_category_id ON skills(category_id);
|
||||
CREATE INDEX idx_skill_links_skill_id ON skill_links(skill_id);
|
||||
CREATE INDEX idx_users_team_id ON users(team_id);
|
||||
CREATE INDEX idx_user_evaluations_user_id ON user_evaluations(user_id);
|
||||
CREATE INDEX idx_category_evaluations_user_evaluation_id ON category_evaluations(user_evaluation_id);
|
||||
@@ -92,6 +122,12 @@ $$ language 'plpgsql';
|
||||
CREATE TRIGGER update_teams_updated_at BEFORE UPDATE ON teams
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_skill_categories_updated_at BEFORE UPDATE ON skill_categories
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_skills_updated_at BEFORE UPDATE ON skills
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON users
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
|
||||
54
scripts/migrate-skills.ts
Normal file
54
scripts/migrate-skills.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { SkillsService } from "../services/skills-service";
|
||||
import { loadSkillCategoriesFromFiles } from "../lib/skill-file-loader";
|
||||
|
||||
async function migrateSkillsToDatabase() {
|
||||
console.log("🚀 Starting skills migration...");
|
||||
|
||||
try {
|
||||
// Load all skill categories from JSON files
|
||||
const skillCategories = loadSkillCategoriesFromFiles();
|
||||
|
||||
console.log(`📊 Found ${skillCategories.length} categories`);
|
||||
|
||||
const totalSkills = skillCategories.reduce(
|
||||
(sum, cat) => sum + cat.skills.length,
|
||||
0
|
||||
);
|
||||
console.log(`🎯 Total skills to migrate: ${totalSkills}`);
|
||||
|
||||
// Bulk insert into database
|
||||
await SkillsService.bulkInsertSkillsFromJSON(skillCategories);
|
||||
|
||||
console.log("✅ Skills migration completed successfully!");
|
||||
|
||||
// Verify the migration
|
||||
const categoriesFromDb = await SkillsService.getSkillCategories();
|
||||
console.log(
|
||||
`✨ Verification: ${categoriesFromDb.length} categories in database`
|
||||
);
|
||||
|
||||
const totalSkillsInDb = categoriesFromDb.reduce(
|
||||
(sum, cat) => sum + cat.skills.length,
|
||||
0
|
||||
);
|
||||
console.log(`✨ Verification: ${totalSkillsInDb} skills in database`);
|
||||
} catch (error) {
|
||||
console.error("❌ Migration failed:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
migrateSkillsToDatabase()
|
||||
.then(() => {
|
||||
console.log("🎉 Migration script completed");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("💥 Migration script failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
export { migrateSkillsToDatabase };
|
||||
Reference in New Issue
Block a user