Skip to content
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

Feature: Prefix table names with schema names #30

Closed
Hirvox opened this issue Mar 8, 2023 · 5 comments · Fixed by #31
Closed

Feature: Prefix table names with schema names #30

Hirvox opened this issue Mar 8, 2023 · 5 comments · Fixed by #31

Comments

@Hirvox
Copy link

Hirvox commented Mar 8, 2023

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 
    }

Loading

This would be more accurate:

erDiagram
    Foo_Bar {
        nchar Baz 
        nchar Qux 
    }

    Quux_Bar {
        nchar Grault 
        nchar Garply 
    }

Loading
@KarnerTh
Copy link
Owner

KarnerTh commented Mar 8, 2023

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
Copy link
Owner

KarnerTh commented Mar 8, 2023

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
Copy link
Author

Hirvox commented Mar 9, 2023

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"
Loading

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"
Loading

@KarnerTh
Copy link
Owner

KarnerTh commented Mar 9, 2023

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

The Version 0.6.1 fixes that issue.

@Hirvox
Copy link
Author

Hirvox commented Mar 10, 2023

0.6.1 works fine, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants