Last active
December 26, 2015 18:49
-
-
Save roa/7197198 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "fmt" | |
| _ "github.com/lib/pq" | |
| ) | |
| const ( | |
| query1 = ` | |
| WITH new_values | |
| ( | |
| id | |
| ) AS | |
| ( | |
| VALUES (12345678) | |
| ) | |
| DELETE FROM test | |
| WHERE id IN ( SELECT id FROM new_values ) | |
| ` | |
| query2 = ` | |
| WITH new_values | |
| ( | |
| id, | |
| ) | |
| AS | |
| ( | |
| VALUES (12345678) | |
| ) | |
| INSERT INTO test | |
| ( | |
| id | |
| ) | |
| SELECT | |
| nv.id | |
| FROM new_values nv | |
| ` | |
| ) | |
| func main() { | |
| queries := []string{query1, query2} | |
| db, err := sql.Open( | |
| "postgres", | |
| fmt.Sprintf( | |
| "user=%s password=%s host=%s port=%d dbname=%s sslmode=%s", | |
| "postgres", | |
| "", | |
| "localhost", | |
| 5432, | |
| "test", | |
| "disable", | |
| ), | |
| ) | |
| if err != nil { | |
| panic(err) | |
| } | |
| tx, err := db.Begin() | |
| if err != nil { | |
| panic(err) | |
| } | |
| for _, query := range queries { | |
| rows, err := tx.Query(query) | |
| if err != nil { | |
| panic(err) | |
| tx.Rollback() | |
| break | |
| } | |
| rows.Close() | |
| } | |
| err = tx.Commit() | |
| if err != nil { | |
| tx.Rollback() | |
| panic(err) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | |
| -- PostgreSQL database dump | |
| -- | |
| SET statement_timeout = 0; | |
| SET lock_timeout = 0; | |
| SET client_encoding = 'UTF8'; | |
| SET standard_conforming_strings = on; | |
| SET check_function_bodies = false; | |
| SET client_min_messages = warning; | |
| -- | |
| -- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: | |
| -- | |
| CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; | |
| -- | |
| -- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: | |
| -- | |
| COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; | |
| SET search_path = public, pg_catalog; | |
| SET default_tablespace = ''; | |
| SET default_with_oids = false; | |
| -- | |
| -- Name: test; Type: TABLE; Schema: public; Owner: postgres; Tablespace: | |
| -- | |
| CREATE TABLE test ( | |
| id integer | |
| ); | |
| ALTER TABLE public.test OWNER TO postgres; | |
| -- | |
| -- Name: public; Type: ACL; Schema: -; Owner: postgres | |
| -- | |
| REVOKE ALL ON SCHEMA public FROM PUBLIC; | |
| REVOKE ALL ON SCHEMA public FROM postgres; | |
| GRANT ALL ON SCHEMA public TO postgres; | |
| GRANT ALL ON SCHEMA public TO PUBLIC; | |
| -- | |
| -- PostgreSQL database dump complete | |
| -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment