private void CreateOrAlterDataFile()

in src/Microsoft.SqlTools.ServiceLayer/Admin/Database/CreateDatabaseObjects.cs [1347:1498]


        private void CreateOrAlterDataFile(Database db)
        {
            // note: This method looks strikingly similar to CreateLogFile(), below.
            // 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.

            // form the file path, create the data file
            string fileSuffix = this.IsPrimaryFile ? ".mdf" : ".ndf";
            DataFile file = null;
            FileGroup fg = db.FileGroups[this.FileGroup.Name];

            if (this.Exists)
            {
                file = db.FileGroups[this.FileGroup.Name].Files[this.originalState.name];

                if (!file.Name.Equals(this.Name))
                {
                    file.Rename(this.Name);
                }
            }
            else
            {
                file = new DataFile(fg, this.Name);
                fg.Files.Add(file);
            }

            if (this.IsPrimaryFile && !this.Exists)
            {
                file.IsPrimaryFile = true;
            }

            // set the file name (you can't change the file name after the file has been created)
            if (!this.Exists)
            {
                file.FileName = MakeDiskFileName(this.Name, this.PhysicalName, fileSuffix);
            }

            // 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 (!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;
                }
            }
        }