/** * @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 inventory_devices table - for pre-registered legitimate devices pgm.createTable('inventory_devices', { id: { type: 'uuid', primaryKey: true, default: pgm.func('gen_random_uuid()'), }, hardware_id: { type: 'varchar(255)', notNull: true, unique: true, }, is_claimed: { type: 'boolean', notNull: true, default: false, }, device_model: { type: 'varchar(255)', comment: 'Optional device model information', }, 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 devices table - for user-claimed devices pgm.createTable('devices', { id: { type: 'uuid', primaryKey: true, default: pgm.func('gen_random_uuid()'), }, user_profile_id: { type: 'uuid', notNull: true, references: 'user_profiles(id)', onDelete: 'CASCADE', }, hardware_id: { type: 'varchar(255)', notNull: true, unique: true, }, device_name: { type: 'varchar(255)', comment: 'User-assigned friendly name for the device', }, status: { type: 'varchar(50)', notNull: true, default: 'active', comment: 'Device status: active, inactive, maintenance', }, last_seen_at: { type: 'timestamp with time zone', comment: 'Last time device was seen online', }, registered_at: { type: 'timestamp with time zone', notNull: true, default: pgm.func('now()'), }, 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('inventory_devices', 'hardware_id'); pgm.createIndex('inventory_devices', 'is_claimed'); pgm.createIndex('devices', 'user_profile_id'); pgm.createIndex('devices', 'hardware_id'); pgm.createIndex('devices', 'status'); }; /** * @param pgm {import('node-pg-migrate').MigrationBuilder} * @param run {() => void | undefined} * @returns {Promise | void} */ exports.down = (pgm) => { pgm.dropTable('devices'); pgm.dropTable('inventory_devices'); };