Flyway Found non-empty schema on empty schema - java

I am trying to implement DB migration with Flyway 4.2.0 + Oracle 11g
I have this empty schema:
And when I try to migrate, Flyway says:
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty
schema(s) "PASHA" without metadata table! Use baseline() or set
baselineOnMigrate to true to initialize the metadata table.
This is the config:
#Bean(initMethod = "migrate")
Flyway flyway() {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(false);
flyway.setSchemas("PASHA");
flyway.setLocations("classpath:db/migration/oracle");
flyway.setDataSource("jdbc:oracle:thin:#host:1521:test", "login", "password");
return flyway;
}
Why do I get this message? My base is empty.

Flyway itself uses a query to check if the schema is empty.
In the case of oracle, the query is:
SELECT * FROM ALL_OBJECTS WHERE OWNER = ?
Execute that query (with your owner in the place of ?) and see if it returns something (it does).
For instance, LOBs that haven't been purged show there. If that's the case, try:
purge recyclebin;
and the query should be empty now.

You need to either let Flyway create the schema itself (meaning there should not be a 'PASHA' schema created before hand), or baseline the existing schema (meaning setting your configuration with flyway.setBaselineOnMigrate(true) ).
Basically, Flyway tries to create a schema ('PASHA' in your example) which already exists.

Adding all of these helped. But the one without spring actually did the trick! Silly as it is, but just worked!
spring.flyway.baselineOnMigrate=true
spring.flyway.baseline-on-migrate = true
flyway.baseline-on-migrate= true
flyway.baselineOnMigrate=true

Related

Hibernate #Formula / ERROR: schema "FOO" does not exist

I'm updating old libraries from a legacy system. Just now i'm trying to update Hibernate 3.4.0.GA to 4.3.11.Final, i just needed to change small things in the code, everything was fine. But when i put the system to run, i'm receiving a "schema "FOO" does not exist" while execute a query. Trying to isolate the problem, i discovered this happen from Hibernate 3.5.1 to 3.5.2 and the reasons.
Hibernate when generating the sql, is adding schema to functions. I show now the difference in two versions.
protocolo_1 is the alias of main schema, this is a subquery added by #Formula in Protocolo.java, the name of schema is protocolo too.
#Formula
select max (pm2.id) from protocolo.protocolomovimento pm2 where pm2.id_protocolo = id
Hibernate 3.5.1 SQL generated
select max (pm2.id) from protocolo.protocolomovimento pm2 where pm2.id_protocolo = protocolo1_.id
Hibernate 3.5.2 SQL generated
select protocolo_1.max (pm2.id) from protocolo.protocolomovimento pm2 where pm2.id_protocolo = protocolo1_.id
I'm using PostgreSQL 9.4.12 with respective driver and org.hibernate.dialect.PostgreSQLDialect (in this versions of hibernate, it's the unique dialect to PostgreSQL)
I found another guy with similar problem here Why is Hibernate adding schema name to Hsql functions? but i think its only similar, it's not my case.
Why is Hibernate doing this? How can i fix this?
Looks like hibernate don't understand space character between max and ( in expression max (pm2.id), so it thinks that max is column name and adds table alias there.
Removing space will solve the problem.

HsqlException: type not found or user lacks privilege: NUMBER

I'm using hsql 2.3.3. According to documentation I can fix this either by adding additional statement to the sql
SET DATABASE SQL SYNTAX ORA TRUE
or by adding property to the url
url: jdbc:h2:mem:test;sql.syntax_ora=true
First one fixes the issue for me (so this is root cause of it), but I would like to use second one as it looks more generalized for me. But url property does't do the trick. What I'm missing?
The URL property works only if you create a new database with the connection property. The new database will then include the SQL statement and run it automatically at each startup.

separate persistence context for PostgreSQL's schema [duplicate]

