Improve naming

Hopefully this naming scheme will be more straightforward.
This commit is contained in:
Devin Christensen 2016-01-27 17:15:48 -07:00
parent 93c64375e9
commit 737df30939
3 changed files with 17 additions and 17 deletions

View file

@ -30,7 +30,7 @@ func newPostgreSQLBackend(conf map[string]string) (Backend, error) {
unquoted_table, ok := conf["table"] unquoted_table, ok := conf["table"]
if !ok { if !ok {
unquoted_table = "vault" unquoted_table = "vault_kv_store"
} }
quoted_table := pq.QuoteIdentifier(unquoted_table) quoted_table := pq.QuoteIdentifier(unquoted_table)
@ -51,16 +51,16 @@ func newPostgreSQLBackend(conf map[string]string) (Backend, error) {
// upsert. // upsert.
var put_statement string var put_statement string
if upsert_required { if upsert_required {
put_statement = "SELECT vault_upsert($1, $2)" put_statement = "SELECT vault_kv_put($1, $2)"
} else { } else {
put_statement = "INSERT INTO " + quoted_table + " VALUES($1, $2)" + put_statement = "INSERT INTO " + quoted_table + " VALUES($1, $2)" +
" ON CONFLICT (vault_key) DO " + " ON CONFLICT (key) DO " +
" UPDATE SET vault_value = $2" " UPDATE SET value = $2"
} }
// Setup the backend. // Setup the backend.
m := &PostgreSQLBackend{ m := &PostgreSQLBackend{
table: unquoted_table, table: quoted_table,
client: db, client: db,
statements: make(map[string]*sql.Stmt), statements: make(map[string]*sql.Stmt),
} }
@ -68,9 +68,9 @@ func newPostgreSQLBackend(conf map[string]string) (Backend, error) {
// Prepare all the statements required // Prepare all the statements required
statements := map[string]string{ statements := map[string]string{
"put": put_statement, "put": put_statement,
"get": "SELECT vault_value FROM " + quoted_table + " WHERE vault_key = $1", "get": "SELECT value FROM " + quoted_table + " WHERE key = $1",
"delete": "DELETE FROM " + quoted_table + " WHERE vault_key = $1", "delete": "DELETE FROM " + quoted_table + " WHERE key = $1",
"list": "SELECT vault_key FROM " + quoted_table + " WHERE vault_key LIKE $1", "list": "SELECT key FROM " + quoted_table + " WHERE key LIKE $1",
} }
for name, query := range statements { for name, query := range statements {
if err := m.prepare(name, query); err != nil { if err := m.prepare(name, query); err != nil {

View file

@ -15,7 +15,7 @@ func TestPostgreSQLBackend(t *testing.T) {
table := os.Getenv("PGTABLE") table := os.Getenv("PGTABLE")
if table == "" { if table == "" {
table = "vault" table = "vault_kv_store"
} }
// Run vault tests // Run vault tests

View file

@ -315,29 +315,29 @@ The PostgreSQL backend has the following options:
A list of all supported parameters can be found in [the pq library documentation](https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters). A list of all supported parameters can be found in [the pq library documentation](https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters).
* `table` (optional) - The name of the table to write vault data to. Defaults * `table` (optional) - The name of the table to write vault data to. Defaults
to "vault". to "vault_kv_store".
Make sure the PostgreSQL database you choose (or create) for vault storage has Make sure the PostgreSQL database you choose (or create) for vault storage has
a table suitable for storing vault's data: a table suitable for storing vault's data:
```sql ```sql
CREATE TABLE vault ( CREATE TABLE vault_kv_store (
vault_key TEXT PRIMARY KEY, key TEXT PRIMARY KEY,
vault_value BYTEA value BYTEA
); );
``` ```
If you're using a version of PostgreSQL prior to 9.5, vault will expect an If you're using a version of PostgreSQL prior to 9.5, vault will expect an
upsert function to exist named "vault_upsert". The recommanded function to use upsert function to exist named "vault_kv_put". The recommanded function to use
for this operation is: for this operation is:
```sql ```sql
CREATE FUNCTION vault_upsert(_key TEXT, _value BYTEA) RETURNS VOID AS CREATE FUNCTION vault_kv_put(_key TEXT, _value BYTEA) RETURNS VOID AS
$$ $$
BEGIN BEGIN
LOOP LOOP
-- first try to update the key -- first try to update the key
UPDATE vault SET vault_value = _value WHERE vault_key = _key; UPDATE vault_kv_store SET value = _value WHERE key = _key;
IF found THEN IF found THEN
RETURN; RETURN;
END IF; END IF;
@ -345,7 +345,7 @@ BEGIN
-- if someone else inserts the same key concurrently, -- if someone else inserts the same key concurrently,
-- we could get a unique-key failure -- we could get a unique-key failure
BEGIN BEGIN
INSERT INTO vault (vault_key, vault_value) VALUES (_key, _value); INSERT INTO vault_kv_store (key, value) VALUES (_key, _value);
RETURN; RETURN;
EXCEPTION WHEN unique_violation THEN EXCEPTION WHEN unique_violation THEN
-- Do nothing, and loop to try the UPDATE again. -- Do nothing, and loop to try the UPDATE again.