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
ご覧のとおり、両方のステートメントを正しく識別して実行します。
しかし、2 つの SELECT @email ステートメントをコメント アウトし、3 番目のステートメントのコメントを解除すると、この出力が得られ、移行が失敗します。(独自のロギングによると) 1 つのステートメントのみを識別しますが、とにかく 2 つのステートメントを実行します。繰り返しますが、2 つの間の唯一の変更点は、カーソル ループで使用されている 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
そして、はい、私の実際の移行は実際にループ内で何かを行います-ストアドプロシージャを呼び出して新しいユーザーを挿入します。これは、誰でも実行および再現できることを期待して作成したデモ コードです。