1.31.0 • Published 11 months ago

@wmfs/pg-diff-sync v1.31.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

pg-diff-sync

Tymly Package npm (scoped) [CircleCI codecov CodeFactor Dependabot badge Commitizen friendly JavaScript Style Guide license

Takes two objects that describe the structure of a database and produces the PostgreSQL statements required to get from one to the other.

Install

$ npm install pg-diff-sync --save

Usage

  const pgDiffSync = require('pg-diff-sync')
  const currentDbStructure = {...} 
  const expectedDbStructure = {...} 

  const statements = pgDiffSync(
    currentDbStructure,
    expectedDbStructure
  )  
  // Returns an array of DDL statements to apply on the PostgreSQL database

API

pgDiffSync(baseDbStructure, targetDbStructure)

Arguments:

ArgTypeNotes
baseDbStructureobjectAn object representing the original starting position of a database - most likely the output conjured from the pg-info package
targetDbStructureobjectAnd an object representing how the database needs to be - in the same form as baseDbStructure (again see the pg-info package for more details)

Output

The output from pg-diff-sync is a simple array of strings.

  • Each string is a separate DDL statement, that should be run on the PostgreSQL database that produced the baseDbStructure object.
  • With some caveats, running these statements on the base database will turn it into the target database

Example output

[
  "CREATE SCHEMA pg_diff_sync_test;",
  "COMMENT ON SCHEMA pg_diff_sync_test IS 'Schema auto-generated by Relationize.js!';",
  "CREATE TABLE pg_diff_sync_test.people();",
  "COMMENT ON TABLE pg_diff_sync_test.people IS 'Just a simple list of people!';",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN employee_no text NOT NULL;",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN first_name text NOT NULL;",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN last_name text NOT NULL;",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN age integer;",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN _created timestamp with time zone NOT NULL DEFAULT now();",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN _created_by text;",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN _modified timestamp with time zone NOT NULL DEFAULT now();",
  "ALTER TABLE pg_diff_sync_test.people ADD COLUMN _modified_by text;",
  "COMMENT ON COLUMN pg_diff_sync_test.people.first_name IS 'Person''s first name';",
  "COMMENT ON COLUMN pg_diff_sync_test.people.age IS 'Age in years';",
  "COMMENT ON COLUMN pg_diff_sync_test.people._created IS 'Timestamp for when this record was created';",
  "COMMENT ON COLUMN pg_diff_sync_test.people._created_by IS 'UserID that created this record (if known)';",
  "COMMENT ON COLUMN pg_diff_sync_test.people._modified IS 'Timestamp for when this record was last updated';",
  "COMMENT ON COLUMN pg_diff_sync_test.people._modified_by IS 'UserID that last modified this record (if known)';",
  "ALTER TABLE pg_diff_sync_test.people ADD PRIMARY KEY (employee_no);",
  "CREATE UNIQUE INDEX people_first_name_last_name_idx ON pg_diff_sync_test.people (first_name,last_name);"
]

Testing

$ npm test

License

MIT

1.30.0

11 months ago

1.31.0

11 months ago

1.29.0

1 year ago

1.28.0

1 year ago

1.26.0

2 years ago

1.27.0

2 years ago

1.25.0

3 years ago

1.24.0

3 years ago

1.23.0

4 years ago

1.22.0

4 years ago

1.21.0

4 years ago

1.20.0

4 years ago

1.19.1

4 years ago

1.19.0

4 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.2

5 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago