Liquibase empty rollback deletes latest row from databasechangelog - spring-boot

I have the following changelog -
--liquibase formatted sql
--changeset erfan:1
CREATE TABLE IF NOT EXISTS product (
id BIGINT NOT NULL,
name VARCHAR(40) NOT NULL,
quantity INT,
CONSTRAINT pk_product PRIMARY KEY (id)
);
CREATE SEQUENCE product_seq INCREMENT BY 1;
--rollback DROP TABLE product;
--rollback DROP SEQUENCE product_seq;
--changeset erfan:2
ALTER TABLE IF EXISTS product
ADD COLUMN created TIMESTAMP,
ADD COLUMN updated TIMESTAMP,
ADD COLUMN version INT;
--rollback none
While performing rollback with gradle ./gradlew rollbackCount -PliquibaseCommandValue=1 causes the following error
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "empty"
--rollback empty also gives the same error.
Changing the rollback statement to --rollback SELECT 'N\A' according to this discussion, the error resolves but it deletes the corresponding update entry from the databasechangelog table while keeping the product table same.
For this, when I re-run ./gradlew update command, it fails because the columns already exist. Since the record of changeset erfan:2 is removed from the databasechangelog table, the update command tries to execute the changeset erfan:2 again.
I am using
liquibase 4.18.0
Spring boot 2.7.7
gradle 7.6
liquibase gradle plugin 2.0.4
p.s. corresponding question link in liquibase forum.

Related

Raw query to set AutoIncrement to false?

The id column in the student table is an auto incrementing one.I wanted to make that to non - autoincrementing. May i know, how can i modify the below query to work as such?
DB::statement("ALTER TABLE student SET AUTO_INCREMENT=FALSE;");
the above code shows the below error.
Illuminate\Database\QueryException
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 1: ALTER TABLE student SET AUTO_INCREMENT=FALSE;
^ (SQL: ALTER TABLE student SET AUTO_INCREMENT=FALSE;)
The correct syntax in PostgreSQL would be:
ALTER TABLE student ALTER COLUMN id DROP DEFAULT;
Where id is the serial column.
You might also want to drop the not null constraint :
ALTER TABLE student ALTER COLUMN id DROP NOT NULL;
This is an example of why posting table definitions (ddl) and Postgres version can be critical. #Zakaria is correct if the Postgres version is prior to version 10, or if version 10 or later and is still defined as serial/bigserial. However the preferred definition for version 10 and later is generated always as identity. If defined as identity you need:
alter table student alter column id drop identity.
I would not drop a not null constraint, and if it is the PK it is moot point as it will automatically be not null.

ORA-02289: sequence doesn't exist

