public void WriteBatches() { DbStatementBatchWriter batchWriter = new DbStatementBatchWriter(); batchWriter.BatchWriter.Write("Batch 1"); batchWriter.EndBatch(); batchWriter.BatchWriter.Write("Batch 2"); batchWriter.EndBatch(); batchWriter.BatchWriter.Write("Batch 3"); string[] batches = batchWriter.GetStatementBatches(); Assert.AreEqual(3, batches.Length); Assert.AreEqual("Batch 1", batches[0]); Assert.AreEqual("Batch 2", batches[1]); Assert.AreEqual("Batch 3", batches[2]); }
public override void Visit(IMigrationNode migrationNode) { foreach(IVersionNode versionNode in Filter<IVersionNode>(migrationNode.ChildNodes)) { List<string> ddlScripts = new List<string>(); foreach(IAstNode upgradeNode in GetNode(versionNode, migrationMode).ChildNodes) { DbStatementBatchWriter batchWriter = new DbStatementBatchWriter(); IDbScriptGenerator scriptGenerator = dbPlatform.Dialect.CreateScriptGenerator(batchWriter); scriptGenerator.SetMigrationMode(migrationMode); //scriptGenerator.SetEnvironment(Environment); scriptGenerator.SetNativeSqlResourceProvider(nativeSqlResourceProvider); upgradeNode.Accept(scriptGenerator); ddlScripts.AddRange(batchWriter.GetStatementBatches()); } // foreach migrationScripts.Add(new MigrationScript(versionNode.Number, ddlScripts.ToArray())); } // foreach }
public void GenerateScript() { IAstNode astNode; using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("octalforty.Wizardby.Tests.Resources.Blog.mdl")) { IMdlParser mdlParser = new MdlParser(MdlParserTestFixture.CreateScanner(new StreamReader(resourceStream, Encoding.UTF8))); astNode = mdlParser.Parse(); } // using IDbPlatform platform = new SqlServer2000Platform(); Environment environment = new Environment(); IMdlCompiler compiler = new MdlCompiler(new NullCodeGenerator(), environment); compiler.RemoveCompilerStage<DowngradeGenerationStage>(); compiler.RemoveCompilerStage<UpgradeGenerationStage>(); compiler.AddCompilerStageAfter<AstFlattenerCompilerStage>(new DbNamingCompilerStage(platform.NamingStrategy)); compiler.Compile(astNode, MdlCompilationOptions.All); DbStatementBatchWriter batchWriter = new DbStatementBatchWriter(); IDbScriptGenerator scriptGenerator = platform.Dialect.CreateScriptGenerator(batchWriter); //scriptGenerator.SetEnvironment(environment); foreach(IVersionNode versionNode in Algorithms.Filter<IAstNode, IVersionNode>(astNode.ChildNodes)) versionNode.Accept(scriptGenerator); System.Console.WriteLine(batchWriter.GetStatementBatches()[0].Clean()); Assert.AreEqual(@"create table [SchemaInfo] ( [Version] bigint not null ); create table [Author] ( [ID] int not null identity , [FirstName] nvarchar(200) not null , [LastName] nvarchar(200) not null , [EmailAddress] nvarchar(200) not null , [Login] nvarchar(200) not null , [Password] varbinary(64) null , primary key (ID) ); create table [Tag] ( [ID] int not null identity , [Name] nvarchar(200) not null , primary key (ID) ); create table [Blog] ( [ID] int not null identity , [Name] nvarchar(200) not null , [Description] nvarchar(max) not null , primary key (ID) ); create table [BlogPost] ( [ID] int not null identity , [Title] nvarchar(200) not null , [Slug] nvarchar(200) not null , [BlogID] int not null , [AuthorID] int not null , primary key (ID) ); create table [BlogPostTagJunction] ( [BlogPostID] int not null, [TagID] int not null, ); create unique nonclustered index [UQ_Version] on [SchemaInfo] ([Version]); create unique nonclustered index [IX_EmailAddress] on [Author] ([EmailAddress]); create unique nonclustered index [IX_Login] on [Author] ([Login]); alter table [BlogPost] add constraint [FK1] foreign key ([BlogID]) references [Blog] ([ID]); alter table [BlogPost] add constraint [FK2] foreign key ([AuthorID]) references [Author] ([ID]); alter table [BlogPostTagJunction] add constraint [FK3] foreign key ([BlogPostID]) references [BlogPost] ([ID]); alter table [BlogPostTagJunction] add constraint [FK4] foreign key ([TagID]) references [Tag] ([ID]); create table [BlogPostComment] ( [ID] int not null identity primary key, [BlogPostID] int not null, [AuthorEmailAddress] nvarchar(200) not null, [Content] nvarchar(max) not null, ); alter table [BlogPostComment] add constraint [FK5] foreign key ([BlogPostID]) references [BlogPost] ([ID]); create table [Media] ( [ID] int not null identity primary key, [TypeID] int , [Name] nvarchar(200) not null, [MimeType] nvarchar(200) not null, [Length] int , [BlogPostID] int null, [BlogPostCommentID] int null, ); create table [User] ( [ID] int not null identity primary key, [Login] nvarchar(200) not null, [Password] varbinary(64) not null, ); alter table [Media] add constraint [DF_MimeType] default ('text/xml') for [MimeType]; alter table [Media] add constraint [FK10] foreign key ([BlogPostID]) references [BlogPost] ([ID]); alter table [Media] add constraint [FK11] foreign key ([BlogPostCommentID]) references [BlogPostComment] ([ID]); create unique nonclustered index [IX_Login] on [User] ([ID], [Login] desc); create table [Forum] ( [ID] int not null identity primary key, [Name] nvarchar(200) not null, [ModeratorUserID] int not null, ); alter table [Forum] add constraint [FK_FOO] foreign key ([ModeratorUserID]) references [User] ([ID]); drop index [IX_Login] on [User]; create unique nonclustered index [IX_Login] on [User] ([ID], [Login] desc); create table [BlogAuthorJunction] ( [BlogID] int not null, [AuthorID] int not null, ); alter table [BlogAuthorJunction] add constraint [FK12] foreign key ([BlogID]) references [Blog] ([ID]); alter table [BlogAuthorJunction] add constraint [FK13] foreign key ([AuthorID]) references [Author] ([ID]); alter table [Forum] add [Slug] nvarchar(200) not null; alter table [Forum] drop column [Slug]; alter table [Forum] add [Slug] nvarchar(200) not null; alter table [Forum] alter column [Slug] nvarchar(200) null; alter table [Forum] drop constraint [FK_FOO]; drop table [Forum]; alter table [BlogPostTagJunction] drop constraint [FK4]; drop table [Tag]; ".Clean(), batchWriter.GetStatementBatches()[0].Clean()); }