using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exferia_EntityFramework.Script_Actualizacion
{
    class Scripts_Version_1365
    {
        public static Version G_VRS_VERSION = new Version("1.3.6.5");
        public static long G_LNG_VERSION = 1365;
        public static int G_INT_TIPO = 0;
        public static string G_STR_SCRIPT =
                                    //CREAR TABLA PRV_LicenciaDatos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'PRV_LicenciaDatos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE PRV_LicenciaDatos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idCliente] [bigint] NOT NULL, " +
                                            " [nombreComercial] [varchar](250) NOT NULL, " +
                                            " [razonSocial] [varchar](250) NOT NULL, " +
                                         " CONSTRAINT[PK_PRV_LicenciaDatos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                             " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CAMBIAR RELACION EN LA TABLA DE FK_MAE_Clientes_Terminales_Sincronizacion_MAE_Clientes_Terminales 
                                    " IF exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS " +
                                        " WHERE (CONSTRAINT_NAME = 'FK_MAE_Clientes_Terminales_Sincronizacion_MAE_Clientes_Terminales') " +
                                    " )  " +
                                    " BEGIN " +
                                        //Borrar relación antigua
                                        " ALTER TABLE MAE_Clientes_Terminales_Sincronizacion DROP CONSTRAINT[FK_MAE_Clientes_Terminales_Sincronizacion_MAE_Clientes_Terminales] " +

                                        //Crear la Nueva
                                        " ALTER TABLE MAE_Clientes_Terminales_Sincronizacion WITH CHECK ADD CONSTRAINT[FK_MAE_Clientes_Terminales_Sincronizacion_MAE_Clientes_Terminales_1] FOREIGN KEY(idClienteTerminal) REFERENCES[MAE_Clientes_Terminales] (id) ON DELETE CASCADE " +
                                        " ALTER TABLE MAE_Clientes_Terminales_Sincronizacion CHECK CONSTRAINT[FK_MAE_Clientes_Terminales_Sincronizacion_MAE_Clientes_Terminales_1] " +
                                    " END " +
                                     //AÑADIR campo idTerminal a la tabla MAE_Clientes_Terminales
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='idTerminal' AND TABLE_NAME = 'MAE_Clientes_Terminales' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Clientes_Terminales]ADD[idTerminal] bigint NULL  " +
                                        //Relaciones
                                        " ALTER TABLE[MAE_Clientes_Terminales] WITH CHECK ADD  CONSTRAINT[FK_MAE_Clientes_Terminales_GEN_Terminales] FOREIGN KEY([idTerminal]) REFERENCES[GEN_Terminales] ([id]) " +
                                        " ALTER TABLE[MAE_Clientes_Terminales] CHECK CONSTRAINT[FK_MAE_Clientes_Terminales_GEN_Terminales] " +
                                    "END ";











    }

}