I created a sequence EMP_SEQ then added a column to a table with EMP_SEQ as column as below:
ALTER TABLE EMPLOYEE ADD EMP_SEQ;
UPDATE EMPLOYEE SET EMP_SEQ = EMP_SEQ.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ DEFAULT ON NULL EMP_SEQ.NEXTVAL);
Later I had to rename the sequence to EMP_SEQ_AUTO_INC and column name of EMPLOYEE table to the same as below:
ALTER TABLE EMPLOYEE COLUMN EMP_SEQ TO EMP_SEQ_AUTO_INC;
RENAME EMP_SEQ TO EMP_SEQ_AUTO_INC;
And now the ORA 02289 occurs when I try to insert data into the table with column_name EMP_SEQ_AUTO_INC.
Should I complete the below steps with new column name to overcome this error:
UPDATE EMPLOYEE SET EMP_SEQ_AUTO_INC = EMP_SEQ_AUTO_INC.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ_AUTO_INC DEFAULT ON NULL EMP_SEQ_AUTO_INC.NEXTVAL);
That's not Oracle 11g, it doesn't support syntax you used. I suggest you fix the database version tag.
Anyway: with all modifications you made, you'll just need to re-run
ALTER TABLE employee MODIFY
(emp_seq_auto_inc DEFAULT ON NULL emp_seq_auto_inc.NEXTVAL);
and that's all. No need to update rows once again, you won't get anything new because values already exist, and sequence was just renamed (so it continues with its values, i.e. you won't get duplicates which might happen if you dropped it and created a new one).

Spring Boot: How do I specify execute order of different schema.sql files?

I have created a table that has a foreign key constraint on spring-session-jdbc's spring_session table. The main motivation is that spring-session would delete the rows so that it would cascade and delete entries associated with the actual session. It became a "only works on my machine" problem because only me have had the table already in place when I start the development server. It would only work if others comment out the table first, initialize the server, then revert and do it again. Otherwise, nested exception is java.sql.SQLException: Failed to open the referenced table 'spring_session'.
I think the solution is to specify the run order of (or dependencies between) the initialization sql files. I cannot find that setting after some searching, so I am here.
schema.sql:
drop table if exists foo;
create table if not exists foo (
sid char(36) not null,
foreign key (sid) references spring_session (session_id) on delete cascade,
-- other columns and constraints
);
Possible workarounds:
Workaround #1: put an alter table add constraint statement like this in data.sql.
Workaround #2: grab spring-session-jdbc's schema.sql and put it into my schema.sql, then set spring.session.jdbc.initialize-schema=never in application.properties.
U can try flyway,it can manage your init sql files by giving them a version number. And it can record which sql have been executed, so if add another sql files, it will excute the sql you added, pass the others that have been executed.

Dropping table identity causes ORA-00600 error on Oracle 12c

My Oracle DB version is 12.1.0.2.0.
I'm having a hard time removing the column identity. Also tried to drop the column and dropping the table with purge command, but every time I'm getting the same Oracle error:
ORA-00600: internal error code, arguments: [12811], [96650], [], [], [], [], [], [], [], [], [], []
Just can't touch the identity column. I tried below commands but no luck:
ALTER TABLE DYS_CATEGORY MODIFY CATEGORY_ID DROP IDENTITY;
ALTER TABLE DYS_CATEGORY DROP COLUMN CATEGORY_ID;
DROP TABLE DYS_CATEGORY PURGE;
I can drop any other column from the table, but the problem is with identity column.
Identity columns are new to Oracle, just introduced in 12c.
This is a problem with Oracle 12.1.0.2.0. At least one other person has reported it (on Windows, which may be relevant).
The error you have is an ORA-00600, which is Oracle's default message for unhandled exceptions i.e. Oracle bugs. The correct answer is to raise a Service Request with Oracle Support; they will be able to provide you with a patch or a workaround if you have a corrupted table you need to fix. If you don't have a Support contract you may be out of luck.
For future reference dropping identity columns is a two-stage process:
alter table t42 modify id drop identity;
alter table t42 drop column id;
As it happens, this is not a problem on the very latest version of the product. In Oracle 18c we can just drop the column without modifying it first. LiveSQL demo.
As William said above it looks like there is/was a system generated the sequence for the identity column that was deleted but the record in idnseq$ remains intact.
I would not recommend this to anyone, but I created a new sequence called junk in the same schema. I then found the object_id for the table and the sequence I created and updated idnseq$ manually changing the seqobj# to the object_id of my new sequence for the object# of the table in question.
I was then able to drop the table and purge the recyclebin successfully.
Really don't recommend hacking oracle system tables, but this was a test system that didn't really matter and it worked.
after lots of search and hard work if the table stil showing error ORA-00600: internal error code, arguments:
Do the below step.
Take a backup of original tables
syntax: Create table original_table_back as select * from original_table;
Rename original table to some new table name
syntax: Rename original_table to original_table_1;
Rename backup to the original table
syntax: Rename Original_table_back to original_table.

A simple scenario in which the effect of the ROLLBACK command can not be observed in Oracle 9i

Suppose, on the SQL prompt in Oracle 9i, I'm creating a simple table say orderStatus with only two columns with the following CREATE SQL command.
CREATE TABLE orderStatus(statusID varchar2(6) primary key, status varchar2(15));
just then I'm entering four rows with the following INSERT INTO commands.
INSERT INTO orderStatus VALUES("S0001", "Fulfilled");
INSERT INTO orderStatus VALUES("S0002", "Back order");
INSERT INTO orderStatus VALUES("S0003", "In process");
INSERT INTO orderStatus VALUES("S0004", "Cancelled");
On successful execution of the above commands, I'm issuing a COMMIT command to complete the current transaction.
COMMIT;
just then I need to fire the following DELETE command.
DELETE FROM orderStatus WHERE statusID="S0004";
which deletes one row with the statusID S0004. The table now contains only 3 rows.
Suppose, I need to add the NOT NULL constraint to the status column which requires the following ALTER TABLE command.
ALTER TABLE orderStatus MODIFY(status varchar(15) NOT NULL);
The table orderStatus will be altered to add a NOT NULL constraint to the status column.
Now, suppose I execute a ROLLBACK command on the SQL prompt which causes the current transaction to finish with all the data which are previously affected in this table to be undone. With this, the previously deleted row with the DELETE command above should be undone and the table should now contain the original four rows but the effect of ROLLBACK can not be observed and the table would have only three rows. Why does this happen?
DDL (CREATE TABLE and ALTER TABLE, for example) issues an implicit commit before the statement is executed and after the statement completes. You cannot roll back DDL and you cannot roll back DML changes (like your DELETE) after the implicit commits your DDL causes.
This is why you do not generally want to mix DDL and DML.

Resources