public bool ModifyColumn(DatabaseContext dc, string tableName, DatabaseColumnStructure newColumnStructure) { if (ToString() != newColumnStructure.ToString()) { // FIXME - We want to add IDENTITY(1,1) property to column if (ToString().IndexOf("IDENTITY(1,1) ") == NotFound && newColumnStructure.ToString().IndexOf("IDENTITY(1,1)") > NotFound) { /*CREATE TABLE dbo.Tmp_Events (ID bigint NOT NULL IDENTITY (1, 1), language_element_id bigint NOT NULL, note nvarchar(200) NULL, checksum nvarchar(400) NULL) ON [PRIMARY] * ALTER TABLE dbo.Tmp_Events SET (LOCK_ESCALATION = TABLE) * SET IDENTITY_INSERT dbo.Tmp_Events ON * IF EXISTS(SELECT * FROM dbo.Events) * EXEC('INSERT INTO dbo.Tmp_Events (ID, language_element_id, note, checksum) * SELECT ID, language_element_id, note, checksum FROM dbo.Events WITH (HOLDLOCK TABLOCKX)') * SET IDENTITY_INSERT dbo.Tmp_Events OFF * DROP TABLE dbo.Events * EXECUTE sp_rename N'dbo.Tmp_Events', N'Events', 'OBJECT' * ALTER TABLE dbo.Events ADD CONSTRAINT PK__Events__3214EC2707020F21 PRIMARY KEY CLUSTERED (ID) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]*/ return(false); //return dc.ExecuteNonQuery(String.Format("SET IDENTITY_INSERT {0} ON"), table_name); //dc.ExecuteQuery(String.Format("ALTER TABLE {0} ALTER COLUMN {1};", table_name, new_column_structure.ToString())); } // FIXME - We want to remove IDENTITY(1,1) property from column if (ToString().IndexOf("IDENTITY(1,1) ") > NotFound && newColumnStructure.ToString().IndexOf("IDENTITY(1,1)") == NotFound) { /*CREATE TABLE dbo.Tmp_Events (ID bigint NOT NULL, language_element_id bigint NOT NULL, note nvarchar(200) NULL, checksum nvarchar(400) NULL) ON [PRIMARY] * ALTER TABLE dbo.Tmp_Events SET (LOCK_ESCALATION = TABLE) * IF EXISTS(SELECT * FROM dbo.Events) * EXEC('INSERT INTO dbo.Tmp_Events (ID, language_element_id, note, checksum) * SELECT ID, language_element_id, note, checksum FROM dbo.Events WITH (HOLDLOCK TABLOCKX)') * DROP TABLE dbo.Events * EXECUTE sp_rename N'dbo.Tmp_Events', N'Events', 'OBJECT' * ALTER TABLE dbo.Events ADD CONSTRAINT PK__Events__3214EC2707020F21 PRIMARY KEY CLUSTERED (ID) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]*/ // Create temporary column /*string column_specification = GetToString("_temporary_column_"); * * if (this.default_value != null) * { * if (this.type == "NVARCHAR") column_specification += String.Format(" DEFAULT '{0}'", this.default_value.ToString()); * else column_specification += String.Format(" DEFAULT {0}", this.default_value.ToString()); * } * * CreateColumn(dc, table_name, column_specification); * dc.ExecuteNonQuery(String.Format("UPDATE Events SET _temporary_column_ = {0};", this.column_name)); */ return(false); //return dc.ExecuteNonQuery(String.Format("SET IDENTITY_INSERT {0} OFF", table_name)); } return(dc.ExecuteNonQuery($"ALTER TABLE {tableName} ALTER COLUMN {newColumnStructure};")); } return(false); }
public void ModifyColumnStructure(DatabaseContext dc, string tableName, DatabaseColumnStructure destinationDcs) { if (this == destinationDcs) { return; } // WARNING - This will recreate the original column //var def_value_str = DefaultValue.ConvertToString(); //var def_value = def_value_str.FirstChar() == '\'' && def_value_str.LastChar() == '\''; if (DefaultValue != destinationDcs.DefaultValue) //if (destination_dcs.DefaultValue != null) { var constraintNames = DatabaseUtils.GetDefaultConstraintNames(dc, tableName, ColumnName); foreach (var constraintName in constraintNames) { dc.ExecuteNonQuery($"ALTER TABLE {tableName} DROP CONSTRAINT {constraintName}"); } // Delete old column, create new with default values DeleteColumn(dc, tableName); CreateColumn(dc, tableName, destinationDcs.ToString()); } else // Modify column type { ModifyColumn(dc, tableName, destinationDcs); } }