liquibase updateSQL - maven-plugin

Hibernate Core: 3.5.6
DB: Oracle 11g
I am using the updateSQL goal to generate alter scripts (SQL files).
The resulting SQL files do not have the schema name (username) prepended. Is there any way this can be achieved?
A create table changeSet will result in
CREATE TABLE FOO ...
What I want is
CREATE TABLE XYZ.FOO ... where XYZ is the default schema name.
I have tried adding defaultSchemaName in the generate-resources
Would appreciate if I can get some help.
Thanks

I used liquibase-maven-plugin 2.0.2 and that resolved it. I was using 2.0.1 earlier.

Related

Liquibase diffChangeLog does not report stored procedures

I am new to Liquibase. I am using Liquibase version 4.0 and on Windows 10 Home edition
Starting Liquibase at 23:08:42 (version 4.0.0 #19 built at 2020-07-13 19:45+0000)
Liquibase Version: 4.0.0
Liquibase Community 4.0.0 by Datical
I added one table and SP to DATABASEONE. I also generated diff between two databases and it worked fine.
liquibase diffChangeLog
liquibase.properties
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
classpath=../mssql-jdbc-8.4.0.jre11.jar
url=jdbc:sqlserver://localhost;databaseName=DATABASETWO
username=sa
password=Password123
referenceUrl=jdbc:sqlserver://localhost;databaseName=DATABASEONE
referenceUsername=sa
referencePassword=Password123
changeLogFile=diff.xml
However, Liquibase does not report difference on stored procedures.
How can I get the newly added (or) missing stored procedure information in diffChangeLog ?
EDIT
I used Pro License Key (14-day trial) to generate the diff for Stored Procedures
I also generated liquibase updateSQL > update.sql. Now I want to run 'update.sql' against another database. How can I do that?
Running an update on the update.sql generatated the command liquibase updateSQL > update.sql will return extra stuff that is debug info. Not just straight SQL. You will want to:
use generateChangelog to produce a changelog file (let's call it change.postgresql.xml)
run liuqibase update using the above produced changelog file and url of destination db
Note, if they are different db platforms, like say from postgres->mysql, then you will have to review the generate changelog (in this case change.postgresql.xml) for non spec db objects of the target db (in this example mysql).
Unfortunately, you can't diff stored function / procedure according to diff-changelog document
Additional Functionality with Liquibase Pro
While Liquibase Open Source stores all changesets in a changelog, Liquibase Pro creates a directory called Objects and places the directory at the same level as your changelog. The Objects directory contains a subdirectory for each of the following stored logic types:
checkconstraint
package
packagebody
procedure
function
trigger
synonyms

ASP.NET Core 3.0 - Can't Update-Database

I just created the project using the command dotnet new angular -o <output_directory_name> -au Individual and scaffold identity then I installed Microsoft.EntityFrameworkCore.SqlServer but when I run the command update-database, I get the error below.
Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
[Id] TEXT NOT NULL,
[Name] TEXT(256) NULL,
[NormalizedName] TEXT(256) NULL,
[ConcurrencyStamp] TEXT NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
and then at the end end another error
Error Number:2716,State:1,Class:16
Column, parameter, or variable #2: Cannot specify a column width on data type text.
Below is the generated CreateIdentitySchema migration
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
ApplicationDbContextModelSnapshot.cs
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(256)")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasColumnType("nvarchar(256)")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles");
});
How to fix this errors so I can update-database?
Changing 'TEXT' to 'VARCHAR' in 00000000000000_CreateIdentitySchema.Designer.cs resulted in the following error. The same happens when I change it to 'NVARCHAR'
Data type 'VARCHAR' for property 'Id' is not supported in this form. Either specify the length explicitly in the type name, for example as 'NVARCHAR(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.
I had this error and it can be fixed as follows.
Open up the project and delete 2 migration files and 1 snapshot file. Most likely named 0000...InitialCreate.cs, 0000...InitialCreate.Designer.cs and the ...DbContextModelSnapshot.cs. Leave the DbContextClass.
Delete the database.
Use dotnet to create a new migration and update the database. I also script the migration to check it.
dotnet ef migrations add InitialCreate --context ApplicationDbContext -v
dotnet ef database update --context ApplicationDbContext -v
dotnet ef migrations script --context ApplicationDbContext -v
These answers were all helpful, especially the one from #klent. I had generated a new project using:
dotnet new angular -o [MY PROJECT] -au Individual
This produced a new project that used Sqlite as the data provider, although I wasn't aware of that. I changed the connection string to my SQL Server, then ran:
dotnet ef database update
and got the error posted in the original question. After reviewing the answers here I realized that I had the incorrect data provider, and the migrations were targeting Sqlite syntax, which was using TEXT(256) for one of the column specifications. So my specific steps to correct were to:
Remove the Microsoft.EntityFrameworkCore.Sqlite Nuget package.
Install the Microsoft.EntityFrameworkCore.SqlServer Nuget package.
In the ConfigureServices method of my Startup.cs, change the services.AddDbContext call to use options.UseSqlServer instead of options.UseSqlite.
Remove the _CreateIdentitySchema and ApplicationDbContentsModelSnapshot classes from the Migrations folder.
After ensuring the connection string in AppSettings.json points to the SQL Server and not Sqlite, run dotnet ef migrations add CreateIdentitySchema -o Data\Migrations. Note that the output folder specified in the -o parameter may be different for you.
Check the Visual Studio Project Explorer. Two new classes should now appear in the Migrations folder.
Run dotnet ef database update. This will create the tables in the database noted in the connection string.
Additionally, if you want your tables to be in a specific schema, for example "Auth", open your ApplicationDbContext class and add:
protected override void OnModelCreating( ModelBuilder builder )
{
base.OnModelCreating( builder );
builder.HasDefaultSchema( "Auth" );
}
However, this will set the schema for all access that uses this DbContext. It wasn't a problem for me since I'm not using EF for my application data, just Identity data. YMMV.
As already answered by a few others the default Identity installation seems to create the wrong column types; namely that some of the Id columns should be varchar and not text.
I had exactly the same issues as you and spent ages trying to effectively solve it.
The quickest way I found to solve it, and for anyone else coming across this problem, is to:
Create the new project
Delete the Migrations folder completely
Install the package for Sql Server (Or whatever data system you're using) and connection string details
Add your own migration using dotnet ef migrations add <MIGRATION NAME> or Add-Migration (this will write new migration designer files from scratch with the correct column structure
Lastly, update your database with dotnet ef database update or Update-Database
As per comments from #sepupic and #PeterSmith, the problem was that the auto-generated code in 00000000000000_CreateIdentitySchema.Designer.cs had TEXT fields with HasMaxLength so I changed it to VARCHAR then I added HasMaxLength(450) to all VARCHAR Ids then I tried to update-database again and it worked.
I run the project and tried to Register a user and I got the error below
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Bolean'
Since I still had errors I did the following:
I deleted the generated table then created a new project with the command dotnet new angular -o <output_directory_name> -au Individual
I didn't scaffold new identity to the newly created project
I copied the db name from my previous project and run the command update-database
There were two missing tables DeviceCodes and PersistedGrants so I run the command add-migration but it didn't generate anything so I copied the migration code of the two tables from my previous project and pasted it the empty migration code I created earlier.
I run the project again and register a new user and it finally worked
I still don't know why I got errors since the project I did before didn't have any problem with the auto-generated code.
I had an error like yours as
Column 'Id' in table 'Roles' is of a type that is invalid for use as a key column in an index.
Because I changed the table names those are coming with Individual Identity of Asp.Net Core web application.
To put it simply,
Delete everything in Migrations Folder apart from ApplicationDBContext.
Add-Migration
Make sure if your tables names are matching in Up, Down and BuildTargetModel
Then update-database
It worked for me!
Edit : My main point is that make sure if your tables names are matching in Up, Down and BuildTargetModel. After that update-database will work.
I resolved this database update error with the first answer by #abCsharp, but not until I noticed that the project, which I started with the option of Identity, had created a new migrations folder under the project folder. The original migrations folder was under a Data folder that also contained the ApplicationDbContext.
I deleted the original folder (for cleanliness) and ran the newly created migration which worked. I also deleted the migrations history table in the database, which contains tables for which I am not using migrations.
FWIW All the data types for text in all the tables are nvarchar.

Missing Migration data in SQL when trying to add-migration

I have made use of a backup database and have added a new table using Code First. Now when I tried using add-migration, I got this error:
Unable to generate an explicit migration because the following
explicit migrations are pending
Suggesting that the Migrations in the project do not match which are in the DB, which when I looked, there are NO migrations in the __migration table any more ?
As these tables exist in the DB, how do I get all of these migrations inserted into my SQL DB ? Is there a way I can get them all added, so that the projects migrations and the SQL DB migrations will match, so that I can continue adding/editing tables ?
Thanks in advance for any help.
So I found something that did help! It unfortunately meant that I lost all of my migration files, but thats not the end of the world.
The below is reference from this site: Click here
Remove the _MigrationHistory table from the Database
Remove the individual migration files in your project's Migrations folder
Enable-Migrations in Package Manager Console
Add-migration Initial in PMC
Comment out the code inside of the Up method in the Initial Migration
Update-database in PMC (does nothing but creates Migration Entry)
Remove comments in the Initial method
I hope this helps someone else as well.

Liquibase creates log tables with user schema

When implementing liquibase on Sql Server. Below tables are created on USER schema not with DBO schema. Not sure how to fix this.
AMAR.DATABASECHANGELOG
AMAR.DATABASECHANGELOGLOCK
It should be
DBO.DATABASECHANGELOG
DBO.DATABASECHANGELOGLOCK
I don't have any issues on creating table. It always creates on dbo schema. I'm using integratedSecurity and Liquibase version is 3.4.2
You can specify
--liquibaseSchemaName=dbo
as a parameter when running Liquibase from the command line.
I prefer to that into my liquibase.properties file.
This works for me using Liquibase 3.5.3 - don't know about 3.4.2 though
Have a look at changelogSchemaName parameters (http://www.liquibase.org/documentation/maven/generated/updateSQL-mojo.html) to specify schema where Liquibase stores tables.

Oracle sql Developer 10g - How to import/replace a ,dmp database to existing database

i have an existing database which is old version and i want to replace it with the latest version of database(.dmp file).i am newbie so Could someone show me step by step on how to import a full database to it.
Thanks in advance.
FYI: i put the oracle sql developer in virtualbox ,WIN7 64bit.
To replace everything, it's usually easier and faster to just drop and recreate the schema instead of dropping all objects in the schema individually.
drop user WHATEVERMYNAMEIS cascade;
create user WHATEVERMYNAMEIS identified by MYSECREDPASSWORD default tablespace USERS;
grant CONNECT, RESOURCE to WHATEVERMYNAMEIS;
(Note that this is just an example. You need to supply your own username, password, tablespace name, privileges etc.)
Once that is done, the .dmp file can easily be imported from the command line:
imp WHATEVERMYNAMEIS/MYSECREDPASSWORD#MYDATABASE file=whatever.dmp fromuser=WHATEVERMYNAMEIS touser=WHATEVERMYNAMEIS

Resources