Files
Termix/plugins/host-metrics/migrations/postgres/0001_adopt_host_metrics_tables.sql
T

56 lines
2.6 KiB
SQL

-- host-metrics 0001: adopt_host_metrics_tables
-- Generated by termix-plugin migrations. Review before committing.
CREATE TABLE IF NOT EXISTS "host_metrics_preferences" (
"id" serial PRIMARY KEY,
"user_id" varchar(255) NOT NULL,
"host_id" integer NOT NULL,
"layout" text NOT NULL,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE,
FOREIGN KEY ("host_id") REFERENCES "ssh_data" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "idx_host_metrics_prefs_user_host" ON "host_metrics_preferences" ("user_id", "host_id");
ALTER TABLE "host_metrics_preferences" RENAME TO "p_host_metrics_host_metrics_preferences";
CREATE TABLE IF NOT EXISTS "host_health_checks" (
"id" serial PRIMARY KEY,
"user_id" varchar(255) NOT NULL,
"host_id" integer NOT NULL,
"checks" text NOT NULL,
"interval_seconds" integer NOT NULL DEFAULT 300,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE,
FOREIGN KEY ("host_id") REFERENCES "ssh_data" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "idx_host_health_checks_user_host" ON "host_health_checks" ("user_id", "host_id");
ALTER TABLE "host_health_checks" RENAME TO "p_host_metrics_host_health_checks";
CREATE TABLE IF NOT EXISTS "host_health_history" (
"id" serial PRIMARY KEY,
"user_id" varchar(255) NOT NULL,
"host_id" integer NOT NULL,
"check_id" varchar(255) NOT NULL,
"ts" varchar(255) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"ok" boolean NOT NULL,
"latency_ms" integer,
"detail" text,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE,
FOREIGN KEY ("host_id") REFERENCES "ssh_data" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_host_health_history_lookup" ON "host_health_history" ("user_id", "host_id", "check_id", "ts");
ALTER TABLE "host_health_history" RENAME TO "p_host_metrics_host_health_history";
CREATE TABLE IF NOT EXISTS "host_metrics_history" (
"id" serial PRIMARY KEY,
"host_id" integer NOT NULL,
"ts" varchar(255) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"cpu_percent" double precision,
"mem_percent" double precision,
"disk_percent" double precision,
"net_rx_bytes" integer,
"net_tx_bytes" integer,
FOREIGN KEY ("host_id") REFERENCES "ssh_data" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_host_metrics_history_host_ts" ON "host_metrics_history" ("host_id", "ts");
ALTER TABLE "host_metrics_history" RENAME TO "p_host_metrics_host_metrics_history";