c/bmaas/bmdb: add Agent{Started,Heartbeat} tags and queries

This should be the required tags and queries for the first interactions
with the Shepherd subsystem.

Change-Id: I8c663803cfd936b11c59bce7db5abc94b99dd1db
Reviewed-on: https://review.monogon.dev/c/monogon/+/962
Tested-by: Jenkins CI
Reviewed-by: Mateusz Zalega <mateusz@monogon.tech>
diff --git a/cloud/bmaas/bmdb/model/migrations/1662136250_initial.down.sql b/cloud/bmaas/bmdb/model/migrations/1662136250_initial.down.sql
index 6a85991..5f336d1 100644
--- a/cloud/bmaas/bmdb/model/migrations/1662136250_initial.down.sql
+++ b/cloud/bmaas/bmdb/model/migrations/1662136250_initial.down.sql
@@ -1,6 +1,3 @@
-DROP TABLE machine_agent_report;
-DROP TABLE machine_agent_installed;
-DROP TABLE machine_provided;
 DROP TABLE work;
 DROP TABLE sessions;
 DROP TABLE machines;
diff --git a/cloud/bmaas/bmdb/model/migrations/1662136250_initial.up.sql b/cloud/bmaas/bmdb/model/migrations/1662136250_initial.up.sql
index b31324f..7b5b812 100644
--- a/cloud/bmaas/bmdb/model/migrations/1662136250_initial.up.sql
+++ b/cloud/bmaas/bmdb/model/migrations/1662136250_initial.up.sql
@@ -49,25 +49,4 @@
     process process NOT NULL,
     UNIQUE (machine_id, process),
     CONSTRAINT "primary" PRIMARY KEY (machine_id, session_id, process)
-);
-
--- The following three tables are for illustrative purposes only.
-
-CREATE TABLE machine_provided (
-    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE CASCADE,
-    provider STRING NOT NULL,
-    provider_id STRING NOT NULL,
-    CONSTRAINT "primary" PRIMARY KEY (machine_id)
-);
-
-CREATE TABLE machine_agent_installed (
-    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE CASCADE,
-    CONSTRAINT "primary" PRIMARY KEY (machine_id)
-);
-
-CREATE TABLE machine_agent_report (
-    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE CASCADE,
-    shape_cpu_count INT NOT NULL,
-    shape_memory_megabytes INT NOT NULL,
-    CONSTRAINT "primary" PRIMARY KEY (machine_id)
-);
+);
\ No newline at end of file
diff --git a/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.down.sql b/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.down.sql
new file mode 100644
index 0000000..90bb586
--- /dev/null
+++ b/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.down.sql
@@ -0,0 +1,3 @@
+DROP TABLE machine_provided;
+DROP TABLE machine_agent_started;
+DROP TABLE machine_agent_heartbeat;
diff --git a/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.up.sql b/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.up.sql
new file mode 100644
index 0000000..14701a6
--- /dev/null
+++ b/cloud/bmaas/bmdb/model/migrations/1667232160_agent_tags.up.sql
@@ -0,0 +1,46 @@
+CREATE TYPE provider AS ENUM (
+    'Equinix'
+    -- More providers will follow in subsequent migrations.
+);
+
+-- tag MachineProvided {
+--     Provider Provider
+--     ProviderID String
+-- }
+-- Represents the fact that a machine is backed by a machine from a given
+-- provider identified there with a given provider id.
+CREATE TABLE machine_provided (
+    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE RESTRICT,
+    provider provider NOT NULL,
+    provider_id STRING(128) NOT NULL,
+    CONSTRAINT "primary" PRIMARY KEY (machine_id),
+    UNIQUE (provider, provider_id)
+);
+
+-- tag AgentStarted {
+--     StartedAt time.Time
+--     PublicKey []byte
+-- }
+-- Represents the fact that a machine has had the Agent started on it at some
+-- given time, and that the agent returned a given public key which it will use
+-- to authenticate itself to the bmdb API server.
+CREATE TABLE machine_agent_started (
+    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE RESTRICT,
+    agent_started_at TIMESTAMPTZ NOT NULL,
+    agent_public_key BYTES NOT NULL,
+    CONSTRAINT "primary" PRIMARY KEY(machine_id)
+);
+
+-- tag AgentHeartbeat {
+--     At time.Time
+-- }
+-- Represents a successful heartbeat send by the Agent running on a machine at
+-- some given time.
+CREATE TABLE machine_agent_heartbeat (
+    machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE RESTRICT,
+    agent_heartbeat_at TIMESTAMPTZ NOT NULL,
+    CONSTRAINT "primary" PRIMARY KEY(machine_id)
+);
+
+-- Used by the Shepherd when performing direct actions against a machine.
+ALTER TYPE process ADD VALUE IF NOT EXISTS 'ShepherdInstall';
\ No newline at end of file