title: Statement parsing bug in flyway?
body: <p>I've encountered a very strange error using flyway.  First, here is a version of my update script that works. (This is in a file named V57.3__foo.sql): </p>
  
  <pre class="lang-sql prettyprint-override"><code>use database
  go
  set nocount on
  
  create table userz (
  username char(30),
  firstname char(30),
  lastname char(30),
  activeind char(1)
  )
  
  insert into userz (username, firstname, lastname, activeind) Values('jsmith', 'John', 'Smith','a')
  
  
  declare @username varchar(30),
          @firstname varchar(255),
          @lastname varchar(255),
          @activeind char(1)
  
  DECLARE the_cursor CURSOR FAST_FORWARD LOCAL FOR 
      SELECT username, firstname, lastname, activeind FROM userz
  OPEN the_cursor
  
  FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  WHILE (@@FETCH_STATUS &lt;&gt; -1) BEGIN
  
      declare @email varchar(255)
      SELECT @email=replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com'
      SELECT @email=lower(@email)
  --    SELECT @email=lower(replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com')
  
      declare @deleted bit
      SELECT @deleted= case when @activeind='a' then 0 else 1 end
  
      FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  END
  
  CLOSE the_cursor
  DEALLOCATE the_cursor
  
  drop table userz
  
  set nocount off
  go
  </code></pre>
  
  <p>When I turn on the application, I get this output: </p>
  
  <pre class="lang-sql prettyprint-override"><code>[2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.DbSupportFactory - Database: Microsoft SQL Server 10.50
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.Flyway - DDL Transactions Supported: true
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.Flyway - Schema: dbo
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.command.DbSchemas - Schema [dbo] already exists. Skipping schema creation.
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.metadatatable.MetaDataTableTo20FormatUpgrader - No upgrade to the Flyway 2.0 format necessary for metadata table [dbo].[schema_version]
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.metadatatable.MetaDataTableTo202FormatUpgrader - No metadata table upgrade to the Flyway 2.0.2 format necessary
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.metadatatable.MetaDataTableTo21FormatUpgrader - No metadata table upgrade to the Flyway 2.1 format necessary
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.Table - Locking table [dbo].[schema_version]...
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.Table - Lock acquired for table [dbo].[schema_version]
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.util.FeatureDetector - Spring Jdbc available: true
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.util.scanner.classpath.ClassPathScanner - Scanning for classpath resources at 'db/migration' (Prefix: 'V', Suffix: '.sql')
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.util.scanner.classpath.ClassPathScanner - Scanning URL: file:/home/mcurwen/projects/core/target/classes/db/migration
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.util.FeatureDetector - JBoss VFS v2 available: false
  
  ---- very large snip of scanning/filtering ----
  
  [2013-07-08 14:37:31] [] INFO localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Current version of schema [dbo]: 57.2
  [2013-07-08 14:37:31] [] WARN localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - outOfOrder mode is active. Migration of schema [dbo] may not be reproducible.
  [2013-07-08 14:37:31] [] INFO localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Migrating schema [dbo] to version 57.3
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Found statement at line 1: use database
  
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Found statement at line 3: set nocount on
  
  create table userz (
  username char(30),
  firstname char(30),
  lastname char(30),
  activeind char(1)
  )
  
  insert into userz (username, firstname, lastname, activeind) Values('jsmith', 'John', 'Smith','a')
  
  
  declare @username varchar(30),
          @firstname varchar(255),
          @lastname varchar(255),
          @activeind char(1)
  
  DECLARE the_cursor CURSOR FAST_FORWARD LOCAL FOR 
      SELECT username, firstname, lastname, activeind FROM userz
  OPEN the_cursor
  
  FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  WHILE (@@FETCH_STATUS &lt;&gt; -1) BEGIN
  
      declare @email varchar(255)
      SELECT @email=replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com'
      SELECT @email=lower(@email)
  --    SELECT @email=lower(replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com')
  
      declare @deleted bit
      SELECT @deleted= case when @activeind='a' then 0 else 1 end
  
      FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  END
  
  CLOSE the_cursor
  DEALLOCATE the_cursor
  
  drop table userz
  
  set nocount off
  
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Executing SQL: use database
  
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Executing SQL: set nocount on
  
  create table userz (
  username char(30),
  firstname char(30),
  lastname char(30),
  activeind char(1)
  )
  
  insert into userz (username, firstname, lastname, activeind) Values('jsmith', 'John', 'Smith','a')
  
  
  declare @username varchar(30),
          @firstname varchar(255),
          @lastname varchar(255),
          @activeind char(1)
  
  DECLARE the_cursor CURSOR FAST_FORWARD LOCAL FOR 
      SELECT username, firstname, lastname, activeind FROM userz
  OPEN the_cursor
  
  FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  WHILE (@@FETCH_STATUS &lt;&gt; -1) BEGIN
  
      declare @email varchar(255)
      SELECT @email=replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com'
      SELECT @email=lower(@email)
  --    SELECT @email=lower(replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com')
  
      declare @deleted bit
      SELECT @deleted= case when @activeind='a' then 0 else 1 end
  
      FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  END
  
  CLOSE the_cursor
  DEALLOCATE the_cursor
  
  drop table userz
  
  set nocount off
  
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Successfully completed and committed migration of schema [dbo] to version 57.3
  [2013-07-08 14:37:31] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Finished migrating schema [dbo] to version 57.3 (execution time 00:00.060s)
  [2013-07-08 14:37:32] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.metadatatable.MetaDataTableImpl - MetaData table [dbo].[schema_version] successfully updated to reflect changes
  </code></pre>
  
  <p>As you see, it correctly identifies and then executes both statements. </p>
  
  <p>But, if I comment out the two SELECT @email statements, and uncomment the third one, I get this output, and a failed migration. It only identifies one statement (according to its own logging) but then runs two statements anyway - the second one didn't trim the 'go' from the bottom.  again, the <em>only</em> change between the two is the SELECT statements being used in the cursor loop. </p>
  
  <pre class="lang-sql prettyprint-override"><code>[2013-07-08 14:41:54] [] INFO localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Migrating schema [dbo] to version 57.3
  [2013-07-08 14:41:54] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Found statement at line 1: use database
  
  [2013-07-08 14:41:54] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Executing SQL: use database
  
  [2013-07-08 14:41:54] [] DEBUG localhost-startStop-1  com.googlecode.flyway.core.dbsupport.SqlScript - Executing SQL: set nocount on
  
  create table userz (
  username char(30),
  firstname char(30),
  lastname char(30),
  activeind char(1)
  )
  
  insert into userz (username, firstname, lastname, activeind) Values('jsmith', 'John', 'Smith','a')
  
  
  declare @username varchar(30),
          @firstname varchar(255),
          @lastname varchar(255),
          @activeind char(1)
  
  DECLARE the_cursor CURSOR FAST_FORWARD LOCAL FOR 
      SELECT username, firstname, lastname, activeind FROM userz
  OPEN the_cursor
  
  FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  WHILE (@@FETCH_STATUS &lt;&gt; -1) BEGIN
  
      declare @email varchar(255)
  --    SELECT @email=replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com'
  --    SELECT @email=lower(@email)
      SELECT @email=lower(replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com')
  
      declare @deleted bit
      SELECT @deleted= case when @activeind='a' then 0 else 1 end
  
      FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  END
  
  CLOSE the_cursor
  DEALLOCATE the_cursor
  
  drop table userz
  
  set nocount off
  go
  [2013-07-08 14:41:54] [] ERROR localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - com.googlecode.flyway.core.api.FlywayException: Error executing statement at line 3: set nocount on
  
  create table userz (
  username char(30),
  firstname char(30),
  lastname char(30),
  activeind char(1)
  )
  
  insert into userz (username, firstname, lastname, activeind) Values('jsmith', 'John', 'Smith','a')
  
  
  declare @username varchar(30),
          @firstname varchar(255),
          @lastname varchar(255),
          @activeind char(1)
  
  DECLARE the_cursor CURSOR FAST_FORWARD LOCAL FOR 
      SELECT username, firstname, lastname, activeind FROM userz
  OPEN the_cursor
  
  FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  WHILE (@@FETCH_STATUS &lt;&gt; -1) BEGIN
  
      declare @email varchar(255)
  --    SELECT @email=replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com'
  --    SELECT @email=lower(@email)
      SELECT @email=lower(replace(coalesce(@firstname, 'xxxxx')+'.'+coalesce(@lastname, 'xxxxx'),' ','')+'@domain.com')
  
      declare @deleted bit
      SELECT @deleted= case when @activeind='a' then 0 else 1 end
  
      FETCH NEXT FROM the_cursor INTO @username, @firstname, @lastname, @activeind
  END
  
  CLOSE the_cursor
  DEALLOCATE the_cursor
  
  drop table userz
  
  set nocount off
  go
  [2013-07-08 14:41:54] [] ERROR localhost-startStop-1  com.googlecode.flyway.core.command.DbMigrate - Caused by java.sql.SQLException: Incorrect syntax near 'go'.
  [2013-07-08 14:41:54] [] ERROR localhost-startStop-1  org.springframework.web.context.ContextLoader - Context initialization failed
  </code></pre>
  
  <p>And yes, my real migration does actually do something inside the loop - it calls a stored procedure to insert the new user.  This is demo code I made in the hopes that anyone could run and reproduce. </p>