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.
Related
Sometimes strange duplicate key error
duplicate key value violates unique constraint "session_pkey"
Key (workplace)=(WIN-N9BSKUNKBC8 ) already exists.
Occurs in script:
delete from session where workplace='WIN-N9BSKUNKBC8' ;
INSERT INTO session (workplace,ipaddress,logintime,loggeduser)
SELECT 'WIN-N9BSKUNKBC8' , inet_client_addr()::CHAR(14),current_timestamp::CHAR(28),CURRENT_USER WHERE NOT EXISTS (SELECT 1 FROM session WHERE workplace='WIN-N9BSKUNKBC8' )
Sript is running form windows task scheduler on every 10 minutes. Error occurs only sometimes.
How to fix this ?
Table is defined as
CREATE TABLE public.session
(
loggeduser character(10) ,
workplace character(16) NOT NULL,
ipaddress character(20) ,
logintime character(28) ,
activity timestamp with time zone,
CONSTRAINT session_pkey PRIMARY KEY (workplace)
)
Environment:
PostgreSQL 13.1, compiled by Visual C++ build 1914, 64-bit
Windows server 2019
psqlODBC driver 13.00.0000
You can use on conflict:
INSERT INTO session (workplace,ipaddress,logintime,loggeduser)
SELECT 'WIN-N9BSKUNKBC8',
inet_client_addr()::CHAR(14),current_timestamp::CHAR(28),
CURRENT_USER
ON CONFLICT DO NOTHING;
Given that you have only one row going in, my guess is that the problem is due to concurrency issues.
I'm learning DDL to create and define an SQL database with Postgresql 10.
I have the something like the following SQL code in an .sql file, and I want to input it in psql or PgAdmin 4, just to test the syntax and see the database structure:
CREATE DATABASE database;
CREATE TYPE t_name AS
( first VARCHAR(30),
last VARCHAR(60)
);
CREATE TABLE telephone_m
( tnumber VARCHAR(15) NOT NULL UNIQUE
);
CREATE TABLE people
( curp CHAR(18) NOT NULL PRIMARY KEY,
pname t_name NOT NULL,
birth_date DATE NOT NULL,
telephone_m VARCHAR(15) REFERENCES telephone_m
);
CREATE TABLE clients
( curp CHAR(18) NOT NULL PRIMARY KEY,
cid SERIAL NOT NULL REFERENCES cards,
clocation VARCHAR(29)
) INHERITS (people);
CREATE TABLE cards
( cid BIGSERIAL NOT NULL PRIMARY KEY,
curp CHAR(18) NOT NULL REFERENCES clients,
trips SMALLINT,
distance NUMERIC,
points NUMERIC
);
CREATE TABLE drivers
( curp CHAR(18) NOT NULL PRIMARY KEY,
rfc CHAR(22) NOT NULL UNIQUE,
adress t_adress NOT NULL
) INHERITS (people);
I've tried in PgAdmin 4 making right-click on a new database -> CREATE Script, it opens Query Editor, I copy paste my code and execute it, but it returns:
ERROR: CREATE DATABASE cannot be executed from a function or multi-command string
SQL state: 25001
I've also tried using Query Tool directly from the PgAdmin tools menu with the same results.
The database is created just fine. But if you want to create object in the new DB, you have to connect to it. In any client, including pgAdmin4.
And you cannot run CREATE DATABASE inside of a transaction. Must be committed on it's own. Executing multiple commands at once is automatically wrapped into a single transaction in pgAdmin.
You have to execute CREATE DATABASE mydb; on its own (for instance by selecting only that line and pressing F5 while being connected to any DB, even the maintenance db "postgres". Then click on "Databases" in the object browser in the pgadmin4 main window / tab, hit F5 to refresh the view, click on the new DB, open up a new query tool with the flash icon (in a new window / tab) and execute the rest of your script there.
psql scripts manage by using the meta-command \c to connect to the new db after creating it, within the same session.
Asides:
"database" is no good name for a database.
CREATE TYPE AS (...), but just CREATE TABLE (...). No AS.
And you typically don't want to use the data type CHAR(18). See:
Any downsides of using data type "text" for storing strings?
Get sum of integers for UNIQUE ids
What is the overhead for varchar(n)?
Should I add an arbitrary length limit to VARCHAR columns?
There is the ; missing after the CREATE DATABASE database (and perhaps give the db a better name).
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.
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
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....