2017-01-04 4 views
0

flywaydbでは、テーブルがデータベースに既に存在するかどうかを判断する必要があります。その場合は、テーブルの作成をスキップするか、作成しないでください。私はこれを試してみましたが、私がログに奇妙なエラーが発生します。..スクリプトを使用してflywaydbでテーブルを作成することはできますか?

SQLは、移行フォルダに

declare 
    c number:=0; 
begin 
    SELECT count(*) into c 
     FROM dba_tables where table_name = 'sample_table' AND OWNER='TDS_INT'; 

     if c = 0 then 
      CREATE TABLE TDS_INT.sample_table 
      ( 
       sample_id    NUMBER(19)   , 
       WORK_ORDER_ID   VARCHAR2(20)  CONSTRAINT TBT895_CK_010_N NOT NULL, 
       TASK_TYPE    VARCHAR2(8)   CONSTRAINT TBT895_CK_015_N NOT NULL, 
       LEAD_TIME    VARCHAR2(20)  CONSTRAINT TBT895_CK_025_N NOT NULL, 
       LAST_MODIFIED_DATE  DATE    CONSTRAINT TBT895_CK_030_N NOT NULL, 
       LAST_MODIFIED_USERID VARCHAR2(8)   CONSTRAINT TBT895_CK_035_N NOT NULL 

      );  
     end if; 
end; 

エラーログ:

Migrating schema "ASAP" to version 2.1 - create sample table 
DEBUG: Executing SQL: declare 
     c number:=0; 
begin 
     SELECT count(*) into c 
      FROM dba_tables where table_name = 'sample_table' AND OWNER='TDS_INT'; 


      if c = 0 then 
        CREATE TABLE TDS_INT.sample_table 
         (
           sample_id        NUMBER(1 
9)    , 
           WORK_ORDER_ID     VARCHAR2(20) 
     CONSTRAINT TBT895_CK_010_N NOT NULL, 
           TASK_TYPE       VARCHAR2(8) 
       CONSTRAINT TBT895_CK_015_N NOT NULL, 
           LEAD_TIME        VARCHAR2 
(20)   CONSTRAINT TBT895_CK_025_N NOT NULL, 
           LAST_MODIFIED_DATE    DATE 
       CONSTRAINT TBT895_CK_030_N NOT NULL, 
           LAST_MODIFIED_USERID VARCHAR2(8) 
CONSTRAINT TBT895_CK_035_N NOT NULL 

         ); 
       end if; 
end; 

DEBUG: Rolling back transaction... 
DEBUG: Transaction rolled back 
ERROR: Migration of schema "ASAP" to version 2.1 - create sample table failed! P 
lease restore backups and roll back database and code! 
DEBUG: MetaData table "ASAP"."schema_version" successfully updated to reflect ch 
anges 
ERROR: Unexpected error 
org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException: 
Migration V2_1__create_sample_table.sql failed 
---------------------------------------------- 
SQL State : 65000 
Error Code : 6550 
Message : ORA-06550: line 8, column 5: 
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following: 

    begin case declare exit for goto if loop mod null pragma 
    raise return select update while with <an identifier> 
    <a double-quoted delimited-identifier> <a bind variable> << 
    close current delete fetch lock insert open rollback 
    savepoint set sql execute commit forall merge pipe 
The symbol "lock was inserted before "CREATE" to continue. 
ORA-06550: line 9, column 4: 
PLS-00103: Encountered the symbol "(" when expecting one of the following: 

    . , @ in <an identifier> 
    <a double-quoted delimited-identifier> partition subpartition 
ORA-06550: line 11, column 36: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 12, column 35: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 13, column 33: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 14, column 34: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    := . () , @ % not null range with default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 15, column 40: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 18, column 3: 
PLS-00103: Encountered the symbol "END" when expecting one of the following: 

    begin function package pragma procedure subtype type use 
    <an identifier> <a double-quoted delimited-identifier> form 
    current cursor 
Location : C:/Project/Tools/Migrations/V2_1__create_sample_table.sql (C:\Proje 
ct\Tools\Migrations\V2_1__create_sample_table.sql) 
Line  : 1 
Statement : declare 
     c number:=0; 
