I can`t see my created tables in HSQLDB, what could be the issue? - maven

My environment is HSQLDB, Maven and spring-boot.
I have created 2 entity POJOs. I do see the CREATE TABLE command under testedb.log file. But when I open Data Source Explorer in Eclipse, I can`t see my tables, albeit I do see all the system tables.
I have looked at this question too, but no vail: Where can I see the HSQL database and tables
Here is my partial pom.xml:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<scope>runtime</scope>
</dependency>
Here is my partial application.properties:
# DataSource
#spring.datasource.driverClassName=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:file:resources/db/testedb;DATABASE_TO_UPPER=false
#spring.datasource.url=jdbc:hsqldb:mem:memTestdb
spring.datasource.username=sa
spring.datasource.password=
# Hibernate
spring.jpa.show-sql=true
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=create
And below is my HSQLDB created on disk:
HSQLDB folder in my workspace
Here is my partial testedb.script:
SET FILES LOG SIZE 50
CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e'
ALTER USER SA SET LOCAL TRUE
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
SET SCHEMA PUBLIC
CREATE SEQUENCE PUBLIC.HIBERNATE_SEQUENCE AS INTEGER START WITH 1
CREATE MEMORY TABLE PUBLIC.ENQUIRY_HISTORY(ID BIGINT NOT NULL PRIMARY KEY,FROM_AMOUNT DOUBLE NOT NULL,FROM_CURRENCY VARCHAR(255) NOT NULL,QUERY_DATE TIMESTAMP NOT NULL,TO_AMOUNT DOUBLE NOT NULL,TO_CURRENCY VARCHAR(255) NOT NULL,USER_ID BIGINT NOT NULL,VERSION INTEGER NOT NULL)
CREATE MEMORY TABLE PUBLIC.USERS(ID BIGINT NOT NULL PRIMARY KEY,EMAIL VARCHAR(255) NOT NULL,LAST_LOGIN TIMESTAMP NOT NULL,PASSWORD VARCHAR(255) NOT NULL,VERSION VARCHAR(255) NOT NULL)
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
ALTER SEQUENCE PUBLIC.HIBERNATE_SEQUENCE RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
Please see above that the CREATE TABLE contains the word MEMORY even though I have created file DB.
And by testsdb.log:
/*C12*/SET SCHEMA PUBLIC
drop table enquiry_history if exists
drop table users if exists
drop sequence hibernate_sequence if exists
create sequence hibernate_sequence start with 1 increment by 1
create table enquiry_history (id bigint not null, from_amount float not null, from_currency varchar(255) not null, query_date timestamp not null, to_amount float not null, to_currency varchar(255) not null, user_id bigint not null, version integer not null, primary key (id))
create table users (id bigint not null, email varchar(255) not null, last_login timestamp not null, password varchar(255) not null, version varchar(255) not null, primary key (id))
/*C14*/SET SCHEMA PUBLIC
DISCONNECT
/*C17*/SET SCHEMA PUBLIC
And Finally here is my screen shot of Database
Data Source Explorer
Any pointer will be awesome, thanks for your time.

file and mem are in-process modes. For testing/debugging, if you need concurrent access to data from another process, start database in Server mode.
Check various available modes here.

