Change certificates.not_after and certificates.renew_retry to allow null values

This commit is contained in:
Melon 2025-01-31 18:54:12 +00:00
parent c247a50472
commit c373f18336
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
10 changed files with 63 additions and 29 deletions

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.25.0 // sqlc v1.28.0
// source: certificate.sql // source: certificate.sql
package database package database
@ -19,7 +19,7 @@ VALUES (?, ?, ?, ?)
type AddCertificateParams struct { type AddCertificateParams struct {
Owner string `json:"owner"` Owner string `json:"owner"`
Dns sql.NullInt64 `json:"dns"` Dns sql.NullInt64 `json:"dns"`
NotAfter time.Time `json:"not_after"` NotAfter sql.NullTime `json:"not_after"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
} }
@ -75,7 +75,7 @@ FROM certificates AS cert
WHERE cert.active = 1 WHERE cert.active = 1
AND (cert.auto_renew = 1 OR cert.not_after IS NULL) AND (cert.auto_renew = 1 OR cert.not_after IS NULL)
AND cert.renewing = 0 AND cert.renewing = 0
AND DATETIME() > DATETIME(cert.renew_retry) AND (cert.renew_retry IS NULL OR DATETIME() > DATETIME(cert.renew_retry))
AND (cert.not_after IS NULL OR DATETIME(cert.not_after, 'utc', '-30 days') < DATETIME()) AND (cert.not_after IS NULL OR DATETIME(cert.not_after, 'utc', '-30 days') < DATETIME())
ORDER BY cert.temp_parent, cert.not_after DESC NULLS FIRST ORDER BY cert.temp_parent, cert.not_after DESC NULLS FIRST
LIMIT 1 LIMIT 1
@ -83,7 +83,7 @@ LIMIT 1
type FindNextCertRow struct { type FindNextCertRow struct {
ID int64 `json:"id"` ID int64 `json:"id"`
NotAfter time.Time `json:"not_after"` NotAfter sql.NullTime `json:"not_after"`
Type sql.NullString `json:"type"` Type sql.NullString `json:"type"`
Token sql.NullString `json:"token"` Token sql.NullString `json:"token"`
TempParent sql.NullInt64 `json:"temp_parent"` TempParent sql.NullInt64 `json:"temp_parent"`
@ -120,8 +120,8 @@ type FindOwnedCertsRow struct {
AutoRenew bool `json:"auto_renew"` AutoRenew bool `json:"auto_renew"`
Active bool `json:"active"` Active bool `json:"active"`
Renewing bool `json:"renewing"` Renewing bool `json:"renewing"`
RenewRetry time.Time `json:"renew_retry"` RenewRetry sql.NullTime `json:"renew_retry"`
NotAfter time.Time `json:"not_after"` NotAfter sql.NullTime `json:"not_after"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Domain string `json:"domain"` Domain string `json:"domain"`
} }
@ -190,7 +190,7 @@ WHERE id = ?
` `
type UpdateCertAfterRenewalParams struct { type UpdateCertAfterRenewalParams struct {
NotAfter time.Time `json:"not_after"` NotAfter sql.NullTime `json:"not_after"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
ID int64 `json:"id"` ID int64 `json:"id"`
} }
@ -209,7 +209,7 @@ WHERE id = ?
type UpdateRenewingStateParams struct { type UpdateRenewingStateParams struct {
Renewing bool `json:"renewing"` Renewing bool `json:"renewing"`
RenewRetry time.Time `json:"renew_retry"` RenewRetry sql.NullTime `json:"renew_retry"`
ID int64 `json:"id"` ID int64 `json:"id"`
} }

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.25.0 // sqlc v1.28.0
// source: certificate_domains.sql // source: certificate_domains.sql
package database package database

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.25.0 // sqlc v1.28.0
package database package database

View File

@ -0,0 +1,29 @@
-- null not after
ALTER TABLE certificates
RENAME COLUMN not_after TO not_after_2;
ALTER TABLE certificates
ADD COLUMN not_after DATETIME NULL;
UPDATE certificates
SET not_after = not_after_2
WHERE not_after IS NULL;
ALTER TABLE certificates
DROP COLUMN not_after_2;
-- null renew retry
ALTER TABLE certificates
RENAME COLUMN renew_retry TO renew_retry_2;
ALTER TABLE certificates
ADD COLUMN renew_retry DATETIME NULL;
UPDATE certificates
SET renew_retry = renew_retry_2
WHERE renew_retry IS NULL;
ALTER TABLE certificates
DROP COLUMN renew_retry_2;

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT. // Code generated by sqlc. DO NOT EDIT.
// versions: // versions:
// sqlc v1.25.0 // sqlc v1.28.0
package database package database
@ -16,10 +16,10 @@ type Certificate struct {
AutoRenew bool `json:"auto_renew"` AutoRenew bool `json:"auto_renew"`
Active bool `json:"active"` Active bool `json:"active"`
Renewing bool `json:"renewing"` Renewing bool `json:"renewing"`
NotAfter time.Time `json:"not_after"` NotAfter sql.NullTime `json:"not_after"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
TempParent sql.NullInt64 `json:"temp_parent"` TempParent sql.NullInt64 `json:"temp_parent"`
RenewRetry time.Time `json:"renew_retry"` RenewRetry sql.NullTime `json:"renew_retry"`
} }
type CertificateDomain struct { type CertificateDomain struct {

View File

@ -5,7 +5,7 @@ FROM certificates AS cert
WHERE cert.active = 1 WHERE cert.active = 1
AND (cert.auto_renew = 1 OR cert.not_after IS NULL) AND (cert.auto_renew = 1 OR cert.not_after IS NULL)
AND cert.renewing = 0 AND cert.renewing = 0
AND DATETIME() > DATETIME(cert.renew_retry) AND (cert.renew_retry IS NULL OR DATETIME() > DATETIME(cert.renew_retry))
AND (cert.not_after IS NULL OR DATETIME(cert.not_after, 'utc', '-30 days') < DATETIME()) AND (cert.not_after IS NULL OR DATETIME(cert.not_after, 'utc', '-30 days') < DATETIME())
ORDER BY cert.temp_parent, cert.not_after DESC NULLS FIRST ORDER BY cert.temp_parent, cert.not_after DESC NULLS FIRST
LIMIT 1; LIMIT 1;

View File

@ -292,7 +292,7 @@ func (s *Service) findNextCertificateToRenew() (*localCertData, error) {
d.id = row.ID d.id = row.ID
d.dns.name = row.Type d.dns.name = row.Type
d.dns.token = row.Token d.dns.token = row.Token
d.notAfter = row.NotAfter d.notAfter = row.NotAfter.Time
d.tempParent = row.TempParent d.tempParent = row.TempParent
return d, nil return d, nil
@ -412,7 +412,7 @@ func (s *Service) renewCert(localData *localCertData) error {
// set the NotAfter/NotBefore in the database // set the NotAfter/NotBefore in the database
err = s.db.UpdateCertAfterRenewal(context.Background(), database.UpdateCertAfterRenewalParams{ err = s.db.UpdateCertAfterRenewal(context.Background(), database.UpdateCertAfterRenewalParams{
NotAfter: cert.NotAfter, NotAfter: sql.NullTime{Time: cert.NotAfter, Valid: true},
UpdatedAt: cert.NotBefore, UpdatedAt: cert.NotBefore,
ID: localData.id, ID: localData.id,
}) })

View File

@ -73,8 +73,8 @@ func NewApiServer(listen string, db *database.Queries, signer *mjwt.KeyStore, do
AutoRenew: row.AutoRenew, AutoRenew: row.AutoRenew,
Active: row.Active, Active: row.Active,
Renewing: row.Renewing, Renewing: row.Renewing,
RenewRetry: row.RenewRetry, RenewRetry: row.RenewRetry.Time,
NotAfter: row.NotAfter, NotAfter: row.NotAfter.Time,
UpdatedAt: row.UpdatedAt, UpdatedAt: row.UpdatedAt,
} }
d := row.Domain d := row.Domain
@ -136,7 +136,7 @@ func NewApiServer(listen string, db *database.Queries, signer *mjwt.KeyStore, do
err := db.AddCertificate(req.Context(), database.AddCertificateParams{ err := db.AddCertificate(req.Context(), database.AddCertificateParams{
Owner: b.Subject, Owner: b.Subject,
Dns: sql.NullInt64{}, Dns: sql.NullInt64{},
NotAfter: time.Now(), NotAfter: sql.NullTime{Time: time.Now(), Valid: true},
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}) })
if err != nil { if err != nil {

View File

@ -8,3 +8,8 @@ sql:
package: "database" package: "database"
out: "database" out: "database"
emit_json_tags: true emit_json_tags: true
overrides:
- column: certificates.not_after
go_type: database/sql.NullTime
- column: certificates.renew_retry
go_type: database/sql.NullTime