I'm using Flyway on a SpringBoot Project to manage my migrations.
These are the options in the application.properties file:
spring.flyway.locations=classpath:DB/migration
flyway.enabled = true
spring.flyway.baselineOnMigrate = true
And these are the options in the build.gradle file:
flyway {
url = "****"
user = "****"
password = "****"
baselineOnMigrate = true
}
On this DB it seems the migrations were successfully applied, I can see in the flyway_schema_history table all the migrations with the success column set to 1.
But I can't run the application because none of the table/column of the migrations scripts were inserted in the database.
Related
Hei guys, im pretty new to SpringBoot and i have 1 problem.
When i start the app the spring read the schema.sql and make the tabels but the data.sql is not read, so i dont have no data on my db.
this is my app.prop
spring.datasource.url = jdbc:mysql://localhost:3306/garagesaledb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.sql.init.mode=always
spring.datasource.schema = classpath:db/schema.sql
spring.datasource.data = classpath:db/data.sql
spring.jpa.hibernate.ddl-auto=update
spring.liquibase.drop-first=true
spring.jpa.defer-datasource-initialization=true
schema.sql
create table asset
(
id bigint not null auto_increment,
category varchar(255),
price decimal(10,2),
quantity integer,
purchaseOrder_id bigint,
primary key(id),
);
data.sql
INSERT INTO garagesaledb.asset(category, price, quantity,purchaseOrder_id) VALUES
('MOUSE', 10.0, 1, null);
It's just a simple app and query but it doesnt read it.
Either you put the file somewhere outside the resources (external file) or provided the location wrongly, - please check the logs
To confirm that, change below line
spring.datasource.data = classpath:db/data.sql
to :
spring.datasource.data = file:<full-path>db/data.sql
OR
in some spring-boot versions,
spring.sql.init.data-locations=classpath:db/data.sql
Add this property to app.prop spring.datasource.initialization-mode=always
Refer Link : https://docs.spring.io/spring-boot/docs/2.1.0.M1/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
Spring Boot automatically creates the schema of an embedded DataSource. This behaviour can be customized by using the spring.datasource.initialization-mode property. For instance, if you want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
I hope this helps.
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
First of all please review my steps which I followed are correct or not.
When I create new user by URl http://com.cust:8080/sampleapp/user/create it's working but issue is that is not using database which I configure for domain com.cust i.e custdb instead using sampleappdb which is global in DataSource.groovy.
And in user table its adding tenant id with 0 as Value instead 2
And on console I'm getting error Exception in Multi-tenant data source provider
javax.naming.NameNotFoundException: Name jdbc:mysql: is not bound in this Context
I am configuring a basic sample application with multi-tenant plugin.
I have followed below steps to configure multi-tenant plugin.
Created sampleapp grails project
Executed grails create-domain-class com.myapp.User
Executed generate-all com.myapp.User
configured MySQL database to use
installed plugins
plugins.multi-tenant-core=1.0.3
plugins.multi-tenant-ehcache=1.0.1
plugins.falcone-util=1.0
Executed command grails create-data-source-map
Executed command grails create-dns-map
Executed command generate-all tenant.DataSourceTenantMap
Executed command generate-all tenant.DomainTenantMap
Annotated User domain class with #MultiTenant
package com.myapp
import grails.plugin.multitenant.core.groovy.compiler.MultiTenant
#MultiTenant
class User {
String firstName;
String lastName;
static constraints = {
}
}
Added tenant block in config.groovy
tenant {
mode = "singleTenant"
datasourceResolver.type = "db"
resolver.request.dns.type = "config"
resolver.type = "request"
domainTenantMap {
localhost = 1
com.cust = 2
}
}
Executed below sql command
INSERT INTO data_source_tenant_map ( VERSION, data_source, mapped_tenant_id) VALUES (0 , "jdbc:mysql://localhost:3306/cust1db?user=user&password=pwd" , 1)
INSERT INTO data_source_tenant_map ( VERSION, data_source, mapped_tenant_id) VALUES (0 , "jdbc:mysql://localhost:3306/custdb?user=user&password=pwd" , 2);
INSERT INTO domain_tenant_map ( VERSION, domain_name, mapped_tenant_id, NAME) VALUES (0 , 'localhost', 1,'Sample Localhost');
INSERT INTO domain_tenant_map ( VERSION, domain_name, mapped_tenant_id, NAME) VALUES (0 , 'com.cust', 2,'127.0.0.1');
DataSource.groovy
development {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/sampleappdb?autoreconnect=true"
}
}
Using grails 1.3.7 version.
Please help stuck from last 1 week. Thank You.
We are evaluating flyway to setup an Oracle database that has an OWNER schema where all tables live, and an USER, who gets granted access to OWNER tables in the form of synonyms. Sometimes the USER needs to write their own tables, too. We'd also like to keep the upgrade sql scripts in one subdirectory, rather than having a separate directory for OWNER and USER each.
So, OWNER has the following config:
flyway.url=jdbc:oracle:thin:#localhost:1521
flyway.user=OWNER
flyway.schemas=OWNER
flyway.password=OWNER
and USER, correspondingly
flyway.url=jdbc:oracle:thin:#localhost:1521
flyway.user=USER
flyway.schemas=OWNER, USER
flyway.password=USER
where i would expect that flyway will access the SCHEMA_VERSION table in the OWNER schema, as it is the first one in the list of schemas.
The whole thing runs in vagrant, and when i say vagrant up i get an error message. The relevant snippet is:
==> vagrantbox: Successfully applied 1 migration to schema "OWNER" (execution time 00:00.388s).
==> vagrantbox: flyway -configFile=config/USER.conf -target=1.99999 migrate
==> vagrantbox: Flyway 3.2.1 by Boxfuse
==> vagrantbox: Database: jdbc:oracle:thin:#localhost:1521 (Oracle 11.2)
==> vagrantbox: Validated 4 migrations (execution time 00:00.119s)
==> vagrantbox: Creating Metadata table: "OWNER"."schema_version"
==> vagrantbox: ERROR:
==> vagrantbox: Script failed
==> vagrantbox: -------------
==> vagrantbox: SQL State : 42000
==> vagrantbox: Error Code : 1031
==> vagrantbox: Message : ORA-01031: insufficient privileges
==> vagrantbox: Line : 17
==> vagrantbox: Statement : CREATE TABLE "OWNER"."schema_version" (
which looks like flyway is able to determine that the schema_version table for USER is supposed to be in the OWNER schema, but fails to see that it's already there.
In the flyway sources i see that this check is done in MetadataTableImpl like this:
private void createIfNotExists() {
if (table.exists()) {
return;
}
which in turn for Oracle results in a call to the connection's metadata:
resultSet = jdbcTemplate.getMetaData().getTables(
catalog == null ? null : catalog.getName(),
schema == null ? null : schema.getName(),
table,
types);
found = resultSet.next();
Does this work with a stock Oracle XE version 11.2.0 JDBC driver? After adding xdb6.jar and xmlparserv2.jar from SQL Developer to the classpath the following groovy snippet
def results = conn.getMetaData().getTables(null, "OWNER", "schema_version")
println results.next() as boolean
claims otherwise:
$ groovy TestMetaData.groovy
false
That would explain that flyway seems to think the table is not there, but now i am completely stumped. What is going on here? Surely the call to getMetaData().getTables() must work in general? Or is this something the thin driver can't do?
everyone, my app based on Spring MVC and i was trying to persist Arabic text in MySQL (i use JPA) but it's save ???? instead of Arabic characters. I tried to use this:
spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8
but i got error when i tried to save text in Arabian:
java.sql.SQLException: Incorrect string value: '\xD8\xAA\xD8\xB1\xD8\xAD...' for column 'last_name' at row 1
Thanks in advance.
Hi after a bit of search in the internet I added a couple of lines to my application.properties in order to use UTF-8 encoding and it finally worked. here is the file. don't forget to change the url and password etc.
# DataSource settings: set here your own configurations for the
# #database
# connection. In this example we have "netgloo_blog" as database name #and
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/newDB?useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
spring.datasource.username = root
spring.datasource.password = somePassword
# Keep the connection alive if idle for a long time (needed in #production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
server.tomcat.uri-encoding=UTF-8
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the #prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen #database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
server.port=8080