From ae9e7560fd9faa9503448b4afef554938fadf598 Mon Sep 17 00:00:00 2001 From: Tadas Sutkaitis Date: Tue, 1 Dec 2026 10:39:29 +0200 Subject: [PATCH 1/2] feat(designate): add zone and recordset query Signed-off-by: Tadas Sutkaitis --- README.md | 1 + designate/db/db.go | 31 ++++++++++++++ designate/db/models.go | 31 ++++++++++++++ designate/db/queries.sql | 23 +++++++++++ designate/db/queries.sql.go | 81 +++++++++++++++++++++++++++++++++++++ schema/designate/schema.sql | 23 +++++++++++ sqlc.yaml | 8 ++++ tests/queries_test.go | 46 +++++++++++++++++++++ 8 files changed, 244 insertions(+) create mode 100644 designate/db/db.go create mode 100644 designate/db/models.go create mode 100644 designate/db/queries.sql create mode 100644 designate/db/queries.sql.go create mode 100644 schema/designate/schema.sql diff --git a/README.md b/README.md index 5a2eddf..baa6743 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ repository objects. Placement queries live under `placement/objects`. | `nova/db/api` | `FlavorGetAll`, `AggregateGetAll`, quota projections | | `cinder/db` | `ServiceGetAll`, `QuotaGetProjectLimits`, `QuotaUsageGetAll` | | `glance/db` | `ImageGetAll` | +| `designate/db` | `ZoneGetAllWithRecordsets` | | `keystone/db` | `ListProjects`, `ListDomains`, `ListUsers` | | `heat/db` | `StackGetAll` | | `ironic/db` | `GetNodeList` | diff --git a/designate/db/db.go b/designate/db/db.go new file mode 100644 index 0000000..ea582ad --- /dev/null +++ b/designate/db/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package designate + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/designate/db/models.go b/designate/db/models.go new file mode 100644 index 0000000..fa39085 --- /dev/null +++ b/designate/db/models.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package designate + +import ( + "database/sql" +) + +type Record struct { + ID string + RecordsetID string + Status string +} + +type Recordset struct { + ID string + ZoneID sql.NullString + Name string + Type string +} + +type Zone struct { + ID string + Name string + TenantID sql.NullString + Status string + Type string + Deleted string +} diff --git a/designate/db/queries.sql b/designate/db/queries.sql new file mode 100644 index 0000000..a77798c --- /dev/null +++ b/designate/db/queries.sql @@ -0,0 +1,23 @@ +-- name: ZoneGetAllWithRecordsets :many +-- A recordset takes the worst child record status. An empty recordset is ACTIVE. +SELECT + z.id AS zone_id, + z.name AS zone_name, + z.tenant_id, + z.status AS zone_status, + z.type AS zone_type, + rs.id AS recordset_id, + rs.name AS recordset_name, + rs.type AS recordset_type, + CASE + WHEN MAX(r.status = 'ERROR') THEN 'ERROR' + WHEN MAX(r.status = 'PENDING') THEN 'PENDING' + WHEN MAX(r.status = 'ACTIVE') THEN 'ACTIVE' + WHEN MAX(r.status = 'DELETED') THEN 'DELETED' + ELSE 'ACTIVE' + END AS recordset_status +FROM zones z +LEFT JOIN recordsets rs ON rs.zone_id = z.id +LEFT JOIN records r ON r.recordset_id = rs.id +WHERE z.deleted = '0' +GROUP BY z.id, z.name, z.tenant_id, z.status, z.type, rs.id, rs.name, rs.type; diff --git a/designate/db/queries.sql.go b/designate/db/queries.sql.go new file mode 100644 index 0000000..7d1b6cf --- /dev/null +++ b/designate/db/queries.sql.go @@ -0,0 +1,81 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 +// source: queries.sql + +package designate + +import ( + "context" + "database/sql" +) + +const ZoneGetAllWithRecordsets = `-- name: ZoneGetAllWithRecordsets :many +SELECT + z.id AS zone_id, + z.name AS zone_name, + z.tenant_id, + z.status AS zone_status, + z.type AS zone_type, + rs.id AS recordset_id, + rs.name AS recordset_name, + rs.type AS recordset_type, + CASE + WHEN MAX(r.status = 'ERROR') THEN 'ERROR' + WHEN MAX(r.status = 'PENDING') THEN 'PENDING' + WHEN MAX(r.status = 'ACTIVE') THEN 'ACTIVE' + WHEN MAX(r.status = 'DELETED') THEN 'DELETED' + ELSE 'ACTIVE' + END AS recordset_status +FROM zones z +LEFT JOIN recordsets rs ON rs.zone_id = z.id +LEFT JOIN records r ON r.recordset_id = rs.id +WHERE z.deleted = '0' +GROUP BY z.id, z.name, z.tenant_id, z.status, z.type, rs.id, rs.name, rs.type +` + +type ZoneGetAllWithRecordsetsRow struct { + ZoneID string + ZoneName string + TenantID sql.NullString + ZoneStatus string + ZoneType string + RecordsetID sql.NullString + RecordsetName sql.NullString + RecordsetType sql.NullString + RecordsetStatus string +} + +// A recordset takes the worst child record status. An empty recordset is ACTIVE. +func (q *Queries) ZoneGetAllWithRecordsets(ctx context.Context) ([]ZoneGetAllWithRecordsetsRow, error) { + rows, err := q.db.QueryContext(ctx, ZoneGetAllWithRecordsets) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ZoneGetAllWithRecordsetsRow + for rows.Next() { + var i ZoneGetAllWithRecordsetsRow + if err := rows.Scan( + &i.ZoneID, + &i.ZoneName, + &i.TenantID, + &i.ZoneStatus, + &i.ZoneType, + &i.RecordsetID, + &i.RecordsetName, + &i.RecordsetType, + &i.RecordsetStatus, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/schema/designate/schema.sql b/schema/designate/schema.sql new file mode 100644 index 0000000..0434d05 --- /dev/null +++ b/schema/designate/schema.sql @@ -0,0 +1,23 @@ +CREATE TABLE zones ( + id CHAR(32) NOT NULL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + tenant_id VARCHAR(36), + status VARCHAR(16) NOT NULL, + type VARCHAR(16) NOT NULL, + deleted CHAR(32) NOT NULL DEFAULT '0' +); + +CREATE TABLE recordsets ( + id CHAR(32) NOT NULL PRIMARY KEY, + zone_id CHAR(32), + name VARCHAR(255) NOT NULL, + type VARCHAR(16) NOT NULL, + FOREIGN KEY (zone_id) REFERENCES zones(id) ON DELETE CASCADE +); + +CREATE TABLE records ( + id CHAR(32) NOT NULL PRIMARY KEY, + recordset_id CHAR(32) NOT NULL, + status VARCHAR(16) NOT NULL, + FOREIGN KEY (recordset_id) REFERENCES recordsets(id) ON DELETE CASCADE +); diff --git a/sqlc.yaml b/sqlc.yaml index a04754c..6ca7f91 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -32,6 +32,14 @@ sql: package: glance out: glance/db emit_exported_queries: true +- engine: mysql + schema: schema/designate/schema.sql + queries: designate/db/queries.sql + gen: + go: + package: designate + out: designate/db + emit_exported_queries: true - engine: mysql schema: schema/keystone/schema.sql queries: keystone/db/queries.sql diff --git a/tests/queries_test.go b/tests/queries_test.go index e350060..8f2c760 100644 --- a/tests/queries_test.go +++ b/tests/queries_test.go @@ -6,6 +6,7 @@ import ( "context" "github.com/stretchr/testify/require" cinder "github.com/vexxhost/openstackdb/cinder/db" + designate "github.com/vexxhost/openstackdb/designate/db" glance "github.com/vexxhost/openstackdb/glance/db" heat "github.com/vexxhost/openstackdb/heat/db" "github.com/vexxhost/openstackdb/internal/testutil" @@ -99,6 +100,51 @@ func TestGlanceQueries(t *testing.T) { _, err = q.ImageGetAll(ctx) require.NoError(t, err, "ImageGetAll") } + +func TestDesignateZonesWithRecordsets(t *testing.T) { + conn := testutil.NewMySQLContainer(t, "designate", "../sql/designate/schema.sql") + queries := designate.New(conn) + + rows, err := queries.ZoneGetAllWithRecordsets(context.Background()) + require.NoError(t, err) + require.Empty(t, rows) + + testutil.SeedSQL(t, conn, + `INSERT INTO zones (id, name, tenant_id, status, type, deleted) VALUES + ('zone-1', 'example.org.', 'project-1', 'ACTIVE', 'PRIMARY', '0'), + ('empty-zone', 'empty.example.', NULL, 'PENDING', 'SECONDARY', '0'), + ('deleted-zone', 'deleted.example.', 'project-1', 'DELETED', 'PRIMARY', 'deleted-zone')`, + `INSERT INTO recordsets (id, zone_id, name, type) VALUES + ('error', 'zone-1', 'error.example.org.', 'A'), + ('pending', 'zone-1', 'pending.example.org.', 'A'), + ('active', 'zone-1', 'active.example.org.', 'A'), + ('deleted', 'zone-1', 'deleted.example.org.', 'A'), + ('empty', 'zone-1', 'empty.example.org.', 'TXT'), + ('hidden', 'deleted-zone', 'deleted.example.', 'NS')`, + `INSERT INTO records (id, recordset_id, status) VALUES + ('1', 'error', 'PENDING'), ('2', 'error', 'ERROR'), + ('3', 'pending', 'ACTIVE'), ('4', 'pending', 'PENDING'), + ('5', 'active', 'DELETED'), ('6', 'active', 'ACTIVE'), + ('7', 'deleted', 'DELETED')`, + ) + + rows, err = queries.ZoneGetAllWithRecordsets(context.Background()) + require.NoError(t, err) + require.Len(t, rows, 6) + statuses := make(map[string]string) + for _, row := range rows { + if !row.RecordsetID.Valid { + require.Equal(t, "empty-zone", row.ZoneID) + continue + } + require.Equal(t, "zone-1", row.ZoneID) + statuses[row.RecordsetID.String] = row.RecordsetStatus + } + require.Equal(t, map[string]string{ + "error": "ERROR", "pending": "PENDING", "active": "ACTIVE", "deleted": "DELETED", "empty": "ACTIVE", + }, statuses) +} + func TestKeystoneQueries(t *testing.T) { ctx := context.Background() conn := testutil.NewMySQLContainer(t, "keystone", "../sql/keystone/schema.sql") From 6baf0c33a388b55f4d21e4b44a2c3090705648f7 Mon Sep 17 00:00:00 2001 From: Tadas Sutkaitis Date: Tue, 1 Dec 2026 17:35:04 +0200 Subject: [PATCH 2/2] fix(designate): split queries and normalize UUIDs Signed-off-by: Tadas Sutkaitis --- API.md | 5 ++ README.md | 2 +- designate/db/designate.go | 51 ++++++++++++ designate/db/models.go | 2 +- designate/db/queries.sql | 53 ++++++++---- designate/db/queries.sql.go | 161 ++++++++++++++++++++++++++++-------- go.mod | 2 +- schema/designate/schema.sql | 4 +- tests/queries_test.go | 96 ++++++++++++++------- 9 files changed, 291 insertions(+), 85 deletions(-) create mode 100644 designate/db/designate.go diff --git a/API.md b/API.md index f264cfe..af8487d 100644 --- a/API.md +++ b/API.md @@ -25,6 +25,9 @@ with identical semantics upstream. | `cinder/db` | `GetSnapshotCount` | `SnapshotCount` | | `cinder/db` | `GetAllVolumes` | `VolumeGetAllWithAttachments` | | `glance/db` | `GetAllImages` | `ImageGetAll` | +| `designate/db` | `ListZonesAndRecordsets` (zone details) | `ZoneGetAll` | +| `designate/db` | `ListZonesAndRecordsets` (per-zone counts) | `RecordsetCountByZone` | +| `designate/db` | `ListZonesAndRecordsets` (recordset details) | `RecordsetGetAllWithStatus` | | `heat/db` | `GetStackMetrics` | `StackGetAll` | | `ironic/db` | `GetNodeMetrics` | `GetNodeList` | | `keystone/db` | `GetProjectMetrics` | `ListProjects` | @@ -62,6 +65,7 @@ with identical semantics upstream. - [Nova API database](https://github.com/openstack/nova/tree/master/nova/db/api) and [objects](https://github.com/openstack/nova/tree/master/nova/objects): flavor, aggregate, and quota data. - [Cinder DB API](https://github.com/openstack/cinder/blob/master/cinder/db/api.py): `volume_get`, `volume_get_all`, `volume_type_get_all`, `service_get_all`. - [Glance DB API](https://github.com/openstack/glance/blob/master/glance/db/sqlalchemy/api.py): `image_get`, `image_get_all`. +- [Designate storage](https://github.com/openstack/designate/blob/master/designate/storage/sqlalchemy/__init__.py): `find_zones`, `find_recordsets`, `count_recordsets`. The library also follows [Designate's UUID conversion](https://github.com/openstack/designate/blob/master/designate/storage/sqlalchemy/types.py) and [recordset status precedence](https://github.com/openstack/designate/blob/master/designate/objects/recordset.py). - [Ironic DB API](https://github.com/openstack/ironic/blob/master/ironic/db/api.py) and [Magnum DB API](https://github.com/openstack/magnum/blob/master/magnum/db/api.py): `get_node_list`, `get_cluster_list`. - [Heat DB API](https://github.com/openstack/heat/blob/master/heat/db/api.py): `stack_get_all`. - [Manila DB API](https://github.com/openstack/manila/blob/master/manila/db/api.py): share operations; the instance join is an explicit extension. @@ -77,6 +81,7 @@ with identical semantics upstream. - Current exporter queries retain their existing deleted/hidden/status predicates; they are not full upstream administrative listings. - Errors are Go/database errors; absent single resources return `sql.ErrNoRows` rather than Python service-specific exceptions. - Aggregate queries such as `QuotaGetAllWithUsage`, `GetResourceProviderInventories`, and `GetResourceCountsByProject` are explicitly library extensions. +- Designate returns canonical zone and recordset UUIDs and excludes soft-deleted zones. `RecordsetCountByZone` includes zero-count zones and can run without reading individual records. `RecordsetGetAllWithStatus` uses the child precedence `ERROR`, `PENDING`, `ACTIVE`, `DELETED` and returns `ACTIVE` for an empty recordset. Recordsets with a NULL `zone_id` have no visible zone and are omitted. - No cross-cell routing or historic schema negotiation is performed. Nova archive access is explicitly selected with `InstanceFilters.Archive`. ## Resource lifecycle and metadata reads diff --git a/README.md b/README.md index baa6743..46520c4 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ repository objects. Placement queries live under `placement/objects`. | `nova/db/api` | `FlavorGetAll`, `AggregateGetAll`, quota projections | | `cinder/db` | `ServiceGetAll`, `QuotaGetProjectLimits`, `QuotaUsageGetAll` | | `glance/db` | `ImageGetAll` | -| `designate/db` | `ZoneGetAllWithRecordsets` | +| `designate/db` | `ZoneGetAll`, `RecordsetCountByZone`, `RecordsetGetAllWithStatus` | | `keystone/db` | `ListProjects`, `ListDomains`, `ListUsers` | | `heat/db` | `StackGetAll` | | `ironic/db` | `GetNodeList` | diff --git a/designate/db/designate.go b/designate/db/designate.go new file mode 100644 index 0000000..2e9a2c8 --- /dev/null +++ b/designate/db/designate.go @@ -0,0 +1,51 @@ +package designate + +import ( + "context" + + "github.com/google/uuid" +) + +type ( + ZoneGetAllRow = zoneGetAllRow + RecordsetCountByZoneRow = recordsetCountByZoneRow + RecordsetGetAllWithStatusRow = recordsetGetAllWithStatusRow +) + +// ZoneGetAll returns non-deleted zones with API-format UUIDs. +func (q *Queries) ZoneGetAll(ctx context.Context) ([]ZoneGetAllRow, error) { + rows, err := q.zoneGetAll(ctx) + for i := range rows { + rows[i].ID = formatUUID(rows[i].ID) + } + return rows, err +} + +// RecordsetCountByZone returns counts for non-deleted zones, including empty zones. +func (q *Queries) RecordsetCountByZone(ctx context.Context) ([]RecordsetCountByZoneRow, error) { + rows, err := q.recordsetCountByZone(ctx) + for i := range rows { + rows[i].ZoneID = formatUUID(rows[i].ZoneID) + } + return rows, err +} + +// RecordsetGetAllWithStatus returns recordsets in non-deleted zones. +// Status follows Designate's child record precedence and defaults to ACTIVE. +func (q *Queries) RecordsetGetAllWithStatus(ctx context.Context) ([]RecordsetGetAllWithStatusRow, error) { + rows, err := q.recordsetGetAllWithStatus(ctx) + for i := range rows { + rows[i].ID = formatUUID(rows[i].ID) + rows[i].ZoneID = formatUUID(rows[i].ZoneID) + } + return rows, err +} + +// Designate stores compact UUIDs and returns canonical UUIDs through its API. +func formatUUID(value string) string { + id, err := uuid.Parse(value) + if err != nil { + return value + } + return id.String() +} diff --git a/designate/db/models.go b/designate/db/models.go index fa39085..94c4ac7 100644 --- a/designate/db/models.go +++ b/designate/db/models.go @@ -16,7 +16,7 @@ type Record struct { type Recordset struct { ID string - ZoneID sql.NullString + ZoneID string Name string Type string } diff --git a/designate/db/queries.sql b/designate/db/queries.sql index a77798c..03f7c33 100644 --- a/designate/db/queries.sql +++ b/designate/db/queries.sql @@ -1,23 +1,44 @@ --- name: ZoneGetAllWithRecordsets :many --- A recordset takes the worst child record status. An empty recordset is ACTIVE. +-- name: zoneGetAll :many +SELECT id, name, tenant_id, status, type +FROM zones +WHERE deleted = '0'; + +-- name: recordsetCountByZone :many SELECT z.id AS zone_id, z.name AS zone_name, z.tenant_id, - z.status AS zone_status, - z.type AS zone_type, - rs.id AS recordset_id, - rs.name AS recordset_name, - rs.type AS recordset_type, - CASE - WHEN MAX(r.status = 'ERROR') THEN 'ERROR' - WHEN MAX(r.status = 'PENDING') THEN 'PENDING' - WHEN MAX(r.status = 'ACTIVE') THEN 'ACTIVE' - WHEN MAX(r.status = 'DELETED') THEN 'DELETED' - ELSE 'ACTIVE' - END AS recordset_status + COUNT(rs.id) AS recordset_count FROM zones z LEFT JOIN recordsets rs ON rs.zone_id = z.id -LEFT JOIN records r ON r.recordset_id = rs.id WHERE z.deleted = '0' -GROUP BY z.id, z.name, z.tenant_id, z.status, z.type, rs.id, rs.name, rs.type; +GROUP BY z.id, z.name, z.tenant_id; + +-- name: recordsetGetAllWithStatus :many +SELECT + rs.id, + rs.name, + rs.type, + z.id AS zone_id, + z.name AS zone_name, + CASE rs_status.severity + WHEN 1 THEN 'ERROR' + WHEN 2 THEN 'PENDING' + WHEN 3 THEN 'ACTIVE' + WHEN 4 THEN 'DELETED' + ELSE 'ACTIVE' + END AS status +FROM recordsets rs +JOIN zones z ON z.id = rs.zone_id +LEFT JOIN ( + SELECT recordset_id, + MIN(CASE status + WHEN 'ERROR' THEN 1 + WHEN 'PENDING' THEN 2 + WHEN 'ACTIVE' THEN 3 + WHEN 'DELETED' THEN 4 + END) AS severity + FROM records + GROUP BY recordset_id +) rs_status ON rs_status.recordset_id = rs.id +WHERE z.deleted = '0'; diff --git a/designate/db/queries.sql.go b/designate/db/queries.sql.go index 7d1b6cf..963c2f9 100644 --- a/designate/db/queries.sql.go +++ b/designate/db/queries.sql.go @@ -10,62 +10,151 @@ import ( "database/sql" ) -const ZoneGetAllWithRecordsets = `-- name: ZoneGetAllWithRecordsets :many +const RecordsetCountByZone = `-- name: recordsetCountByZone :many SELECT z.id AS zone_id, z.name AS zone_name, z.tenant_id, - z.status AS zone_status, - z.type AS zone_type, - rs.id AS recordset_id, - rs.name AS recordset_name, - rs.type AS recordset_type, - CASE - WHEN MAX(r.status = 'ERROR') THEN 'ERROR' - WHEN MAX(r.status = 'PENDING') THEN 'PENDING' - WHEN MAX(r.status = 'ACTIVE') THEN 'ACTIVE' - WHEN MAX(r.status = 'DELETED') THEN 'DELETED' - ELSE 'ACTIVE' - END AS recordset_status + COUNT(rs.id) AS recordset_count FROM zones z LEFT JOIN recordsets rs ON rs.zone_id = z.id -LEFT JOIN records r ON r.recordset_id = rs.id WHERE z.deleted = '0' -GROUP BY z.id, z.name, z.tenant_id, z.status, z.type, rs.id, rs.name, rs.type +GROUP BY z.id, z.name, z.tenant_id ` -type ZoneGetAllWithRecordsetsRow struct { - ZoneID string - ZoneName string - TenantID sql.NullString - ZoneStatus string - ZoneType string - RecordsetID sql.NullString - RecordsetName sql.NullString - RecordsetType sql.NullString - RecordsetStatus string +type recordsetCountByZoneRow struct { + ZoneID string + ZoneName string + TenantID sql.NullString + RecordsetCount int64 } -// A recordset takes the worst child record status. An empty recordset is ACTIVE. -func (q *Queries) ZoneGetAllWithRecordsets(ctx context.Context) ([]ZoneGetAllWithRecordsetsRow, error) { - rows, err := q.db.QueryContext(ctx, ZoneGetAllWithRecordsets) +func (q *Queries) recordsetCountByZone(ctx context.Context) ([]recordsetCountByZoneRow, error) { + rows, err := q.db.QueryContext(ctx, RecordsetCountByZone) if err != nil { return nil, err } defer rows.Close() - var items []ZoneGetAllWithRecordsetsRow + var items []recordsetCountByZoneRow for rows.Next() { - var i ZoneGetAllWithRecordsetsRow + var i recordsetCountByZoneRow if err := rows.Scan( &i.ZoneID, &i.ZoneName, &i.TenantID, - &i.ZoneStatus, - &i.ZoneType, - &i.RecordsetID, - &i.RecordsetName, - &i.RecordsetType, - &i.RecordsetStatus, + &i.RecordsetCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const RecordsetGetAllWithStatus = `-- name: recordsetGetAllWithStatus :many +SELECT + rs.id, + rs.name, + rs.type, + z.id AS zone_id, + z.name AS zone_name, + CASE rs_status.severity + WHEN 1 THEN 'ERROR' + WHEN 2 THEN 'PENDING' + WHEN 3 THEN 'ACTIVE' + WHEN 4 THEN 'DELETED' + ELSE 'ACTIVE' + END AS status +FROM recordsets rs +JOIN zones z ON z.id = rs.zone_id +LEFT JOIN ( + SELECT recordset_id, + MIN(CASE status + WHEN 'ERROR' THEN 1 + WHEN 'PENDING' THEN 2 + WHEN 'ACTIVE' THEN 3 + WHEN 'DELETED' THEN 4 + END) AS severity + FROM records + GROUP BY recordset_id +) rs_status ON rs_status.recordset_id = rs.id +WHERE z.deleted = '0' +` + +type recordsetGetAllWithStatusRow struct { + ID string + Name string + Type string + ZoneID string + ZoneName string + Status string +} + +func (q *Queries) recordsetGetAllWithStatus(ctx context.Context) ([]recordsetGetAllWithStatusRow, error) { + rows, err := q.db.QueryContext(ctx, RecordsetGetAllWithStatus) + if err != nil { + return nil, err + } + defer rows.Close() + var items []recordsetGetAllWithStatusRow + for rows.Next() { + var i recordsetGetAllWithStatusRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.Type, + &i.ZoneID, + &i.ZoneName, + &i.Status, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const ZoneGetAll = `-- name: zoneGetAll :many +SELECT id, name, tenant_id, status, type +FROM zones +WHERE deleted = '0' +` + +type zoneGetAllRow struct { + ID string + Name string + TenantID sql.NullString + Status string + Type string +} + +func (q *Queries) zoneGetAll(ctx context.Context) ([]zoneGetAllRow, error) { + rows, err := q.db.QueryContext(ctx, ZoneGetAll) + if err != nil { + return nil, err + } + defer rows.Close() + var items []zoneGetAllRow + for rows.Next() { + var i zoneGetAllRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.TenantID, + &i.Status, + &i.Type, ); err != nil { return nil, err } diff --git a/go.mod b/go.mod index 0e2fae3..904b683 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.25.5 require ( github.com/DATA-DOG/go-sqlmock v1.5.2 github.com/go-sql-driver/mysql v1.9.3 + github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.11.1 github.com/testcontainers/testcontainers-go v0.40.0 github.com/testcontainers/testcontainers-go/modules/mariadb v0.40.0 @@ -31,7 +32,6 @@ require ( github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.8 // indirect github.com/klauspost/compress v1.18.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect diff --git a/schema/designate/schema.sql b/schema/designate/schema.sql index 0434d05..525f0fa 100644 --- a/schema/designate/schema.sql +++ b/schema/designate/schema.sql @@ -9,10 +9,10 @@ CREATE TABLE zones ( CREATE TABLE recordsets ( id CHAR(32) NOT NULL PRIMARY KEY, - zone_id CHAR(32), + zone_id CHAR(32) NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(16) NOT NULL, - FOREIGN KEY (zone_id) REFERENCES zones(id) ON DELETE CASCADE + CONSTRAINT recordsets_zone_fk FOREIGN KEY (zone_id) REFERENCES zones(id) ON DELETE CASCADE ); CREATE TABLE records ( diff --git a/tests/queries_test.go b/tests/queries_test.go index 8f2c760..ee83701 100644 --- a/tests/queries_test.go +++ b/tests/queries_test.go @@ -4,6 +4,7 @@ package tests import ( "context" + "database/sql" "github.com/stretchr/testify/require" cinder "github.com/vexxhost/openstackdb/cinder/db" designate "github.com/vexxhost/openstackdb/designate/db" @@ -104,45 +105,84 @@ func TestGlanceQueries(t *testing.T) { func TestDesignateZonesWithRecordsets(t *testing.T) { conn := testutil.NewMySQLContainer(t, "designate", "../sql/designate/schema.sql") queries := designate.New(conn) + ctx := context.Background() - rows, err := queries.ZoneGetAllWithRecordsets(context.Background()) + zones, err := queries.ZoneGetAll(ctx) + require.NoError(t, err) + require.Empty(t, zones) + counts, err := queries.RecordsetCountByZone(ctx) + require.NoError(t, err) + require.Empty(t, counts) + recordsets, err := queries.RecordsetGetAllWithStatus(ctx) require.NoError(t, err) - require.Empty(t, rows) + require.Empty(t, recordsets) testutil.SeedSQL(t, conn, `INSERT INTO zones (id, name, tenant_id, status, type, deleted) VALUES - ('zone-1', 'example.org.', 'project-1', 'ACTIVE', 'PRIMARY', '0'), - ('empty-zone', 'empty.example.', NULL, 'PENDING', 'SECONDARY', '0'), - ('deleted-zone', 'deleted.example.', 'project-1', 'DELETED', 'PRIMARY', 'deleted-zone')`, + ('a86dba5800434cc6a1bb69d5e86f3ca3', 'example.org.', 'project-1', 'ACTIVE', 'PRIMARY', '0'), + ('2150b1bfdee242219d8511f7886fb15f', 'empty.example.', NULL, 'PENDING', 'SECONDARY', '0'), + ('23527f480fa04efc82264f2468c7c40e', 'deleted.example.', 'project-1', 'DELETED', 'PRIMARY', '23527f480fa04efc82264f2468c7c40e')`, `INSERT INTO recordsets (id, zone_id, name, type) VALUES - ('error', 'zone-1', 'error.example.org.', 'A'), - ('pending', 'zone-1', 'pending.example.org.', 'A'), - ('active', 'zone-1', 'active.example.org.', 'A'), - ('deleted', 'zone-1', 'deleted.example.org.', 'A'), - ('empty', 'zone-1', 'empty.example.org.', 'TXT'), - ('hidden', 'deleted-zone', 'deleted.example.', 'NS')`, + ('10000000000040008000000000000001', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'error.example.org.', 'A'), + ('10000000000040008000000000000002', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'pending.example.org.', 'A'), + ('10000000000040008000000000000003', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'active.example.org.', 'A'), + ('10000000000040008000000000000004', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'deleted.example.org.', 'A'), + ('10000000000040008000000000000005', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'empty.example.org.', 'TXT'), + ('10000000000040008000000000000006', '23527f480fa04efc82264f2468c7c40e', 'deleted.example.', 'NS')`, `INSERT INTO records (id, recordset_id, status) VALUES - ('1', 'error', 'PENDING'), ('2', 'error', 'ERROR'), - ('3', 'pending', 'ACTIVE'), ('4', 'pending', 'PENDING'), - ('5', 'active', 'DELETED'), ('6', 'active', 'ACTIVE'), - ('7', 'deleted', 'DELETED')`, + ('1', '10000000000040008000000000000001', 'PENDING'), ('2', '10000000000040008000000000000001', 'ERROR'), + ('3', '10000000000040008000000000000002', 'ACTIVE'), ('4', '10000000000040008000000000000002', 'PENDING'), + ('5', '10000000000040008000000000000003', 'DELETED'), ('6', '10000000000040008000000000000003', 'ACTIVE'), + ('7', '10000000000040008000000000000004', 'DELETED')`, ) - rows, err = queries.ZoneGetAllWithRecordsets(context.Background()) - require.NoError(t, err) - require.Len(t, rows, 6) - statuses := make(map[string]string) - for _, row := range rows { - if !row.RecordsetID.Valid { - require.Equal(t, "empty-zone", row.ZoneID) - continue + checkVisibleRows := func(t *testing.T) { + t.Helper() + const zoneID = "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3" + const emptyZoneID = "2150b1bf-dee2-4221-9d85-11f7886fb15f" + tenantID := sql.NullString{String: "project-1", Valid: true} + zones, err := queries.ZoneGetAll(ctx) + require.NoError(t, err) + require.ElementsMatch(t, []designate.ZoneGetAllRow{ + {ID: zoneID, Name: "example.org.", TenantID: tenantID, Status: "ACTIVE", Type: "PRIMARY"}, + {ID: emptyZoneID, Name: "empty.example.", Status: "PENDING", Type: "SECONDARY"}, + }, zones) + + counts, err := queries.RecordsetCountByZone(ctx) + require.NoError(t, err) + require.ElementsMatch(t, []designate.RecordsetCountByZoneRow{ + {ZoneID: zoneID, ZoneName: "example.org.", TenantID: tenantID, RecordsetCount: 5}, + {ZoneID: emptyZoneID, ZoneName: "empty.example.", RecordsetCount: 0}, + }, counts) + + recordsets, err := queries.RecordsetGetAllWithStatus(ctx) + require.NoError(t, err) + require.Len(t, recordsets, 5) + statuses := make(map[string]string, len(recordsets)) + for _, row := range recordsets { + require.Equal(t, zoneID, row.ZoneID) + require.Equal(t, "example.org.", row.ZoneName) + statuses[row.ID] = row.Status } - require.Equal(t, "zone-1", row.ZoneID) - statuses[row.RecordsetID.String] = row.RecordsetStatus + require.Equal(t, map[string]string{ + "10000000-0000-4000-8000-000000000001": "ERROR", + "10000000-0000-4000-8000-000000000002": "PENDING", + "10000000-0000-4000-8000-000000000003": "ACTIVE", + "10000000-0000-4000-8000-000000000004": "DELETED", + "10000000-0000-4000-8000-000000000005": "ACTIVE", + }, statuses) } - require.Equal(t, map[string]string{ - "error": "ERROR", "pending": "PENDING", "active": "ACTIVE", "deleted": "DELETED", "empty": "ACTIVE", - }, statuses) + t.Run("non-null zone ID", checkVisibleRows) + t.Run("nullable zone ID", func(t *testing.T) { + testutil.SeedSQL(t, conn, + `ALTER TABLE recordsets DROP FOREIGN KEY recordsets_zone_fk`, + `ALTER TABLE recordsets MODIFY zone_id CHAR(32) NULL`, + `ALTER TABLE recordsets ADD CONSTRAINT recordsets_zone_fk FOREIGN KEY (zone_id) REFERENCES zones(id) ON DELETE CASCADE`, + `INSERT INTO recordsets (id, zone_id, name, type) VALUES + ('10000000000040008000000000000007', NULL, 'orphan.example.', 'A')`, + ) + checkVisibleRows(t) + }) } func TestKeystoneQueries(t *testing.T) {