Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dbdiff
# dbdiff-pg-upgraded

Compares two databases and prints SQL commands to modify the first one in order to match the second one.

Expand All @@ -11,16 +11,16 @@ It supports PostgreSQL and MySQL.
Install globally with `npm`

```
npm install dbdiff -g
npm install dbdiff-pg-upgraded -g
```

# CLI Usage

```
dbdiff \
dbdiff-pg-upgraded \
-l safe
dialect://user:pass@host[:port]/dbname1 \
dialect://user:pass@host[:port]/dbname2
dbdiff-pg-upgraded://user:pass@host[:port]/dbname1 \
dbdiff-pg-upgraded://user:pass@host[:port]/dbname2
```

Where `dialect` can be either `postgres` or `mysql`. The first database url denotes the target, the second the source, the sql queries will allow target to be updated to source state.
Expand Down Expand Up @@ -55,10 +55,10 @@ ALTER TABLE table_name

# Usage as a library

You can use `dbdiff` as a library:
You can use `dbdiff-pg-upgraded` as a library:

```javascript
var dbdiff = require('dbdiff')
var dbdiff = require('dbdiff-pg-upgraded')

dbdiff.describeDatabase(connString)
.then((schema) => {
Expand Down
63 changes: 31 additions & 32 deletions dialects/postgres-client.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
var pg = require('pg')
var querystring = require('querystring')

const { Pool } = require("pg");
const DROP_SCHEMA_QUERY = "drop schema public cascade; create schema public;";
class PostgresClient {
constructor (conOptions) {
this.conOptions = conOptions
constructor(connOptions) {
this.connconnOptions = connOptions;
}

dropTables () {
return this.query('drop schema public cascade; create schema public;')
buildPool() {
this.pool = new Pool(this.connconnOptions);
return this.pool;
}

connect () {
return new Promise((resolve, reject) => {
if (this.client) return resolve()
pg.connect(this.conOptions, (err, client, done) => {
if (err) return reject(err)
this.client = client
this.done = done
resolve()
})
})
dropTables() {
return this.query(DROP_SCHEMA_QUERY);
}

query (sql, params = []) {
return this.connect()
.then(() => {
return new Promise((resolve, reject) => {
this.client.query(sql, params, (err, result) => {
this.done()
err ? reject(err) : resolve(result)
})
})
})
async connect() {
if (!this.pool) {
this.buildPool();
const pgClient = await this.pool.connect();

return pgClient;
}
const pgClient = await this.pool.connect();

return pgClient;
}

find (sql, params = []) {
return this.query(sql, params).then((result) => result.rows)
async query(sql, params = []) {
const pgClient = await this.connect();
const resultSet = await pgClient.query(sql, params);
const shouldDestroy = true;
pgClient.release(shouldDestroy);
return resultSet;
}

findOne (sql, params = []) {
return this.query(sql, params).then((result) => result.rows[0])
find(sql, params) {
return this.query(sql, params).then((result) => result.rows);
}
findOne(sql, params) {
return this.query(sql, params).then((result) => result.rows[0]);
}
}

module.exports = PostgresClient
module.exports = PostgresClient;
Loading