2 databases on the one H2 instance - spring

I have JPA Repository with #Query that joins tables from other DBs, that located on the same server.
SELECT id,co.name from Agenc a inner join[other_db_mame].[schema_name].[table_name] co .....
I want to write integration tests to cover the flow with this query.
In intégration tests I use H2 DB
And my question is, how to correctly configure H2 DB to have 2 DBs and make this query work?
Maybe there is a way, to create another db via scripts, or smth like this?

H2 supports direct access only to one database at once, but you can create linked tables to tables from other databases.
To create a linked table, you can use a CREATE LINKED TABLE command:
https://h2database.com/html/commands.html#create_linked_table
CREATE LINKED TABLE targetTableName('', 'jdbcURL', 'username', 'password', 'sourceTableName');
You can also link the whole schema with LINK_SCHEMA function:
https://h2database.com/html/functions.html#link_schema
CALL LINK_SCHEMA('targetSchemaName', '', 'jdbcURL', 'username', 'password', 'sourceSchemaName');
Note that format of fully-qualified table name in H2 (and in the SQL Standard) is catalogName.schemaName.tableName. H2 supports only one catalog (and its name is the same as a name of database) and you can't define additional catalogs. Non-standard -syntax with [identifier] is not accepted by H2 unless you use a MSSQLServer compatibility mode. In this mode you can use that syntax, but you can't have different names of catalogs anyway, so if they are fixed in your application, you have a problem.
Actually H2 can ignore the specified name of catalog if IGNORE_CATALOGS setting is set to TRUE:
https://h2database.com/html/commands.html#set_ignore_catalogs
SET IGNORE_CATALOGS TRUE;
But if combinations of schema and table name aren't unique in your original configuration, there is nothing to do with H2. You can't create different tables with the same schema and table name in H2 in any way.

Related

The problem of openGauss database connection schema

we encountered a problem when migrating the database from oracle to guassdb. It is a compatible version with postgresdb, which uses schema,
For example, select * from table1
Now because gauss uses schema, our table is in a01
The query becomes select * from a01.table1, which causes all the codes to be changed during the migration. The schema is added to the front of each table name, which is too big. Is there a better solution, such as adding schema to the url connection, because we only use one schema for each project
You can configure the user's search_path in the openGauss database. This parameter can also be configured through the url parameter in the jdbc.
for example: jdbc:postgresql://localhost:5432/mydatabase?currentSchema=a1

H2 set schema changes schema_search_path

If I have a schema_search_path set and I wish to create a bunch of tables using a common script by setting the schema and not explicating the
schema in the table create (common script could be used in multiple schemas, this also sets the schema_search_path to just the specified schema.
This seems like an undesirable side affect.
Value set by SET SCHEMA_SEARCH_PATH is not affected by any other commands.
But this value is only used when an identified is not qualified with the schema and an object with this name doesn't exist in the current schema (affected by SET SCHEMA command).
For example, tables referenced by non-qualified names are searched in the following order:
Tables of the current schema.
Local temporary tables. (Currently they also include query aliases from the WITH clauses, but this may be changed when somebody will implement a separate scope of identifiers for these views.)
Tables of each schema from SCHEMA_SEARCH_PATH, if any. When multiple schemas are specified, they order has a meaning, they are processed in the same order.
Legacy or compatibility tables, such as DUAL or SYSDUMMY1 in DB2 and Derby compatibility modes.
The first table matched by its name will be used.
This is a complex case, for the most types of database objects only steps (1) and (3) are performed.
If you think that something is not going as described here and you can create a standalone test case (Java / JDBC / SQL only, no third-party libraries), you can create a bug report on GitHub:
https://github.com/h2database/h2database/issues

Liquibase Oracle: Generate changelog tries to create objects from another schema

I want to generate a changelog XML from an existing Oracle schema, let's name it A. This schema contains references to another schema, schema B. Tables in schema A for example contain foreign keys referencing tables in schema B. User A has only SELECT and REFERENCES privileges on the tables of schema B.
When I try to create a database changelog for schema A, tables and constraints from B are included, even though they are not owned by user A. Is there any way to change this behavior? I tries to set the defaultCatalogName, defaultSchemaName, changelogCatalogName and changelogSchemaName parameters, but nothing changed.
It should work the way you expect, but due to bug https://liquibase.jira.com/browse/CORE-1784 it is not working as expected. It is fixed for the upcoming 3.2.0 release, probably out in mid March.

Do two users access the same database or different?

I installed Oracle on my system, so now orcl is the SID, which is the unique identifier of my database instance.
Now starter db was created as part of the installation. I created 2 users user1 and user2 using the system account.
Using SQL developer I am accessing the users, this shows me 2 different connections with all the database objects like tables, stored procedures views etc.
so
When using these 2 users, am I accessing the same database? I am giving all the ddl commands by logging into the user1 or user 2, does all this data goes into the same .dbf file?
The database instance can be connected to only one database, then does this essentially mean that everytime I create a new database, to make a database instance to point to that, I need to do a configuration change?
In my experience with Oracle, the typical unit of division is a schema. Schemas in Oracle are used more like you would use databases in SQL Server or PostgreSQL. They represent both users and a logical separation of objects. Physical separation would usually be done using tablespaces. Tablespaces are a group of physical files where data is stored. Schemas can share or use different tablespaces. Having one tablespace per schema is uncommon; they usually share a few tablespaces or often even just one.
With that in mind, to answer your questions more directly,
1) Like in any other database, you can specify the schema the object belongs to:
CREATE TABLE MY_SCHEMA.TABLE_X ( X NUMBER )
If the schemas on two CREATE statements are different, then it will create different objects. What's different in Oracle is that the default schema changes for every user. The default schema is always the currently connected schema/user. So if you omit the schema like so:
CREATE TABLE TABLE_X ( X NUMBER )
then the implied schema is the currently connected schema/user. So if I'm logged in as MY_SCHEMA, then the above is equivalent to the first example. When connecting as two different users, then the implied schema will be different and the DDL is not equivalent between the two users. So running the same statement would create two different objects if you do not specify a schema.
The two objects may be stored in the same physical file if they are in the same tablespace. (They are most likely in the USERS tablespace if you did not create one explicitly and did not specify a different default tablespace when creating the schemas.) Regardless, they are still two completely separate objects.
If you specify the schema explicitly like in the first example, then the DDL is equivalent regardless of who executes it (although permissions may prevent some users from executing it). So it would result in creating the object once, and attempting to create it a second time would result in an error unless you're using CREATE OR REPLACE or something similar.
2) I don't know the answer to this question, but as I said, in Oracle, the basic unit of separation is usually the schema, not a database. I believe the question you're asking is a large part of the reason why the schemas are used in the way they are. Having multiple actual databases on the same machine/instance is far more difficult in Oracle than in other databases (if not impossible), so it's much simpler to have a single database with many schemas.

Oracle Database creation with exitsting tables

I've database with 15 tables. Now due to development process one column has to added to all the tables in the database. This changes should not affect the existing process because some other services are also consuming this database. So to accomplish it I thought of creating a new database. Is there are any other way to do it.
Usually it should be enough to create a new schema ("user") and create the tables in that new schema. In Oracle, identically named tables can exist in several schemas.
CREATE USER xxx IDENTIFIED BY yyy
you can create another schema for development and import the table to new schema.Developer should use the development schema instead of production schema.you can also create new database and import from current database but it might be last option
What's wrong with alter table T add (COL varchar2(5)); ?
Of course dependend stored procedures or packages become invalid.
You can leave them alone, then the first call would return an exception and auto-recompile the called procedure. Or you can alter procedure P compile;.

Resources