I was able to see the tables using in build HSQLDB interface, now a fancy one but it still works for me.
I used the following [listed in this answer https://stackoverflow.com/a/35240141/8610216]
java -cp /path/to/hsqldb.jar org.hsqldb.util.DatabaseManager
And then specify path your database:
jdbc:hsqldb:file:mydb
There was one more thing that I was doing incorrect; it was the path of the db. The correct path is spring.datasource.url=jdbc:hsqldb:file:src/main/resources/db/userx;DATABASE_TO_UPPER=falsein application.properties.

Related

Database migration cause errors for H2 database

When i run tests the following migration file causes an
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "ALTER TABLE ACCOUNT
ADD IS_PROVIDER_ROOT_ACCOUNT VARCHAR(1) NOT NULL,[*]
ADD PROVIDER_ORGANISATION_ID VARCHAR(255) NULL"; SQL statement:
alter table account
add is_provider_root_account varchar(1) not null,
add provider_organisation_id varchar(255) null [42000-200]
error
alter table account
add is_provider_root_account varchar(1) not null,
add provider_organisation_id varchar(255) null;
The thing is, if I remove any one of the adds there are no errors. So what can I do here?
My testing configuration file:
spring.datasource.url=jdbc:h2:mem:testdb:MODE=MYSQL
spring.datasource.username=sa
spring.datasource.password=secret
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
Looking at the H2 syntax (note parentheses), when adding multiple columns, one should do:
alter table account
add (
is_provider_root_account varchar(1) not null,
provider_organisation_id varchar(255) null
);

How to use schema-h2.sql

Spring Boot 2.4.3.
schema-h2.sql seems to have no effect.
The H2 database is created, but no tables are created.
schema-h2.sql:
DROP TABLE IF EXISTS Blogger;
CREATE TABLE Blogger(
id bigint NOT NULL,
name varchar(100),
age int,
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS Story;
CREATE TABLE Story(
id bigint NOT NULL,
title varchar(100),
content varchar(400),
posted date,
blogger_id int,
PRIMARY KEY (id)
);
application.properties:
spring.thymeleaf.cache=false
spring.web.locale-resolver=fixed
spring.web.locale=en
spring.h2.console.enabled=true
spring.h2.console.path=/db
spring.datasource.url=jdbc:h2:mem:testdb
Any advice?
Thank you!
Found the solution, I need to add spring.datasource.platform=h2 property

How to get DataMapper to not specify a NULL default value

I'm getting an error when I use DataMapper's auto_upgrade! method to add fields in an SQLite3 db based on the properties defined in my code:
DataObjects::SyntaxError at /history
Cannot add a NOT NULL column with default value NULL
An example of an offending line would be:
property :fieldname, Text, required: true
The error goes away if I (a) remove the line, (b) remove required: true, (c) change true to false, or (d) add a default value.
SQLite does not require a default value to be specified for every field, so this problem is definitely with DataMapper, not SQLite.
How can I get around this, so DataMapper can specify that a field is required without assuming that not specifying a default value automatically means the default should be NULL?
(If you want to know more about why I'm designing this way: there will be another client process accessing SQLite and logging data into the SQLite database, while a Sinatra app will be pulling data out of the db for display in a browser. I want the database therefore to enforce the field requirements, but DM's auto_upgrade is a very convenient way to be able to upgrade the database as needed—so long as it doesn't foul things up in the process.)
You are requiring the field, hence it cannot be NULL. This is simple table properties.
When DataMapper runs auto_upgrade! it runs the SQL commands on the database.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
And doing something like this won't work.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL DEFAULT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
I tested it in MySQL and this is the error.
02:52:43 CREATE TABLE TestTest ( P_Id int NOT NULL, lname
varchar(255) NOT NULL DEFAULT NULL, fname varchar(255), Address
varchar(255), City varchar(255) ) Error Code: 1067. Invalid default
value for 'lname' 0.062 sec
Correction: SQLite does allow you to create a table with such properties. However, when trying to insert anything to that table makes it throw an error whether the field is NULL or not. So DataMapper might be doing some sanitation for your before even creating the table.
It is not clear to me if you are creating a new table or modifying an existing one.
If you have an existing table and are trying to alter it with a column defined as NOT NULL, then you must provide a default value so that the existing rows can be migrated. The RDBMS needs to know what to put in the field for pre-existing rows.
If you are creating a new table, then the property definition you have should be fine.

How to solve SchemaVersionTrackingException in dbdeploy due to no default value in delta_set?

I am trying to do automatic DB migration. I am using dbdeploy for the same.
I followed the steps in this link http://blog.codeborne.com/2012/09/using-dbdeploy-in-gradle.html
I created the change log table as:
CREATE TABLE changelog (
change_number INTEGER NOT NULL,
delta_set VARCHAR(10) NOT NULL,
start_dt TIMESTAMP NOT NULL,
complete_dt TIMESTAMP NULL,
applied_by VARCHAR(100) NOT NULL,
description VARCHAR(500) NOT NULL
);
ALTER TABLE changelog ADD CONSTRAINT Pkchangelog PRIMARY KEY (change_number, delta_set);
The updateDatabase task in build.gradle is:
task updateDatabase << {
ant.dbdeploy(driver: dbDriver,
url: dbUrl,
userid: dbUsername,
password: dbPassword,
dir: './src/main/resources/deploy/sql',
dbms: 'mysql'
)
}
When I do gradle updateDatabase, I get com.dbdeploy.exceptions.SchemaVersionTrackingException: Could not update change log because: Field 'delta_set' doesn't have a default value.
I tried assigning 'main' as default value in the change table log file as:
delta_set VARCHAR(10) NOT NULL DEFAULT 'Main'
But, I still got the same exception.
I also removed the delta_set attribute, I got the same exception. This really confused me.
I am completely new to datamigration. So, any help regarding this error and how I should go about it will be deeply appreciated.
Thank you in advance.
The DBDeploy documentation isn't very explicit about this, but the changelog table format changed between versions 2.X and 3.X (see the upgrade instructions). I suspect that you are using DBDeploy 3.X.
You need to:
Remove the old changelog table:
DROP TABLE changelog;
Recreate it using the new format:
CREATE TABLE changelog (
change_number INTEGER NOT NULL,
complete_dt TIMESTAMP NOT NULL,
applied_by VARCHAR(100) NOT NULL,
description VARCHAR(500) NOT NULL
);
ALTER TABLE changelog ADD CONSTRAINT Pkchangelog PRIMARY KEY (change_number);
After this, everything should work.

ROO: how to create composite primary key in Entity

What can I do if I need to create entity for
a table in production DB (Oracle 10g) with composite primary key.
For example:
[CODE]
CREATE TABLE TACCOUNT
(
BRANCHID NUMBER(3) NOT NULL,
ACC VARCHAR2(18 BYTE) NOT NULL,
DATE_OPEN DATE NOT NULL,
DATE_CLOSE DATE,
NOTE VARCHAR2(38 BYTE)
);
CREATE UNIQUE INDEX PK_TACCOUNT ON TACCOUNT
(BRANCHID, ACC);
I don't want to change the structure of this table.
Is it possible to create an "id" field using roo commands?
I use Spring Roo 1.0.2.RELEASE [rev 638]

Resources