Exemplo n.º 1
0
        public void CreateFromDir(bool overwrite)
        {
            var cnBuilder = new SqlConnectionStringBuilder(Connection);
            if (DBHelper.DbExists(Connection))
            {
                DBHelper.DropDb(Connection);
            }

            //create database
            DBHelper.CreateDb(Connection);

            //run scripts
            if (File.Exists(Dir + "/props.sql"))
            {
                try
                {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/props.sql"));
                }
                catch (SqlBatchException ex)
                {
                    throw new SqlFileException(Dir + "/props.sql", ex);
                }

                // COLLATE can cause connection to be reset
                // so clear the pool so we get a new connection
                DBHelper.ClearPool(Connection);
            }

            if (File.Exists(Dir + "/schemas.sql"))
            {
                try
                {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
                }
                catch (SqlBatchException ex)
                {
                    throw new SqlFileException(Dir + "/schemas.sql", ex);
                }
            }

            // create db objects
            // resolve dependencies by trying over and over
            // if the number of failures stops decreasing then give up
            List<string> scripts = GetScripts();
            var errors = new List<SqlFileException>();
            int prevCount = Int32.MaxValue;
            while (scripts.Count > 0 && errors.Count < prevCount)
            {
                if (errors.Count > 0)
                {
                    prevCount = errors.Count;
                    Console.WriteLine(
                        "{0} errors occurred, retrying...", errors.Count);
                }
                errors.Clear();
                foreach (string f in scripts.ToArray())
                {
                    try
                    {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                        scripts.Remove(f);
                    }
                    catch (SqlBatchException ex)
                    {
                        errors.Add(new SqlFileException(f, ex));
                    }
                }
            }

            Load(); // load the schema first so we can import data
            ImportData(); // load data

            // foreign keys
            if (Directory.Exists(Dir + "/foreign_keys"))
            {
                foreach (string f in Directory.GetFiles(Dir + "/foreign_keys", "*.sql"))
                {
                    try
                    {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                    }
                    catch (SqlBatchException ex)
                    {
                        throw new SqlFileException(f, ex);
                    }
                }
            }
            if (errors.Count > 0)
            {
                var ex = new BatchSqlFileException();
                ex.Exceptions = errors;
                throw ex;
            }
        }
Exemplo n.º 2
0
        public void CreateFromDir(bool overwrite)
        {
            var cnBuilder = new SqlConnectionStringBuilder(Connection);

            if (DBHelper.DbExists(Connection))
            {
                DBHelper.DropDb(Connection);
            }

            //create database
            DBHelper.CreateDb(Connection);

            //run scripts
            if (File.Exists(Dir + "/props.sql"))
            {
                try
                {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/props.sql"));
                }
                catch (SqlBatchException ex)
                {
                    throw new SqlFileException(Dir + "/props.sql", ex);
                }

                // COLLATE can cause connection to be reset
                // so clear the pool so we get a new connection
                DBHelper.ClearPool(Connection);
            }

            if (File.Exists(Dir + "/schemas.sql"))
            {
                try
                {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
                }
                catch (SqlBatchException ex)
                {
                    throw new SqlFileException(Dir + "/schemas.sql", ex);
                }
            }

            // create db objects
            // resolve dependencies by trying over and over
            // if the number of failures stops decreasing then give up
            List <string> scripts   = GetScripts();
            var           errors    = new List <SqlFileException>();
            int           prevCount = Int32.MaxValue;

            while (scripts.Count > 0 && errors.Count < prevCount)
            {
                if (errors.Count > 0)
                {
                    prevCount = errors.Count;
                    Console.WriteLine(
                        "{0} errors occurred, retrying...", errors.Count);
                }
                errors.Clear();
                foreach (string f in scripts.ToArray())
                {
                    try
                    {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                        scripts.Remove(f);
                    }
                    catch (SqlBatchException ex)
                    {
                        errors.Add(new SqlFileException(f, ex));
                    }
                }
            }

            Load();       // load the schema first so we can import data
            ImportData(); // load data

            // foreign keys
            if (Directory.Exists(Dir + "/foreign_keys"))
            {
                foreach (string f in Directory.GetFiles(Dir + "/foreign_keys", "*.sql"))
                {
                    try
                    {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                    }
                    catch (SqlBatchException ex)
                    {
                        throw new SqlFileException(f, ex);
                    }
                }
            }
            if (errors.Count > 0)
            {
                var ex = new BatchSqlFileException();
                ex.Exceptions = errors;
                throw ex;
            }
        }
Exemplo n.º 3
0
        public void CreateFromDir(bool overwrite)
        {
            var cnBuilder = new SqlConnectionStringBuilder(Connection);
            if (DBHelper.DbExists(Connection)) {
                DBHelper.DropDb(Connection);
            }

            Console.WriteLine("Creating database...");
            //create database
            DBHelper.CreateDb(Connection);

            //run scripts
            if (File.Exists(Dir + "/props.sql")) {
                try {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/props.sql"));
                } catch (SqlBatchException ex) {
                    throw new SqlFileException(Dir + "/props.sql", ex);
                }

                // COLLATE can cause connection to be reset
                // so clear the pool so we get a new connection
                DBHelper.ClearPool(Connection);
            }

            if (File.Exists(Dir + "/schemas.sql")) {
                try {
                    DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
                } catch (SqlBatchException ex) {
                    throw new SqlFileException(Dir + "/schemas.sql", ex);
                }
            }

            Console.WriteLine("Creating database objects...");
            // create db objects

            // resolve dependencies by trying over and over
            // if the number of failures stops decreasing then give up
            List<string> scripts = GetScripts();
            var errors = new List<SqlFileException>();
            int prevCount = -1;
            while (scripts.Count > 0 && (prevCount == -1 || errors.Count < prevCount)) {
                if (errors.Count > 0) {
                    prevCount = errors.Count;
                    Console.WriteLine(
                        "{0} errors occurred, retrying...", errors.Count);
                }
                errors.Clear();
                foreach (string f in scripts.ToArray()) {
                    try {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                        scripts.Remove(f);
                    } catch (SqlBatchException ex) {
                        errors.Add(new SqlFileException(f, ex));
                        //Console.WriteLine("Error occurred in {0}: {1}", f, ex);
                    }
                }
            }
            if (!errors.Any() && prevCount > 0)
                Console.WriteLine("All errors resolved, were probably dependency issues...");
            Console.WriteLine();

            Load(); // load the schema first so we can import data
            Console.WriteLine("Importing data...");
            ImportData(); // load data

            Console.WriteLine("Data imported successfully.");
            if (Directory.Exists(Dir + "/after_data")) {
                foreach (string f in Directory.GetFiles(Dir + "/after_data", "*.sql")) {
                    try {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                    } catch (SqlBatchException ex) {
                        errors.Add(new SqlFileException(f, ex));
                    }
                }
            }

            Console.WriteLine("Adding foreign key constraints...");
            // foreign keys
            if (Directory.Exists(Dir + "/foreign_keys")) {
                foreach (string f in Directory.GetFiles(Dir + "/foreign_keys", "*.sql")) {
                    try {
                        DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
                    } catch (SqlBatchException ex) {
                        //throw new SqlFileException(f, ex);
                        errors.Add(new SqlFileException(f, ex));
                    }
                }
            }
            if (errors.Count > 0) {
                var ex = new BatchSqlFileException();
                ex.Exceptions = errors;
                throw ex;
            }
        }