feat: resources segregation by tenant
This commit is contained in:
41
migrations/sqlite/0000003_tenant.down.sql
Normal file
41
migrations/sqlite/0000003_tenant.down.sql
Normal file
@ -0,0 +1,41 @@
|
||||
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
|
||||
);
|
||||
|
||||
INSERT INTO agents SELECT id, thumbprint, keyset, metadata, status, created_at, updated_at, label, contacted_at FROM _agents;
|
||||
DROP TABLE _agents;
|
||||
|
||||
---
|
||||
|
||||
ALTER TABLE specs RENAME TO _specs;
|
||||
|
||||
CREATE TABLE specs
|
||||
(
|
||||
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 ""
|
||||
);
|
||||
|
||||
INSERT INTO specs SELECT id, agent_id, name, revision, data, created_at, updated_at FROM _specs;
|
||||
DROP TABLE _specs;
|
||||
|
||||
---
|
||||
|
||||
DROP TABLE tenants;
|
||||
|
64
migrations/sqlite/0000003_tenant.up.sql
Normal file
64
migrations/sqlite/0000003_tenant.up.sql
Normal file
@ -0,0 +1,64 @@
|
||||
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;
|
Reference in New Issue
Block a user