emissary/migrations/sqlite/0000003_tenant.up.sql

64 lines
1.5 KiB
SQL

CREATE TABLE tenants (
id TEXT PRIMARY KEY,
label TEXT NOT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
);
-- Insert default tenant
INSERT INTO tenants
( id, label, created_at, updated_at )
VALUES (
'00000000-0000-0000-0000-000000000000',
'Default',
date('now'),
date('now')
)
;
-- Add foreign key to agents
ALTER TABLE agents RENAME TO _agents;
CREATE TABLE agents
(
id INTEGER PRIMARY KEY,
thumbprint TEXT UNIQUE,
keyset TEXT,
metadata TEXT,
status INTEGER NOT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
label TEXT DEFAULT "",
contacted_at datetime,
tenant_id TEXT,
FOREIGN KEY (tenant_id) REFERENCES tenants (id)
);
INSERT INTO agents SELECT id, thumbprint, keyset, metadata, status, created_at, updated_at, label, contacted_at, 0 FROM _agents;
DROP TABLE _agents;
-- Add foreign key to specs
ALTER TABLE specs RENAME TO _specs;
CREATE TABLE specs
(
id INTEGER PRIMARY KEY,
agent_id INTEGER,
name TEXT NOT NULL,
revision INTEGER DEFAULT 0,
data TEXT,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
tenant_id TEXT,
FOREIGN KEY (tenant_id) REFERENCES tenants (id),
FOREIGN KEY (agent_id) REFERENCES agents (id) ON DELETE CASCADE,
UNIQUE(agent_id, name) ON CONFLICT REPLACE
);
INSERT INTO specs SELECT id, agent_id, name, revision, data, created_at, updated_at, 0 FROM _specs;
DROP TABLE _specs;