Joomla : libraries mysql schema does not working for Insert records, add primary key, drop table command - joomla3.8

On extension update, instead of using update SQL schema I have given a custom button like fix database under that extension. It working fine. Custom script executes from same schema folder structure i.e. "/sql/updates/mysql"
But some DDL commands are not working like INSERT, ALTER for adding primary key for already existing table, DROP to delete table.
I have checked the MysqlChangeItem.php file (using Joomla 3.8.10) under "libraries/src/Schema/ChangeItem" & found the different DDL commands which are handled but does not found about INSERT/adding primary key for existing table/drop table.
Can you please suggest the solution

Related

SQL Express Alter table add primary key auto increment

I'm trying to alter my table named Vettura to modify field ID and making it both Primary and Auto increment, i have already looked in other answers on other similiar conversation, but i can't find the right answer, can you pls help me?
You might not be able to alter the table this way depending on what data is there and if there are dependencies.
If you are using SSMS, I would recommend scripting the database to an SQL file including both the schema and the data and setting DROP and CREATE. Then you can edit the table definition(s) and regenerate the database.
By the way, for SQL-Server, the Auto increment is called IDENTITY.

How do I link the File Table with my existing table in SQL server

I have a requirement of saving employees image in database. I have been asked to use File Tables. After googling on this I got some fine contents as to how to get started with File Tables. And I managed to create them, also inserting data to these tables is as simple as copy/paste to specified folder and the data gets inserted into the table. The problem is how do I relate each employee row set with their respective images
The overall flow will be like the employee will upload his/her image or doc through front end, which will save the image on the server. the problem is how to add reference of employee with its respective image. One option can be having a FileName column in the employee table which refers to the name column in the FileTable, but I need something more efficient such as relating the id's. Any other way of achieving this would be appreciated.
First of it consumed a lot of time to get things right as filetable being a new feature introduced from SQLSERVER 2012 onwards. Secondly this is the approach I took to counter my issue. I will be providing some usefull links to make life easier for other developers. Hope it helps.
File table is almost similar to normal SQL tables, but has some configurations to be made to get it started, as it resides in a separate section of your database, inside tables-->FIleTables. Since the table structure is predefined, you cannot change the table schema. So the approach I took is to refer the stream_id as the reference key to link with the table that I want to use and handle the constrains logic through stored procedure. In my scenario the Employee table will have a stream_id column which will be linked to the File table via stream_id, also to make rows in the File table unique, I inserted the Employee Id as the name of the stored image and not the actual name of the file. With this even if anyone tries to insert the file directly by copy/paste it wouldn't have any relation with the Employee table, also you can provide access right to give desired permission. Check the links for more details. And that's it, with a little bit of tweaks I was able to solve my issue.
Below SQL scripts will be helpful to activate file stream at instance level at one shot.
EXEC sp_configure filestream_access_level, 2
RECONFIGURE
GO
--For new DB
CREATE DATABASE <DATABASE name>
WITH FILESTREAM
(
NON_TRANSACTED_ACCESS = FULL,
DIRECTORY_NAME = N'<DIRECTORY name>'
);
GO
--If you have an existing DB
ALTER DATABASE <DATABASE name>
ADD FILEGROUP <File group name>
CONTAINS FILESTREAM
GO
ALTER DATABASE <DATABASE name>
ADD FILE (NAME='<File group name>', FILENAME='<DIRECTORY name>')
TO FILEGROUP <File group name>
GO
ALTER DATABASE <DATABASE name>
SET FILESTREAM ( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'<File
group name>')
GO
use <DATABASE name>
GO
CREATE TABLE <Filetable NAME> AS FileTable;
GO
Also some links which are very helpful:
https://www.mssqltips.com/sqlservertip/2667/filetable-feature-in-sql-server-2012/
http://sql-articles.com/articles/general/working-with-filetables/
https://michaelfirsov.wordpress.com/working-with-sql-server-file-tables-part-1/
https://michaelfirsov.wordpress.com/working-with-sql-server-file-tables-part-2/
https://msdn.microsoft.com/pl-pl/library/gg492084(v=sql.110).aspx
Thanks.
Wouldn't just having a "joining (many-to-many) table" in between employer and the filetable be just as easy? You wouldn't have to use it as a many-to-many table. Call it EmployeeImages and add a description attribute/property i.e "Hiring photo", "Just got fired photo". with both primary keys from employee and filetable. simples!
You have to insert in your program the code for copying file.
File.Copy(...) Instead of just copying and pasting into file system.
So that, each instance that your File.Copy executes, you can get its respective stream_id, which then you attach on your employees table..

symfony doctrine build-sql error

I have some big problems with symfony and doctrine at the beginning of a new project. I have created database diagram with mysql workbench, inserted the sql into phpmyadmin and then I've tried symfony doctrine:build-schema to generate the YAML schema.
It generates a wrong schema (relations don't have on delete/on update) and after this I've tried symfony doctrine:build --sql and symfony doctrine:insert-sql
The insert-sql statement generates error (can't create table ... failing query alter table add constraint ....), so I've decided to take a look over the generated sql and I've found out some differences between the sql generated from mysql workbench (which works perfect, including relations) and the sql generated by doctrine.
I'll be short from now: I have to tables, EVENT and FORM and a 1 to n relation (each event may have multiple forms) so the correct constraint (generated with workbench) is
ALTER TABLE `form` ADD CONSTRAINT `fk_form_event1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
doctrine generated statement is:
ALTER TABLE event ADD CONSTRAINT event_id_form_event_id FOREIGN KEY (id) REFERENCES form(event_id);
It's totally reversed and I am sure here is the error. What should I do? It's also correct like this?
It it broken, I've wrote the schema manually and it works perfect. I didn't want to do that because it was a very large file, but I've also learned from it!
Thank you guys!
To get both visual representation and automatic code generation you can use ORM Designer so you will have everything covered by one tool.

sql server helper stored procedure or utility for alter table alter column IDENTITY(1,1)

I wanted to modify a column in a sql server 2005 table to IDENTITY(1,1)
Incidentally this table is empty and the column to be changed is a primary key.
This column is also a foreign key for two other tables.
After googling I found that you cannot use Alter table syntax to modify a column and make it an indentity column.
Link #1 : How do I add the identity property to an existing column in SQL Server
Link #2 : Adding an identity to an existing column -SQL Server
I ended up checking the dependent tables (2 of them) removing the foreign keys (generated the script from SSMS) then dropping the main table then re-creating with identity. (could try the rename option here as well)
Then re-created the foreign keys for the earlier dependent two tables.
But all this was manual work, any scripts or SPs out there to make this easier.
Ideally all these steps would be done by such a script/tool/utility:
Check dependent tables keys
Generate Create and drop foreign key scripts for this
Generate create script for the main table
drop the main table (or rename the table if the table has data)
re-create the table with identity column enabled
re-create foreign keys
You can use SSMS to generate a script (Edit a table, save script), but otherwise it's a manual process as you identified.
The SSMS scripts will pick up dependencies etc. For this kind of work, I tend to use SSMS to generate a basic script, pimp it a bit, run it carefully, then use a comparison tool (such as Red Gate compare) to generate a safer version.
Edit: The SSMS error is not an error, it's a safety check that can be switched off
(This is merely a follow-up to gbn's post with more details -- it isn't all that easy to figure this stuff out.)(
It isn't impossible to write a utility to do this, just very complex and very hard. Fortunately, Microsoft has already done it -- its called SSMS (or SMO?). To generate such a script:
In the Object Explorer, drill down to the database and table that you want to modify
Right click and select Design
Make the desired changes to the one table in the design screen. It's reasonably intuitive.
To add/remove the identity property, select the column in the upper pane, and in the lower pane/"Column Properties" tab, expand and configure the settings under "Identity Specification".
To generate a script to implement all your changes, incorporating all the dependent key changes, click on the "Generate Change Script" toolbar button. This is also an option under the "Table Designer" menu.
I also do this to generate scripts (that I later modify--SSMS doesn't always produce the most efficient code.) Once done, you can exit out without saving your changes -- leaving you a DB you can test your new script on.
drop the pk and build the same datatype column
copy the data of the column which you want to set identity to the new column.
drop the old column
reset primary key
ALTER TABLE UserRole
DROP CONSTRAINT PK_XX
ALTER TABLE XX
ADD newX int not null identity(1,1) primary key
update XX set newX = oldX
alter table XX
DROP COLUMN oldX
this is the simplest way to set identity column.
if you don't want to use the long generated script.

SchemaExport, NHibernate and deleting foreign keys

I am building my mapping and then using schema export to update my DB. However, if I delete an association in my mapping, since it's no longer in the mapping, when I run SchemaExport, it will not delete the foreign key for the deleted association. This means that it then fails to drop the table associated with that foreign key. Which further means that it can't recreate the table and I get a "There is already an object named Foo in the database" exception. Is there any way to brute delete the table via Schema Export?
The cleanest way is to do SchemaExport.Drop with the old nhibernate configuration, then create with the new one.
Alternatively you could drop and recreate the database itself, here's an example which does this at file level for SQL Server Express: http://nicholas.piasecki.name/blog/2010/01/integration-testing-with-sql-server-express-2008-nhibernate-and-mstest/