public static void Deploy() { ITransaction deploymentTransaction = SQLProviderFactory.GenerateTransaction(); try { HashSet <string> schemas = Instance.schemaObjects.Select(schemaObject => schemaObject.SchemaName).ToHashSet(); foreach (string schema in schemas) { ICreateSchema createSchema = SQLProviderFactory.GetCreateSchemaQuery(); createSchema.SchemaName = schema; createSchema.Execute(deploymentTransaction); } foreach (SchemaObject schemaObject in Instance.schemaObjects) { ICreateTable createTable = SQLProviderFactory.GetCreateTableQuery(); createTable.SchemaName = schemaObject.SchemaName; createTable.TableName = schemaObject.ObjectName; foreach (Field field in schemaObject.GetFields()) { FieldSpecification fieldSpec = new FieldSpecification(field.FieldType, field.DataSize, field.DataScale); if (field == schemaObject.PrimaryKeyField) { fieldSpec.IsPrimary = true; } createTable.Columns.Add(field.FieldName, fieldSpec); } createTable.Execute(deploymentTransaction); } foreach (Relationship relationship in Instance.schemaObjects.SelectMany(so => so.GetRelationships())) { string fkName = $"FK{relationship.ParentSchemaObject.ObjectName}_{relationship.RelatedSchemaObject.ObjectName}_{relationship.ForeignKeyField.FieldName}"; IAlterTable alterTableQuery = SQLProviderFactory.GetAlterTableQuery(); alterTableQuery.Schema = relationship.ParentSchemaObject.SchemaName; alterTableQuery.Table = relationship.ParentSchemaObject.ObjectName; alterTableQuery.AddForeignKey(fkName, relationship.ForeignKeyField.FieldName, relationship.RelatedSchemaObject.SchemaName, relationship.RelatedSchemaObject.ObjectName, relationship.ParentKeyField.FieldName, deploymentTransaction); } deploymentTransaction.Commit(); } finally { if (deploymentTransaction.IsActive) { deploymentTransaction.Rollback(); } } }
public static void UnDeploy() { ITransaction undeploymentTransaction = null; try { undeploymentTransaction = SQLProviderFactory.GenerateTransaction(); foreach (Relationship relationship in Instance.schemaObjects.SelectMany(so => so.GetRelationships())) { string fkName = $"FK{relationship.ParentSchemaObject.ObjectName}_{relationship.RelatedSchemaObject.ObjectName}_{relationship.ForeignKeyField.FieldName}"; IAlterTable alterTableQuery = SQLProviderFactory.GetAlterTableQuery(); alterTableQuery.Schema = relationship.ParentSchemaObject.SchemaName; alterTableQuery.Table = relationship.ParentSchemaObject.ObjectName; alterTableQuery.DropConstraint(fkName, undeploymentTransaction); } foreach (SchemaObject schemaObject in Instance.schemaObjects) { IDropTable dropTable = SQLProviderFactory.GetDropTableQuery(); dropTable.Schema = schemaObject.SchemaName; dropTable.Table = schemaObject.ObjectName; dropTable.Execute(undeploymentTransaction); } HashSet <string> schemas = Instance.schemaObjects.Select(so => so.SchemaName).ToHashSet(); foreach (string schema in schemas) { IDropSchema dropSchema = SQLProviderFactory.GetDropSchemaQuery(); dropSchema.Schema = schema; dropSchema.Execute(undeploymentTransaction); } undeploymentTransaction.Commit(); } finally { if (undeploymentTransaction != null && undeploymentTransaction.IsActive) { undeploymentTransaction.Rollback(); } } }