KarnerTh / mermerd

Create Mermaid-Js ERD diagrams from existing tables
MIT License
460 stars 35 forks source link

Feature: Prefix table names with schema names #30

Closed Hirvox closed 1 year ago

Hirvox commented 1 year ago

I have an SQL Server database with multiple schemas. Some of the table names in these schemas overlap, which causes mermerd to generate two entities with identical names. When rendered, mermaid will combine these entities into a single entity.

Ideally, these table names would be prefixed with the schema name in the generated Mermaid graph, optionally with a configurable separator like an underscore.

For example, for a database with these schemas and tables:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE SCHEMA Foo
GO

CREATE TABLE [Foo].[Bar](
    [Baz] [nchar](10) NULL,
    [Qux] [nchar](10) NULL
) ON [PRIMARY]
GO

CREATE SCHEMA Quux
GO

CREATE TABLE [Quux].[Bar](
    [Grault] [nchar](10) NULL,
    [Garply] [nchar](10) NULL
) ON [PRIMARY]
GO

mermerd will generate this graph:

erDiagram
    Bar {
        nchar Baz 
        nchar Qux 
    }

    Bar {
        nchar Grault 
        nchar Garply 
    }

This would be more accurate:

erDiagram
    Foo_Bar {
        nchar Baz 
        nchar Qux 
    }

    Quux_Bar {
        nchar Grault 
        nchar Garply 
    }
KarnerTh commented 1 year ago

Thanks for bringing that up and creating the detailed feature request - I really appreciate it!

That makes total sense - I will be releasing that feature shortly

KarnerTh commented 1 year ago

This feature is available in Version 0.6.0

The schema prefix is disabled by default, but can be enabled by the config showSchemaPrefix. For the separator . is used by default, but can be overwritten by schemaPrefixSeparator.

All the configs can either be set via the command line, run config or the global mermerd config (see readme for details)

Your described example can e.g. be achieved by mermerd --showSchemaPrefix --schemaPrefixSeparator="_"

If anything does not work as expected, please let me know

Hirvox commented 1 year ago

Thanks, the entities are now generated correctly with schema prefixes. But showSchemaPrefix doesn't seem to apply to entity relationships.

A slightly more complicated example:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE SCHEMA Foo
GO

CREATE TABLE [Foo].[Bar](
    [Baz] [nchar](10) NULL,
    [Qux] [nchar](10) NULL,
    [Id] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_Bar] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [Foo].[Fred](
    [BarId] [uniqueidentifier] NOT NULL,
    [Waldo] [nchar](10) NULL
) ON [PRIMARY]
GO

ALTER TABLE [Foo].[Fred]  WITH CHECK ADD  CONSTRAINT [FK_Fred_Bar] FOREIGN KEY([BarId])
REFERENCES [Foo].[Bar] ([Id])
GO

ALTER TABLE [Foo].[Fred] CHECK CONSTRAINT [FK_Fred_Bar]
GO

will generate this graph:

erDiagram
    "Foo.Bar" {
        nchar Baz 
        nchar Qux 
        uniqueidentifier Id PK
    }

    "Foo.Fred" {
        uniqueidentifier BarId FK
        nchar Waldo 
    }

    Fred }o--|| Bar : "BarId"

But it should have generated:

erDiagram
    "Foo.Bar" {
        nchar Baz 
        nchar Qux 
        uniqueidentifier Id PK
    }

    "Foo.Fred" {
        uniqueidentifier BarId FK
        nchar Waldo 
    }

    "Foo.Fred" }o--|| "Foo.Bar" : "BarId"
KarnerTh commented 1 year ago

Thanks for the quick feedback - this was poorly tested on my side.

The Version 0.6.1 fixes that issue.

Hirvox commented 1 year ago

0.6.1 works fine, thanks.