Me pidieron cambiar dos columnas de una tabla, donde ambas columnas son string's sueltos ('Payment_Method' y 'Evaluation_Format', ahora deberían ser FKs numéricas a dos tablas independientes).
Este es mi script de migración:
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ExternalCourses')
BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Payment_Method' and [system_type_id] = 56) --int
BEGIN
ALTER TABLE [dbo].[ExternalCourses] ADD PaymentType int;
END
IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Evaluation_Format' and [system_type_id] = 56)
BEGIN
ALTER TABLE [dbo].[ExternalCourses] ADD EvaluationFormat int;
END
--HOMOGENEIZAR DATOS STRING VIEJOS Y DATOS NUEVOS EN TABLAS
UPDATE [dbo].[ExternalCourses] SET Payment_Method = 'Reembolso' where Payment_Method = 'Otro';
UPDATE [dbo].[ExternalCourses] SET Payment_Method = 'Ninguna' where Payment_Method = 'Ninguno';
UPDATE [dbo].[ExternalCourses] SET Evaluation_Format = 'Hexacta' where Evaluation_Format = 'HX';
UPDATE [dbo].[ExternalCourses] SET Evaluation_Format = 'Con evaluación externa' where Evaluation_Format = 'Cert';
UPDATE [dbo].[ExternalCourses] SET Evaluation_Format = 'Todas' where Evaluation_Format = 'Todos';
IF (EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Evaluation_Format' AND [system_type_id] = 231) --nvarchar
AND EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Payment_Method' AND [system_type_id] = 231))
BEGIN
UPDATE C
SET C.PaymentType = (SELECT P.ID FROM [dbo].[PaymentTypes] P INNER JOIN [dbo].[ExternalCourses] E ON P.Name = E.Payment_Method and E.id = c.id),
C.EvaluationFormat = (SELECT EF.ID FROM [dbo].[EvaluationFormats] EF INNER JOIN [dbo].[ExternalCourses] EC ON EC.Evaluation_Format = EF.Name and EC.id = c.id)
from [dbo].[ExternalCourses] C
END
IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Evaluation_Format' and [system_type_id] = 231)
BEGIN
ALTER TABLE [dbo].[ExternalCourses] DROP COLUMN Evaluation_Format
END
IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Payment_Method' and [system_type_id] = 231 )
BEGIN
ALTER TABLE [dbo].[ExternalCourses] DROP COLUMN Payment_Method
END
IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'PaymentType')
BEGIN
EXEC sp_rename 'ExternalCourses.PaymentType', 'Payment_Method', 'COLUMN';
END
IF EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'EvaluationFormat')
BEGIN
EXEC sp_rename 'ExternalCourses.EvaluationFormat', 'Evaluation_Format', 'COLUMN';
END
IF NOT EXISTS(SELECT 1 FROM sys.foreign_keys WHERE [name] = 'FK_Payment_type')
BEGIN
ALTER TABLE [dbo].[ExternalCourses]
ADD CONSTRAINT FK_Payment_type FOREIGN KEY (Payment_Method) REFERENCES [dbo].[PaymentTypes](Id)
END
IF NOT EXISTS(SELECT 1 FROM sys.foreign_keys WHERE [name] = 'FK_Evaluation_format')
BEGIN
ALTER TABLE [dbo].[ExternalCourses]
ADD CONSTRAINT FK_Evaluation_format FOREIGN KEY (Evaluation_Format) REFERENCES [dbo].[EvaluationFormats](Id)
END
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
DECLARE @ERRORSEVERITYNUMBER INT
SET @ERRORSEVERITYNUMBER = ERROR_SEVERITY()
RAISERROR('Error al migrar datos de paymentType y EvaluationFormat', @ERRORSEVERITYNUMBER, 1)
END CATCH
END
El problema está en:
IF (EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Evaluation_Format' AND [system_type_id] = 231) --nvarchar
AND EXISTS(SELECT 1 FROM sys.columns WHERE [name] = N'Payment_Method' AND [system_type_id] = 231))
BEGIN
UPDATE C
SET C.PaymentType = (SELECT P.ID FROM [dbo].[PaymentTypes] P INNER JOIN [dbo].[ExternalCourses] E ON P.Name = E.Payment_Method and E.id = c.id),
C.EvaluationFormat = (SELECT EF.ID FROM [dbo].[EvaluationFormats] EF INNER JOIN [dbo].[ExternalCourses] EC ON EC.Evaluation_Format = EF.Name and EC.id = c.id)
from [dbo].[ExternalCourses] C
END
Donde C.PaymentType
es el nombre provisorio de la columna de int, esta sentencia es para migrar y sólo se ejecuta si existen las columnas iniciales de tipo string, el if funciona porque lo probé pero aún así me dice que C.PaymentType
y C.EvaluationFormat
no existen, y claro, inicialmente no existen pero primero se crean.
Si ejecuto el script por partes la migración está correcta, no sé que puede pasar.