Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import from SQL schema #7

Closed
7ute opened this issue Jan 2, 2024 · 3 comments · Fixed by #9
Closed

Import from SQL schema #7

7ute opened this issue Jan 2, 2024 · 3 comments · Fixed by #9
Assignees
Labels
enhancement New feature or request implementing Indicates that the task is now being actively developed or implemented by developers or contributors

Comments

@7ute
Copy link

7ute commented Jan 2, 2024

A good complement would be to be able to import from a SQL schema / dump, the same way one can export to SQL.
It could help bootstraping the creation of a schema, or just visualize an existing database.

@DefinitelyNotAnAssassin DefinitelyNotAnAssassin added the enhancement New feature or request label Jan 2, 2024
@Finestwork
Copy link
Owner

Heyaaa! Thank you for this suggestion. I will consider and discuss it with the other contributors. 🙏

@Finestwork Finestwork added the in review This issue is currently being evaluated or examined label Jan 2, 2024
@7ute
Copy link
Author

7ute commented Jan 2, 2024

If that can help, here's a definition parser written in TS that can help extracting the fields/relations from the SQL DDL/dump.
https://github.com/duartealexf/sql-ddl-to-json-schema

@Finestwork Finestwork added implementing Indicates that the task is now being actively developed or implemented by developers or contributors and removed in review This issue is currently being evaluated or examined labels Jan 3, 2024
@DefinitelyNotAnAssassin DefinitelyNotAnAssassin linked a pull request Jan 3, 2024 that will close this issue
@7ute
Copy link
Author

7ute commented Jan 5, 2024

Many thanks @DefinitelyNotAnAssassin and @Finestwork for that fast implementation!

Works well one the DDL is cleaned up, but with a fresh dump from PhpMyAdmin, I encountered a couple bugs.
I don't know if you'd rather me open a couple new issues on this repo, but I'll summarize them here.
For each test, you can create a .sql file containing the SQL dumps snippets below :

  • When a contraint is named after the foreign key, the relation is not created (here "second_first_id_foreign")
# -- test-fail1.sql - Constraint named after foreign key: Relation not created
CREATE TABLE `first` (`id` bigint(20) UNSIGNED NOT NULL, `label` varchar(255) DEFAULT NULL);
CREATE TABLE `second` (`id` bigint(20) UNSIGNED NOT NULL, `first_id` bigint(20) DEFAULT NULL, `label` varchar(255) DEFAULT NULL);
ALTER TABLE `first` ADD PRIMARY KEY (`id`);
ALTER TABLE `second` ADD PRIMARY KEY (`id`), ADD KEY `second_first_id_foreign` (`first_id`);
ALTER TABLE `second` ADD CONSTRAINT `second_first_id_foreign` FOREIGN KEY (`first_id`) REFERENCES `first` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
  • When the constraint doesn't have any action, or is missing the update action, an error is thrown.
# -- test-fail2.sql - Missing OnUpdate action : Error -> TypeError: Cannot read properties of undefined (reading 'action')
CREATE TABLE `first` (`id` bigint(20) UNSIGNED NOT NULL, `label` varchar(255) DEFAULT NULL);
CREATE TABLE `second` (`id` bigint(20) UNSIGNED NOT NULL, `first_id` bigint(20) DEFAULT NULL, `label` varchar(255) DEFAULT NULL);
ALTER TABLE `first` ADD PRIMARY KEY (`id`);
ALTER TABLE `second` ADD PRIMARY KEY (`id`), ADD KEY `second_first_id_foreign` (`first_id`);
ALTER TABLE `second` ADD CONSTRAINT `second_ibfk_17f1cf36` FOREIGN KEY (`first_id`) REFERENCES `first` (`id`) ON DELETE CASCADE;

For this one, it comes from src/renderer/src/utilities/ImportHelper.ts:113 :

constraints: {
    onDelete: fk.reference.on[0].action.toUpperCase(),
    onUpdate: fk.reference.on[1].action.toUpperCase(), /* <~~ action is undefined if the "ON UPDATE" clause is missing */
},

Here's a valid version of the above

# -- test-valid.sql - Valid
CREATE TABLE `first` (`id` bigint(20) UNSIGNED NOT NULL, `label` varchar(255) DEFAULT NULL);
CREATE TABLE `second` (`id` bigint(20) UNSIGNED NOT NULL, `first_id` bigint(20) DEFAULT NULL, `label` varchar(255) DEFAULT NULL);
ALTER TABLE `first` ADD PRIMARY KEY (`id`);
ALTER TABLE `second` ADD PRIMARY KEY (`id`), ADD KEY `second_first_id_foreign` (`first_id`);
ALTER TABLE `second` ADD CONSTRAINT `second_ibfk_17f1cf36` FOREIGN KEY (`first_id`) REFERENCES `first` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request implementing Indicates that the task is now being actively developed or implemented by developers or contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants