Skip to content

Commit

Permalink
feat(resources): support multiple resource declarations per sql file
Browse files Browse the repository at this point in the history
enables support for dbdump sql files

closes #47
  • Loading branch information
uladkasach committed Jun 15, 2024
1 parent bf9f86d commit 5f6e920
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.md
src/logic/__test_assets__/exampleProject/**/*
src/contract/__test_assets__/src/generated/**/*
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"postinstall": "[ -d .git ] && npx husky install || exit 0"
},
"dependencies": {
"@ehmpathy/error-fns": "1.0.2",
"@oclif/core": "2.0.11",
"@oclif/plugin-help": "3.1.0",
"chalk": "2.4.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: mysql
dialect: 5.7
resources:
- "schema/**/*.sql"
generates:
types: src/generated/fromSql/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
-- per https://github.com/ehmpathy/sql-code-generator/issues/47#issuecomment-1473832676

-- MariaDB dump 10.19 Distrib 10.4.25-MariaDB, for Win64 (AMD64)
--
-- Host: localhost Database: xcregv2
-- ------------------------------------------------------
-- Server version 10.4.25-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `meets`
--

DROP TABLE IF EXISTS `meets`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `meets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`name` varchar(255) NOT NULL,
`date` datetime DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`registrationOpens` datetime DEFAULT NULL,
`registrationCloses` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_68c082afef70fa9572114da1ae` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `meets`
--

LOCK TABLES `meets` WRITE;
/*!40000 ALTER TABLE `meets` DISABLE KEYS */;
INSERT INTO `meets` VALUES (1,'2023-03-14 21:15:07','2023-03-14 21:15:07.866946','Lutheran National XC Meet',NULL,'http://purdue.rivals.com',NULL,NULL),(3,'2023-03-14 21:15:23','2023-03-14 21:15:23.742028','Lutheran National XC Meet = 2023',NULL,'http://purdue.rivals.com',NULL,NULL);
/*!40000 ALTER TABLE `meets` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `pendingusers`
--

DROP TABLE IF EXISTS `pendingusers`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `pendingusers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`schoolName` varchar(25) NOT NULL,
`city` varchar(25) NOT NULL,
`state` varchar(2) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_dcbe1c9362e2a5954ed37cacb8` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `pendingusers`
--

LOCK TABLES `pendingusers` WRITE;
/*!40000 ALTER TABLE `pendingusers` DISABLE KEYS */;
/*!40000 ALTER TABLE `pendingusers` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `races`
--

DROP TABLE IF EXISTS `races`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `races` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`raceName` varchar(255) NOT NULL,
`distance` decimal(4,1) NOT NULL,
`units` enum('km','mi') NOT NULL,
`sex` enum('boy','girl') NOT NULL,
`maxRunners` int(11) NOT NULL DEFAULT -1,
`maxRunnersPerSchool` int(11) NOT NULL DEFAULT -1,
`meetId` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_bdd9289e2e5229c12245e60301` (`raceName`,`meetId`),
KEY `FK_0d00f6079e05f60cbc597e07a3e` (`meetId`),
CONSTRAINT `FK_0d00f6079e05f60cbc597e07a3e` FOREIGN KEY (`meetId`) REFERENCES `meets` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `races`
--

LOCK TABLES `races` WRITE;
/*!40000 ALTER TABLE `races` DISABLE KEYS */;
INSERT INTO `races` VALUES (1,'2023-03-14 21:16:05','2023-03-14 21:30:20.000000','Boys 1.0 mile',1.1,'mi','boy',-1,999999,1),(2,'2023-03-14 21:16:18','2023-03-14 21:16:18.157242','Boys 1.5 mile',1.5,'mi','boy',-1,-1,1);
/*!40000 ALTER TABLE `races` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `runners`
--

DROP TABLE IF EXISTS `runners`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `runners` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`grade` int(11) NOT NULL,
`sex` enum('boy','girl') NOT NULL,
`raceId` int(11) NOT NULL,
`schoolId` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_643667fbd7cc4845902d8f6f8e` (`schoolId`,`raceId`,`firstName`,`lastName`,`sex`,`grade`),
KEY `FK_d84623e31d7a37508f3b7280261` (`raceId`),
CONSTRAINT `FK_9646c5e15b243bd94af980c068c` FOREIGN KEY (`schoolId`) REFERENCES `schools` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `FK_d84623e31d7a37508f3b7280261` FOREIGN KEY (`raceId`) REFERENCES `races` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `runners`
--

LOCK TABLES `runners` WRITE;
/*!40000 ALTER TABLE `runners` DISABLE KEYS */;
INSERT INTO `runners` VALUES (1,'2023-03-14 21:17:53','2023-03-14 21:38:25.000000','GEORGE','OFTHEJUNGLE',3,'boy',1,1),(3,'2023-03-14 21:18:12','2023-03-14 21:18:12.834505','TOMJR','HOFFMAN',8,'boy',1,1);
/*!40000 ALTER TABLE `runners` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `schools`
--

DROP TABLE IF EXISTS `schools`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `schools` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`schoolName` varchar(25) NOT NULL,
`city` varchar(25) NOT NULL,
`state` varchar(2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_311a56dc3afc916619f75a29e2` (`schoolName`,`city`,`state`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `schools`
--

LOCK TABLES `schools` WRITE;
/*!40000 ALTER TABLE `schools` DISABLE KEYS */;
INSERT INTO `schools` VALUES (1,'2023-03-14 21:17:44','2023-03-14 21:17:44.980996','ST PETER LUTHERAN','ARLINGTON HEIGHTS','IL');
/*!40000 ALTER TABLE `schools` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createdAt` datetime NOT NULL DEFAULT current_timestamp(),
`updatedAt` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`failedLogins` bigint(20) DEFAULT 0,
`successfulLogins` bigint(20) DEFAULT 0,
`resetCode` smallint(6) DEFAULT -1,
`schoolId` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_97672ac88f789774dd47f7c8be` (`email`),
KEY `FK_435e192698a6b7d10849295643d` (`schoolId`),
CONSTRAINT `FK_435e192698a6b7d10849295643d` FOREIGN KEY (`schoolId`) REFERENCES `schools` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2023-03-17 8:13:19
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*
!.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,82 @@ export const sqlQuerySelectUserNameFromView = async ({
"
`;
exports[`generate mysql-dbdump should be able to read the example config provisioned in __test_assets__ 1`] = `
"// types for table 'meets'
export interface SqlTableMeets {
id: number;
createdAt: Date;
updatedAt: Date;
name: string;
date: Date | null;
url: string | null;
registrationOpens: Date | null;
registrationCloses: Date | null;
}
// types for table 'pendingusers'
export interface SqlTablePendingusers {
id: number;
createdAt: Date;
updatedAt: Date;
schoolName: string;
city: string;
state: string;
email: string;
}
// types for table 'races'
export interface SqlTableRaces {
id: number;
createdAt: Date;
updatedAt: Date;
raceName: string;
distance: number;
units: string;
sex: string;
maxRunners: number;
maxRunnersPerSchool: number;
meetId: number;
}
// types for table 'runners'
export interface SqlTableRunners {
id: number;
createdAt: Date;
updatedAt: Date;
firstName: string;
lastName: string;
grade: number;
sex: string;
raceId: number;
schoolId: number;
}
// types for table 'schools'
export interface SqlTableSchools {
id: number;
createdAt: Date;
updatedAt: Date;
schoolName: string;
city: string;
state: string;
}
// types for table 'users'
export interface SqlTableUsers {
id: number;
createdAt: Date;
updatedAt: Date;
email: string;
password: string;
failedLogins: number | null;
successfulLogins: number | null;
resetCode: number | null;
schoolId: number;
}
"
`;
exports[`generate postgres should be able to read the example config provisioned in __test_assets__ 1`] = `
"// types for table 'ice_cream'
export interface SqlTableIceCream {
Expand Down
28 changes: 28 additions & 0 deletions src/logic/commands/generate/generate.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ describe('generate', () => {
expect(error.message).toContain('Cannot find module');
expect(error.message).toContain('queryFunctions.ts');

// expect the look right
const typesCode = (
await readFile(testAssetPaths.generatedTypesCode)
).toString();
expect(typesCode).toMatchSnapshot();
});
});
describe('mysql-dbdump', () => {
const testAssetPaths = {
codegenYml: `${TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/codegen.sql.yml`,
generatedTypesCode: `${TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/src/generated/fromSql/types.ts`,
generatedQueryFunctionsCode: `${TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/src/generated/fromSql/queryFunctions.ts`,
};
it('should be able to read the example config provisioned in __test_assets__', async () => {
await generate({
configPath: testAssetPaths.codegenYml,
});

// expect that the types code does not have compile errors
await import(testAssetPaths.generatedTypesCode);

// expect that the query functions code does not get produced, since not asked for in the config
const error = await getError(
import(testAssetPaths.generatedQueryFunctionsCode),
);
expect(error.message).toContain('Cannot find module');
expect(error.message).toContain('queryFunctions.ts');

// expect the look right
const typesCode = (
await readFile(testAssetPaths.generatedTypesCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ END
},
ResourceDeclaration {
"path": "schema/functions/upsert_image.sql",
"sql": "/*
generated by https://github.com/uladkasach/schema-generator
*/
CREATE FUNCTION \`upsert_image\`(
"sql": "CREATE FUNCTION \`upsert_image\`(
in_url varchar(190),
in_caption varchar(190),
in_credit varchar(190),
Expand Down Expand Up @@ -124,10 +121,7 @@ END
},
ResourceDeclaration {
"path": "schema/tables/communication_channel.sql",
"sql": "-- ----------------------------------------
-- create communication channel table
-- ----------------------------------------
CREATE TABLE \`communication_channel\` (
"sql": "CREATE TABLE \`communication_channel\` (
-- meta
\`id\` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
\`uuid\` VARCHAR(36) NOT NULL,
Expand Down
Loading

0 comments on commit 5f6e920

Please sign in to comment.