Giter VIP home page Giter VIP logo

pg-dump-upsert's Introduction

pg-dump-upsert ๐Ÿ˜๐Ÿ’ฉ

CI GoDoc Go Report Card

Simple tool to dump a Postgresql table as INSERT statements with ON CONFLICT clause (also known as "upsert" statements).

Pre-alpha software. Expect crashes, data loss, silent data corruption etc.

Rationale

The pg_dump command can dump tables as INSERT statements however you can't directly restore such dumps if the database has conflicting rows. Furthermore pg_dump is doing more work than simply querying the data and this sometimes causes seemingly unrelated failures.

Installation

$ go install github.com/tomyl/pg-dump-upsert@latest
$ pg-dump-upsert -h
Usage of pg-dump-upsert:
  -conflict-column string
        Append an ON CONFLICT clause for this column. All other columns will be included in a DO UPDATE SET list.
  -dsn string
        Connection string. Example: postgres://user:password@host:5432/db
  -from string
        Source table to dump.
  -insert-columns string
        Comma-separated list of columns to include in INSERT statement. Defaults to all columns.
  -noconflict
        Append ON CONFLICT DO NOTHING.
  -query string
        Use custom SELECT query. By default fetches all rows. Note that column order must match -insert-columns. It is also valid to just specify a WHERE clause. It will be appended to the default query.
  -to string
        Table name to use in INSERT statements. Defaults to the source table.
  -tx   
        Wrap INSERT statements in transaction.
  -verbose
        Log query statement to stderr.

Examples

Dump all rows in table employee:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee 
INSERT INTO employee (id, created_at, name, salary) VALUES (1, '2018-06-13 21:10:34.769555+08', 'Jane Doe', 123456);
...

Choose which columns to dump:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee -insert-columns id,name
INSERT INTO employee (id, name) VALUES (1, 'Jane Doe');
...

Ignore conflicts:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee -noconflict
INSERT INTO employee (id, created_at, name, salary) VALUES (1, '2018-06-13 21:10:34.769555+08', 'Jane Doe' 123456) ON CONFLICT DO NOTHING;
...

Update columns on conflict:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee -conflict-column id
INSERT INTO employee (id, created_at, name, salary) VALUES (1, '2018-06-13 21:10:34.769555+08', 'Jane Doe', 123456) ON CONFLICT (id) DO UPDATE SET created_at=EXCLUDED.created_at, name=EXCLUDED.name;
...

Fetch a subset of the rows:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee -query "WHERE salary > 12345"
INSERT INTO employee (id, created_at, name, salary) VALUES (1, '2018-06-13 21:10:34.769555+08', 'Jane Doe', 123456);
...

Use a different table name in the INSERT statements:

$ pg-dump-upsert -dsn "postgres://user:password@host:5432/db" -from employee -to minions
INSERT INTO minions (id, created_at, name, salary) VALUES (1, '2018-06-13 21:10:34.769555+08', 'Jane Doe', 123456);
...

To restore a dump, simply use the \i command in psql.

TODO

  • Implement support for all Postgres data types.
  • Skip generated columns when dumping views.
  • Allow which columns to update when specifying -conflict-column?
  • More unit tests would be nice...
  • Finish this TODO list.

pg-dump-upsert's People

Contributors

dependabot[bot] avatar izzette avatar m110 avatar tomyl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pg-dump-upsert's Issues

view support

Postgres' pg_dump can't dump data from a view.
Would it be possible to have this tool support views?
Thanks for considering.

json/jsonb column support

Love this project!... except can't use it without json and jsonb column support.

My Go is very, very rusty, otherwise I'd submit a PR :p.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.