/** * Migration: Add location coordinates to weather_stations table * * Fixes: WeatherStation has no location/coordinates - how do you know where the station is? * * @type {import('node-pg-migrate').ColumnDefinitions | undefined} */ export const shorthands = undefined; /** * @param pgm {import('node-pg-migrate').MigrationBuilder} * @param run {() => void | undefined} * @returns {Promise | void} */ export const up = (pgm) => { console.log('Adding location coordinates to weather_stations...'); // Add location coordinate columns pgm.addColumns('weather_stations', { latitude: { type: 'decimal(10,7)', notNull: false, comment: 'Latitude coordinate of the weather station (-90 to 90)', }, longitude: { type: 'decimal(10,7)', notNull: false, comment: 'Longitude coordinate of the weather station (-180 to 180)', }, elevation: { type: 'decimal(6,1)', notNull: false, comment: 'Elevation above sea level in meters', }, timezone: { type: 'varchar(50)', notNull: false, comment: 'Timezone identifier (e.g., Asia/Shanghai)', }, }); // Add CHECK constraints for coordinate validation pgm.addConstraint('weather_stations', 'chk_weather_stations_latitude', { check: 'latitude IS NULL OR (latitude >= -90 AND latitude <= 90)', }); pgm.addConstraint('weather_stations', 'chk_weather_stations_longitude', { check: 'longitude IS NULL OR (longitude >= -180 AND longitude <= 180)', }); // Add index for geospatial queries (basic, for more advanced use PostGIS) pgm.createIndex('weather_stations', ['latitude', 'longitude'], { name: 'idx_weather_stations_coordinates', where: 'latitude IS NOT NULL AND longitude IS NOT NULL', }); console.log('Weather station location columns added successfully.'); }; /** * @param pgm {import('node-pg-migrate').MigrationBuilder} * @param run {() => void | undefined} * @returns {Promise | void} */ export const down = (pgm) => { console.log('Removing location coordinates from weather_stations...'); // Drop index first pgm.dropIndex('weather_stations', ['latitude', 'longitude'], { name: 'idx_weather_stations_coordinates', ifExists: true, }); // Drop constraints pgm.dropConstraint('weather_stations', 'chk_weather_stations_longitude', { ifExists: true, }); pgm.dropConstraint('weather_stations', 'chk_weather_stations_latitude', { ifExists: true, }); // Drop columns pgm.dropColumns('weather_stations', ['latitude', 'longitude', 'elevation', 'timezone']); console.log('Weather station location columns removed.'); };