Red Gate forums :: View topic - Scripting against an empty folder
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Comparison SDK Previous Versions
SQL Comparison SDK Previous Versions forum

Scripting against an empty folder

Search in SQL Comparison SDK Previous Versions forum
Post new topic   Reply to topic
Jump to:  
Author Message
dmory



Joined: 15 Oct 2007
Posts: 3

PostPosted: Wed May 27, 2009 9:18 pm    Post subject: Scripting against an empty folder Reply with quote

I'm trying to do a comparison between two script folders, one with valid SQL scripts and a temporary one which is empty, so that I can merge all the script files of the first folder into a single script. When I try to do this, it does not script any key constraints (primary keys, foreign keys, etc).

Code:
        private string CreateScript(string sScriptPath)
        {
            string sScript = "";
            string sTempFolder = CreateTemporaryFolder();
            Database dbSource = new Database();
            Database dbEmpty = new Database();
            ReadFromScriptDatabaseInformation r = new ReadFromScriptDatabaseInformation();
            r.SQLServerDBVersion = RedGate.Shared.SQL.Server.SQLVersion.SqlServer2005;
            r.DefaultCollation = "SQL_Latin1_General_CP1_CI_AS";               
            dbSource.Register(sScriptPath, r, Options.Default);
            dbEmpty.Register(sTempFolder, r, Options.Default);
            Differences diffs = dbSource.CompareWith(dbEmpty, Options.Default);
            foreach (Difference diff in diffs)
            {
                diff.Selected = true;
            }

            Work w = new Work();
            w.BuildFromDifferences(diffs, Options.Default, true);
            sScript = w.ExecutionBlock.GetString();
            Directory.Delete(sTempFolder);
            return sScript;
        }

        private string CreateTemporaryFolder()
        {
            Random r = new Random();           
            string sPath = "";
            DirectoryInfo di = null;

            int nNumAttempts = 0;
            while (di == null)
            {
                sPath = Path.GetTempPath() + "_" + r.Next(0, 1000000).ToString();
                try
                {
                    nNumAttempts++;
                    di = Directory.CreateDirectory(sPath);
                }
                catch(IOException)
                {
                    if (nNumAttempts >= 50)
                    {
                        throw;
                    }
                }
            }
            return sPath;
        }

Have I coded this correctly? Is it even possible to do this?

Any help would be much appreciated.
Back to top
View user's profile Send private message
Michelle Taylor



Joined: 30 Oct 2006
Posts: 529
Location: Red Gate Software

PostPosted: Thu May 28, 2009 1:46 pm    Post subject: Reply with quote

If you're trying to produce a synchronization script from a comparison between two script folders, you'll need to specify the option Options.ForceSyncScriptGeneration (just change Options.Default to Options.Default | Options.ForceSyncScriptGeneration). Otherwise SQL Compare doesn't bother to generate a full sync script because it doesn't actually need one to synchronize the two script folders.
Back to top
View user's profile Send private message
dmory



Joined: 15 Oct 2007
Posts: 3

PostPosted: Thu May 28, 2009 2:25 pm    Post subject: Reply with quote

It looks like that worked. Thank you.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group