private void CreateOrAlterLogFile()

in src/Microsoft.SqlTools.ServiceLayer/Admin/Database/CreateDatabaseObjects.cs [1577:1722]


        private void CreateOrAlterLogFile(Database db)
        {
            // note: This method looks strikingly similar to CreateDataFile(), above.
            // The initialization code can't be shared because LogFile and DataFile have
            // no common base class, so we can't use polymorphism to our advantage.

            LogFile file = null;

            if (this.Exists)
            {
                file = db.LogFiles[this.originalState.name];

                if (!file.Name.Equals(this.Name))
                {
                    file.Rename(this.Name);
                }
            }
            else
            {
                file = new LogFile(db, this.Name);
                db.LogFiles.Add(file);
            }

            // set its path and initial size
            if (!this.Exists)
            {
                file.FileName = MakeDiskFileName(this.Name, this.PhysicalName, ".ldf");
            }

            // set its initial size
            double originalSize = this.originalState.initialSize;
            double newSize = this.currentState.initialSize;

            // if the file does not exist or if the existing file size was changed in the UI and is 
            // significantly larger than the value on the server, set the file size
            if (!this.Exists || ((1e-6 < Math.Abs(newSize - originalSize)) && (1e-6 < (newSize - file.Size))))
            {
                file.Size = newSize;
            }
            else if ((newSize < originalSize) && (newSize < file.Size))
            {
                file.Shrink(KilobytesToMegabytes(newSize), ShrinkMethod.Default);
            }


            // if auto-growth is enabled, set auto-growth data
            if (!this.Exists)
            {
                // if auto-growth is enabled, set auto-growth data
                if (this.Autogrowth.IsEnabled)
                {
                    if (this.Autogrowth.IsGrowthRestricted)
                    {
                        file.MaxSize = this.Autogrowth.MaximumFileSizeInKilobytes;
                    }

                    if (this.Autogrowth.IsGrowthInPercent)
                    {
                        file.GrowthType = FileGrowthType.Percent;
                        file.Growth = (double) this.Autogrowth.GrowthInPercent;
                    }
                    else
                    {
                        file.GrowthType = FileGrowthType.KB;
                        file.Growth = this.Autogrowth.GrowthInKilobytes;
                    }
                }
                else
                {
                    file.GrowthType = FileGrowthType.None;
                }
            }
            else
            {
                FileGrowthType newFileGrowthType = FileGrowthType.None;
                FileGrowthType originalFileGrowthType = FileGrowthType.None;
                double newGrowth = 0.0;
                double originalGrowth = 0.0;
                double newMaxSize = 0.0;
                double originalMaxSize = 0.0;

                if (this.currentState.autogrowth.IsEnabled)
                {
                    if (this.currentState.autogrowth.IsGrowthRestricted)
                    {
                        newMaxSize = this.currentState.autogrowth.MaximumFileSizeInKilobytes;
                    }

                    if (this.currentState.autogrowth.IsGrowthInPercent)
                    {
                        newFileGrowthType = FileGrowthType.Percent;
                        newGrowth = (double) this.currentState.autogrowth.GrowthInPercent;
                    }
                    else
                    {
                        newFileGrowthType = FileGrowthType.KB;
                        newGrowth = this.currentState.autogrowth.GrowthInKilobytes;
                    }
                }

                if (this.originalState.autogrowth.IsEnabled)
                {
                    if (this.originalState.autogrowth.IsGrowthRestricted)
                    {
                        originalMaxSize = this.originalState.autogrowth.MaximumFileSizeInKilobytes;
                    }

                    if (this.originalState.autogrowth.IsGrowthInPercent)
                    {
                        originalFileGrowthType = FileGrowthType.Percent;
                        originalGrowth = (double) this.originalState.autogrowth.GrowthInPercent;
                    }
                    else
                    {
                        originalFileGrowthType = FileGrowthType.KB;
                        originalGrowth = this.originalState.autogrowth.GrowthInKilobytes;
                    }
                }

                // if file growth type has changed in the UI and is different from the value on the server
                if ((newFileGrowthType != originalFileGrowthType) && (file.GrowthType != newFileGrowthType))
                {
                    // change the file growth type
                    file.GrowthType = newFileGrowthType;
                }

                // if the growth amount has changed in the UI and is different from the value on the server
                if ((1e-6 < (Math.Abs(newGrowth - originalGrowth))) && (1e-6 < Math.Abs(newGrowth - file.Growth)))
                {
                    // change the growth amount
                    file.Growth = newGrowth;
                }

                // when file size is unlimited, MaxSize is non-positive.  For the
                // purposes of our code, convert non-positive to zero, then compare
                // vs. the prototype file size.
                double fileMaxSize = (0.0 <= file.MaxSize) ? file.MaxSize : 0.0;

                // if the max size has changed in the UI and is different from the value on the server
                if ((1e-6 < Math.Abs(originalMaxSize - newMaxSize)) && (1e-6 < Math.Abs(fileMaxSize - newMaxSize)))
                {
                    // set the file max size
                    file.MaxSize = newMaxSize;
                }
            }
        }