.../articles/
Writing SchemaSpy Metadata in YAML

Writing SchemaSpy Metadata in YAML

2019.12.23

I often use SchemaSpy to share database structure within a project, but without comments, it's not easy for new members or clients to understand it just by looking.

On this point, SchemaSpy lets you add comments to tables and columns by writing an additional XML file, even if the database itself doesn't have that information directly.

While this XML file itself isn't that complex, I found writing it a bit of a hassle, so I decided to write the equivalent information in YAML instead and generate the XML from that.


Setup

The following steps have been verified to work in a Rails project.

  • ruby: 2.6.5
  • Rails: 6.0.1

Adding a Rake task

Add a Rake task to the Rails project that parses YAML and generates XML.

$ bundle exec rails g task meta_xml

Edit the generated lib/tasks/meta_xml.rake as follows.

require 'nokogiri'
require 'yaml'

namespace :meta_xml do
  desc 'Generate meta.xml for schemaspy from meta.yml'
  task generate: :environment do
    meta_yml_path = Rails.root.join('db/meta.yml')
    meta_yml = YAML.load_file(meta_yml_path).with_indifferent_access
    builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
      xml.schemaMeta('xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                     'xsi:noNamespaceSchemaLocation': 'http://schemaspy.org/xsd/6/schemameta.xsd') do
        xml.comments meta_yml[:comments]
        xml.tables do
          meta_yml[:tables].each do |table, props|
            xml.table(name: table, comments: props[:comments]) do
              props[:columns]&.each do |column, comments|
                xml.column(name: column, comments: comments)
              end
            end
          end
        end
      end
    end
    meta_xml_path = Rails.root.join('db/meta.xml')
    IO.write(meta_xml_path, builder.to_xml(indent: 2))
  end
end

Writing the YAML

As an example, I wrote information for the following tables in db/meta.yml. It includes comments for the users table, the todos table, and each of their columns.

comments: サンプルプロジェクトのデータベース

tables:
  users:
    comments: ユーザーテーブル
    columns:
      id: ユーザーID
      email: メールアドレス
  todos:
    comments: TODOテーブル
    columns:
      id: TODO ID
      user_id: ユーザーID
      text: テキスト

Outputting the XML

Now, running bundle exec rake meta_xml:generate generates the following db/meta.xml based on the contents of db/meta.yml.

<?xml version="1.0" encoding="UTF-8"?>
<schemaMeta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schemaspy.org/xsd/6/schemameta.xsd">
  <comments>サンプルプロジェクトのデータベース</comments>
  <tables>
    <table name="users" comments="ユーザーテーブル">
      <column name="id" comments="ユーザーID"/>
      <column name="email" comments="メールアドレス"/>
    </table>
    <table name="todos" comments="TODOテーブル">
      <column name="id" comments="TODO ID"/>
      <column name="user_id" comments="ユーザーID"/>
      <column name="text" comments="テキスト"/>
    </table>
  </tables>
</schemaMeta>

All that's left is to have SchemaSpy read this in.

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us