DotNetMigrations.Migrations.MigrationScriptFile.Read C# (CSharp) Method

Read() public method

Parses and returns the contents of the migration script file.
public Read ( ) : MigrationScriptContents
return MigrationScriptContents
        public MigrationScriptContents Read()
        {
            string setupScript = string.Empty;
            string teardownScript = string.Empty;

            string allLines = File.ReadAllText(FilePath);

            Match setupMatch = SetupRegex.Match(allLines);
            if (setupMatch.Success)
            {
                setupScript = setupMatch.Groups[1].Value;
            }

            // don't include the setup portion of the script
            // when matching the teardown
            Match teardownMatch = TeardownRegex.Match(allLines, setupMatch.Length);
            if (teardownMatch.Success)
            {
                teardownScript = teardownMatch.Groups[1].Value;
            }

            if (!setupMatch.Success && !teardownMatch.Success)
            {
                // assume entire file is the setup and there is no teardown
                setupScript = allLines;
            }

            return new MigrationScriptContents(setupScript, teardownScript);
        }

Usage Example

        public void Read_should_assume_entire_script_is_the_setup_if_no_tags_are_present()
        {
            //  arrange
            var tempFilePath = Path.GetTempFileName();
            using (DisposableFile.Watch(tempFilePath))
            {
                File.WriteAllText(tempFilePath, "this is the setup");
                var file = new MigrationScriptFile(tempFilePath);

                //  act
                var contents = file.Read();

                //  assert
                Assert.AreEqual("this is the setup", contents.Setup, "setup not parsed correctly");
                Assert.AreEqual(string.Empty, contents.Teardown, "teardown should be empty");
            }
        }
All Usage Examples Of DotNetMigrations.Migrations.MigrationScriptFile::Read