public static void TestCopySchema(string pathToSchemaScript) { TestHelper.DropDb("TEST_SOURCE"); TestHelper.DropDb("TEST_COPY"); //create the db from sql script TestHelper.ExecSql("CREATE DATABASE TEST_SOURCE", ""); TestHelper.ExecBatchSql(File.ReadAllText(pathToSchemaScript), "TEST_SOURCE"); //load the model from newly created db and create a copy var copy = new Database("TEST_COPY"); copy.Connection = TestHelper.GetConnString("TEST_SOURCE"); copy.Load(); SqlConnection.ClearAllPools(); TestHelper.ExecBatchSql(copy.ScriptCreate(), "master"); //compare the dbs to make sure they are the same var source = new Database("TEST_SOURCE"); source.Connection = TestHelper.GetConnString("TEST_SOURCE"); source.Load(); copy.Load(); TestCompare(source, copy); }
public void TestScript() { var db = new Database("TEST_TEMP"); var t1 = new Table("dbo", "t1"); t1.Columns.Add(new Column("col1", "int", false, null)); t1.Columns.Add(new Column("col2", "int", false, null)); t1.Constraints.Add(new Constraint("pk_t1", "PRIMARY KEY", "col1,col2")); t1.FindConstraint("pk_t1").Clustered = true; var t2 = new Table("dbo", "t2"); t2.Columns.Add(new Column("col1", "int", false, null)); t2.Columns.Add(new Column("col2", "int", false, null)); t2.Columns.Add(new Column("col3", "int", false, null)); t2.Constraints.Add(new Constraint("pk_t2", "PRIMARY KEY", "col1")); t2.FindConstraint("pk_t2").Clustered = true; t2.Constraints.Add(new Constraint("IX_col3", "UNIQUE", "col3")); db.ForeignKeys.Add(new ForeignKey(t2, "fk_t2_t1", "col2,col3", t1, "col1,col2")); db.Tables.Add(t1); db.Tables.Add(t2); TestHelper.DropDb("TEST_TEMP"); SqlConnection.ClearAllPools(); TestHelper.ExecBatchSql(db.ScriptCreate(), "master"); var db2 = new Database(); db2.Connection = TestHelper.GetConnString("TEST_TEMP"); db2.Load(); TestHelper.DropDb("TEST_TEMP"); foreach (Table t in db.Tables) { Assert.IsNotNull(db2.FindTable(t.Name, t.Owner)); Assert.IsFalse(db2.FindTable(t.Name, t.Owner).Compare(t).IsDiff); } }