Skip to content

Commit

Permalink
Merge pull request #23 from MattGson/fix/empty-constraints-crash
Browse files Browse the repository at this point in the history
fix(introspect): fixes crash on empty contraints list
  • Loading branch information
MattGson committed Sep 27, 2021
2 parents cd75965 + 8c23049 commit 51557d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/cli/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ yargs(hideBin(process.argv))
user: { type: 'string', default: 'root' },
password: { type: 'string', default: '' },
database: { type: 'string', default: 'public' },
schema: { type: 'string', default: 'public' },
outdir: { type: 'string', default: './gen' },
format: { choices: formats, default: Format.json },
prettierConfig: { type: 'string', description: 'Path to a prettierrc file' },
Expand Down Expand Up @@ -104,6 +105,7 @@ async function introspect(args: any) {
user: args.user,
password: args.password,
database: args.database,
schema: args.schema,
},
};

Expand All @@ -122,6 +124,7 @@ async function introspect(args: any) {
await generate({ conn, outdir: GENERATED_DIR, format, prettierConfig, logLevel: logs, options });
} catch (e) {
logger.error(e);
logger.debug(e.stack);
logger.info('Use: "relation -h" to see help');
process.exit(1);
}
Expand Down
14 changes: 14 additions & 0 deletions src/introspection/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Introspection } from './introspection';
import { PostgresIntrospection } from './postgres-introspection';
import { MySQLIntrospection } from './mysql-introspection';
import { TableSchemaBuilder } from './table-schema-builder';
import { logger } from '../lib/logger';

/**
* Build schema from database connection
Expand Down Expand Up @@ -41,14 +42,25 @@ export const introspectSchema = async (params: {
tables: {},
};

logger.debug(`Introspecting ${JSON.stringify(relationalSchema)}`);

try {
const tables = await DB.getSchemaTables();
logger.debug(`Found ${tables.length} tables`);
const enums = await DB.getEnumTypesForTables(tables);
logger.debug(`Introspected ${Object.entries(enums).length} enums`);
const definitions = await DB.getTableTypes(tables, enums);
logger.debug(`Introspected ${Object.entries(definitions).length} tables`);
const constraints = await DB.getTableConstraints(tables);
logger.debug(`Introspected ${Object.entries(constraints).length} constraints`);

const forward = await DB.getForwardRelations(tables);
const backwards = await DB.getBackwardRelations(tables);

logger.debug(
`Introspected ${Object.entries(forward).length + Object.entries(backwards).length} foreign key relations`,
);

tables.forEach((table) => {
relationalSchema.tables[table] = new TableSchemaBuilder(
table,
Expand All @@ -60,6 +72,8 @@ export const introspectSchema = async (params: {
).buildTableDefinition(options);
});

logger.debug(`Built ${Object.entries(relationalSchema.tables).length} table schemas`);

await knex.destroy();
} catch (e) {
await knex.destroy();
Expand Down
2 changes: 1 addition & 1 deletion src/introspection/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class Introspection {
*/
protected async query<T>(query: Knex.QueryBuilder<T>): Promise<T> {
if (this.logLevel === LogLevel.debug) {
logger.debug('Executing query: ', query.toSQL());
logger.debug('Executing query: ', query.toSQL().sql);
}
return await query;
}
Expand Down
8 changes: 4 additions & 4 deletions src/introspection/table-schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ export class TableSchemaBuilder {
});

tableBackwardRelations.forEach((backwardRelation) => {
// get the other (forward) side of the relation to check cardinalityu
// get the other (forward) side of the relation to check cardinality
const keys = this.constraints[backwardRelation.toTable];
const [forwardRelation] = this.forwardRelations[backwardRelation.toTable].filter(
const forwardRelation = this.forwardRelations[backwardRelation.toTable]?.find(
(r) => r.toTable === this.tableName,
);

if (CardinalityResolver.isOneToOneRelation({ forwardRelation, keys })) {
if (forwardRelation && CardinalityResolver.isOneToOneRelation({ forwardRelation, keys })) {
uniqueRelations.push(backwardRelation);
}
});
Expand Down Expand Up @@ -204,7 +204,7 @@ export class TableSchemaBuilder {
* Get the schema definition for a table
*/
public buildTableDefinition(options?: { transitiveRelations: boolean }): TableSchemaDefinition {
const tableConstraints = this.constraints[this.tableName];
const tableConstraints = this.constraints[this.tableName] ?? [];
const tableEnums = this.enums[this.tableName];

const tableColumns = this.tableDefinitions[this.tableName];
Expand Down

0 comments on commit 51557d0

Please sign in to comment.