/// <summary> /// Drops file groups from the database /// </summary> /// <param name="dto">The SMO database object.</param> private static void DropFileGroups(smo::Database dto) { while (dto.FileGroups.Count > 0) { dto.FileGroups[0].Drop(); } }
/// <summary> /// Drops log files from the database. /// </summary> /// <param name="dto">The SMO database object.</param> private static void DropLogFiles(smo::Database dto) { while (dto.LogFiles.Count > 0) { dto.LogFiles[0].Drop(); } }
private FileGroup DiscoverLogFileGroup(smo::Database smodb, FileGroup fg) { if (fg == null) { fg = new FileGroup(this); } fg.Name = "LOG"; fg.FileGroupName = "LOG"; fg.FileGroupType = FileGroupType.Log; fg.AllocationType = FileGroupAllocationType.CrossVolume; fg.LayoutType = FileGroupLayoutType.Monolithic; fg.DiskVolumeType = DiskVolumeType.Log; fg.FileCount = 0; // TODO: check this to be one file/volume fg.DeploymentState = Registry.DeploymentState.Deployed; fg.RunningState = Registry.RunningState.Running; // Calculate log size fg.AllocatedSpace = 0; foreach (var smolf in smodb.LogFiles.Cast <smo::LogFile>()) { fg.AllocatedSpace += (long)Math.Ceiling(smolf.Size * 0x400L); // given in KB, bug in docs! } fg.AllocatedSpace = Math.Max(0x1000000L, fg.AllocatedSpace); // 16 MB minimum return(fg); }
/// <summary> /// Drops a database. /// </summary> /// <param name="databaseInstance">The database instance cluster schema object.</param> public override void Undeploy() { // Make sure it's new or undeployed if (this.DeploymentState != DeploymentState.Deployed) { throw new DeployException(ExceptionMessages.CannotUndeployDatabase); } // Change deployment state to deploying this.DeploymentState = DeploymentState.Undeploying; this.Save(); switch ((RunningState)this.RunningState) { case RunningState.Attached: // Drop database using SMO smo::Database d = this.GetSmoDatabase(); d.Parent.KillDatabase(d.Name); this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.Undeploy[Drop database]", this.Guid)); break; case RunningState.Detached: // Database not attached, simply delete files this.LoadFileGroups(false); foreach (DatabaseInstanceFileGroup fg in this.FileGroups.Values) { fg.LoadAllChildren(); foreach (DatabaseInstanceFile f in fg.Files.Values) { File.Delete(f.GetFullLocalFilename()); } } this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.Undeploy[Delete files]", this.Guid)); break; default: throw new NotImplementedException(); } // Change deployment state to undeployed this.DeploymentState = DeploymentState.Undeployed; this.RunningState = (int)RunningState.Unknown; this.Save(); this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.Undeploy[Done]", this.Guid)); }
private void Discover(smo::Database smodb, List <Entity> update, List <Entity> delete, List <Entity> create) { InitializeDiscovery(update, delete, create); if (smodb != null) { LoadFromSmo(smodb); // Process file groups // --- add standard file groups foreach (var smofg in smodb.FileGroups.Cast <smo::FileGroup>()) { var fg = FileGroups.Values.FirstOrDefault(i => Entity.StringComparer.Compare(i.FileGroupName, smofg.Name) == 0); if (fg == null) { fg = new DatabaseInstanceFileGroup(this); } fg.DiscoverFileGroup(smofg, update, delete, create); } // --- add dummy file group for logs { var fg = FileGroups.Values.FirstOrDefault(i => i.FileGroupType == FileGroupType.Log); if (fg == null) { fg = new DatabaseInstanceFileGroup(this); } fg.DiscoverLogFileGroup(smodb, update, delete, create); } DiscoverDeletedFileGroups(update, delete, create); } else { foreach (var fg in FileGroups.Values) { fg.DiscoverFileGroup(null, update, delete, create); } } }
internal void DiscoverLogFileGroup(smo::Database smodb, List <Entity> update, List <Entity> delete, List <Entity> create) { InitializeDiscovery(update, delete, create); LoadFromSmo(smodb); DiscoverDatabaseDefinitionFileGroup(); foreach (var smofile in smodb.LogFiles.Cast <smo::LogFile>()) { var file = Files.Values.FirstOrDefault(i => Entity.StringComparer.Compare(i.LogicalName, smofile.Name) == 0); if (file == null) { file = new DatabaseInstanceFile(this); } file.DiscoverLogFile(smofile, update, delete, create); } DiscoverDeletedFiles(update, delete, create); }
internal void LoadFromSmo(smo::Database smodb) { this.Name = "LOG"; this.FileGroupName = "LOG"; this.FileGroupType = FileGroupType.Log; this.PartitionReference.Guid = Guid.Empty; this.DeploymentState = Registry.DeploymentState.Deployed; this.RunningState = Registry.RunningState.Running; // Calculate log size this.AllocatedSpace = 0; this.UsedSpace = 0; this.ReservedSpace = 0; foreach (var smolf in smodb.LogFiles.Cast <smo::LogFile>()) { this.AllocatedSpace += (long)Math.Ceiling(smolf.Size * 0x400L); // given in KB, bug in docs! this.UsedSpace += (long)Math.Ceiling(smolf.UsedSpace * 0x400L); this.ReservedSpace += smolf.MaxSize == -1 ? 0L : (long)Math.Ceiling(smolf.MaxSize * 0x400L); } }
/// <summary> /// Allocates database files and creates file groups without copying /// database objects /// </summary> /// <param name="databaseInstance">The database instance object.</param> public override void Deploy() { // Make sure it's new or undeployed if (this.DeploymentState != DeploymentState.New && this.DeploymentState != DeploymentState.Undeployed) { throw new DeployException(ExceptionMessages.CannotDeployDatabase); } // Change deployment state to deploying this.DeploymentState = DeploymentState.Deploying; this.Save(); // Get SMO object to the target database smo::Server sto = this.ServerInstance.GetSmoServer(); smo::Database dto = new smo::Database(sto, this.DatabaseName); // Important non-default settings dto.RecoveryModel = smo.RecoveryModel.Simple; dto.Collation = "SQL_Latin1_General_CP1_CI_AS"; // --- Delete old LogFiles, FileGroups and create new ones --- DropLogFiles(dto); DropFileGroups(dto); this.CreateFileGroups(dto); // Create the empty database with filegroups and files dto.Create(); this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.Deploy[Create database]", this.Guid)); // Change deployment state to deployed this.DeploymentState = DeploymentState.Deployed; this.RunningState = RunningState.Attached; this.Save(); this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.Deploy[Done]", this.Guid)); }
/* *** TODO: delete /// <summary> /// Adds files to a database based on the cluster schema information. /// </summary> /// <param name="databaseInstance">The database instance object.</param> /// <param name="dto">The SMO object pointing to the target database.</param> private void CreateFiles(smo::Database dto) { this.LoadFiles(false); foreach (DatabaseInstanceFile f in this.Files.Values) { f.DeploymentState = DeploymentState.Deploying; f.Save(); string localFilename = f.GetFullLocalFilename(); string networkFilename = f.GetFullUncFilename(); // Check directory string dir = Path.GetDirectoryName(networkFilename); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // Create new file logfile if (f.DatabaseFileType == DatabaseFileType.Log) { smo::LogFile lf = new smo::LogFile(dto, f.Filename); lf.FileName = localFilename; lf.Growth = 0; lf.GrowthType = smo::FileGrowthType.None; //nf.MaxSize = (double)fi.AllocatedSpace / 0x400; // in kilobytes lf.Size = (double)f.AllocatedSpace / 0x400; // in kilobytes dto.LogFiles.Add(lf); } f.DeploymentState = DeploymentState.Deployed; f.Save(); } }*/ /// <summary> /// Adds file group to a database based on the cluster schema information. /// </summary> /// <param name="databaseInstance">The database instance object.</param> /// <param name="dto">The SMO object pointing to the target database.</param> private void CreateFileGroups(smo::Database dto) { // Add FileGroups this.LoadFileGroups(false); foreach (DatabaseInstanceFileGroup fg in this.FileGroups.Values) { fg.DeploymentState = DeploymentState.Deploying; fg.Save(); if (fg.FileGroupType == FileGroupType.Log) { fg.LoadFiles(false); foreach (DatabaseInstanceFile fi in fg.Files.Values) { fi.DeploymentState = DeploymentState.Deploying; fi.Save(); string localFilename = fi.GetFullLocalFilename(); string networkFilename = fi.GetFullUncFilename(); // Check directory string dir = Path.GetDirectoryName(networkFilename); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // Create new File smo::LogFile lf = new smo::LogFile(dto, fi.LogicalName); lf.FileName = localFilename; lf.Growth = 0; lf.GrowthType = smo::FileGrowthType.None; //nf.MaxSize = (double)fi.AllocatedSpace / 0x400; // in kilobytes lf.Size = (double)fi.AllocatedSpace / 0x400; // in kilobytes dto.LogFiles.Add(lf); fi.DeploymentState = DeploymentState.Deployed; fi.Save(); } } else if (fg.FileGroupType == FileGroupType.Data) { // Create new File Group smo::FileGroup nfg = new smo::FileGroup(dto, fg.FileGroupName); // Add files to the File Group fg.LoadFiles(false); foreach (DatabaseInstanceFile fi in fg.Files.Values) { fi.DeploymentState = DeploymentState.Deploying; fi.Save(); string localFilename = fi.GetFullLocalFilename(); string networkFilename = fi.GetFullUncFilename(); // Check directory string dir = Path.GetDirectoryName(networkFilename); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // Create new File smo::DataFile nf = new smo::DataFile(nfg, fi.LogicalName); nf.FileName = localFilename; nf.Growth = 0; nf.GrowthType = smo::FileGrowthType.None; //nf.MaxSize = (double)fi.AllocatedSpace / 0x400; // in kilobytes nf.Size = (double)fi.AllocatedSpace / 0x400; // in kilobytes nfg.Files.Add(nf); fi.DeploymentState = DeploymentState.Deployed; fi.Save(); } // Add new File Group to the Database dto.FileGroups.Add(nfg); } else { throw new NotImplementedException(); } fg.DeploymentState = DeploymentState.Deployed; fg.Save(); } this.Context.LogEvent(new Event("Jhu.Graywulf.Registry.DatabaseInstance.CreateFileGroups", this.Guid)); }
public void LoadFromSmo(smo::Database smodb) { this.DatabaseName = smodb.Name; }