我在使用 flyway 时遇到了一个非常奇怪的错误。首先,这是我的更新脚本的一个版本。(这是在一个名为 V57.3__foo.sql 的文件中):
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 <> -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: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 <> -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 <> -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
如您所见,它正确识别并执行这两个语句。
但是,如果我注释掉两个 SELECT @email 语句,并取消注释第三个,我会得到这个输出,并且迁移失败。它只识别一个语句(根据它自己的日志记录),但无论如何都会运行两个语句 - 第二个语句没有从底部修剪“go”。同样,两者之间的唯一变化是游标循环中使用的 SELECT 语句。
[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 <> -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 <> -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
是的,我真正的迁移确实在循环中做了一些事情——它调用一个存储过程来插入新用户。这是我制作的演示代码,希望任何人都可以运行和重现。