Skip to content

Introspecting a part of existing database #807

Closed as not planned
Closed as not planned

Description

@INovozhilov

Hi.
I have a legacy MySQL database. When I introspect a whole database I getting the error Error: Command failed with exit code 7 (E2BIG): /usr/local/lib/node_modules/prisma2/query-engine-darwin cli --dmmf_to_dml. Okay...
But I want to introspect and work with only several tables in my DB. Am I able to?

Activity

  1. janpio commented on Oct 21, 2019

    @janpio
    Contributor

    Not out of the box - but that is an interesting request. How do you imagine this could work?

    Obviously the proper fix here is to make E2BIG go away and you being able to just introspect the whole database - but I can not give you any ETA on that. Please make sure to share as much information as possible so we can reproduce your specific problem.

    As a workaround, you could create a copy of your database and get rid of the parts you think are causing the problem or you are not actually interested in. Introspection also just needs the schema, not the data - so creating a schema only copy should not be too hard.

  2. INovozhilov commented on Oct 22, 2019

    @INovozhilov
    ContributorAuthor

    It could be:

    1. a first parameter for the introspecting function - a list or file with a collection of tables that I want to introspect.
    2. a second parameter with meaning "sync only existing models" because of my legacy DB continuously changes.
  3. janpio commented on Oct 22, 2019

    @janpio
    Contributor

    Thanks, we will take those into consideration.

    Re 2) That means you use the param from 1) once, and the next time it picks up on the existing models in your Prisma schema and only looks at those again to create a new schema, correct?

  4. INovozhilov commented on Oct 22, 2019

    @INovozhilov
    ContributorAuthor

    yeah. This is my suggestion.

  5. added
    kind/feature
    on Oct 22, 2019
  6. INovozhilov commented on Oct 22, 2019

    @INovozhilov
    ContributorAuthor

    I checked another workaround, and it's work for me now. I created a new Mysql user and granted only needed tables — a not big overhead.

  7. janpio commented on Oct 23, 2019

    @janpio
    Contributor

    Ohh, that i a great idea! With the "limited" user the introspection works fine?

    (cc @nikolasburk - as this might be a useful workaround to document as long as we have these limitations)

  8. nikolasburk commented on Oct 23, 2019

    @nikolasburk
    Contributor
  9. INovozhilov commented on Oct 23, 2019

    @INovozhilov
    ContributorAuthor

    Ohh, that i a great idea! With the "limited" user the introspection works fine?

    Yes. With a restricted user, introspection works as designed.

  10. jcloutz-radix commented on Dec 9, 2019

    @jcloutz-radix

    +1 on this, when using another migration tool like knex it would be nice to be able to prevent the client code for the migrations and migrations_lock tables from being generated.

  11. janpio commented on Dec 15, 2019

    @janpio
    Contributor

    Rough feature outline:

    • Supply list of tables to be introspected (only look at those)
    • Supply list of tables to be ignored (to not look at those)

    Simplest solution: additional params to connection string, comma joined string of table names. E.g. &only_tables=foo,bar or &ignore_tables=fizz,buzz.

    Complication: What happens with foreign keys to tables that are excluded?

    Internal Note: Having this feature could be a perfect escape hatch for all the cases where we can not make Prisma work with some existing tables of bigger schemas.

  12. jcloutz-radix commented on Dec 16, 2019

    @jcloutz-radix

    Would it make more sense to have a config file rather than appending to the connection string? It would allow more fine grained control, and can be expanded for future use cases beyond just the topic of this issue. It would be super helpful to allow the the introspection and model generation process run with defaults and without configuration, but also allow overriding of specific behavior when the need arises.

    • Ignore tables.
    • Define table name to model name mappings
    • Define relationship names, for instance when a users table refers to itself should the relation be called users, parents, children etc. Same goes for relationships to other tables.
    • Pivoting through tables where there is no defined foreign key.
    • Read only models based on views
    • Polymorphic table configuration which can be a thing when migrating from another ORM.
  13. janpio commented on Dec 17, 2019

    @janpio
    Contributor

    Best open a new issue for this @jcloutz-radix - many (actually: all) of those are great suggestions, but clearly out of scope for this specific problem this issue is about.

    For the "Introspecting part of a database" usecase the connection string might be enough, for those others we definitely want to have more configuration. (Link to this issue in the new one, so we make the connection between the both of them). Thanks!

  14. 8 remaining items

  15. sorenbs commented on Apr 26, 2021

    @sorenbs
    Member

    @joeldarling - would it be sufficient for you to be able to split the prisma schema between multiple files? That way you could have your normal clean schema file, and another one called ignoredTables.prisma that looks like this:

    model TableWeDontCareAbout { @@ignore }
    model AnotherTableWeDontCareAbout { @@ignore }
    
  16. removed their assignment
    on May 3, 2021
  17. joeldarling commented on May 27, 2021

    @joeldarling

    @sorenbs that's actually how I have it now... as I decide a need a certain table, I move it from my "ignore" file to main schema file. Of course now, whenever I run introspect, it plops in all the models I don't want.

    Being able to ignore those tables in the first place would be nice. The approach you mentioned would be fine

  18. jiaranda commented on Feb 24, 2022

    @jiaranda

    How is this feature request going? It's been more than 2 years since this discussion started and it would be really useful to be able to ignore some tables.

  19. arieljr10699 commented on Nov 2, 2022

    @arieljr10699

    If it works for someone else, I've been able to only introspect certain tables from my SQL Server DB by creating a user and granting access (select, insert, update, delete and alter) to the tables I'm working with. Then, in the connection string that Prisma uses, I specify the DB, user, and password I'm using. When I do an instrospect it only pulls the tables with the granted access.

  20. Baldrani commented on Aug 22, 2023

    @Baldrani

    Has anybody solved that problem for PostgreSQL ? I've tried to REVOKE privileges for a specific user, but it's not enough.

  21. jtomaszewski commented on Feb 7, 2024

    @jtomaszewski

    I've just used https://github.com/MrLeebo/prisma-ast to parse the introspected schema file, add ignore attributes to any models or relations than are not on my "allowed model names" list, and output the schema file.

    So now generating the prisma file looks like this to me:

    prisma db pull --force && ts-node prisma/add-ignore-decorators.ts && prisma generate

    Here's the source code of the add-ignore-decorators.ts script for those interested:

    import {
      Block,
      Field,
      Model,
      getSchema,
      printSchema,
    } from '@mrleebo/prisma-ast';
    import { readFileSync, writeFileSync } from 'fs';
    
    const schemaPath = 'prisma/schema.prisma';
    const allowedModuleNames: (string | RegExp)[] = [
      /^EVAL_CurricularWorksheet*/,
      'EVAL_Competencies',
    ];
    
    const schemaString = readFileSync(schemaPath).toString();
    
    const schema = getSchema(schemaString);
    
    const allModuleNames = schema.list.flatMap((b) =>
      b.type === 'model' ? [b.name] : [],
    );
    schema.list.forEach((block) => {
      ignoreIfUnallowedModel(block);
      ignoreUnallowedModelRelations(block);
    });
    
    const output = printSchema(schema);
    writeFileSync(schemaPath, output);
    
    function ignoreIfUnallowedModel(block: Block) {
      if (block.type !== 'model') return;
      if (isAllowedModuleName(block.name)) return;
      if (hasIgnoreAttribute(block)) return;
    
      block.properties.push({
        type: 'attribute',
        name: 'ignore',
        kind: 'object',
        args: [],
      });
    }
    
    function ignoreUnallowedModelRelations(block: Block) {
      if (block.type !== 'model') return;
      // if (block.name !== 'EVAL_CurricularWorksheetScores') return;
      if (!isAllowedModuleName(block.name)) return;
    
      block.properties
        .filter((p): p is Field => p.type === 'field')
        .filter(
          (field) =>
            typeof field.fieldType === 'string' &&
            isUnallowedModuleName(field.fieldType),
        )
        .forEach((field) => {
          // if (field.name !== 'EVAL_OutcomeNarrative') return;
          ignoreField(field);
        });
    }
    
    function isAllowedModuleName(name: string) {
      return allowedModuleNames.some((match) => {
        return typeof match === 'string' ? name === match : match.test(name);
      });
    }
    
    function isUnallowedModuleName(name: string) {
      return allModuleNames.includes(name) && !isAllowedModuleName(name);
    }
    
    function hasIgnoreAttribute(block: Model) {
      return block.properties.find(
        (p) => p.type === 'attribute' && p.name === 'ignore',
      );
    }
    
    function ignoreField(field: Field) {
      field.attributes = field.attributes ?? [];
      if (field.attributes.some((attr) => attr.name === 'ignore')) return;
    
      field.attributes.push({
        type: 'attribute',
        name: 'ignore',
        kind: 'field',
      });
    }
  22. erw13n commented on Feb 23, 2024

    @erw13n

    Hi @jtomaszewski nice share.
    I just got to many tables and need drop that not being used. Just be careful with relation.

    import { produceSchema } from '@mrleebo/prisma-ast';
    import { readFileSync, writeFileSync } from 'fs-extra';
    
    const schemaPath = 'prisma/schema.prisma';
    const allowedModuleNames: (string | RegExp)[] = [
      /^allow_this_table*/,
      'allow_this_table'
    ];
    
    const schemaString = readFileSync(schemaPath).toString();
    
    function isAllowedModuleName(name: string) {
      return allowedModuleNames.some((match) => {
        return typeof match === 'string' ? name === match : match.test(name);
      });
    }
    
    const output = produceSchema(schemaString, (builder) => {
      builder.getSchema().list.forEach((block) => {
        if (block.type === 'model' && !isAllowedModuleName(block.name))
          builder.drop(block.name)
      })
    });
    
    writeFileSync(schemaPath, output);
    
  23. igalklebanov commented on Dec 26, 2025

    @igalklebanov
    Contributor

    Hey 👋

    Closing this for being dusty and stale.

  24. locked and limited conversation to collaborators on Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featuretopic: introspection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions