Liquibase is trying to create the same table twice - liquibase

I'm new to liquibase and I'm trying to run some test(version 3.6.3).
I created an empty database named 'liquibase_testing'
I created a migrations.xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<includeAll path="migrations" relativeToChangelogFile="true"/>
</databaseChangeLog>
I created a simple CREATE TABLE script and saved in in the migrations directory as 001-liquibase-testing-create-names-table.sql. It looks like this:
CREATE TABLE dbo.names (
name_id int identity,
first_name nvarchar(100),
last_name nvarchar(100),
primary key clustered (name_id)
)
I ran the liquibase migration with cli like this:
./_liquibase/liquibase-3.6.3-bin/liquibase
--driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
--classpath=./_jdbc/sqljdbc_7.2/enu/mssql-jdbc-7.2.0.jre8.jar
--changeLogFile=./liquibase_testing/resources/migrations.xml
--url="jdbc:sqlserver://myDbServer;database=liquibase_testing"
--username=liquibase_testing
--password=password
--logLevel=debug
migrate
And I get this error:
Starting Liquibase at Thu, 14 Feb 2019 12:54:52 EST (version 3.6.3 built at 2019-01-29 11:34:48)
Unexpected error running Liquibase: There is already an object named 'names' in the database. [Failed SQL: CREATE TABLE dbo.names (
name_id int identity,
first_name nvarchar(100),
last_name nvarchar(100),
primary key clustered (name_id)
)]
liquibase.exception.MigrationFailedException: Migration failed for change set liquibase_testing/resources/migrations/001-liquibase-testing-create-names-table.sql::raw::includeAll:
Reason: liquibase.exception.DatabaseException: There is already an object named 'names' in the database. [Failed SQL: CREATE TABLE dbo.names (
name_id int identity,
first_name nvarchar(100),
last_name nvarchar(100),
primary key clustered (name_id)
)]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:637)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
at liquibase.Liquibase.update(Liquibase.java:202)
at liquibase.Liquibase.update(Liquibase.java:179)
at liquibase.integration.commandline.Main.doMigration(Main.java:1220)
at liquibase.integration.commandline.Main.run(Main.java:199)
at liquibase.integration.commandline.Main.main(Main.java:137)
Caused by: liquibase.exception.DatabaseException: There is already an object named 'names' in the database. [Failed SQL: CREATE TABLE dbo.names (
name_id int identity,
first_name nvarchar(100),
last_name nvarchar(100),
primary key clustered (name_id)
)]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:356)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:57)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:125)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1229)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1211)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:600)
... 7 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: There is already an object named 'names' in the database.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:256)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1621)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:868)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:768)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2930)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:248)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:223)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.execute(SQLServerStatement.java:744)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:352)
... 12 common frames omitted
For more information, please use the --logLevel flag
So the names table gets created but then for some reason Liquibase tries to create it a second time and fails. I ran a profiler trace on the database and can see 2 CREATE TABLE calls.

In case you still have this problem... I would look into the database table DATABASECHANGELOG to see if the creation of that table is known to liquibase.
If not, you could either drop the table, or find out how do you inform liquibase that this changeset has already been applied (I am a liquibase beginner too... but it looks like the command line option "generateChangeLog" might be what you need...)

Just the same issue with Liquibase 3.2.0 + PostgreSQL.
!!! UPD: It's all ok with Liquibase & PostgreSQL.
It was Hibernate trying to create the table too, because of its ddl-auto setting. That's why CREATE TABLE was executed twice.

Related

while creating a table using changlog file in liquibase it shows error

While creating a table in snowflake using the sql queries with changelog file in liquibase.In the schema DATABASECHANGELOGLOCK has created. But its shows the following error:
Unexpected error running Liquibase: SQL compilation error:
Object 'DATABASECHANGELOGLOCK' already exists. [Failed SQL: (2002) CREATE TABLE DATABASECHANGELOGLOCK (ID INT NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP_NTZ, LOCKEDBY VARCHAR(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))]
there is already saved your sql script file or changeset of xml file for liquibase in DATABASECHANGELOGLOCK table in database.
Resolution: you need to DELETE related row in DATABASECHANGELOGLOCK table in database

SQL Informix - adding foreign key

I am struggling while creating new database tables with foreign keys in Informix DB.
I need to migrate from one database to another. I am using DBeaver as DB browsing and editing tool. I am trying to run script like this (I just changed names). The commands come from DBeaver DDL export itself, so this is how it should be.
DROP TABLE IF EXISTS eapp:test_table1;
CREATE TABLE eapp:test_table1 (
column1 smallint NOT NULL,
PRIMARY KEY (column1) CONSTRAINT table1_pk
);
DROP TABLE IF EXISTS eapp:test_table2;
CREATE TABLE eapp:test_table2 (
column1 smallint NOT NULL,
column2 varchar(100) NOT NULL,
column3 smallint NOT NULL,
column4 smallint,
PRIMARY KEY (column1) CONSTRAINT table2_pk,
FOREIGN KEY (column4) REFERENCES eapp:test_table1(column1) ON DELETE RESTRICT ON UPDATE RESTRICT CONSTRAINT table2_fk
);
However, execution keeps crashing with a weird trimmed error message:
The language is Czech and it says something like "Syntax error. (Important notice: Database servers, that". But the rest is trimmed even in detail, so I have no idea what is going on.
Full stack:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [42000]: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:134)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:473)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$0(SQLQueryJob.java:412)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:405)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:865)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:2833)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:98)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:96)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$17.run(ResultSetViewer.java:3356)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:103)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLException: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.a(IfxSqli.java:3537)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3853)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2653)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2569)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2446)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2421)
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:393)
at com.informix.jdbc.IfxStatement.a(IfxStatement.java:1326)
at com.informix.jdbc.IfxStatement.executeImpl(IfxStatement.java:1296)
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1007)
at com.informix.jdbc.IfxStatement.execute(IfxStatement.java:893)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:338)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Caused by: java.sql.SQLException
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3858)
... 23 more
I was unable to Google message like that, so I am trying to ask here, if anyone has some experience with Informix DB and could possibly give me some hints.
I already tried to remove FK declaration from table creation and add it afterwards via ALTER TABLE, but it yields the same...
DBeaver is not generating valid Informix DDL. Informix does not understand ON DELETE RESTRICT ON UPDATE RESTRICT syntax.
From the online manual:
REFERENCES Clause
|--REFERENCES--table--+------------------+---------------------->
| .-,------. |
| V | |
'-(----column-+--)-'
>--+-------------------------+----------------------------------|
| (1) |
'-------ON DELETE CASCADE-'

flyway hsql db create tables sql migrate hangs

I am executing flyway using maven.
I have a sql(It has DDL create tables - DEPARTMENT , EMPLOYEE as shown below)
I run mvn compile flyway:migrate
Here is the console log.
......
[INFO] Successfully validated 3 migrations (execution time 00:00.097s)
[DEBUG] Schema "PUBLIC" already exists. Skipping schema creation.
[DEBUG] Locking table "PUBLIC"."schema_version"...
[DEBUG] Lock acquired for table "PUBLIC"."schema_version"
[INFO] Current version of schema "PUBLIC": 1
[INFO] Migrating schema "PUBLIC" to version 1.1 - department
[DEBUG] Found statement at line 2: CREATE TABLE Department (
ID INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1) PRIMARY KEY,
NAME VARCHAR(32) NOT NULL ,
DESCRIPTION VARCHAR(100)
)
[DEBUG] Found statement at line 8: CREATE TABLE EMPLOYEE (
ID INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1) PRIMARY KEY ,
NAME VARCHAR(100) NOT NULL ,
DEPARTMENTID INTEGER FOREIGN KEY REFERENCES PUBLIC.DEPARTMENT(ID)
)
[DEBUG] Executing SQL: CREATE TABLE Department (
ID INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1) PRIMARY KEY,
NAME VARCHAR(32) NOT NULL ,
DESCRIPTION VARCHAR(100)
)
The execution hangs after first create table i.e Department table.
Then I kill using ctrl+C I see schema_version and Department table alone gets created.
I tried other ways of create table i.e without ID Generation , adding ';' at end , adding GO after each CREATE TABLE but it did not help.
The same create table sql runs successfully using Squirrel SQL client.
I am using flyway version 4.0.3 and hsqldb 2.3.4.
On debug it is seen that it is waiting for response I/O from DB to complete at this point =>
boolean hasResults = statement.execute(sql); within the class org.flywaydb.core.internal.dbsupport.JdbcTemplate
UPDATE:
As mentioned by Fredt , this issue is not seen when using hsqldb-2.3.3
This issue is specific to HSQLDB 2.3.4 which has more strict locking. You can set the transaction model to MVCC to avoid the issue. Alternatively use HSQLDB version 2.3.3.

Liquibase ignorable comments

Is there a way we can add comments in liquibase file which are not parsed by the program?
We are using the text format for the changes.sql and this is how it looks
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
create table liqui_test9 (
id int primary key,
name varchar(255)
);
create table liqui_test10 (
id int primary key,
name varchar(255)
);
Our organization has been using similar change log for years and while migrating to Liquibase we want to be able to do two things.
Add dashes or hashes after each changeset.
And after every production build we add a comment at the end of the changes file.
For instance
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
-----------------------------------------------------------------
--changeset Sapan.Parikh:MyUniqueAlphaNumericId4
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test12 (
id int primary key,
name varchar(255)
);
###------------------Build 10.0.1 Made-------------------
Now if we add just dashes- or # the luqibase task is breaking and DB upgrade does not happen. Is there a way to accommodate comments which are not parsed by liquibase engine?
You can just persist your comments and strip them right before executing liquibase
- can be done easily using sed

"There is already an object named XXXX in the database" after creating New database

I have an SQL file which I am executing on an SQL Server instance which contains the schema for a database. The file creates a brand new database (as in, a database with this name does not exist on this server):
CREATE DATABASE PROJECT;
and begins to create a relation:
CREATE TABLE Courses (
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
);
...
and here is what SQL Server tells me right off the bat:
Msg 2714, Level 16, State 6, Line 3
There is already an object named 'Courses' in the database.
Any idea why SQL Server tells me that there already exists a relation by the name of Courses when clearly there isn't?
Thank you for your time.
Check the database if you are using PROJECT
CREATE DATABASE PROJECT
GO
USE PROJECT
GO
CREATE TABLE Courses
(
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
)
GO
You are likely missing USE PROJECT statement and therefore trying to create Courses table in the master database, not in the PROJECT database.
Long shot but try using:
Use project;
CREATE TABLE Courses....