我可以使用脚本来决定在flywaydb中创建表吗?

时间:2017-01-04 18:38:13

标签: flyway

在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 个答案:

答案 0 :(得分:1)

Flyway将执行每个迁移脚本,并且不会提示您是否执行它。这取决于你编写正确的代码来创建表。但是,如果您有创建和迁移数据库的flyway,那么您应该没有检查现有表的情况。 我只能考虑在没有Flyway支持的情况下从不同版本迁移到Flyway托管数据库。

上次我遇到这个问题(不是用oracle)我使用了两个小的迁移脚本。第一个脚本包含一个真正的SQL过程,其中存在检查表和创建。第二个脚本包含删除SQL过程。