Fix jwt-token
This commit is contained in:
@@ -156,32 +156,33 @@ async function startServer() {
|
|||||||
await sequelize.authenticate();
|
await sequelize.authenticate();
|
||||||
console.log('Database connected successfully.');
|
console.log('Database connected successfully.');
|
||||||
|
|
||||||
// Run migrations first
|
// STEP 1: Sync database first to create base tables
|
||||||
try {
|
|
||||||
await runMigrations();
|
|
||||||
} catch (migrationError) {
|
|
||||||
console.error('Migration error:', migrationError);
|
|
||||||
console.log('Continuing with database sync...');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always sync database in containerized environments or development
|
|
||||||
// Check if tables exist before syncing
|
|
||||||
try {
|
try {
|
||||||
// Use alter: false to prevent destructive changes in production
|
// Use alter: false to prevent destructive changes in production
|
||||||
await sequelize.sync({ force: false, alter: false });
|
await sequelize.sync({ force: false, alter: false });
|
||||||
console.log('Database synchronized.');
|
console.log('Database synchronized.');
|
||||||
|
|
||||||
// Seed database with initial data
|
|
||||||
await seedDatabase();
|
|
||||||
} catch (syncError) {
|
} catch (syncError) {
|
||||||
console.error('Database sync error:', syncError);
|
console.error('Database sync error:', syncError);
|
||||||
// If sync fails, try force sync (this will drop and recreate tables)
|
// If sync fails, try force sync (this will drop and recreate tables)
|
||||||
console.log('Attempting force sync...');
|
console.log('Attempting force sync...');
|
||||||
await sequelize.sync({ force: true });
|
await sequelize.sync({ force: true });
|
||||||
console.log('Database force synchronized.');
|
console.log('Database force synchronized.');
|
||||||
|
}
|
||||||
// Seed database with initial data
|
|
||||||
|
// STEP 2: Run migrations after tables exist
|
||||||
|
try {
|
||||||
|
await runMigrations();
|
||||||
|
} catch (migrationError) {
|
||||||
|
console.error('Migration error:', migrationError);
|
||||||
|
throw migrationError; // Fatal error - don't continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// STEP 3: Seed database with initial data
|
||||||
|
try {
|
||||||
await seedDatabase();
|
await seedDatabase();
|
||||||
|
} catch (seedError) {
|
||||||
|
console.error('Seeding error:', seedError);
|
||||||
|
throw seedError; // Fatal error - don't continue
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
|
|||||||
@@ -7,44 +7,32 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async up(queryInterface, Sequelize) {
|
async up(queryInterface, Sequelize) {
|
||||||
try {
|
// Check if is_approved column already exists
|
||||||
// Check if devices table exists first
|
const tableDescription = await queryInterface.describeTable('devices');
|
||||||
const tables = await queryInterface.showAllTables();
|
|
||||||
if (!tables.includes('devices')) {
|
if (!tableDescription.is_approved) {
|
||||||
console.log('⚠️ Devices table does not exist yet, skipping device approval migration...');
|
// Add is_approved column to devices table
|
||||||
return;
|
await queryInterface.addColumn('devices', 'is_approved', {
|
||||||
}
|
type: Sequelize.BOOLEAN,
|
||||||
|
defaultValue: false,
|
||||||
|
allowNull: false,
|
||||||
|
comment: 'Whether the device is approved to send data'
|
||||||
|
});
|
||||||
|
|
||||||
// Check if is_approved column already exists
|
// Add index for is_approved for better query performance
|
||||||
const tableDescription = await queryInterface.describeTable('devices');
|
await queryInterface.addIndex('devices', ['is_approved'], {
|
||||||
|
name: 'devices_is_approved_idx'
|
||||||
if (!tableDescription.is_approved) {
|
});
|
||||||
// Add is_approved column to devices table
|
|
||||||
await queryInterface.addColumn('devices', 'is_approved', {
|
|
||||||
type: Sequelize.BOOLEAN,
|
|
||||||
defaultValue: false,
|
|
||||||
allowNull: false,
|
|
||||||
comment: 'Whether the device is approved to send data'
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add index for is_approved for better query performance
|
// Approve all existing devices by default (backward compatibility)
|
||||||
await queryInterface.addIndex('devices', ['is_approved'], {
|
await queryInterface.sequelize.query(
|
||||||
name: 'devices_is_approved_idx'
|
'UPDATE devices SET is_approved = true WHERE created_at < NOW()'
|
||||||
});
|
);
|
||||||
|
|
||||||
// Approve all existing devices by default (backward compatibility)
|
console.log('✅ Added is_approved field to devices table');
|
||||||
await queryInterface.sequelize.query(
|
console.log('✅ Approved all existing devices for backward compatibility');
|
||||||
'UPDATE devices SET is_approved = true WHERE created_at < NOW()'
|
} else {
|
||||||
);
|
console.log('⚠️ Column is_approved already exists, skipping...');
|
||||||
|
|
||||||
console.log('✅ Added is_approved field to devices table');
|
|
||||||
console.log('✅ Approved all existing devices for backward compatibility');
|
|
||||||
} else {
|
|
||||||
console.log('⚠️ Column is_approved already exists, skipping...');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log('⚠️ Migration skipped - tables may not exist yet:', error.message);
|
|
||||||
// Don't throw error, just skip this migration if tables don't exist
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user