I am currently evaluating the use of RedGate SQL Source Control and SQLCompare for our Continuous Integration initiative. Starting out everything went very smoothly, it was easy to Add/Drop tables, Add nullable columns, drop columns, and so on without any problem. That is until I started looking at migration scripts. The simple example I was trying out was making a nullable column non nullable.
Our process: Change is made in shared development SQL Server -> Change is checked into source control -> Deploy to test environment by comparing source control with test database.
Setup:
- Source Control: GIT
- Model: Shared - All Developers work on the same database
- Temporary database: LocalDB - Developers do not have ability to create databases on servers
- Use the source controlled as the "truth center" because someone might be in the middle of a change on the Development Server. Unintended/unfinished changes might be pushed up.
Sample migration script:
DECLARE @ShouldRunMigrationScript BIT
SET @ShouldRunMigrationScript = 1
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA = 'dbo' AND table_name='Test' AND COLUMN_NAME = 'testColumn' AND IS_NULLABLE = 'No')
BEGIN
SET @ShouldRunMigrationScript = 0;
PRINT 'Column [testColumn] in [dbo].[Test] is already not nullable - skipping migration';
END
IF @ShouldRunMigrationScript = 1
BEGIN
UPDATE [dbo].[Test] SET testColumn = anotherTestColumn WHERE TestColumn IS NULL;
ALTER TABLE [dbo].[Test] ALTER COLUMN [testColumn] VARCHAR(500) NOT NULL;
END
I run SQL Compare from the command prompt:
"C:\Program Files (x86)\Red Gate\SQL Compare 10\sqlcompare" /scripts1:"[Folder where SQL Source Control Saves]" /Server2:[Test SQL Server] /Database2:[Test Database] /scriptfile:"c:\Migrations.txt" /f /Options:Defaults,UseMigrationsV2
It gives me this error "Error while running migration script: Invalid object name 'dbo.Test.'
Both development and test SQL servers have dbo.Test. I'm not dropping the column or the table in any of my scripts. Does anybody have any idea why that would happen?