UNPKG

@magic-mustard/sqlsync

Version:

SQLSync simplifies database schema evolution by allowing a declarative approach to table management

80 lines (79 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const alter_table_processor_1 = require("./alter.table.processor"); const namespace_1 = require("./namespace"); // Mock dependencies describe('AlterTablesProcessor', () => { let processor; let mockFile; let mockMigrations; let mockState; let mockContentBetweenFlags; beforeEach(() => { mockFile = { path: 'test.sql', contents: '' }; mockMigrations = { append: jest.fn(), getLatestMigrationName: jest.fn().mockReturnValue('test-migration') }; mockState = { getMigrationState: jest.fn().mockReturnValue({}), updateMigrationState: jest.fn(), getNamespaceState: jest.fn().mockReturnValue({}), updateNamespaceState: jest.fn() }; mockContentBetweenFlags = { extractContentBetweenFlags: jest.fn().mockImplementation(() => []) }; }); describe('process', () => { test('should process ALTER TABLE statements and update state correctly', () => { const statement = 'ALTER TABLE public.test_table ADD COLUMN new_column INT;'; const file = { path: 'test.sql', contents: statement }; const processor = new alter_table_processor_1.AlterTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags); processor.process(); expect(mockMigrations.append).toHaveBeenCalledWith({ token: statement, filePath: file.path, checksum: expect.any(String) }); expect(mockState.updateMigrationState).toHaveBeenCalledWith(namespace_1.Tables.AlterTableStateIndex, expect.objectContaining({ alterTableStatements: expect.objectContaining({ 'public.test_table': expect.arrayContaining([expect.any(String)]) }) })); }); test('should not process if no ALTER TABLE statements are found', () => { const file = { path: 'empty.sql', contents: '-- No ALTER TABLE statements here' }; const processor = new alter_table_processor_1.AlterTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags); processor.process(); expect(mockMigrations.append).not.toHaveBeenCalled(); expect(mockState.updateNamespaceState).not.toHaveBeenCalled(); }); test('extracts ALTER TABLE statements directly from content', () => { const fileContent = ` -- some comment ALTER TABLE public.test_table ADD COLUMN new_column INT; -- another comment INSERT INTO test_table (new_column) VALUES (1); ALTER TABLE public.another_table ADD COLUMN another_column TEXT; `; const file = { path: 'test.sql', contents: fileContent }; const processor = new alter_table_processor_1.AlterTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags); // Test indirectly through public method expect(processor.hasAlterTableStatements()).toBe(true); // Process the file to check if statements are extracted and processed processor.process(); expect(mockMigrations.append).toHaveBeenCalledTimes(2); expect(mockMigrations.append).toHaveBeenCalledWith({ token: expect.stringContaining('ALTER TABLE public.test_table'), filePath: file.path, checksum: expect.any(String) }); expect(mockMigrations.append).toHaveBeenCalledWith({ token: expect.stringContaining('ALTER TABLE public.another_table'), filePath: file.path, checksum: expect.any(String) }); }); }); });