Is it possible? Can i specify it on the connection URL? How to do that?
I know this was answered already, but I just ran into the same issue trying to specify the schema to use for the liquibase command line.
Update
As of JDBC v9.4 you can specify the url with the new currentSchema parameter like so:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
Appears based on an earlier patch:
http://web.archive.org/web/20141025044151/http://postgresql.1045698.n5.nabble.com/Patch-to-allow-setting-schema-search-path-in-the-connectionURL-td2174512.html
Which proposed url's like so:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
As of version 9.4, you can use the currentSchema parameter in your connection string.
For example:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
If it is possible in your environment, you could also set the user's default schema to your desired schema:
ALTER USER user_name SET search_path to 'schema'
I don't believe there is a way to specify the schema in the connection string. It appears you have to execute
set search_path to 'schema'
after the connection is made to specify the schema.
DataSource – setCurrentSchema
When instantiating a DataSource implementation, look for a method to set the current/default schema.
For example, on the PGSimpleDataSource class call setCurrentSchema.
org.postgresql.ds.PGSimpleDataSource dataSource = new org.postgresql.ds.PGSimpleDataSource ( );
dataSource.setServerName ( "localhost" );
dataSource.setDatabaseName ( "your_db_here_" );
dataSource.setPortNumber ( 5432 );
dataSource.setUser ( "postgres" );
dataSource.setPassword ( "your_password_here" );
dataSource.setCurrentSchema ( "your_schema_name_here_" ); // <----------
If you leave the schema unspecified, Postgres defaults to a schema named public within the database. See the manual, section 5.9.2 The Public Schema. To quote hat manual:
In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema.
I submitted an updated version of a patch to the PostgreSQL JDBC driver to enable this a few years back. You'll have to build the PostreSQL JDBC driver from source (after adding in the patch) to use it:
http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php
http://jdbc.postgresql.org/
In Go with "sql.DB" (note the search_path with underscore):
postgres://user:password#host/dbname?sslmode=disable&search_path=schema
Don't forget SET SCHEMA 'myschema' which you could use in a separate Statement
SET SCHEMA 'value' is an alias for SET search_path TO value. Only one
schema can be specified using this syntax.
And since 9.4 and possibly earlier versions on the JDBC driver, there is support for the setSchema(String schemaName) method.

Hibernate save SQL exception

Hi guys I am trying to save an object to a MySQL data base via Hibernate. if I execute following code
User user = new User();
user.setData_1("my data 5");
user.setFirstname("Freddy");
user.setLastname("Bob");
user.setId(5);
session.save(user);
session.getTransaction().commit();
I get a
'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'xxx.my_table_1' doesn't exist'
exception. However, querying from the same table using the same config works just fine.
What could be the issue?
Check your connection string in the configuration something like hibernate.connection.url = jdbc:postgresql://localhost/mydatabase You may be missing the schema name in the url(mydatabase).
So, after some trial and error, I came to find out that the issue with the .get() (and apparently .save() too) was that I did not have a hibernate.default_schema set in config. Looks like it is used to create the 'dynamic' SQL for .save() and .get(), but if you use .createSQLQuery(), it just uses what ever String you pass as an argument for the SQL, and therefor works with out needing to have hibernate.default_schema set.

Querying the appropriate database schema

This is a follow-on question to my earlier question about specifying multiple schemata in java using jooq to interact with H2.
My test H2 DB currently has 2 schemata, PUBLIC and INFORMATION_SCHEMA. PUBLIC is specified as the default schema by H2. When running a query that should extract information from eg INFORMATION_SCHEMA.TABLES the query fails with a "table unknown" SQL error. I am only able to execute such queries by executing a factory.use(INFORMATION_SCHEMA). There are no build errors etc and eclipse properly autocompletes eg TABLES.TABLE_NAME.
If I dont do this, jooq doesnt seem to prepend the appropriate schema even though I create the correct Factory object for the schema eg
InformationSchemaFactory info = new InformationSchemaFactory(conn);
I read about mapping but am a bit confused as to which schema I would use as the input/output.
By default, the InformationSchemaFactory assumes that the supplied connection is actually connected to the INFORMATION_SCHEMA. That's why schema names are not rendered in SQL. Example:
// This query...
new InformationSchemaFactory(conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL (with the asterisk expanded):
SELECT * FROM "TABLES";
The above behaviour should be documented in your generated InformationSchemaFactory Javadoc. In order to prepend "TABLES" with "INFORMATION_SCHEMA", you have several options.
Use a regular factory instead, which is not tied to any schema:
// This query...
new Factory(H2, conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL:
SELECT * FROM "INFORMATION_SCHEMA"."TABLES";
Use another schema's factory, such as the generated PublicFactory:
// This query...
new PublicFactory(conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL:
SELECT * FROM "INFORMATION_SCHEMA"."TABLES";
Use Settings and an appropriate schema mapping to force the schema name to be rendered.
The first option is probably the easiest one.
This blog post here will give you some insight about how to log executed queries to your preferred logger output: http://blog.jooq.org/2011/10/20/debug-logging-sql-with-jooq/

Categories