I have added flyway to our spring boot java application. The only way I can successfully build the project with maven is to comment out
the test class. Otherwise I receive a flyway error when flyway encounters a SQL script that is attempting to create indexes within a
Create Table script. The errors are below:
Caused by: org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException:
Message : Unknown data type: "IDK_COMPANIES_CITY"; SQL statement:
How can I get my project to build without commenting out the tests? The test class is below: Note: I can comment out the Test class, build and deploy the project successfully. All the SQL scripts migrate successfully. The application is using a MariaDB in the cloud. Thanks.
package com.spring.sample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = DatabaseTestApplication.class)
#WebAppConfiguration
public class DatabaseTestApplicationTests {
#Test
public void contextLoads() {
}
}
I assume you're using an in-memory database in your tests, rather than connecting to a live one. That could mean that the in-memory database that is being created when your tests are run does not support the syntax inside your Flyway-scripts.
Try setting this in your test application properties:
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLDialect
The reason I suggest using MySQLDialect is that there is none for MariaDB and because it says so in MariaDB's Knowledge Base.
If the above doesn't work, try this property instead:
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
Related
The main idea is to add a EF Core nuget package to a .NET Core library project and then use that library in a bunch of applications (e.g., ASP.NET Core, Win Service, Console App) without configuring EF in each of them. And, if possible, without adding EF packages to each of them.
I'm wondering if it's possible.
My current problem is that I can't create a database based on the model I have in the library project.
It seems I can't just select the library project in the Package Manager Console and run update-database. It wants me to implement 'IDesignTimeDbContextFactory'.
I'm using .NET Core 2.1. Would it help if I update it to the latest version?
As mentioned by the error, you need to implement IDesignTimeDbContextFactory which is part of the Microsoft.EntityFrameworkCore.Design package so go ahead and install that in your library. Then create a class that implements IDesignTimeDbContextFactory appropriately.
Since you created a .NET Core library, set that as your startup project.
Then in your Package Manager Console, select your library as the Default project and run update-database.
Yes, you can do this.
Make sure you have all the prerequisites installed.
Create a .NET Core Console app
Create a Core Class library for Entity Framework
Reference the Class library from the Console App
Scaffold your database, go to Tools > Package Manager Console
From the dropdown set your default project to your class library so it will scaffold there.
Run this in the console (database first approach): Scaffold-DbContext "Your connecting string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/EF -f
Create a class to get your context
public class Context
{
// See all queries generated by EF in debug window
public static readonly LoggerFactory MyLoggerFactory
= new LoggerFactory(new[] { new DebugLoggerProvider((s, level) => level >= LogLevel.Trace) });
public static DF.Data.Models.EF.YourContext GetContext()
{
var optionsBuilder = new DbContextOptionsBuilder<DF.Data.Models.EF.YourContext>();
optionsBuilder.UseSqlServer(
"Your Connection String").UseLoggerFactory(MyLoggerFactory);
return new DF.Data.Models.EF.YourContext(optionsBuilder.Options);
}
public partial class YourContext : DbContext
{
public YourContext(DbContextOptions optionsBuilderOptions) : base(optionsBuilderOptions)
{
}
}
}
Create a Repository class to store your queries if you would like.
Note: When you scaffold the database again make sure you select the Class library project as the default project from the dropdown. Then set your other project back to the startup project.
I created a table in prod and now trying import it to dev but when importing it throws an error. "A table, Extended Data Type, Base Enum or class called ..... already exists. Import of Table aborted." However the table doesn't exist in neither AOT nor SQL.
I know I shouldn't have created the table in Prod first but I needed a fast soluiton.
Anyways I restarted dev service and synchronised data dictionary in AOT but it didn't work. What else I should do?
Ok, I found the solution. After deleting cahce files (.AUC) in directory
C:\Users[USERNAME]\AppData\Local and restarting client import worked.
Found non-empty schema "public" without metadata table! Use init() or set initOnMigrate to true to initialize the metadata table.
I'm using Postgres 9.2 with Postgis 2.0. This means that by default when I create a new database there will be a table created in public schema called spatial_ref_sys.
When I run flyway migrate on this database, I get the above error. Running init seems to create the public.schema_version table and mark version 1 as SUCCEDED without actually running the the migration file. I've also tried combinations of initOnMigrate with no success. Flyway is not configured to manage any schemas.
Any ideas on how I can run a migration in this scenario?
The title is somewhat contradictory, as the database is indeed not virgin as you installed, through the PostGIS extension, a number of objects in the public schema.
You can either
set flyway.schemas to a new schema, say my_app, which will then be created automatically by Flyway. Your application should then use this one instead of public (recommended)
set flyway.baselineOnMigrate to true or invoke flyway.baseline() against the public schema. This will work, but public will then contain a mix of both your application objects and the PostGIS objects
If you are using Gradle you can run
./gradlew -Dflyway.schemas=public flywayClean flywayMigrate
Where public is the name of the database containing the schema_versions table. That should delete the table and metadata as well as running the migrations to get it back up to date.
Caution!
This will delete all data in public schema
I think this error comes only with latest version of Flyway i.e. above 4.03. I didn't received in the earlier project but got it when I am using Flyway version 5.07 in my latest project. Putting the code here that resolve my issues
public class FlywayConfig {
#Autowired
DataSource dataSource;
#Autowired
Config config;
#Bean
public Flyway flyway(){
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setSqlMigrationPrefix("V");
flyway.setLocations(new String[] { config.getSqlLocation() });
flyway.setBaselineOnMigrate(true);
// *******************flyway.clean(); ********************// this will wipe out the DB, be careful
flyway.migrate();
return flyway;
}
}
this work for me , i were figthin with the same problema a lot of time
my project was building on maven
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("db/your_db");
flyway.setTable("name_of_schema");
next a added this line
flyway.setBaselineOnMigrate(true);
flyway.clean();
next this lines
MigrationInfo migrationInfo = flyway.info().current();
flyway.migrate();
and i let you the URL of my references from flyway.org
Flyway.org/documentation/commandline/baseline
In my case the problem started when I deleted all the rows in the table myschema.schema_version
./gradlew flywayInit did the trick and the error is not showed anymore.
I'm trying to generate a full SQL script to deploy my application for the first time. I know that this command from the package manager console is supposed to create a full SQL script (see this SO question)
Update-Database -Script -SourceMigration:0
I've also read that $InitialDatabase is supposed to work instead of '0' for the source migration.
However, I have two configurations in my migrations folder. I'm using the following syntax for it:
Update-Database -Script -SourceMigration:0 -ConfigurationTypeName MyConfig
I'm getting back an empty script for all my configurations.
As a note, I have automatic migrations enabled and haven't added any explicit migration to it.
Having two migrations configurations in the same namespace is an unsupported scenario. We closed a bug similar to this as "by design" recently. The workaround is to have your migrations configurations in separate namespaces.
The most reliable solution to this issue would just be to put your configurations in different folders and use the MigrationsDirectory variable as shown below, then any explicit migrations generated for the configuration should be placed in the same folder
internal sealed class Configuration : DbMigrationsConfiguration<TestMultipleMigrationsScript.BlogContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
MigrationsDirectory = #"directory";
}
}
I'm trying to use wsadmin with Jython to deploy an EAR file. Before the actual deployment, I need to run a DB update using a Java class. I'm running into a ClassNotFoundException that isn't making sense to me.
Background:
The EAR file is exploded. The wsadmin tool is started with the following options:
-wsadmin_classpath %CP%
-javaoption -Dpython.path=%CP%
Both of those point to the same classpath, which contains all necessary JARs.
The jython script gets a connection to a database, and calls a utility class to create a database script. The utility class uses reflection to load other classes from the classpath (that is a hard-and-fast requirement of the library we are using, it can't be changed). It basically looks like this:
from liquibase import Liquibase
def main(args):
conn = getConnection(args)
updater = Liquibase(conn)
updater.update()
During the update() method, Liquibase uses reflection to instantiate some Java classes. This is where I get a ClassNotFoundException, for example ClassNotFoundException: com.foo.CustomUpdate
In my script, I can import the com.foo.CustomUpdate class and get no errors:
from com.foo import CustomUpdate
c = CustomUpdate("select 1")
print c.getUpdate()
So I know that the class is on the classpath. My only idea is that it has something to do with the reflection aspect of the library we are using. Has anybody else run up against anything like this?
My only other idea, if the above is unfixable, is to split things out into a shell script and use Java to run the DB update and then wsadmin to deploy the EAR.