-- Datto RMM sync tables: sites, devices, alerts -- Sites map to Autotask companies via autotask_company_id CREATE TABLE IF NOT EXISTS datto_rmm_sites ( id INTEGER PRIMARY KEY, uid TEXT UNIQUE NOT NULL, account_uid TEXT, name TEXT NOT NULL, description TEXT, notes TEXT, on_demand BOOLEAN DEFAULT false, autotask_company_id INTEGER REFERENCES companies(id), autotask_company_name TEXT, number_of_devices INTEGER DEFAULT 0, number_of_online_devices INTEGER DEFAULT 0, number_of_offline_devices INTEGER DEFAULT 0, portal_url TEXT, synced_at TIMESTAMPTZ DEFAULT NOW(), created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS datto_rmm_devices ( id INTEGER PRIMARY KEY, uid TEXT UNIQUE NOT NULL, site_id INTEGER REFERENCES datto_rmm_sites(id), site_uid TEXT, site_name TEXT, hostname TEXT, description TEXT, device_type_category TEXT, device_type TEXT, operating_system TEXT, domain TEXT, int_ip_address TEXT, ext_ip_address TEXT, last_logged_in_user TEXT, last_seen TIMESTAMPTZ, last_reboot TIMESTAMPTZ, last_audit_date TIMESTAMPTZ, creation_date TIMESTAMPTZ, online BOOLEAN DEFAULT false, suspended BOOLEAN DEFAULT false, deleted BOOLEAN DEFAULT false, reboot_required BOOLEAN DEFAULT false, a64_bit BOOLEAN DEFAULT true, cag_version TEXT, display_version TEXT, antivirus_product TEXT, antivirus_status TEXT, patch_status TEXT, patches_approved_pending INTEGER DEFAULT 0, patches_not_approved INTEGER DEFAULT 0, patches_installed INTEGER DEFAULT 0, software_status TEXT, portal_url TEXT, web_remote_url TEXT, warranty_date TIMESTAMPTZ, snmp_enabled BOOLEAN DEFAULT false, device_class TEXT, network_probe BOOLEAN DEFAULT false, udf JSONB, synced_at TIMESTAMPTZ DEFAULT NOW(), created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS datto_rmm_alerts ( alert_uid TEXT PRIMARY KEY, device_uid TEXT, device_name TEXT, site_uid TEXT, site_name TEXT, priority TEXT, alert_context JSONB, alert_monitor_info JSONB, diagnostics TEXT, resolved BOOLEAN DEFAULT false, resolved_by TEXT, resolved_on TIMESTAMPTZ, muted BOOLEAN DEFAULT false, ticket_number TEXT, autoresolve_mins INTEGER, response_actions JSONB, timestamp TIMESTAMPTZ NOT NULL, synced_at TIMESTAMPTZ DEFAULT NOW(), created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); -- Indexes CREATE INDEX IF NOT EXISTS idx_datto_rmm_sites_uid ON datto_rmm_sites(uid); CREATE INDEX IF NOT EXISTS idx_datto_rmm_sites_company ON datto_rmm_sites(autotask_company_id); CREATE INDEX IF NOT EXISTS idx_datto_rmm_devices_uid ON datto_rmm_devices(uid); CREATE INDEX IF NOT EXISTS idx_datto_rmm_devices_site ON datto_rmm_devices(site_id); CREATE INDEX IF NOT EXISTS idx_datto_rmm_devices_hostname ON datto_rmm_devices(hostname); CREATE INDEX IF NOT EXISTS idx_datto_rmm_devices_online ON datto_rmm_devices(online); CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_device ON datto_rmm_alerts(device_uid); CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_site ON datto_rmm_alerts(site_uid); CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_resolved ON datto_rmm_alerts(resolved); CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_timestamp ON datto_rmm_alerts(timestamp); CREATE INDEX IF NOT EXISTS idx_datto_rmm_alerts_ticket ON datto_rmm_alerts(ticket_number);