예제 #1
0
파일: Program.cs 프로젝트: WillPK/SqlDeploy
        private static void ProcessScript(KeyValuePair <int, FileInfo> sqlScript)
        {
            var fileName = sqlScript.Value.Name;

            try
            {
                // load
                var script = File.ReadAllText(sqlScript.Value.FullName);

                if (string.IsNullOrEmpty(script))
                {
                    throw new ApplicationException("Script is empty");
                }

                // run
                SqlCommandExecutor.ExecuteNonQuery(script);

                // add version
                VersionTableManager.InsertVersion(sqlScript.Key);

                Console.WriteLine($"Ran script successfully: '{fileName}'");
            }
            catch (Exception ex)
            {
                throw new ApplicationException($"Failed to run: '{fileName}'", ex);
            }
        }
예제 #2
0
        private bool Exists()
        {
            Console.WriteLine($"Version table name is {sqlScripts.VersionTableName}");

            var result = SqlCommandExecutor.ExecuteScalar(sqlScripts.CheckExists);

            if (result == null || result == DBNull.Value)
            {
                return(false);
            }

            return(true);
        }
예제 #3
0
        public void InsertVersion(int version)
        {
            var versionParam = new SqlParameter("@Version", SqlDbType.BigInt)
            {
                Value = version
            };

            var dateParam = new SqlParameter("@Date", SqlDbType.DateTime)
            {
                Value = DateTime.UtcNow
            };

            SqlCommandExecutor.ExecuteNonQuery(sqlScripts.Insert, versionParam, dateParam);
        }
예제 #4
0
        public void EnsureExists()
        {
            if (Exists())
            {
                Console.WriteLine("Version table exists.");
                return;
            }

            Console.WriteLine("Version table does not exists, creating...");

            SqlCommandExecutor.ExecuteNonQuery(sqlScripts.Create);

            Console.WriteLine("Created the version table successfully.");
        }
예제 #5
0
        public int?GetLastVersion()
        {
            var result = SqlCommandExecutor.ExecuteScalar(sqlScripts.GetLatest);

            if (result == null || result == DBNull.Value)
            {
                Console.WriteLine("No existing version number was found.");
                return(null);
            }

            int lastVersion = Convert.ToInt16(result);;

            Console.WriteLine("Latest version: " + lastVersion);

            return(lastVersion);
        }