database: add wt_public_id domain type (#30)
This commit is contained in:
1 parent
05330db741
commit
8824a6f7f9
5 files changed
+143
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
_ "github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
func TestPublicIdDomain(t *testing.T) {
|
||||
const (
|
||||
createTable = `
|
||||
create table test_table (
|
||||
id bigint generated always as identity primary key,
|
||||
public_id wt_public_id
|
||||
);
|
||||
`
|
||||
insert = `
|
||||
insert into test_table (public_id)
|
||||
values ($1)
|
||||
returning id;
|
||||
`
|
||||
)
|
||||
|
||||
cleanup, conn := TestSetup(t, "migrations/postgres")
|
||||
defer cleanup()
|
||||
defer conn.Close()
|
||||
|
||||
db := conn.DB()
|
||||
if _, err := db.Exec(createTable); err != nil {
|
||||
t.Fatalf("query: \n%s\n error: %s", createTable, err)
|
||||
}
|
||||
|
||||
failTests := []string{
|
||||
" ",
|
||||
"bar",
|
||||
"0000000001000000000200000000031",
|
||||
}
|
||||
for _, tt := range failTests {
|
||||
value := tt
|
||||
t.Run(tt, func(t *testing.T) {
|
||||
t.Logf("insert value: %q", value)
|
||||
if _, err := db.Query(insert, value); err == nil {
|
||||
t.Errorf("want error, got no error for inserting public_id: %s", value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
okTests := []string{
|
||||
"l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl",
|
||||
"00000000010000000002000000000312",
|
||||
"00000000010000000002000000000032",
|
||||
"12345678901234567890123456789012",
|
||||
"ec2_12345678901234567890123456789012",
|
||||
"prj_12345678901234567890123456789012",
|
||||
}
|
||||
for _, tt := range okTests {
|
||||
value := tt
|
||||
t.Run(tt, func(t *testing.T) {
|
||||
t.Logf("insert value: %q", value)
|
||||
if _, err := db.Query(insert, value); err != nil {
|
||||
t.Errorf("%s: want no error, got error %v", value, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
begin;
|
||||
|
||||
drop domain wt_public_id;
|
||||
|
||||
commit;
|
||||
@@ -0,0 +1,10 @@
|
||||
begin;
|
||||
|
||||
create domain wt_public_id as text
|
||||
check(
|
||||
length(trim(value)) > 31
|
||||
);
|
||||
comment on domain wt_public_id is
|
||||
'Random ID generated with github.com/hashicorp/vault/sdk/helper/base62';
|
||||
|
||||
commit;
|
||||
Reference in new issue
Block a user