Skip to content

Commit

Permalink
Merge pull request #1098 from schemacrawler/issue1091
Browse files Browse the repository at this point in the history
Add example code to generate Markdown
  • Loading branch information
sualeh committed Apr 23, 2023
2 parents 1f8021e + 198be76 commit 2dd8131
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2023, Sualeh Fatehi <[email protected]>.
All rights reserved.
------------------------------------------------------------------------
SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.
You may elect to redistribute this code under any of these licenses.
The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html
The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/
========================================================================
*/

package schemacrawler.test.script;

import static org.hamcrest.MatcherAssert.assertThat;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
import static schemacrawler.test.utility.FileHasContent.hasSameContentAs;
import static schemacrawler.test.utility.FileHasContent.outputOf;
import static schemacrawler.test.utility.ScriptTestUtility.scriptExecution;
import org.junit.jupiter.api.Test;
import schemacrawler.test.utility.AssertNoSystemOutOutput;
import schemacrawler.test.utility.ResolveTestContext;
import schemacrawler.test.utility.TestContext;
import schemacrawler.test.utility.WithSystemProperty;
import schemacrawler.test.utility.WithTestDatabase;
import us.fatehi.utility.datasource.DatabaseConnectionSource;

@AssertNoSystemOutOutput
@ResolveTestContext
@WithTestDatabase
public class MarkdownScriptTest {

@Test
@WithSystemProperty(key = "python.console.encoding", value = "UTF-8")
public void markdown(final TestContext testContext, final DatabaseConnectionSource dataSource)
throws Exception {
assertThat(
outputOf(scriptExecution(dataSource, "/markdown.py")),
hasSameContentAs(classpathResource(testContext.testMethodFullName() + ".txt")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# FROM TEST: Database Schema Diagram

## PUBLIC.BOOKS.AUTHORS

## Columns
- ID (INTEGER)
- FIRSTNAME (VARCHAR)
- LASTNAME (VARCHAR)
- ADDRESS1 (VARCHAR)
- ADDRESS2 (VARCHAR)
- CITY (VARCHAR)
- STATE (CHARACTER)
- POSTALCODE (VARCHAR)
- COUNTRY (VARCHAR)

## Primary Key
("ID")

## Indexes
- IDX_B_AUTHORS ("LASTNAME", "FIRSTNAME")
- IDX_A_AUTHORS ("CITY", "STATE", "POSTALCODE", "COUNTRY")

## Foreign Keys
- PUBLIC.BOOKS.AUTHORS --> PUBLIC.BOOKS.BOOKAUTHORS



## PUBLIC.BOOKS.BOOKS

## Columns
- ID (INTEGER)
- TITLE (VARCHAR)
- DESCRIPTION (VARCHAR)
- PUBLISHERID (INTEGER)
- PUBLICATIONDATE (DATE)
- PRICE (DOUBLE)
- PREVIOUSEDITIONID (INTEGER)

## Primary Key
("ID")

## Indexes
- U_PREVIOUSEDITION ("PREVIOUSEDITIONID"), unique index
- FK_PREVIOUSEDITION ("PREVIOUSEDITIONID")

## Foreign Keys
- PUBLIC.BOOKS.BOOKS --> PUBLIC.BOOKS.BOOKAUTHORS
- PUBLIC.BOOKS.BOOKS --> PUBLIC.BOOKS.BOOKS
- PUBLIC.BOOKS.BOOKS --> PUBLIC."PUBLISHER SALES".SALES



## PUBLIC.BOOKS."Celebrities"

## Columns
- Id (INTEGER)
- NAME (VARCHAR)

## Primary Key
("Id")

## Indexes

## Foreign Keys
- PUBLIC.BOOKS."Celebrities" --> PUBLIC.BOOKS."Celebrity Updates"



## PUBLIC.BOOKS.COUPONS

## Columns
- ID (INTEGER)
- DATA (CLOB)
- COUPONS (PUBLIC.BOOKS."INTEGER ARRAY")
- BOOKS (PUBLIC.BOOKS."VARCHAR(20) ARRAY[10]")

## Primary Key
("ID")

## Indexes



## PUBLIC.BOOKS.CUSTOMERDATA

## Columns
- ID (INTEGER)
- DATA (PUBLIC.BOOKS.VALID_STRING)

## Primary Key
("ID")

## Indexes



## PUBLIC.BOOKS.CUSTOMERS

## Columns
- ID (INTEGER)
- FIRSTNAME (PUBLIC.BOOKS.NAME_TYPE)
- LASTNAME (PUBLIC.BOOKS.NAME_TYPE)
- AGE (PUBLIC.BOOKS.AGE_TYPE)

## Primary Key
("ID")

## Indexes



## PUBLIC.BOOKS.PUBLISHERS

## Columns
- ID (INTEGER)
- PUBLISHER (VARCHAR)

## Primary Key
("ID")

## Indexes

## Foreign Keys
- PUBLIC.BOOKS.PUBLISHERS --> PUBLIC.BOOKS.ΒΙΒΛΊΑ



## PUBLIC."PUBLISHER SALES".REGIONS

## Columns
- CITY (VARCHAR)
- STATE (VARCHAR)
- POSTALCODE (VARCHAR)
- COUNTRY (VARCHAR)

## Primary Key
("POSTALCODE", "COUNTRY")

## Indexes

## Foreign Keys
- PUBLIC."PUBLISHER SALES".REGIONS --> PUBLIC."PUBLISHER SALES".SALES



## PUBLIC."PUBLISHER SALES".SALESDATA

## Columns
- SALESDATAID (INTEGER)
- YEARLYAMOUNT (DOUBLE)

## Indexes
- UQ_CUSTOMERS ("SALESDATAID"), unique index

## Foreign Keys
- PUBLIC."PUBLISHER SALES".SALESDATA --> PUBLIC."PUBLISHER SALES".SALES



## PUBLIC.BOOKS.BOOKAUTHORS

## Columns
- BOOKID (INTEGER)
- AUTHORID (INTEGER)
- SOMEDATA (VARCHAR)

## Indexes
- Z_FK_AUTHOR ("AUTHORID")
- SYS_FK_10120 ("BOOKID")
- UIDX_BOOKAUTHORS ("BOOKID", "AUTHORID"), unique index



## PUBLIC.BOOKS."Celebrity Updates"

## Columns
- Celebrity Id (INTEGER)
- UPDATE (VARCHAR)

## Primary Key
("Celebrity Id")

## Indexes



## PUBLIC.BOOKS.ΒΙΒΛΊΑ

## Columns
- ΜΟΝΑΔΙΚΌΣ (SMALLINT)
- ΤΊΤΛΟΣ (VARCHAR)
- ΠΕΡΙΓΡΑΦΉ (VARCHAR)
- ΕΚΔΌΤΗΣ (SMALLINT)

## Primary Key
("ΜΟΝΑΔΙΚΌΣ")

## Indexes
- FK_ΒΙΒΛΊΑ_PUBLISHERS ("ΕΚΔΌΤΗΣ")



## PUBLIC."PUBLISHER SALES".SALES

## Columns
- POSTALCODE (VARCHAR)
- COUNTRY (VARCHAR)
- BOOKID (INTEGER)
- COUPON_ID (INTEGER)
- PERIODENDDATE (DATE)
- TOTALAMOUNT (DOUBLE)
- SALESDATAID (INTEGER)

## Indexes
- FK_SALES_BOOK ("BOOKID")
- FK_SALES_SALESDATA ("SALESDATAID")
- FK_SALES_REGIONS ("POSTALCODE", "COUNTRY")



## PUBLIC.BOOKS.AUTHORSLIST

## Columns
- ID (INTEGER)
- FIRSTNAME (VARCHAR)
- LASTNAME (VARCHAR)



61 changes: 61 additions & 0 deletions schemacrawler-scripting/src/test/resources/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import print_function
import re
from schemacrawler.schema import \
TableRelationshipType # pylint: disable=import-error
from schemacrawler.schemacrawler import \
IdentifiersBuilder # pylint: disable=import-error
from schemacrawler.schemacrawler import \
IdentifierQuotingStrategy # pylint: disable=import-error
from schemacrawler.utility import \
MetaDataUtility # pylint: disable=import-error

if title:
print('# ' + title)
else:
print('# Database Schema')

identifiers = \
IdentifiersBuilder.builder() \
.withIdentifierQuotingStrategy(IdentifierQuotingStrategy.quote_all) \
.toOptions()

print('')
for table in catalog.tables:
print('## ' + table.fullName)

print('')
print('## Columns')
for column in table.columns:
print('- ' + column.name + ' (' + column.columnDataType.toString() + ')')

if table.hasPrimaryKey():
print('')
print('## Primary Key')
primaryKey = table.primaryKey
print('('
+ MetaDataUtility.getColumnsListAsString(primaryKey, identifiers) + ') ')

if not table.indexes.isEmpty():
print('')
print('## Indexes')
for index in table.indexes:
if table.hasPrimaryKey() and \
MetaDataUtility.getColumnsListAsString(table.primaryKey, identifiers) == \
MetaDataUtility.getColumnsListAsString(index, identifiers):
continue
print('- ' + index.name + ' ('
+ MetaDataUtility.getColumnsListAsString(index, identifiers) + ')',
end='')
if index.unique:
print(', unique index', end='')
print('')

if not table.referencingTables.isEmpty():
print('')
print('## Foreign Keys')
for childTable in table.referencingTables:
print('- ' + table.fullName + ' --> ' + childTable.fullName)

print('')
print('')
print('')

0 comments on commit 2dd8131

Please sign in to comment.