/** * @type {import('node-pg-migrate').ColumnDefinitions | undefined} */ exports.shorthands = undefined; /** * @param pgm {import('node-pg-migrate').MigrationBuilder} * @param run {() => void | undefined} * @returns {Promise | void} */ exports.up = (pgm) => { // Create raw_events table for storing uploaded event data pgm.createTable('raw_events', { id: { type: 'uuid', primaryKey: true, default: pgm.func('gen_random_uuid()'), }, device_id: { type: 'uuid', notNull: true, references: 'devices(id)', onDelete: 'CASCADE', comment: 'The device that uploaded this event', }, user_profile_id: { type: 'uuid', notNull: true, references: 'user_profiles(id)', onDelete: 'CASCADE', comment: 'Owner of the device', }, file_path: { type: 'text', notNull: true, comment: 'S3 file path/key for the uploaded file', }, file_size: { type: 'bigint', comment: 'File size in bytes', }, file_type: { type: 'varchar(100)', comment: 'MIME type of the uploaded file', }, original_filename: { type: 'varchar(255)', comment: 'Original filename from the client', }, event_type: { type: 'varchar(50)', notNull: true, comment: 'Type of event (motion, alert, etc.)', }, event_timestamp: { type: 'timestamp with time zone', notNull: true, comment: 'When the event occurred on the device', }, metadata: { type: 'jsonb', comment: 'Additional event metadata from the device', }, processing_status: { type: 'varchar(20)', notNull: true, default: 'pending', comment: 'Processing status: pending, processing, completed, failed', }, sqs_message_id: { type: 'varchar(255)', comment: 'SQS message ID for tracking', }, processed_at: { type: 'timestamp with time zone', comment: 'When processing was completed', }, created_at: { type: 'timestamp with time zone', notNull: true, default: pgm.func('now()'), }, updated_at: { type: 'timestamp with time zone', notNull: true, default: pgm.func('now()'), }, }); // Create indexes for better performance pgm.createIndex('raw_events', 'device_id'); pgm.createIndex('raw_events', 'user_profile_id'); pgm.createIndex('raw_events', 'event_type'); pgm.createIndex('raw_events', 'event_timestamp'); pgm.createIndex('raw_events', 'processing_status'); pgm.createIndex('raw_events', ['device_id', 'event_timestamp']); pgm.createIndex('raw_events', ['user_profile_id', 'created_at']); }; /** * @param pgm {import('node-pg-migrate').MigrationBuilder} * @param run {() => void | undefined} * @returns {Promise | void} */ exports.down = (pgm) => { pgm.dropTable('raw_events'); };