2025-07-03 10:59:17 -04:00
|
|
|
import { compareIndexes } from 'src/sql-tools/comparers/index.comparer';
|
2025-04-07 15:12:12 -04:00
|
|
|
import { DatabaseIndex, Reason } from 'src/sql-tools/types';
|
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
|
|
|
|
const testIndex: DatabaseIndex = {
|
|
|
|
|
name: 'test',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
columnNames: ['column1', 'column2'],
|
|
|
|
|
unique: false,
|
|
|
|
|
synchronize: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('compareIndexes', () => {
|
|
|
|
|
describe('onExtra', () => {
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(compareIndexes.onExtra(testIndex)).toEqual([
|
|
|
|
|
{
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'IndexDrop',
|
2025-04-07 15:12:12 -04:00
|
|
|
indexName: 'test',
|
|
|
|
|
reason: Reason.MissingInSource,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onMissing', () => {
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(compareIndexes.onMissing(testIndex)).toEqual([
|
|
|
|
|
{
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'IndexCreate',
|
2025-04-07 15:12:12 -04:00
|
|
|
index: testIndex,
|
|
|
|
|
reason: Reason.MissingInTarget,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onCompare', () => {
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(compareIndexes.onCompare(testIndex, testIndex)).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should drop and recreate when column list is different', () => {
|
|
|
|
|
const source = {
|
|
|
|
|
name: 'test',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
columnNames: ['column1'],
|
|
|
|
|
unique: true,
|
|
|
|
|
synchronize: true,
|
|
|
|
|
};
|
|
|
|
|
const target = {
|
|
|
|
|
name: 'test',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
columnNames: ['column1', 'column2'],
|
|
|
|
|
unique: true,
|
|
|
|
|
synchronize: true,
|
|
|
|
|
};
|
|
|
|
|
expect(compareIndexes.onCompare(source, target)).toEqual([
|
|
|
|
|
{
|
|
|
|
|
indexName: 'test',
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'IndexDrop',
|
2025-04-07 15:12:12 -04:00
|
|
|
reason: 'columns are different (column1 vs column1,column2)',
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'IndexCreate',
|
2025-04-07 15:12:12 -04:00
|
|
|
index: source,
|
|
|
|
|
reason: 'columns are different (column1 vs column1,column2)',
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|