예제 #1
0
파일: Program.cs 프로젝트: s4p0/DbScriptOut
        static void Main1(string[] args)
        {
            var server   = @"";
            var dbName   = ""; // "ZENITH_PRODUCTION";
            var user     = "******";
            var password = "";

            var workingDir = AppDomain.CurrentDomain.BaseDirectory;
            var outputDir  = System.IO.Path.Combine(workingDir, directory);

            if (!System.IO.Directory.Exists(outputDir))
            {
                System.IO.Directory.CreateDirectory(outputDir);
            }


            /// connection
            var conn = new ServerConnection(server, user, password);
            //conn.BeginTransaction();
            //conn.CommitTransaction();

            // server
            Server srv = new Server(conn);

            // database
            var db = srv.Databases[dbName];

            db.CompatibilityLevel = CompatibilityLevel.Version100;

            // scripter engine
            Scripter scp = new Scripter(srv);

            #region Scripter Options
            scp.Options.NoCollation         = true;
            scp.Options.NoCommandTerminator = true;
            // only schema
            scp.Options.ScriptSchema = true;
            scp.Options.ScriptData   = false;
            // no GO's
            scp.Options.NoCommandTerminator = false;
            // without output stream (all objects at once)
            scp.Options.ToFileOnly = true;
            // objects defaults
            scp.Options.AllowSystemObjects = false;
            scp.Options.Permissions        = true;
            scp.Options.SchemaQualify      = true;
            scp.Options.AnsiFile           = true;
            scp.Options.AnsiPadding        = false;

            scp.Options.SchemaQualifyForeignKeysReferences = true;
            scp.Options.DriAllConstraints   = true;
            scp.Options.DriIndexes          = true;
            scp.Options.DriClustered        = true;
            scp.Options.DriNonClustered     = true;
            scp.Options.Indexes             = true;
            scp.Options.NonClusteredIndexes = true;
            scp.Options.ClusteredIndexes    = true;
            scp.Options.FullTextIndexes     = true;

            scp.Options.EnforceScriptingOptions = true;
            /// case of our tests
            scp.Options.WithDependencies = false;
            #endregion


            var values        = db.Tables["SAVGLOBALIZATION_VALUES"];
            var globalization = db.Tables["SAVGLOBALIZATION_VALUES"];



            using (var repo = new Repository(outputDir))
            {
                var branchName = GetCurrentBranchName();
                var branch     = repo.CreateBranch(branchName);
                var master     = repo.Branches["master"];
                Commands.Checkout(repo, branch);

                // remove all .sql files before start
                foreach (var file in System.IO.Directory.GetFiles(outputDir, "*.SQL"))
                {
                    System.IO.File.Delete(file);
                }

                scp.Options.ScriptSchema = false;
                scp.Options.ScriptData   = true;
                scp.ExportFilesToScriptAndManifest(new[] { values.Urn, globalization.Urn }, "DataManifest.txt", null, outputDir: outputDir);

                scp.Options.ScriptSchema = true;
                scp.Options.ScriptData   = false;

                // prepare tables to export
                var tables = db.Tables.Cast <Table>()
                             .Where(tbl => !tbl.IsSystemObject)
                             .Select(n => n.Urn).ToArray();

                // Export it as manifest
                HashSet <Urn> hash = new HashSet <Urn>();

                scp.FilterCallbackFunction = FilterTablesOnly;
                scp.ExportFilesToScriptAndManifest(tables, "TableManifest.txt", hash, outputDir: outputDir);

                // Export tables to script and manifest
                var views = db.Views.Cast <View>()
                            .Where(vw => !vw.IsSystemObject)
                            .Select(n => n.Urn).ToArray();

                // Export views to script and manifest
                scp.FilterCallbackFunction = FilterViewsOnly;
                scp.ExportFilesToScriptAndManifest(views, "ViewManifest.txt", hash, outputDir: outputDir);


                Commands.Stage(repo, "*");

                Signature author    = new Signature("Felipe Correa", "*****@*****.**", DateTime.Now);
                Signature committer = author;

                var committed = repo.Commit(string.Format("Adds {0} ", branchName), author, committer);


                Commands.Checkout(repo, master);
                var result = repo.Merge(branch, author);

                repo.Branches.Remove(branch);
            }
        }