Using h2 in-memory database for unit testing with Spring. The production database is PostgreSQL. I have following script which works fine on Postgres but failing on h2:
CREATE TABLE address (
address_id SERIAL NOT NULL,
province_code char(2),
created_timestamp timestamp(35) with time zone DEFAULT now() NOT NULL
)
SQL State : 90051
Error Code : 90051
Message : Scale($"35") must not be bigger than precision("23");
Do I need to specify something else in the URL or configuration. Here is the url:
database.url=jdbc:h2:mem:myDb;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS TEST
Related
I am using Spring Boot and Spring Data JPA. In my application application is running over Oracle DB and Functional Test cases against the H2 DB.
I've to check if key is expired or not and its a Date field in DB. So Sysdate< ExpiryDate and with Oracle this works fine, but sysdate is not a function in H2 and I simply cant keep two queries 1 for Oracle and Other for H2.
#Query("SELECT new com......Test( "
+ "p.id,p.keyId, p.keyType, p.expiryDate...., sysdate) "
+ "FROM Table A p "
+ "WHERE ......")
List<EnableEncryptionKeyProjection> findBySrcClientId(....);
You could use current_timestamp for both.
I have
PHP 8.0
Ubuntu 20.4 running in a VM
Symfony Version 5
doctrine/orm 2.8.2
doctrine/dbal 2.12.1
doctrine/migrations 3.1.1
and oracle19c running
I have also some entities which I want now to make:migrate with doctrine. But at this point I stuck on this error:
More information:
In doctrine.yaml:
dbal:
default_connection: oracle
connections:
default:
schema_filter: ~^(?!t_cmdb_|m_cmdb_|migration_versions)~
mapping_types:
enum: string
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
oracle:
schema_filter: ~^(?!t_cmdb_|m_cmdb_|migration_versions)~
mapping_types:
enum: string
# configure these for your database server
url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
servicename: 'pdb'
service: true
driver: 'oci8'
server_version: ~
charset: AL32UTF8
I have a oracle database created with:
php bin/console doctrine:database:create --connection oracle
which goes through without any problems
Than I call
php bin/console make:migration -vvv
I got this error message:
[Doctrine\DBAL\Exception]
Unknown database type interval day(3) to second(2) requested, Doctrine\DBAL\Platforms\OraclePlatform may not support it.
All my date columns have the type datetime there is no interval in there!
I have searched for this problem but I can't come through.
Did somebody has this Problem while making migration for doctrine?
Thanks a lot for any hint.
Michael
Doctrine commands usually scan all tables in the user/schema defined in Symfony's .env file (look for DATABASE_USER). That error means that at least one of the tables in that schema has a column of type "INTERVAL...".
If you are sure that you don't have any table with an "INTERVAL..." type column, it may be that you are using directly the SYSTEM user/schema (or any other Oracle reserved user/schema). In this case, you need to create a separate user/schema for your project and move your project tables there.
Anyway, run this to check which tables under which user/schema (OWNER) have a column of type "INTERVAL...":
SELECT
OWNER,
TABLE_NAME ,
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
DATA_PRECISION,
DATA_SCALE
FROM ALL_TAB_COLUMNS
where
data_type like 'INTERVAL%'
-- and OWNER = 'YOUR_USER_SCHEMA' -- must be in uppercase
ORDER BY TABLE_NAME
If you do use such a column type in your project, then you must add that column type in Doctrine's configuration: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html
If you discover that you were using the SYSTEM user/schema directly (perhaps in a Docker container?), then create your separate project user/schema:
CREATE USER your_separate_user_schema IDENTIFIED BY oracle; -- oracle is the password
Grant create session to your_separate_user_schema ; -- to be able to login with the new user
ALTER USER your_separate_user_schema quota unlimited on USERS; -- give your user schema disk space
To copy only the necessary tables from SYSTEM to your_separate_user_schema: while in system/sysdba, for each table do:
create table your_separate_user_schema.table_name as select * from table_name ;
I'm trying to run in development mode (with H2) a setup currently used in production with a PostgreSQL database and I get an error. It would be best if I could reuse the production SQL without any change to it.
Using this setup:
# H2 Database
spring.datasource.datasource-class-name=org.h2.jdbcx.JdbcDataSource
spring.datasource.url=jdbc:h2:mem:userregistry;DB_CLOSE_DELAY=-1;MODE=PostgreSQL
This query:
CREATE TABLE IF NOT EXISTS users.user_userrole (
user_signum VARCHAR(20) NOT NULL,
role VARCHAR(255) NOT NULL,
CONSTRAINT user_userrole_pk PRIMARY KEY (user_signum, role),
CONSTRAINT user_fk FOREIGN KEY (user_signum) REFERENCES users.user (signum) MATCH SIMPLE,
CONSTRAINT role_fk FOREIGN KEY (role) REFERENCES users.userrole
(role_name) MATCH SIMPLE
);
Raises this exception:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "<<SQL OMITTED FOR BREVITY>>";
expected "INDEX, ON, NOT, DEFERRABLE, NOCHECK, CHECK, ,, )"; [42001-185]
Note that I am already using Mode=PostgreSQL. Any ideas?
Thanks
The H2 database does not support MATCH SIMPLE (same as Oracle, MS SQL Server, Apache Derby).
using h2 version h2-1.3.176 (with in memory db and also with local storage and also by tcp/ip)
I have strange lock, see below.
connection sample: jdbc:h2:mem:test1;MVCC=TRUE;LOCK_TIMEOUT=30000
it's not a timeout problem because the program is indefinitely locked.
I've noticed the error starts after I create a temporary table: the create temporary table just goes fine, but after some ddl query, the error appears.
There is'nt an error, if I don't create any temporary table.
PS: I can't use H2 1.4 because it's not stable: with that version I have the error "java.lang.IllegalStateException: Chunk XYZ not found"
Timeout trying to lock table "SYS"; SQL statement:
CREATE TABLE TestCalcField1(
a VARCHAR(255),
b VARCHAR(255),
i INTEGER ,
j INTEGER ) [50200-176]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
at org.h2.message.DbException.get(DbException.java:178)
at org.h2.message.DbException.get(DbException.java:154)
at org.h2.table.RegularTable.doLock(RegularTable.java:530)
at org.h2.table.RegularTable.lock(RegularTable.java:464)
at org.h2.engine.Database.lockMeta(Database.java:869)
at org.h2.command.ddl.CreateTable.update(CreateTable.java:134)
at org.h2.command.CommandContainer.update(CommandContainer.java:79)
at org.h2.command.Command.executeUpdate(Command.java:254)
at org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:132)
at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:117)
at it.novabyte.ntools.database.NQuery.executeUpdateSQL(NQuery.java:640)
... 21 more
I wonder why each refresh of the page of the website I'm writing, or moving between different pages, creates new session record (I'm storing them in DB instead of using standard solution). In other words, each single click (I'm not using JS / AJAX) creates new record / new session.
Here's my configuration of sessions in config file:
session:
lifetime: 7200
domain: MY_SERVER
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
services:
pdo:
class: PDO
arguments:
dsn: HOST
user: USER
password: PSWD
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: [#pdo, %pdo.db_options%]
I tried to use:
auto_start: false
But I'm getting then the following error from Symfony2.1:
The auto_start setting is deprecated. Just remove it from your configuration file.
So what should I do to have normal sessions?
UPDATE I also used the following:
arguments:
- "mysql:host=%database_host%;dbname=%database_name%"
- %database_user%
- %database_password%
that is, my basic Data Base settings for whole project (stored in parameters.yml).
This can happen if you do not have session_id set as primary key. Create the session table using (from the documentation, MySQL):
CREATE TABLE `session` (
`session_id` varchar(255) NOT NULL,
`session_value` text NOT NULL,
`session_time` int(11) NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
For other databases, see How to use PdoSessionHandler to store Sessions in the Database