begin 
     SELECT count(*) into c 
      FROM dba_tables where table_name = 'sample_table' AND OWNER='TDS_INT'; 


      if c = 0 then 
        CREATE TABLE TDS_INT.sample_table 
         (
           sample_id        NUMBER(1 
9)    , 
           WORK_ORDER_ID     VARCHAR2(20) 
     CONSTRAINT TBT895_CK_010_N NOT NULL, 
           TASK_TYPE       VARCHAR2(8) 
       CONSTRAINT TBT895_CK_015_N NOT NULL, 
           LEAD_TIME        VARCHAR2 
(20)   CONSTRAINT TBT895_CK_025_N NOT NULL, 
           LAST_MODIFIED_DATE    DATE 
       CONSTRAINT TBT895_CK_030_N NOT NULL, 
           LAST_MODIFIED_USERID VARCHAR2(8) 
CONSTRAINT TBT895_CK_035_N NOT NULL 

         ); 
       end if; 
end; 


     at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java 
:117) 
     at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.execute(
SqlMigrationExecutor.java:71) 
     at org.flywaydb.core.internal.command.DbMigrate.doMigrate(DbMigrate.java 
:352) 
     at org.flywaydb.core.internal.command.DbMigrate.access$1100(DbMigrate.ja 
va:47) 
     at org.flywaydb.core.internal.command.DbMigrate$4.doInTransaction(DbMigr 
ate.java:308) 
     at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(Tran 
sactionTemplate.java:72) 
     at org.flywaydb.core.internal.command.DbMigrate.applyMigration(DbMigrate 
.java:305) 
     at org.flywaydb.core.internal.command.DbMigrate.access$1000(DbMigrate.ja 
va:47) 
     at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigr 
ate.java:230) 
     at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigr 
ate.java:173) 
     at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(Tran 
sactionTemplate.java:72) 
     at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:1 
73) 
     at org.flywaydb.core.Flyway$1.execute(Flyway.java:959) 
     at org.flywaydb.core.Flyway$1.execute(Flyway.java:917) 
     at org.flywaydb.core.Flyway.execute(Flyway.java:1373) 
     at org.flywaydb.core.Flyway.migrate(Flyway.java:917) 
     at org.flywaydb.commandline.Main.executeOperation(Main.java:139) 
     at org.flywaydb.commandline.Main.main(Main.java:102) 
Caused by: java.sql.SQLException: ORA-06550: line 8, column 5: 
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following: 

    begin case declare exit for goto if loop mod null pragma 
    raise return select update while with <an identifier> 
    <a double-quoted delimited-identifier> <a bind variable> << 
    close current delete fetch lock insert open rollback 
    savepoint set sql execute commit forall merge pipe 
The symbol "lock was inserted before "CREATE" to continue. 
ORA-06550: line 9, column 4: 
PLS-00103: Encountered the symbol "(" when expecting one of the following: 

    . , @ in <an identifier> 
    <a double-quoted delimited-identifier> partition subpartition 
ORA-06550: line 11, column 36: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 12, column 35: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 13, column 33: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 14, column 34: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    := . () , @ % not null range with default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 15, column 40: 
PLS-00103: Encountered the symbol "CONSTRAINT" when expecting one of the followi 
ng: 

    :=) , not null default external character 
The symbol "," was substituted for "CONSTRAINT" to continue. 
ORA-06550: line 18, column 3: 
PLS-00103: Encountered the symbol "END" when expecting one of the following: 

    begin function package pragma procedure subtype type use 
    <an identifier> <a double-quoted delimited-identifier> form 
    current cursor 

     at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) 
     at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) 
     at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) 
     at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) 
     at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) 
     at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) 
     at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194) 
     at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000 
) 
     at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStateme 
nt.java:1307) 
     at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.ja 
va:1882) 
     at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1847) 

     at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrap 
per.java:301) 
     at org.flywaydb.core.internal.dbsupport.JdbcTemplate.executeStatement(Jd 
bcTemplate.java:238) 
     at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java 
:114) 
     ... 17 more 

答えて

1

フライウェイは、各移行スクリプトを実行し、あなたにそれを実行するかどうかのヒントは与えない。それはあなたがテーブルを作成するための正しいコードを書くかどうかによって決まります。しかし、データベースを作成して移行するためのフライウェイがある場合は、既存のテーブルをチェックする必要がありません。 私は、Flywayをサポートしていない別のバージョンからFlywayの管理対象データベースに移行すると思うだけです。

私は前回(オラクルではなく)この問題を抱えていましたが、2つの小さな移行スクリプトを使用しました。最初のスクリプトには、チェックオフテーブルが存在し、作成された実際のSQLプロシージャが含まれています。 2番目のスクリプトには、SQLプロシージャーの削除が含まれています。

+0

ありがとうございました、私たちはそれから離れてLiquibaseを使用することに決めました。その場合、liquibaseはより成熟しています。 – Zeus

関連する問題