DBUnit order for dataset - dbunit

I have the following quite strange dataset for DBUnit:
<persons id = "1"
first_name ="TestName99"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_OWNER"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<carwash id = "1"
name = "TestCarWash"
address = "test car wash address "
phone_number = " 123456789"
box_count = "5"
first_shift = "08:00:00"
second_shift = "20:00:00"
created_by = "1"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "2"
first_name ="TestName100"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_WASHERMAN"
date_of_creation = "2016-09-23 23:09:28"
carwash = "1"
enable = "1" />
the most strange thinks is persons table has foreign key to carwash however this column is nullable (therefore you cannot find carwash in person with id=1) and carwash table has FK to persons in column create_by
This scheme leads to special order for put data in DB. As I understand DBUnit doesn't put value in DB based on order the value mentioned in dataset by default. And consequently when this dataset is executing it lead to exception
MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test_pitstop`.`persons`, CONSTRAINT `persons_ibfk_1` FOREIGN KEY (`carwash`) REFERENCES `carwash` (`id`))
Is any way to push DBUnit put data in DB in string order as it mentioned in xml ?

In your dataset.xml file you must specify tables in correctly insertion order, this means, the basic tables first and then the related tables. This way and using DatabaseOperation.CLEAN_INSERT, tables will be deleted correctly too (related tables first and then basic tables).
Your xml file:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<carwash id = "1"
name = "TestCarWash"
address = "test car wash address "
phone_number = " 123456789"
box_count = "5"
first_shift = "08:00:00"
second_shift = "20:00:00"
created_by = "1"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "1"
first_name ="TestName99"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_OWNER"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "2"
first_name ="TestName100"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_WASHERMAN"
date_of_creation = "2016-09-23 23:09:28"
carwash = "1"
enable = "1" />
</dataset>
Hope this helps.

Based on the error message, it is a SQL constraint violation from the database, not a dbUnit issue. Is Persons.carwash a nonnullable column, or requiring FK from Carwash? dbUnit cannot override database constraints, just as your own code cannot.

Related

Laravel | search id table1

I need help for this problem.
I need to store data in two tables. But I want them to have the same ID. i.e. I want to write data in table 1 and then in table 2 where I want the ID of table 1 to be equal to that of table 2
$tabela = new funcionario();
$tabela->nome = $request->nome;
$tabela->email = $request->email;
$tabela2 = new utilizador();
$tabela2->id = ID da tabela
$tabela2->Address = $request->Address ;
the result would be:
Table 1
ID - (auto increment) 10
name - Example
Email - Example#test.com
Table 2
ID - same as ID table 1 (10)
It is not a good approach, maybe you need the foreign key for your one to one relationship but you can add id to your model fillable or add it with the active record:
$tabela = new funcionario();
$tabela->nome = $request->nome;
$tabela->email = $request->email;
$tabela2 = new utilizador();
$tabela2->id = $tablea->id;
$tabela2->Address = $request->Address ;

Update unique values into Postgres Array

I have a situation where I update a Postgres table column which is of type bigint[]. This array should have unique numbers inside it whenever an update query is fired.
The query is as given below
UPDATE book_shelf
SET book_id = book_id || array[CAST(:bookID AS BIGINT)], updated_at = now()
WHERE user_id = :userID AND shelf_name = :shelfName
When the above query is fired, it simply adds the number into the array which I don't want to happen. It should hold only unique values. Please help me.
You can check if it exists in the array before adding it:
UPDATE book_shelf
SET book_id = CASE WHEN CAST(:bookID AS BIGINT) = ANY(book_id) THEN book_id ELSE ARRAY_APPEND(book_id, CAST(:bookID AS BIGINT)) END, updated_at = now()
WHERE user_id = :userID AND shelf_name = :shelfName
Of course if updated_at should only be set if book_id is actually updated, then put the check in the WHERE clause so it's not updated unnecessarily:
UPDATE book_shelf
SET book_id = ARRAY_APPEND(book_id, CAST(:bookID AS BIGINT)), updated_at = now()
WHERE user_id = :userID
AND shelf_name = :shelfName
AND NOT CAST(:bookID AS BIGINT) = ANY(book_id)

using raw query in where methods makes error in query generation

I have an eloquent query as
Role::where("(company_id = Auth::user()->company_id or name = 'admin')and id in(2,3)")->pluck('name');
According to my eloquent the sql should be as
select `name` from `roles` where ( company_id = 1 or name = admin ) and id IN (2, 3) and `roles`.`deleted_at` is null
But it executes as
select `name` from `roles` where ( company_id = 1 or name = admin ) and id IN (2, 3) is null and `roles`.`deleted_at` is null
Can anyone help me concluding why extra is null condition is applied in the query?
Note: I am using soft deletes
You should use whereRaw instead of where
Role::whereRaw("(company_id = Auth::user()->company_id or name = 'admin')and id in(2,3)")->pluck('name');

Oracle Update statement with if conditions

I'm trying to merge three update statements into one.
"UPDATE DOT_WORKS SET START_DATE = :StartDate WHERE ID = :WorksId and END_DATE IS NULL;"
"UPDATE DOT_WORKS SET WORKS_TYPE = :WorksType WHERE ID = WorksId and WORKS_GROUP = :WorksGroup;"
"UPDATE DOT_WORKS SET WORKS_CONNECTION = :WorksConn WHERE ID = WorksId and WORKS_PLACE = :WorksPlace;"
I'm wondering whether there is a way to do that.
The reason why I'm trying to do so is to save the calls to database. It's more efficient to call db once instead of three.
Thanks!
UPDATE DOT_WORKS
SET START_DATE = case when END_DATE IS NULL then :StartDate else START_DATE end,
WORKS_TYPE = case when WORKS_GROUP = :WorksGroup then :WorksType else WORKS_TYPE end,
WORKS_CONNECTION = case when WORKS_PLACE = :WorksPlace then :WorksConn else WORKS_CONNECTION end
WHERE ID = :WorksId
and
(
END_DATE IS NULL OR
WORKS_GROUP = :WorksGroup OR
WORKS_PLACE = :WorksPlace
)

Apache Shiro: PermissionsQuery seems to return nothing

In shiro.ini I declare the following SQL-queries:
jdbcRealm.authenticationQuery = SELECT password FROM Person WHERE email = ?
jdbcRealm.userRolesQuery = SELECT id FROM SecurityRole WHERE id = (SELECT securityRole_id FROM Person WHERE email = ?)
jdbcRealm.permissionsQuery = SELECT action FROM SecurityPermission WHERE id = (SELECT permissions_id FROM securityrole_securitypermission WHERE securityrole_id = ?)
When I replace the ? in the last query with 1 an run it on the db it returns the expected result: rest:*
But SecurityUtils.getSubject().isPermitted(new WildcardPermission("rest")); will return false although the logged in user has an assigned role with id = 1, securityrole_securitypermission has an entry with ids 1 and 1, and securitypermission with id 1 has action = "rest:*".
jdbcRealm.permissionsLookupEnabled = true did the job. ;)

Resources