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
48 changes: 24 additions & 24 deletions dbdiff.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('underscore')
var _ = require('lodash')
var util = require('util')
var dialects = require('./dialects')
var dedent = require('dedent')
Expand Down Expand Up @@ -162,30 +162,30 @@ class DbDiff {

_compareConstraints (table1, table2) {
var tableName = this._fullName(table2)
table2.constraints.forEach((constraint2) => {
var constraint1 = table1 && table1.constraints.find((cons) => constraint2.name === cons.name)
if (constraint1) {
if (_.isEqual(constraint1, constraint2)) return
if (this._dialect === 'postgres') {
this._safe(`ALTER TABLE ${tableName} DROP CONSTRAINT ${this._quote(constraint2.name)};`)
} else {
this._safe(`ALTER TABLE ${tableName} DROP INDEX ${this._quote(constraint2.name)};`)
}
constraint1 = null

const toCreate = _.differenceWith(table2.constraints, table1.constraints, _.isEqual)
const toDrop = _.differenceWith(table1.constraints, table2.constraints, _.isEqual)

toDrop.forEach(constraint2 => {
if (this._dialect === 'postgres') {
this._safe(`ALTER TABLE ${tableName} DROP CONSTRAINT ${this._quote(constraint2.name)};`)
} else {
this._safe(`ALTER TABLE ${tableName} DROP INDEX ${this._quote(constraint2.name)};`)
}
if (!constraint1) {
var keys = constraint2.columns.map((s) => `${this._quote(s)}`).join(', ')
var func = (table1 ? this._warn : this._safe).bind(this)
var fullName = this._quote(constraint2.name)
if (constraint2.type === 'primary') {
if (this._dialect === 'mysql') fullName = 'foo'
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} PRIMARY KEY (${keys});`)
} else if (constraint2.type === 'unique') {
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} UNIQUE (${keys});`)
} else if (constraint2.type === 'foreign') {
var foreignKeys = constraint2.referenced_columns.map((s) => `${this._quote(s)}`).join(', ')
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} FOREIGN KEY (${keys}) REFERENCES ${this._quote(constraint2.referenced_table)} (${foreignKeys});`)
}
})

toCreate.forEach((constraint2) => {
var keys = constraint2.columns.map((s) => `${this._quote(s)}`).join(', ')
var func = (table1 ? this._warn : this._safe).bind(this)
var fullName = this._quote(constraint2.name)
if (constraint2.type === 'primary') {
if (this._dialect === 'mysql') fullName = 'foo'
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} PRIMARY KEY (${keys});`)
} else if (constraint2.type === 'unique') {
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} UNIQUE (${keys});`)
} else if (constraint2.type === 'foreign') {
var foreignKeys = constraint2.referenced_columns.map((s) => `${this._quote(s)}`).join(', ')
func(`ALTER TABLE ${tableName} ADD CONSTRAINT ${fullName} FOREIGN KEY (${keys}) REFERENCES ${this._quote(constraint2.referenced_table)} (${foreignKeys});`)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "MIT",
"dependencies": {
"dedent": "^0.6.0",
"lodash": "^4.17.5",
"mysql": "^2.10.2",
"pg": "^6.0.0",
"pync": "^1.0.1",
Expand Down