-- 0007_iscsi.sql
CREATE TABLE IF NOT EXISTS iscsi_targets (
  id TEXT PRIMARY KEY,
  iqn TEXT NOT NULL UNIQUE,
  name TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS iscsi_portals (
  id TEXT PRIMARY KEY,
  target_id TEXT NOT NULL,
  address TEXT NOT NULL,
  port INTEGER DEFAULT 3260,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY(target_id) REFERENCES iscsi_targets(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS iscsi_initiators (
  id TEXT PRIMARY KEY,
  target_id TEXT NOT NULL,
  initiator_iqn TEXT NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY(target_id) REFERENCES iscsi_targets(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS iscsi_luns (
  id TEXT PRIMARY KEY,
  target_id TEXT NOT NULL,
  lun_id INTEGER NOT NULL,
  zvol TEXT NOT NULL,
  size INTEGER,
  blocksize INTEGER,
  mapped INTEGER DEFAULT 0,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY(target_id) REFERENCES iscsi_targets(id) ON DELETE CASCADE
);
