mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
feat: add created_at/updated_at columns to cluster and machines store tables
This commit is contained in:
@@ -1,31 +1,36 @@
|
|||||||
-- cluster table stores the key-value pairs of the cluster configuration.
|
-- cluster table stores the key-value pairs of the cluster configuration.
|
||||||
CREATE TABLE cluster
|
CREATE TABLE cluster
|
||||||
(
|
(
|
||||||
key TEXT NOT NULL PRIMARY KEY,
|
key TEXT NOT NULL PRIMARY KEY,
|
||||||
value ANY
|
value ANY,
|
||||||
|
-- updated_at is the last time the value was written.
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- machines table stores the basic information of the machines in the cluster.
|
-- machines table stores the basic information of the machines in the cluster.
|
||||||
CREATE TABLE machines
|
CREATE TABLE machines
|
||||||
(
|
(
|
||||||
id TEXT NOT NULL PRIMARY KEY,
|
id TEXT NOT NULL PRIMARY KEY,
|
||||||
name TEXT AS (json_extract(info, '$.name')),
|
name TEXT AS (json_extract(info, '$.name')),
|
||||||
-- info is a JSON-serialized MachineInfo protobuf message.
|
-- info is a JSON-serialized MachineInfo protobuf message.
|
||||||
info TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(info))
|
info TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(info)),
|
||||||
-- TODO: add created_at and updated_at fields to track machine age and last update time.
|
-- created_at is the time the machine record was created.
|
||||||
|
created_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00',
|
||||||
|
-- updated_at is the last time the machine record was updated.
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- containers table stores the Uncloud-managed Docker containers created in the cluster.
|
-- containers table stores the Uncloud-managed Docker containers created in the cluster.
|
||||||
CREATE TABLE containers
|
CREATE TABLE containers
|
||||||
(
|
(
|
||||||
id TEXT NOT NULL PRIMARY KEY,
|
id TEXT NOT NULL PRIMARY KEY,
|
||||||
-- container is a JSON-serialized api.ServiceContainer struct.
|
-- container is a JSON-serialized api.ServiceContainer struct.
|
||||||
container TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(container)),
|
container TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(container)),
|
||||||
machine_id TEXT NOT NULL DEFAULT '',
|
machine_id TEXT NOT NULL DEFAULT '',
|
||||||
service_id TEXT AS (json_extract(container, '$.Config.Labels."uncloud.service.id"')),
|
service_id TEXT AS (json_extract(container, '$.Config.Labels."uncloud.service.id"')),
|
||||||
service_name TEXT AS (json_extract(container, '$.Config.Labels."uncloud.service.name"')),
|
service_name TEXT AS (json_extract(container, '$.Config.Labels."uncloud.service.name"')),
|
||||||
-- sync_status indicates if the record reflects the actual Docker state of the container.
|
-- sync_status indicates if the record reflects the actual Docker state of the container.
|
||||||
sync_status TEXT NOT NULL DEFAULT '',
|
sync_status TEXT NOT NULL DEFAULT '',
|
||||||
-- updated_at is the last time the record was updated.
|
-- updated_at is the last time the record was updated.
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00'
|
updated_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00'
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -49,7 +49,9 @@ func (s *Store) Get(ctx context.Context, key string, value any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Put(ctx context.Context, key string, value any) error {
|
func (s *Store) Put(ctx context.Context, key string, value any) error {
|
||||||
_, err := s.corro.ExecContext(ctx, "INSERT OR REPLACE INTO cluster (key, value) VALUES (?, ?)", key, value)
|
_, err := s.corro.ExecContext(ctx,
|
||||||
|
"INSERT OR REPLACE INTO cluster (key, value, updated_at) VALUES (?, ?, datetime('now'))",
|
||||||
|
key, value)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +121,9 @@ func (s *Store) CreateMachine(ctx context.Context, m *pb.MachineInfo) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal machine info: %w", err)
|
return fmt.Errorf("marshal machine info: %w", err)
|
||||||
}
|
}
|
||||||
_, err = s.corro.ExecContext(ctx, "INSERT INTO machines (id, info) VALUES (?, ?)", m.Id, string(mJSON))
|
_, err = s.corro.ExecContext(ctx,
|
||||||
|
"INSERT INTO machines (id, info, created_at, updated_at) VALUES (?, ?, datetime('now'), datetime('now'))",
|
||||||
|
m.Id, string(mJSON))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("insert query: %w", err)
|
return fmt.Errorf("insert query: %w", err)
|
||||||
}
|
}
|
||||||
@@ -231,7 +235,9 @@ func (s *Store) UpdateMachine(ctx context.Context, m *pb.MachineInfo) error {
|
|||||||
return fmt.Errorf("marshal machine info: %w", err)
|
return fmt.Errorf("marshal machine info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := s.corro.ExecContext(ctx, "UPDATE machines SET info = ? WHERE id = ?", string(mJSON), m.Id)
|
result, err := s.corro.ExecContext(ctx,
|
||||||
|
"UPDATE machines SET info = ?, updated_at = datetime('now') WHERE id = ?",
|
||||||
|
string(mJSON), m.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("update machine: %w", err)
|
return fmt.Errorf("update machine: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user