public virtual IEnumerable <string> DropIdentityForColumn(string columnName, string tableName) { var triggerName = CreateTriggerName(columnName, tableName); using (var writer = FbMigrationSqlGenerator.SqlWriter()) { writer.WriteLine("EXECUTE BLOCK"); writer.WriteLine("AS"); writer.WriteLine("BEGIN"); writer.Indent++; writer.Write("if (exists(select 1 from rdb$triggers where rdb$trigger_name = '"); writer.Write(triggerName); writer.Write("')) then"); writer.WriteLine(); writer.WriteLine("begin"); writer.Indent++; writer.Write("execute statement 'drop trigger "); writer.Write(FbMigrationSqlGenerator.Quote(triggerName)); writer.Write("';"); writer.WriteLine(); writer.Indent--; writer.WriteLine("end"); writer.Indent--; writer.Write("END"); yield return(writer.ToString()); } }
public virtual IEnumerable <string> CreateIdentityForColumn(string columnName, string tableName) { var identitySequenceName = CreateIdentitySequenceName(columnName, tableName); using (var writer = FbMigrationSqlGenerator.SqlWriter()) { writer.WriteLine("EXECUTE BLOCK"); writer.WriteLine("AS"); writer.WriteLine("BEGIN"); writer.Indent++; writer.Write("if (not exists(select 1 from rdb$generators where rdb$generator_name = '"); writer.Write(identitySequenceName); writer.Write("')) then"); writer.WriteLine(); writer.WriteLine("begin"); writer.Indent++; writer.Write("execute statement 'create sequence "); writer.Write(identitySequenceName); writer.Write("';"); writer.WriteLine(); writer.Indent--; writer.WriteLine("end"); writer.Indent--; writer.Write("END"); yield return(writer.ToString()); } using (var writer = FbMigrationSqlGenerator.SqlWriter()) { writer.Write("CREATE OR ALTER TRIGGER "); writer.Write(FbMigrationSqlGenerator.Quote(CreateTriggerName(columnName, tableName))); writer.Write(" ACTIVE BEFORE INSERT ON "); writer.Write(FbMigrationSqlGenerator.Quote(tableName)); writer.WriteLine(); writer.WriteLine("AS"); writer.WriteLine("BEGIN"); writer.Indent++; writer.Write("if (new."); writer.Write(FbMigrationSqlGenerator.Quote(columnName)); writer.Write(" is null) then"); writer.WriteLine(); writer.WriteLine("begin"); writer.Indent++; writer.Write("new."); writer.Write(FbMigrationSqlGenerator.Quote(columnName)); writer.Write(" = next value for "); writer.Write(identitySequenceName); writer.Write(";"); writer.WriteLine(); writer.Indent--; writer.WriteLine("end"); writer.Indent--; writer.Write("END"); yield return(writer.ToString()); } }