示例#1
0
        /// <summary>
        /// Applies the provided base file to the database. If the db is already filled with tables, this function will return false
        /// and will not execute anything.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="schemaName"></param>
        /// <param name="baseFilePath"></param>
        /// <returns></returns>
        public async static Task <bool> ApplyBaseFile(this DbContext db, string schemaName, string baseFilePath, int timeOut = 60, bool debugOutput = false)
        {
            Constants.DebugOutput = debugOutput;
            Constants.SQLTimeout  = timeOut;

            if (await MySqlUpdater.GetTableCount(db, schemaName) != 0)
            {
                if (debugOutput)
                {
                    Console.WriteLine("Schema is not empty, skipping!");
                }
                return(true);
            }


            try
            {
                string content = File.ReadAllText(baseFilePath);
                await MySqlUpdater.ExecuteQuery(db, content);

                return(true);
            }
            catch
            {
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// Applies the given single sql file
        /// </summary>
        /// <param name="db"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public async static Task <bool> ApplySQLFile(this DbContext db, string filePath, bool hashSumTracking = true, int timeOut = 60, bool debugOutput = false)
        {
            if (timeOut <= 0)
            {
                timeOut = 60;
            }


            Constants.DebugOutput     = debugOutput;
            Constants.SQLTimeout      = timeOut;
            Constants.HashSumTracking = hashSumTracking;

            string ext = Path.GetExtension(filePath);

            if (ext != ".sql")
            {
                if (debugOutput)
                {
                    Console.WriteLine($"{filePath} is not an sql file!");
                }
                return(false);
            }


            if (!File.Exists(filePath))
            {
                if (debugOutput)
                {
                    Console.WriteLine($"Could not locate file {filePath}!");
                }
                return(false);
            }

            try
            {
                string content = File.ReadAllText(filePath);

                // Check for emtpy file
                if (string.IsNullOrWhiteSpace(content))
                {
                    if (debugOutput)
                    {
                        Console.WriteLine($"{filePath} has empty content!");
                    }
                    return(false);
                }


                // Check if hashSumTracking is deactivated
                if (!hashSumTracking)
                {
                    await MySqlUpdater.ExecuteQuery(db, content);

                    return(true);
                }


                if (!await MySqlUpdater.IsUpdatesTableAvailable(db))
                {
                    Console.WriteLine("No updates table detected! Aborting! Please use CreateUpdatesTable() to create the required table or set hashSumTracking = false.");
                    return(false);
                }


                if (debugOutput)
                {
                    Console.WriteLine($"Applying {Path.GetFileName(filePath)}");
                }

                TimeSpan ts = await MySqlUpdater.ExecuteQuery(db, content);

                await MySqlUpdater.InsertHash(db, filePath);

                return(true);
            }
            catch
            {
                throw;
            }
        }