forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.go
706 lines (601 loc) · 18.7 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Copyright © 2019 Martin Tournoij – This file is part of GoatCounter and
// published under the terms of a slightly modified EUPL v1.2 license, which can
// be found in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
"zgo.at/errors"
"zgo.at/goatcounter/cfg"
"zgo.at/guru"
"zgo.at/zcache"
"zgo.at/zdb"
"zgo.at/zhttp"
"zgo.at/zlog"
"zgo.at/zstd/zint"
"zgo.at/zvalidate"
)
// Plan column values.
const (
PlanPersonal = "personal"
PlanPersonalPlus = "personalplus" // This is really the "starter" plan.
PlanBusiness = "business"
PlanBusinessPlus = "businessplus"
PlanChild = "child"
)
var Plans = []string{PlanPersonal, PlanPersonalPlus, PlanBusiness, PlanBusinessPlus}
var reserved = []string{
"www", "mail", "smtp", "imap", "static",
"admin", "ns1", "ns2", "m", "mobile", "api",
"dev", "test", "beta", "new", "staging", "debug", "pprof",
"chat", "example", "yoursite", "test", "sql", "license",
}
var statTables = []string{"hit_stats", "system_stats", "browser_stats",
"location_stats", "size_stats"}
type Site struct {
ID int64 `db:"site_id" json:"id,readonly"`
Parent *int64 `db:"parent" json:"parent,readonly"`
// Custom domain, e.g. "stats.example.com"
Cname *string `db:"cname" json:"cname"`
// When the CNAME was verified.
CnameSetupAt *time.Time `db:"cname_setup_at" json:"cname_setup_at,readonly"`
// Domain code (arp242, which makes arp242.goatcounter.com)
Code string `db:"code" json:"code"`
// Site domain for linking (www.arp242.net).
LinkDomain string `db:"link_domain" json:"link_domain"`
Plan string `db:"plan" json:"plan"`
Stripe *string `db:"stripe" json:"-"`
BillingAmount *string `db:"billing_amount" json:"-"`
Settings SiteSettings `db:"settings" json:"setttings"`
// Whether this site has received any data; will be true after the first
// pageview.
ReceivedData bool `db:"received_data" json:"received_data"`
State string `db:"state" json:"state"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
FirstHitAt time.Time `db:"first_hit_at" json:"first_hit_at"`
}
var (
sitesCache = zcache.New(24*time.Hour, 1*time.Hour)
sitesCacheHostname = zcache.NewProxy(sitesCache)
)
// ClearCache clears the cache for this site.
func (s Site) ClearCache(full bool) {
sitesCache.Delete(strconv.FormatInt(s.ID, 10))
// TODO: be more selective about this.
if full {
cachePaths.Flush()
changedTitles.Flush()
}
}
// Defaults sets fields to default values, unless they're already set.
func (s *Site) Defaults(ctx context.Context) {
if s.State == "" {
s.State = StateActive
}
s.Code = strings.ToLower(s.Code)
if s.CreatedAt.IsZero() {
s.CreatedAt = Now()
} else {
t := Now()
s.UpdatedAt = &t
}
if s.FirstHitAt.IsZero() {
s.FirstHitAt = Now()
}
s.Settings.Defaults()
}
var noUnderscore = time.Date(2020, 03, 20, 0, 0, 0, 0, time.UTC)
// Validate the object.
func (s *Site) Validate(ctx context.Context) error {
v := zvalidate.New()
v.Required("code", s.Code)
v.Required("state", s.State)
v.Required("plan", s.Plan)
v.Include("state", s.State, States)
if s.Parent == nil {
v.Include("plan", s.Plan, Plans)
} else {
v.Include("plan", s.Plan, []string{PlanChild})
}
// Must always include all widgets we know about.
for _, w := range defaultWidgets() {
if s.Settings.Widgets.Get(w["name"].(string)) == nil {
v.Append("widgets", fmt.Sprintf("widget %q is missing", w["name"].(string)))
}
}
v.Range("widgets.pages.s.limit_pages", int64(s.Settings.LimitPages()), 1, 100)
v.Range("widgets.pages.s.limit_refs", int64(s.Settings.LimitRefs()), 1, 25)
if _, i := s.Settings.Views.Get("default"); i == -1 || len(s.Settings.Views) != 1 {
v.Append("views", "view not set")
}
if s.Settings.DataRetention > 0 {
v.Range("settings.data_retention", int64(s.Settings.DataRetention), 14, 0)
}
if len(s.Settings.IgnoreIPs) > 0 {
for _, ip := range s.Settings.IgnoreIPs {
v.IP("settings.ignore_ips", ip)
}
}
v.Domain("link_domain", s.LinkDomain)
v.Len("code", s.Code, 2, 50)
v.Exclude("code", s.Code, reserved)
// TODO: compat with older requirements, otherwise various update functions
// will error out.
if !s.CreatedAt.IsZero() && s.CreatedAt.Before(noUnderscore) {
for _, c := range s.Code {
if !(c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
v.Append("code", fmt.Sprintf("%q not allowed; characters are limited to '_', '-', a to z, and numbers", c))
break
}
}
if len(s.Code) > 0 && (s.Code[0] == '_' || s.Code[0] == '-') { // Special domains, like _acme-challenge.
v.Append("code", "cannot start with underscore or dash (_, -)")
}
} else {
labels := v.Hostname("code", s.Code)
if len(labels) > 1 {
v.Append("code", "cannot contain '.'")
}
}
if s.Cname != nil {
v.Len("cname", *s.Cname, 4, 255)
v.Domain("cname", *s.Cname)
if cfg.GoatcounterCom && strings.HasSuffix(*s.Cname, cfg.Domain) {
v.Append("cname", "cannot end with %q", cfg.Domain)
}
var cname uint8
err := zdb.Get(ctx, &cname,
`select 1 from sites where lower(cname) = lower($1) and site_id != $2 limit 1`,
s.Cname, s.ID)
if err != nil && err != sql.ErrNoRows {
return err
}
if cname == 1 {
v.Append("cname", "already exists")
}
}
if s.Stripe != nil && !strings.HasPrefix(*s.Stripe, "cus_") {
v.Append("stripe", "not a valid Stripe customer ID")
}
if !v.HasErrors() {
var code uint8
err := zdb.Get(ctx, &code,
`select 1 from sites where lower(code) = lower($1) and site_id != $2 limit 1`,
s.Code, s.ID)
if err != nil && err != sql.ErrNoRows {
return err
}
if code == 1 {
v.Append("code", "already exists")
}
}
return v.ErrorOrNil()
}
// Insert a new row.
func (s *Site) Insert(ctx context.Context) error {
if s.ID > 0 {
return errors.New("ID > 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
s.ID, err = zdb.InsertID(ctx, "site_id",
`insert into sites (parent, code, cname, link_domain, settings, plan, created_at, first_hit_at) values (?, ?, ?, ?, ?, ?, ?, ?)`,
s.Parent, s.Code, s.Cname, s.LinkDomain, s.Settings, s.Plan, s.CreatedAt.Format(zdb.Date), s.CreatedAt.Format(zdb.Date))
if err != nil && zdb.ErrUnique(err) {
return guru.New(400, "this site already exists: code or domain must be unique")
}
return errors.Wrap(err, "Site.Insert")
}
// Update existing.
func (s *Site) Update(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set settings=$1, cname=$2, link_domain=$3, updated_at=$4 where site_id=$5`,
s.Settings, s.Cname, s.LinkDomain, s.UpdatedAt.Format(zdb.Date), s.ID)
if err != nil {
return errors.Wrap(err, "Site.Update")
}
s.ClearCache(false)
return nil
}
// UpdateStripe sets the Stripe customer ID.
func (s *Site) UpdateStripe(ctx context.Context, stripeID, plan, amount string) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
s.Stripe = &stripeID
s.Plan = plan
if amount == "" {
s.BillingAmount = nil
} else {
s.BillingAmount = &amount
}
err = zdb.Exec(ctx,
`update sites set stripe=$1, plan=$2, billing_amount=$3, updated_at=$4 where site_id=$5`,
s.Stripe, s.Plan, s.BillingAmount, s.UpdatedAt.Format(zdb.Date), s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateStripe")
}
s.ClearCache(false)
return nil
}
// UpdateCode changes the site's domain code (e.g. "test" in
// "test.goatcounter.com").
func (s *Site) UpdateCode(ctx context.Context, code string) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Code = code
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set code=$1, updated_at=$2 where site_id=$3`,
s.Code, s.UpdatedAt.Format(zdb.Date), s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCode")
}
sitesCache.Delete(strconv.FormatInt(s.ID, 10))
return nil
}
func (s *Site) UpdateReceivedData(ctx context.Context) error {
err := zdb.Exec(ctx, `update sites set received_data=1 where site_id=$1`, s.ID)
s.ClearCache(false)
return errors.Wrap(err, "Site.UpdateReceivedData")
}
func (s *Site) UpdateFirstHitAt(ctx context.Context, f time.Time) error {
s.FirstHitAt = f
err := zdb.Exec(ctx,
`update sites set first_hit_at=$1 where site_id=$2`,
s.FirstHitAt.Format(zdb.Date), s.ID)
s.ClearCache(false)
return errors.Wrap(err, "Site.UpdateFirstHitAt")
}
// UpdateCnameSetupAt confirms the custom domain was setup correct.
func (s *Site) UpdateCnameSetupAt(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
n := Now()
s.CnameSetupAt = &n
err := zdb.Exec(ctx,
`update sites set cname_setup_at=$1 where site_id=$2`,
s.CnameSetupAt.Format(zdb.Date), s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCnameSetupAt")
}
s.ClearCache(false)
return nil
}
// Delete a site.
func (s *Site) Delete(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
t := Now()
err := zdb.Exec(ctx,
`update sites set state=$1, updated_at=$2 where site_id=$3 or parent=$3`,
StateDeleted, t.Format(zdb.Date), s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
s.ClearCache(true)
s.ID = 0
s.UpdatedAt = &t
s.State = StateDeleted
return nil
}
// ByID gets a site by ID.
func (s *Site) ByID(ctx context.Context, id int64) error {
k := strconv.FormatInt(id, 10)
ss, ok := sitesCache.Get(k)
if ok {
*s = *ss.(*Site)
return nil
}
err := zdb.Get(ctx, s,
`/* Site.ByID */ select * from sites where site_id=$1 and state=$2`,
id, StateActive)
if err != nil {
return errors.Wrapf(err, "Site.ByID %d", id)
}
sitesCache.SetDefault(k, s)
return nil
}
// ByCode gets a site by code.
func (s *Site) ByCode(ctx context.Context, code string) error {
return errors.Wrapf(zdb.Get(ctx, s,
`/* Site.ByCode */ select * from sites where code=$1 and state=$2`,
code, StateActive), "Site.ByCode %s", code)
}
// ByHost gets a site by host name.
func (s *Site) ByHost(ctx context.Context, host string) error {
ss, ok := sitesCacheHostname.Get(host)
if ok {
*s = *ss.(*Site)
return nil
}
// Custom domain or serve.
if cfg.Serve || !strings.HasSuffix(host, cfg.Domain) {
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(cname)=lower($1) and state=$2`,
zhttp.RemovePort(host), StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from custom domain")
}
sitesCacheHostname.Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// Get from code (e.g. "arp242" in "arp242.goatcounter.com").
p := strings.Index(host, ".")
if p == -1 {
return errors.Errorf("Site.ByHost: no subdomain in host %q", host)
}
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(code)=lower($1) and state=$2`,
host[:p], StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from code")
}
sitesCacheHostname.Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// ListSubs lists all subsites, including the current site and parent.
func (s *Site) ListSubs(ctx context.Context) ([]string, error) {
col := "code"
if cfg.Serve {
col = "cname"
}
var codes []string
err := zdb.Select(ctx, &codes, `/* Site.ListSubs */
select `+col+` from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, s.ID)
return codes, errors.Wrap(err, "Site.ListSubs")
}
// Domain gets the global default domain, or this site's configured custom
// domain.
func (s Site) Domain() string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return cfg.Domain
}
// Display format: just the domain (cname or code+domain).
//
//lint:ignore U1001 used in template.
func (s Site) Display() string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return fmt.Sprintf("%s.%s", s.Code, zhttp.RemovePort(cfg.Domain))
}
// URL to this site.
func (s Site) URL() string {
if s.Cname != nil && s.CnameSetupAt != nil {
return fmt.Sprintf("http%s://%s%s",
map[bool]string{true: "s", false: ""}[cfg.Prod],
*s.Cname, cfg.Port)
}
return fmt.Sprintf("http%s://%s.%s%s",
map[bool]string{true: "s", false: ""}[cfg.Prod],
s.Code, cfg.Domain, cfg.Port)
}
// PlanCustomDomain reports if this site's plan allows custom domains.
func (s Site) PlanCustomDomain(ctx context.Context) bool {
if s.Parent != nil {
var ps Site
err := ps.ByID(ctx, *s.Parent)
if err != nil {
zlog.Error(err)
return false
}
return ps.PlanCustomDomain(ctx)
}
return s.Plan == PlanPersonalPlus || s.Plan == PlanBusiness || s.Plan == PlanBusinessPlus
}
// IDOrParent gets this site's ID or the parent ID if that's set.
func (s Site) IDOrParent() int64 {
if s.Parent != nil {
return *s.Parent
}
return s.ID
}
//lint:ignore U1001 used in template (via ShowPayBanner)
var trialPeriod = time.Hour * 24 * 14
// ShowPayBanner determines if we should show a "please pay" banner for the
// customer.
//
//lint:ignore U1001 used in template.
func (s Site) ShowPayBanner(ctx context.Context) bool {
if s.Parent != nil {
var ps Site
err := ps.ByID(ctx, *s.Parent)
if err != nil {
zlog.Error(err)
return false
}
return ps.ShowPayBanner(ctx)
}
if s.Stripe != nil {
return false
}
return -Now().Sub(s.CreatedAt.Add(trialPeriod)) < 0
}
func (s Site) FreePlan() bool {
return s.Stripe != nil && strings.HasPrefix(*s.Stripe, "cus_free_")
}
func (s Site) PayExternal() string {
if s.Stripe == nil {
return ""
}
if strings.HasPrefix(*s.Stripe, "cus_github_") {
return "GitHub Sponsors"
}
if strings.HasPrefix(*s.Stripe, "cus_patreon_") {
return "Patreon"
}
return ""
}
// DeleteAll deletes all pageviews for this site, keeping the site itself and
// user intact.
func (s Site) DeleteAll(ctx context.Context) error {
return zdb.TX(ctx, func(ctx context.Context) error {
for _, t := range append(statTables, "hit_counts", "ref_counts", "hits", "paths") {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=:id`, zdb.A{"id": s.ID})
if err != nil {
return errors.Wrap(err, "Site.DeleteAll: delete "+t)
}
}
s.ClearCache(true)
return nil
})
}
func (s Site) DeleteOlderThan(ctx context.Context, days int) error {
if days < 14 {
return errors.Errorf("days must be at least 14: %d", days)
}
return zdb.TX(ctx, func(ctx context.Context) error {
ival := interval(ctx, days)
var pathIDs []int64
err := zdb.Select(ctx, &pathIDs, `/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=$1 and hour < `+ival+` group by path_id`, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: get paths")
}
for _, t := range statTables {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=$1 and day < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete "+t)
}
}
err = zdb.Exec(ctx, `delete from hit_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hit_counts")
}
err = zdb.Exec(ctx, `delete from ref_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete ref_counts")
}
err = zdb.Exec(ctx, `delete from hits where site_id=$1 and created_at < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hits")
}
if len(pathIDs) > 0 {
query, args, err := sqlx.In(`/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=? and path_id in (?)`,
s.ID, pathIDs)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
var remainPath []int64
err = zdb.Select(ctx, &remainPath, query, args...)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
diff := zint.Difference(pathIDs, remainPath)
if len(diff) > 0 {
query, args, err := sqlx.In(`delete from paths where site_id=? and path_id in (?)`, s.ID, diff)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
err = zdb.Exec(ctx, query, args...)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
}
}
return nil
})
}
// Admin reports if this site is an admin.
func (s Site) Admin() bool {
return s.ID == 1
}
// Sites is a list of sites.
type Sites []Site
// UnscopedList lists all sites, not scoped to the current user.
func (s *Sites) UnscopedList(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s,
`/* Sites.List */ select * from sites where state=$1`,
StateActive), "Sites.List")
}
// UnscopedListCnames all sites that have CNAME set, not scoped to the current
// user.
func (s *Sites) UnscopedListCnames(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListCnames */
select * from sites where state=$1 and cname is not null`,
StateActive), "Sites.List")
}
// ListSubs lists all subsites for the current site.
func (s *Sites) ListSubs(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListSubs */
select * from sites where parent=$1 and state=$2 order by code`,
MustGetSite(ctx).ID, StateActive), "Sites.ListSubs")
}
// ForThisAccount gets all sites associated with this account.
func (s *Sites) ForThisAccount(ctx context.Context, excludeCurrent bool) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, s, `/* Sites.ForThisAccount */
select * from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, site.ID)
if err != nil {
return errors.Wrap(err, "Sites.ForThisAccount")
}
if excludeCurrent {
ss := *s
for i := range ss {
if ss[i].ID == site.ID {
*s = append(ss[:i], ss[i+1:]...)
break
}
}
}
return nil
}
// ContainsCNAME reports if there is a site with this CNAME set.
func (s *Sites) ContainsCNAME(ctx context.Context, cname string) (bool, error) {
var ok bool
err := zdb.Get(ctx, &ok, `/* Sites.ContainsCNAME */
select 1 from sites where lower(cname)=lower($1) limit 1`, cname)
return ok, errors.Wrapf(err, "Sites.ContainsCNAME for %q", cname)
}
// OldSoftDeleted finds all sites which have been soft-deleted more than a week
// ago.
func (s *Sites) OldSoftDeleted(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, fmt.Sprintf(`/* Sites.OldSoftDeleted */
select * from sites where state=$1 and updated_at < %s`, interval(ctx, 7)),
StateDeleted), "Sites.OldSoftDeleted")
}