Schema in placeholder programmatically - flyway

I am new to Flyway. Flyway is good and friendly.
I want to create and give schema name for my tables in V1__Initial_structure.sql file. I don't know where to assign value to the placeholder. I have configured Flyway programmatically. My sql file contains,
create schema ${schemaName}
create table ${schemaName}.brand(brand_code IDENTITY,
brand_name varchar(50) unique not null, active char(1) default 'Y')
Please help.

This is the method you are looking for: http://flywaydb.org/documentation/javadoc/com/googlecode/flyway/core/Flyway.html#setPlaceholders(java.util.Map)

Related

How to upsert in (key,value) sqlite database table?

I have a (key,value) database table in sqlite. I want to update an existing key with the value in case it already exists. And if the key doesn't exist, insert the key and value into the table.
What would be best sql statement to do that?
I am creating the table using the statement:
CREATE TABLE KeyValueTable ( EntityName TEXT PRIMARY KEY NOT NULL, EntityValue BLOB)
https://www.sqlite.org/lang_UPSERT.html provides the information about how to do upsert in sqlite. Note that this feature was added to SQLite with version 3.24.0 (2018-06-04)
INSERT OR REPLACE INTO KeyValueTable VALUES ($key, $value)
While INSERT OR REPLACE is not, in general, equivalent to an UPSERT, it should work for your situation, and is compatible with older versions of SQLite.

Add constraint to existing SQLite table

I'm using SQLite, which doesn't support adding a constraint to an existing table.
So I can't do something like this (just as an example):
ALTER TABLE [Customer]
ADD CONSTRAINT specify_either_phone_or_email
CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL));
Are there any workarounds for this scenario?
I know:
I can add a constraint for a new table, but it isn't new (and it's generated by my ORM, EF Core)
I can do a "table rebuild" (rename table, create new one, copy old data, drop temp table) but that seems really complex
Ideas
Can I somehow make a copy of the table into a new table, with some schema changes?
Or "get" the schema somehow, and edit it in a SQL script, then add a table with that schema?
To make a copy of a table with some schema changes, you have to do the creation and the copying manually:
BEGIN;
CREATE TABLE Customer_new (
[...],
CHECK ([...])
);
INSERT INTO Customer_new SELECT * FROM Customer;
DROP TABLE Customer;
ALTER TABLE Customer_new RENAME TO Customer;
COMMIT;
To read the schema, execute .schema Customer in the sqlite3 command-line shell.
This gives you the CREATE TABLE statement, which you can edit and execute.
To change the table in place, you can use a backdoor.
First, read the actual table definition (this is the same as what you would get from .schema):
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'Customer';
Add your CHECK constraint to that string, then enable write access to sqlite_master with PRAGMA writable_schema=1; and write your new table definition into it:
UPDATE sqlite_master SET sql='...' WHERE type='table' AND name='Customer';
Then reopen the database.
WARNING: This works only for changes that do not change the on-disk format of the table. If you do make any change that changes the record format (such as adding/removing fields, or modifying the rowid, or adding a constraint that needs an internal index), your database will blow up horribly.

Entity framework - history table

I have the following 2 tables defined in a SQL Server database:
create table Project
(
Id int,
Name varchar(100)
IdIdentity int
)
create table ProjectHistory
(
Id int,
Name varchar(100)
IdIdentity int,
DtChange datetime
)
When I updated "Project" I would like to insert new record to table ProjectHistory with setting DtChange to actual date.
I use Entity Framework 4.1. Can you suggest me some way how this can be accomplished?
There is possibility change sql that generate Entity Framework when saving changes.
The conventional way would be to use an update trigger on the project table.
CREATE TRIGGER trgProjectUpdate
ON Project
AFTER UPDATE
AS
BEGIN
insert ProjectHistory (id, name, dtchange)
select id, name, GETDATE() from deleted
END

How to update a aprimary key field with linq to SQL?

I have tbl_names with following fields:
Id int
Name nvarchar(10)
family nvarchar(20)
Id Name Family
1 John Smith
and suppose Id and name are primary key together(compound primary key).
and I want to update name field according to the value of Id field.
DataclassesContext dac=new DataClassesContext();
var query=from record in Dac.tbl_name where record.id=1 select record;
query.name="Raymond";
Dac.Submitchanges();
but I encounter following error:
Value of member 'co_Workshop' of an object of type 'Tbl_Workshop' changed.
A member defining the identity of the object cannot be changed.
Consider adding a new object with new identity and deleting the existing one instead.
Is it because of name field is primary key? why can't I update a primary key field using linq?
I am not sure that you should find a way around this. I cannot imagine why it would be a good idea to change a value in a PK. The entire nature of a PK is that it is a stable identifier of the row.
In your case, you should drop and recreate the PK to be just the "Id" field and then if you need to improve performance on queries filtering on "name" then just add an Index on the "name" field. The fact that you only use the "Id" field to find the record supports this idea.
EDIT:
I answered before there were comments to the Question. Now that I see the comment from the OP about "it is an old database and can't change it's structure", I would say that if there are no FKs pointing to this PK then this should be a fairly straight-forward change (to drop and recreate the PK with just the "Id" field as I mentioned above). If there are FKs pointing to it then an option (though not a great option and it might not work on all RDBMS's) is to:
Drop the FKs
Drop the PK
Create the new PK on just the "Id" field
Create a UNIQUE INDEX on "Id" and "Name"
Recreate the FK's to point to the UNIQUE INDEX
This will work on Microsoft SQL Server and as much as I dislike the idea of a FK pointing to a UNIQUE INDEX, it should allow for the same structure that you have now plus LINQ will just see the single field PK on "Id" and allow for the update.
Where possible, a workaround is to delete the record whose primary key value needs updating and create a new record in its place.
It looks like there are ways around it, like I mentioned above. Linq won't let you change the primary key.
http://social.msdn.microsoft.com/Forums/en/linqprojectgeneral/thread/64064c2d-1484-4a00-a2c4-764bcb6b774a
Had the same problem. For legacy reasons, I couldn't remove the column I needed to update from being part of the primary key.
A simple solution was to not use Linq in this case, but use T_SQL and do a simple update.
update tbl_name set name = 'Raymond' where id = 1

SQL scripts act on master database instead of practice database

I wrote some sql scripts to create a database and store data. I just noticed that the new tables and data are going to the master database.
I found that I can address the correct database if I scope out the database as so:
CREATE TABLE Practice1.dbo.Experiments
(
ID int IDENTITY (100,1) PRIMARY KEY,
CompanyName nvarchar (50)
)
but I'd rather not have to scope out each command. Is there a way to set the database in the script so I don't have to scope everything out?
INSERT INTO Practice1.dbo.EXPERIMENTS
VALUES
(
'hello world'
)
SELECT * FROM Practice1.dbo.EXPERIMENTS
You have a drop down list on your toolbar that allows you to select what database you want the script to execute on. Also, you can state the database to use at the top of your script.
Example:
Syntax
USE {database}
http://doc.ddart.net/mssql/sql70/ua-uz_7.htm
On SQL Server 2005, to switch the database context, use the command:
USE DatabaseName
in the samples above, the database name is Practice1, hence:
USE Practice1

Resources