feat: unified portal server, single database migration & railway configuration
This commit is contained in:
@@ -14,51 +14,72 @@ app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// Database connection
|
||||
const db = mysql.createConnection({
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'webgis_donations',
|
||||
port: process.env.DB_PORT || 3306
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) {
|
||||
console.error('Database connection failed:', err);
|
||||
return;
|
||||
}
|
||||
console.log('Connected to MySQL database.');
|
||||
|
||||
db.query("SHOW COLUMNS FROM distribution_history LIKE 'verification_status'", (err, results) => {
|
||||
if (err || results.length > 0) return;
|
||||
console.log('Applying distribution_history schema migration...');
|
||||
const alterSql = `
|
||||
ALTER TABLE distribution_history
|
||||
ADD COLUMN verification_status enum('pending','verified','rejected') NOT NULL DEFAULT 'pending',
|
||||
ADD COLUMN verified_by int DEFAULT NULL,
|
||||
ADD COLUMN verified_at datetime DEFAULT NULL,
|
||||
ADD KEY verified_by (verified_by),
|
||||
ADD CONSTRAINT distribution_history_ibfk_4 FOREIGN KEY (verified_by) REFERENCES users (id) ON DELETE SET NULL
|
||||
`;
|
||||
db.query(alterSql, (alterErr) => {
|
||||
if (alterErr) console.error('Failed to migrate distribution_history schema:', alterErr);
|
||||
else console.log('distribution_history schema migrated successfully.');
|
||||
let db;
|
||||
let migrationsRun = false;
|
||||
function handleDisconnect() {
|
||||
const connectionString = process.env.DATABASE_URL || process.env.MYSQL_URL || process.env.MYSQL_PRIVATE_URL;
|
||||
db = connectionString
|
||||
? mysql.createConnection(connectionString)
|
||||
: mysql.createConnection({
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'webgis_donations',
|
||||
port: process.env.DB_PORT || 3306
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) {
|
||||
console.error('Database connection failed:', err);
|
||||
setTimeout(handleDisconnect, 2000);
|
||||
return;
|
||||
}
|
||||
console.log('Connected to MySQL database.');
|
||||
|
||||
if (!migrationsRun) {
|
||||
migrationsRun = true;
|
||||
db.query("SHOW COLUMNS FROM distribution_history LIKE 'verification_status'", (err, results) => {
|
||||
if (err || results.length > 0) return;
|
||||
console.log('Applying distribution_history schema migration...');
|
||||
const alterSql = `
|
||||
ALTER TABLE distribution_history
|
||||
ADD COLUMN verification_status enum('pending','verified','rejected') NOT NULL DEFAULT 'pending',
|
||||
ADD COLUMN verified_by int DEFAULT NULL,
|
||||
ADD COLUMN verified_at datetime DEFAULT NULL,
|
||||
ADD KEY verified_by (verified_by),
|
||||
ADD CONSTRAINT distribution_history_ibfk_4 FOREIGN KEY (verified_by) REFERENCES users (id) ON DELETE SET NULL
|
||||
`;
|
||||
db.query(alterSql, (alterErr) => {
|
||||
if (alterErr) console.error('Failed to migrate distribution_history schema:', alterErr);
|
||||
else console.log('distribution_history schema migrated successfully.');
|
||||
});
|
||||
});
|
||||
// Ensure religious_places has place_type column
|
||||
db.query("SHOW COLUMNS FROM religious_places LIKE 'place_type'", (err2, res2) => {
|
||||
if (err2 || res2.length > 0) return;
|
||||
console.log('Applying religious_places place_type migration...');
|
||||
const alterPlaces = `
|
||||
ALTER TABLE religious_places
|
||||
ADD COLUMN place_type enum('mosque','church_catholic','church_protestant','pura','vihara','kelenteng') NOT NULL DEFAULT 'mosque'
|
||||
`;
|
||||
db.query(alterPlaces, (aErr) => {
|
||||
if (aErr) console.error('Failed to migrate religious_places schema:', aErr);
|
||||
else console.log('religious_places schema migrated successfully.');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
// Ensure religious_places has place_type column
|
||||
db.query("SHOW COLUMNS FROM religious_places LIKE 'place_type'", (err2, res2) => {
|
||||
if (err2 || res2.length > 0) return;
|
||||
console.log('Applying religious_places place_type migration...');
|
||||
const alterPlaces = `
|
||||
ALTER TABLE religious_places
|
||||
ADD COLUMN place_type enum('mosque','church_catholic','church_protestant','pura','vihara','kelenteng') NOT NULL DEFAULT 'mosque'
|
||||
`;
|
||||
db.query(alterPlaces, (aErr) => {
|
||||
if (aErr) console.error('Failed to migrate religious_places schema:', aErr);
|
||||
else console.log('religious_places schema migrated successfully.');
|
||||
});
|
||||
|
||||
db.on('error', err => {
|
||||
console.error('Database error:', err);
|
||||
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
||||
handleDisconnect();
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
handleDisconnect();
|
||||
|
||||
// --- MIDDLEWARES ---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user