Can I use script to decide the creation of table in flywaydb? - flyway

In flywaydb, I need to decide if the table already exists in the database. If so, skip creation of the table, or else create. I tried this and I get weird errors in the logs..
Sql in the migrations folder
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;
Error logs:
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

Flyway will execute each migration script and give you no hint to execute it or not. It depends on you to write the correct code to create the table or not. But if you have flyway to create and migrate your database than you should not have the situation to check existing of table.
I can only think in situation to migrate from different version without Flyway support to a Flyway managed database.
Last time I had this problem (not with oracle) I used two small migration script. First script contains a real SQL procedure with the check off table exist and creation. The second script contains the deletion of the SQL procedure.

Related

PLSQL declare date like result of query

I would like to set variable for date of first monday from sysdate and return this day in put_line but it's not working. I working on oracle database
SET serveroutput on;
DECLARE
first_monday DATE;
BEGIN
first_monday := select NEXT_DAY(sysdate, 'MONDAY') from dual;
DBMS_OUTPUT.PUT_LINE(first_monday);
END;
/
result should be 06-04-20
what is wrong?
my error below
ERROR at line 4:
ORA-06550: line 4, column 29:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternat
ORA-06550: line 4, column 73:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , # ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
ORA-06550: line 8, column 0:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
You can directly call next_day in PL/SQL (no need for a query). This is desirable because it doesn't cause a context switch between the PL/SQL engine and the SQL engine. Most, but not all, scalar functions can be used this way.
If you need to use it as part of a query then INTO is used to send a query result value into a variable.
Here are both in action.
DECLARE
first_monday DATE;
BEGIN
first_monday := next_day(SYSDATE,
'MONDAY');
dbms_output.put_line(first_monday);
SELECT next_day(SYSDATE,
'MONDAY')
INTO first_monday
FROM dual;
dbms_output.put_line(first_monday);
END;
/
The way to assign the result of a single row query to a variable in PL/SQL is via the INTO keyword. You need to change your code as follows:
DECLARE
first_monday DATE;
BEGIN
select NEXT_DAY(sysdate, 'MONDAY')
INTO first_monday
from dual;
DBMS_OUTPUT.PUT_LINE(first_monday);
END;

Getting error while assigning values to records inside collection

I need to loop through some values inside the plsql code.
so trying to create a collection of records .
getting error as mentioned below.
Please help me to resolve
-- please see the code below
declare
type tab_name is record (t_name varchar2(30),col_name varchar2(30));
type tab_list is table of tab_name;
table_names tab_list:=tab_list(tab_name('ABC','abc'),tab_name('XYZ','xyz'));
begin
for i in table_names.first..table_names.last loop
dbms_output.put_line(table_names(i).t_name||'-'||table_names(i).col_name);
end loop;
end;
/
Error report -
ORA-06550: line 4, column 32:
PLS-00222: no function with name 'TAB_NAME' exists in this scope
ORA-06550: line 4, column 13:
PL/SQL: Item ignored
ORA-06550: line 6, column 10:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Record type's name is not its initializer.
In fact, there is no initialization-like function for record types at all in oracle:
Documentation
Note that values are assigned separately to each field of a record in
Example 5-47. You cannot assign a list of values to a record using an
assignment statement. There is no constructor-like notation for
records.
You have to create each object separately and then create collection with them:
declare
type tab_name is record (t_name varchar2(30),col_name varchar2(30));
type tab_list is table of tab_name;
name_1 tab_name;
table_names tab_list;
begin
name_1.t_name := 'ABC';
name_1.col_name := 'abc';
table_names := tab_list();
table_names.extend;
table_names(table_names.last) := name_1;
name_1.t_name := 'XYZ';
name_1.col_name := 'xyz';
table_names.extend;
table_names(table_names.last) := name_1;
for i in table_names.first..table_names.last
loop
dbms_output.put_line(table_names(i).t_name||'-'||table_names(i).col_name);
end loop;
end;
Collections DO have initializers. However, in your case you have a collection of record types, so you would have to have your records pre-created to make use of it.

PL/SQL compilation error

What is wrong with the following statement:
DECLARE
int_exists INTEGER;
BEGIN
SELECT COUNT(ItemKey)
INTO int_exists
FROM BIR_TabsForDashboard
WHERE ItemKey = 'Position';
IF( int_exists = 0 ) THEN --doesnt exist
EXECUTE IMMEDIATE 'ALTER TABLE BIR_TabsForDashboard ADD Position int NULL';
END IF;
END;
I get this report error:
ORA-06550: line 7, column 9:
PL/SQL: ORA-00904: "ITEMKEY": invalid identifier
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
What could be wrong? It seems to work when I use it on another table but for some reason it comes up with this error when I do it to this specific table?
The table BIR_TabsForDashboard does not have a column named ItemKey.
Make sure the procedure which you are using is should properly declared in the place which you are using it. For an example if it is under an another user and in an another package then it should be in that same order. The username, package and procedure name.

Script that exec other scripts

I want to execute this script in PL/SQL but it keeps getting error
set serveroutput on;
set echo on;
declare
v_code varchar2(250);
v_errm varchar2(250);
begin
##"1.sql";
##"2.sql";
##"II_load_438865311678_Reg.sql";
exception
when others then
v_code := sqlcode;
v_errm := substr(sqlerrm, 1, 64);
DBMS_OUTPUT.PUT_LINE('ERROR! - '||DBMS_UTILITY.FORMAT_ERROR_STACK|| DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
rollback;
end;
/
What is wrong with this.
Error: ORA-06550: line 5, colunm 3: (...) ORA-06550: line 6, colunm 3:
(...) ORA-06550: line 7, colunm 3: PLS-00103: Encountered the symbol
"#" when expecting one of the following: ( begin case declare end
exception exit for goto if loop mod null pragma raise return select
update while with... and
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
You only use the PL/SQL code to show the error. That is not necessary; SQL*Plus will already show you any SQL error. Use whenever sqlerror to trigger the rollback.
whenever sqlerror continue rollback
##"1.sql"
##"2.sql"
##"II_load_438865311678_Reg.sql"

PLSQL record created with %rowtype being used as element for table gives compile/run error

I am declaring a table custom data-type of a record , the record is created by %rowtype of a table nemed : TMP_UNREGISTER_TAB , this table exists in the DB . While trying to execute the below plsql anon block i get the error specified below.
PLSQL CODE:
declare
rec_tmp_tab TMP_UNREGISTER_TAB%rowtype ;
v_rec_tmp_tab rec_tmp_tab;
TYPE tab_rec_tmp IS TABLE OF v_rec_tmp_tab%type
INDEX BY BINARY_INTEGER;
v_tab_rec_tmp tab_rec_tmp;
begin
null;
end;
/
SCRIPT OUTPUT:
Error report:
ORA-06550: line 3, column 18:
PLS-00488: 'REC_TMP_TAB' must be a type
ORA-06550: line 3, column 18:
PL/SQL: Item ignored
ORA-06550: line 4, column 33:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 4, column 4:
PL/SQL: Item ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
declare
rec_tmp_tab TMP_UNREGISTER_TAB%rowtype ;
TYPE tab_rec_tmp IS TABLE OF TMP_UNREGISTER_TAB%rowtype
INDEX BY BINARY_INTEGER;
v_tab_rec_tmp tab_rec_tmp;
begin
null;
end;
/

Resources