ddl-manager
Version:
store postgres procedures and triggers in files
73 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexComparator = void 0;
const AbstractComparator_1 = require("./AbstractComparator");
const Index_1 = require("../database/schema/Index");
const Comment_1 = require("../database/schema/Comment");
const lodash_1 = require("lodash");
class IndexComparator extends AbstractComparator_1.AbstractComparator {
drop() {
const allCacheIndexes = lodash_1.flatMap(this.database.tables, table => table.indexes).filter(index => index.comment &&
index.comment.cacheSignature);
for (const dbIndex of allCacheIndexes) {
if (this.existsSameIndexInFS(dbIndex)) {
continue;
}
this.migration.drop({
indexes: [dbIndex]
});
}
}
create() {
for (const file of this.fs.files) {
this.createNewIndexes(file.content.cache);
}
}
existsSameIndexInFS(dbIndex) {
const cachesForThatTable = lodash_1.flatMap(this.fs.files, file => file.content.cache).filter(cache => cache.for.table.equal(dbIndex.table));
for (const cache of cachesForThatTable) {
for (const cacheIndex of cache.indexes) {
const fsIndex = cacheIndexToDbIndex(cache, cacheIndex);
if (fsIndex.equal(dbIndex)) {
return true;
}
}
}
return false;
}
createNewIndexes(caches) {
for (const cache of caches) {
const cacheIndexes = cache.indexes || [];
const indexes = cacheIndexes
.map((cacheIndex) => cacheIndexToDbIndex(cache, cacheIndex))
.filter((fsIndex) => !this.existsSameIndexInDB(fsIndex));
this.migration.create({
indexes
});
}
}
existsSameIndexInDB(fsIndex) {
const dbTable = this.database.getTable(fsIndex.table);
const dbIndexes = dbTable && dbTable.indexes || [];
for (const dbIndex of dbIndexes) {
if (dbIndex.equal(fsIndex)) {
return true;
}
}
return false;
}
}
exports.IndexComparator = IndexComparator;
function cacheIndexToDbIndex(cache, cacheIndex) {
return new Index_1.Index({
name: `${cache.for.table.name}_${cacheIndex.on.join("_")}_cidx`,
table: cache.for.table,
index: cacheIndex.index,
columns: cacheIndex.on.map(elem => elem.toString()),
comment: Comment_1.Comment.fromFs({
objectType: "index",
cacheSignature: cache.getSignature()
})
});
}
//# sourceMappingURL=IndexComparator.js.map