-
Notifications
You must be signed in to change notification settings - Fork 8
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
Comments
Heyaaa! Thank you for this suggestion. I will consider and discuss it with the other contributors. 🙏 |
If that can help, here's a definition parser written in TS that can help extracting the fields/relations from the SQL DDL/dump. |
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.
# -- 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;
# -- 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 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; |
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.
The text was updated successfully, but these errors were encountered: