Open sumit-anantwar opened 2 weeks ago
After some investigation, we found that the migrations are being executed inside an ExecContext
https://github.com/golang-migrate/migrate/blob/2477f639fdba30c69a3b2dee3c788d4877cb2e97/database/sqlserver/sqlserver.go#L236
And it seems that the ALTER TABLE
changes aren't applied until the ExecContext
block exits.
In which case, the new columns don't yet exist when the update
is executed.
So, we tried to change the context for the update
statement
Putting the update
statement inside an SQL EXEC
block did the trick.
EXEC('UPDATE pass SET date_created = start_date, date_updated = start_date')
Iit would be great if someone could confirm if this is the correct approach.
Describe the Bug We need to add two columns to a table and then populate the new columns with some data. Doing this in a single migration fails with error
Invalid column name
Steps to Reproduce Steps to reproduce the behavior:
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pass' AND COLUMN_NAME = 'date_updated') BEGIN ALTER TABLE [pass] ADD date_updated datetime NOT NULL DEFAULT (GETDATE()); END
UPDATE [pass] SET date_created = start_date, date_updated = start_date;
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pass' AND COLUMN_NAME = 'date_updated') BEGIN ALTER TABLE [pass] ADD date_updated datetime NOT NULL DEFAULT (GETDATE()); END
UPDATE [pass] SET date_created = start_date, date_updated = start_date;
(details: mssql: Invalid column name 'date_updated'.)