Skip to content

Commit

Permalink
implement r/w operations w/ postgres for site and core schemas (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggilmore committed Nov 8, 2018
1 parent d826c0d commit 0eea09b
Show file tree
Hide file tree
Showing 19 changed files with 555 additions and 48 deletions.
178 changes: 178 additions & 0 deletions cmd/frontend/db/core_site_configuration_files_test.go
@@ -0,0 +1,178 @@
package db

import (
"strings"
"testing"

dbtesting "github.com/sourcegraph/sourcegraph/cmd/frontend/db/testing"
)

func TestCoreSiteConfigurationFiles_CoreGetLatestEmpty(t *testing.T) {
if testing.Short() {
t.Skip()
}

ctx := dbtesting.TestContext(t)

latestFile, err := CoreSiteConfigurationFiles.CoreGetLatest(ctx)
if err != nil {
t.Fatal(err)
}

if latestFile != nil {
t.Errorf("expected nil latestFile since no site configuration was created, got: %+v", latestFile)
}
}

func TestCoreSiteConfigurationFiles_CoreCreate_RejectInvalidJSON(t *testing.T) {
if testing.Short() {
t.Skip()
}

ctx := dbtesting.TestContext(t)

malformedJSON := "[This is malformed.}"

_, err := CoreSiteConfigurationFiles.CoreCreateIfUpToDate(ctx, nil, malformedJSON)

if err == nil || !strings.Contains(err.Error(), "invalid settings JSON") {
t.Fatalf("expected parse error after creating site configuration with malformed JSON, got: %+v", err)
}
}

func TestCoreSiteConfigurationFiles_CoreCreateIfUpToDate(t *testing.T) {
type input struct {
lastID int32
contents string
}

type output struct {
contents string
}

type pair struct {
input input
expected output
}

type test struct {
name string
sequence []pair
}

for _, test := range []test{
test{
name: "create_one",
sequence: []pair{
pair{
input{
lastID: 0,
contents: `"This is a test."`,
},
output{
contents: `"This is a test."`,
},
},
},
},
test{
name: "create_two",
sequence: []pair{
pair{
input{
lastID: 0,
contents: `"This is the first one."`,
},
output{
contents: `"This is the first one."`,
},
},
pair{
input{
lastID: 1,
contents: `"This is the second one."`,
},
output{
contents: `"This is the second one."`,
},
},
},
},
test{
name: "do_not_update_if_outdated",
sequence: []pair{
pair{
input{
lastID: 0,
contents: `"This is the first one."`,
},
output{
contents: `"This is the first one."`,
},
},
pair{
input{
lastID: 0,
contents: `"This configuration is now behind the first one, so it shouldn't be saved."`,
},
output{
contents: `"This is the first one."`,
},
},
},
},
test{
name: "maintain_commments_and_whitespace",
sequence: []pair{
pair{
input{
lastID: 0,
contents: `{"fieldA": "valueA",
// This is a comment.
"fieldB": "valueB",
}`,
},
output{
contents: `{"fieldA": "valueA",
// This is a comment.
"fieldB": "valueB",
}`,
},
},
},
},
} {
t.Run(test.name, func(t *testing.T) {
ctx := dbtesting.TestContext(t)
for _, p := range test.sequence {
output, err := CoreSiteConfigurationFiles.CoreCreateIfUpToDate(ctx, &p.input.lastID, p.input.contents)
if err != nil {
t.Fatal(err)
}

if output == nil {
t.Fatal("got unexpected nil site configuration file after creation")
}

if output.Contents != p.expected.contents {
t.Fatalf("returned site configuration file contents after creation - expected: %q, got:%q", p.expected.contents, output.Contents)
}

latestFile, err := CoreSiteConfigurationFiles.CoreGetLatest(ctx)
if err != nil {
t.Fatal(err)
}

if latestFile == nil {
t.Errorf("got unexpected nil site configuration file after GetLatest")
}

if latestFile.Contents != p.expected.contents {
t.Fatalf("returned site configuration file contents after GetLatest - expected: %q, got:%q", p.expected.contents, latestFile.Contents)
}
}
})
}
}
96 changes: 96 additions & 0 deletions cmd/frontend/db/migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/frontend/db/mockstores.go
Expand Up @@ -16,7 +16,7 @@ type MockStores struct {
Orgs MockOrgs
OrgMembers MockOrgMembers
Settings MockSettings
SiteConfig MockSiteConfig
SiteIDInfo MockSiteIDInfo
Users MockUsers
UserEmails MockUserEmails

Expand Down
32 changes: 30 additions & 2 deletions cmd/frontend/db/schema.md
Expand Up @@ -37,6 +37,19 @@ Indexes:
```

# Table "public.core_configuration_files"
```
Column | Type | Modifiers
------------+--------------------------+-----------------------------------------------------------------------
id | integer | not null default nextval('core_configuration_files_id_seq'::regclass)
contents | text |
created_at | timestamp with time zone | not null default now()
updated_at | timestamp with time zone | not null default now()
Indexes:
"core_configuration_files_pkey" PRIMARY KEY, btree (id)
```

# Table "public.discussion_comments"
```
Column | Type | Modifiers
Expand Down Expand Up @@ -467,14 +480,29 @@ Foreign-key constraints:
```

# Table "public.site_config"
# Table "public.site_configuration_files"
```
Column | Type | Modifiers
------------+--------------------------+-----------------------------------------------------------------------
id | integer | not null default nextval('site_configuration_files_id_seq'::regclass)
contents | text |
created_at | timestamp with time zone | not null default now()
updated_at | timestamp with time zone | not null default now()
Indexes:
"site_configuration_files_pkey" PRIMARY KEY, btree (id)
"core_configuration_files_unique" UNIQUE, btree (id)
"site_configuration_files_unique" UNIQUE, btree (id)
```

# Table "public.site_id_info"
```
Column | Type | Modifiers
-------------+---------+------------------------
site_id | uuid | not null
initialized | boolean | not null default false
Indexes:
"site_config_pkey" PRIMARY KEY, btree (site_id)
"site_id_info_pkey" PRIMARY KEY, btree (site_id)
```

Expand Down

0 comments on commit 0eea09b

Please sign in to comment.