Skip to content

Loading…

Use transactions where possible to protect me from me. #1

Closed
wants to merge 3 commits into from

1 participant

@mattdeboard

I did a dumb thing so updated the SQL to protect future brain farts of the same type.

@mattdeboard

Also added a newline to an error message.

@mattdeboard mattdeboard closed this
@mattdeboard mattdeboard deleted the mattdeboard:transactions branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 27 additions and 18 deletions.
  1. +1 −1 golang/eventcaptureserver/eventcaptureserver.go
  2. +18 −13 sql/create_eventlog_table.sql
  3. +8 −4 sql/init.sql
View
2 golang/eventcaptureserver/eventcaptureserver.go
@@ -36,7 +36,7 @@ func handleCapture(req *http.Request, params martini.Params) (string, int) {
req.PostFormValue("user_ref"), data}
if err := event.Save(); err != nil {
fmt.Println(err)
- return "Event capture failed", 500
+ return "Event capture failed\n", 500
}
return "", 204
}
View
31 sql/create_eventlog_table.sql
@@ -1,16 +1,21 @@
--- create the eventlog table
-CREATE TABLE eventlog (
- id serial PRIMARY KEY,
- timeutc timestamp with time zone NOT NULL default (now() at time zone 'utc'),
- event_type varchar(200) NOT NULL CHECK (event_type <> ''),
- ext_ref varchar(200),
- user_ref varchar(200),
- data json
-);
-CREATE INDEX eventlog_timeutc_type_user_idx
- ON eventlog (timeutc ASC,
- event_type NULLS LAST,
- user_ref NULLS LAST);
+DO $$ BEGIN
+ IF NOT EXISTS (SELECT * FROM pg_tables WHERE tablename = 'eventlog') THEN
+ -- create the eventlog table
+ CREATE TABLE eventlog (
+ id serial PRIMARY KEY,
+ timeutc timestamp with time zone NOT NULL default (now() at time zone 'utc'),
+ event_type varchar(200) NOT NULL CHECK (event_type <> ''),
+ ext_ref varchar(200),
+ user_ref varchar(200),
+ data json
+ );
+
+ CREATE INDEX eventlog_timeutc_type_user_idx
+ ON eventlog (timeutc ASC,
+ event_type NULLS LAST,
+ user_ref NULLS LAST);
+ END IF;
+END; $$;
-- a function to help in doing inserts
CREATE OR REPLACE FUNCTION insert_eventlog
View
12 sql/init.sql
@@ -1,7 +1,11 @@
---create user for the eventcapture apps
-CREATE ROLE eventcaptureuser LOGIN
- ENCRYPTED PASSWORD 'md59781e8b2674aa82664bae96006fc0c32'
- NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
+DO $$ BEGIN
+ --create user for the eventcapture apps
+ IF NOT EXISTS (SELECT * FROM pg_catalog.pg_user WHERE usename = 'eventcaptureuser') THEN
+ CREATE ROLE eventcaptureuser LOGIN
+ ENCRYPTED PASSWORD 'md59781e8b2674aa82664bae96006fc0c32'
+ NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
+ END IF;
+END; $$;
--create event database
CREATE DATABASE event
Something went wrong with that request. Please try again.