-- DirectDNSOnly — CoreDNS MySQL schema
-- Compatible with cybercinch/coredns_mysql_extend
--
-- managed_by values:
--   'directadmin'  zone is managed via directdnsonly / DirectAdmin push
--   'direct'       zone was created directly (not via DA)
--   NULL           legacy row created before this column was added

CREATE TABLE IF NOT EXISTS `zones` (
  `id`          int(11)      NOT NULL AUTO_INCREMENT,
  `zone_name`   varchar(255) NOT NULL,
  `managed_by`  varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_zone_name` (`zone_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `records` (
  `id`        int(11)      NOT NULL AUTO_INCREMENT,
  `zone_id`   int(11)      NOT NULL,
  `hostname`  varchar(255) NOT NULL,
  `type`      varchar(10)  NOT NULL,
  `data`      text         NOT NULL,
  `ttl`       int(11)      DEFAULT NULL,
  `online`    tinyint(1)   NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_zone_id`  (`zone_id`),
  KEY `idx_hostname` (`hostname`),
  CONSTRAINT `fk_records_zone` FOREIGN KEY (`zone_id`) REFERENCES `zones` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Migration: add managed_by to an existing installation
-- ALTER TABLE `zones` ADD COLUMN `managed_by` varchar(255) DEFAULT NULL;
-- UPDATE `zones` SET `managed_by` = 'directadmin' WHERE `managed_by` IS NULL;
