<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrey M.</title>
    <description>The latest articles on DEV Community by Andrey M. (@andrey_m).</description>
    <link>https://dev.to/andrey_m</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4101118%2Fad70d9c9-9376-429a-bd93-f366bab3aa59.jpg</url>
      <title>DEV Community: Andrey M.</title>
      <link>https://dev.to/andrey_m</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andrey_m"/>
    <language>en</language>
    <item>
      <title>I stopped diffing my Go structs against the schema by hand. Now CI does it.</title>
      <dc:creator>Andrey M.</dc:creator>
      <pubDate>Wed, 14 Oct 2026 13:51:16 +0000</pubDate>
      <link>https://dev.to/andrey_m/i-stopped-diffing-my-go-structs-against-the-schema-by-hand-now-ci-does-it-5db4</link>
      <guid>https://dev.to/andrey_m/i-stopped-diffing-my-go-structs-against-the-schema-by-hand-now-ci-does-it-5db4</guid>
      <description>&lt;p&gt;A while ago I wrote about &lt;a href="https://dev.to/andrey_m/a-column-changed-type-8-days-after-i-mapped-it-and-nothing-told-me-5a2n"&gt;a column that changed type 8 days after I mapped it&lt;/a&gt;. &lt;code&gt;int NOT NULL&lt;/code&gt; became &lt;code&gt;decimal(19,2) NULL&lt;/code&gt;, my hand-written struct still said &lt;code&gt;int64&lt;/code&gt;, and nothing complained. The fix back then was to point a code generator at the live database and diff its output against what I had.&lt;/p&gt;

&lt;p&gt;That worked. It also had a weak spot I didn't want to admit: &lt;em&gt;I&lt;/em&gt; had to remember to run it. The generator lived in an IDE plug-in and talked to the database over JDBC. Great on my machine, useless in a pipeline. A check that depends on a human remembering it is a check that runs on good days.&lt;/p&gt;

&lt;p&gt;So I rewrote the generator in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  One binary, no JVM
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go &lt;span class="nb"&gt;install &lt;/span&gt;github.com/panedrone/go-sdm/cmd/sdm@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's called &lt;code&gt;go-sdm&lt;/code&gt;. One static binary, no runtime dependencies, PostgreSQL, MySQL and SQLite. Same idea as before: an XML manifest says &lt;em&gt;what&lt;/em&gt; to generate, the live database says &lt;em&gt;what it actually looks like&lt;/em&gt;, and the generator writes the models and DAO in Go.&lt;/p&gt;

&lt;p&gt;The part that matters for this story is one command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sdm val
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It generates everything in memory, compares it with the files in the repo, and exits &lt;code&gt;1&lt;/code&gt; if anything differs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;out of date: internal/dbal/models/asset.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the whole trick. If someone runs a migration and nobody regenerates, the build goes red. Not three weeks later as "why is this value off by a few cents" — on the next push.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# GitHub Actions, roughly&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;go install github.com/panedrone/go-sdm/cmd/sdm@latest&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sdm val&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;SDM_PWD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.DB_PASSWORD }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The honest caveat: &lt;code&gt;sdm val&lt;/code&gt; needs a database to read. I don't parse DDL, on purpose — the database knows things the DDL doesn't show: views, the real column types of a query's result set, procedure parameters, ref-cursors. So CI needs a reachable database with the current schema: a staging one, or a container you migrate right before the check.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting from an existing database
&lt;/h2&gt;

&lt;p&gt;The first post was about a DB-first rewrite: eighteen tables already there, nobody planning to redesign them. Writing the manifest by hand for that is exactly the kind of busywork that makes people skip the whole idea. So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sdm xml &lt;span class="nt"&gt;-orm&lt;/span&gt; sqlx &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; start.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;reads every table and view and prints a starting manifest: a model per table and a DAO per table, named after it, with CRUD and the lookups by foreign key already in place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dto-class&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"sqlx-Task"&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"tasks"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;dao-class&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"TasksDao"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;crud&lt;/span&gt; &lt;span class="na"&gt;dto=&lt;/span&gt;&lt;span class="s"&gt;"sqlx-Task"&lt;/span&gt; &lt;span class="na"&gt;table=&lt;/span&gt;&lt;span class="s"&gt;"tasks"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;query-dto-list&lt;/span&gt; &lt;span class="na"&gt;dto=&lt;/span&gt;&lt;span class="s"&gt;"sqlx-Task"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"getTasksByGroup(gID)"&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"tasks(g_id)"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dao-class&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it again after a new table appears and it prints only the new one — whatever is already in the manifest is skipped. &lt;code&gt;-orm&lt;/code&gt; picks plain &lt;code&gt;database/sql&lt;/code&gt;, &lt;code&gt;sqlx&lt;/code&gt; or &lt;code&gt;gorm&lt;/code&gt; for the models, and it's required on purpose: I'd rather type six characters than find out later that a default chose for me.&lt;/p&gt;

&lt;p&gt;This week I pointed it at a Postgres database with 21 tables: one command for the manifest, one &lt;code&gt;sdm gen&lt;/code&gt;, and the data layer compiled. Most of the time went into reading the output.&lt;/p&gt;

&lt;h2&gt;
  
  
  DAO as variables, not structs
&lt;/h2&gt;

&lt;p&gt;One more thing I changed compared to the usual generated DAO. There's no repository struct to construct and pass around, and no interface to mock. Each DAO function is a package-level variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;GetTestRating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tID&lt;/span&gt; &lt;span class="kt"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A unit test swaps the one it needs and puts it back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;dbal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetTestRating&lt;/span&gt;
&lt;span class="n"&gt;dbal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetTestRating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tID&lt;/span&gt; &lt;span class="kt"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;4.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dbal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetTestRating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No mocking library, no database in unit tests. The database shows up exactly once — in &lt;code&gt;sdm val&lt;/code&gt;, which is the one place where it's supposed to be the source of truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  The point, again
&lt;/h2&gt;

&lt;p&gt;Last time the conclusion was: stop hand-maintaining the mapping between a table and a struct, regenerate it from the table. This time it's the second half of that sentence: &lt;strong&gt;and make the build fail when you forget.&lt;/strong&gt; A generator you have to remember to run is just a nicer way to hand-maintain things.&lt;/p&gt;

&lt;p&gt;go-sdm is here: &lt;a href="https://github.com/panedrone/go-sdm" rel="noopener noreferrer"&gt;github.com/panedrone/go-sdm&lt;/a&gt;. A working example with a generated data layer: &lt;a href="https://github.com/panedrone/sdm_todolist_go_react_vue_npm_sqlite3" rel="noopener noreferrer"&gt;sdm_todolist_go_react_vue_npm_sqlite3&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>postgres</category>
      <category>database</category>
      <category>backend</category>
    </item>
    <item>
      <title>A column changed type 8 days after I mapped it, and nothing told me</title>
      <dc:creator>Andrey M.</dc:creator>
      <pubDate>Sun, 30 Aug 2026 07:59:36 +0000</pubDate>
      <link>https://dev.to/andrey_m/a-column-changed-type-8-days-after-i-mapped-it-and-nothing-told-me-5a2n</link>
      <guid>https://dev.to/andrey_m/a-column-changed-type-8-days-after-i-mapped-it-and-nothing-told-me-5a2n</guid>
      <description>&lt;p&gt;I was rewriting a Java/Spring backend to Go. The database wasn't going anywhere — it&lt;br&gt;
already existed, with real data in it, and eighteen tables' worth of history behind it.&lt;br&gt;
The plan was simple: keep the schema, replace the service layer.&lt;/p&gt;

&lt;p&gt;Early on, &lt;code&gt;asset&lt;/code&gt; looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;`asset`&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;`id`&lt;/span&gt;    &lt;span class="nb"&gt;bigint&lt;/span&gt;       &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nv"&gt;`value`&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;          &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I mapped it to a Go struct by hand, the way you do when you're moving fast and the table&lt;br&gt;
is right there on the screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Asset&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;    &lt;span class="kt"&gt;int64&lt;/span&gt;
    &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eight days later, a migration I wasn't looking at changed it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;`asset`&lt;/span&gt;
    &lt;span class="n"&gt;CHANGE&lt;/span&gt; &lt;span class="nv"&gt;`value`&lt;/span&gt; &lt;span class="nv"&gt;`value`&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;int NOT NULL&lt;/code&gt; became &lt;code&gt;decimal(19,2) NULL&lt;/code&gt;. Two changes at once, and my hand-written&lt;br&gt;
struct still said &lt;code&gt;int64&lt;/code&gt;, no pointer, no way to represent NULL. Nothing failed loudly —&lt;br&gt;
GORM will happily scan a decimal into an int64 and either round it or blow up on a NULL,&lt;br&gt;
depending on the day. It's the kind of bug that shows up as "why is this value off by a&lt;br&gt;
few cents" three weeks later, not as a compile error.&lt;/p&gt;

&lt;p&gt;That's when it clicked: I don't need a unit test for "does my Go struct still match the&lt;br&gt;
&lt;code&gt;asset&lt;/code&gt; table." I need something that reads the table and &lt;em&gt;tells&lt;/em&gt; me if it doesn't.&lt;/p&gt;

&lt;p&gt;So instead of writing the struct by hand, I pointed a small code generator I'd built for&lt;br&gt;
exactly this — reads live schema over JDBC, spits out Go structs — at the same database,&lt;br&gt;
and diffed the output against what I had. It caught &lt;code&gt;value&lt;/code&gt; immediately: wrong type,&lt;br&gt;
wrong nullability, no compile error to warn me.&lt;/p&gt;

&lt;p&gt;None of this is exotic. If you're doing DB-first Go with GORM, this is just what happens&lt;br&gt;
when the schema evolves and your structs are maintained by hand, by a human, on a Tuesday.&lt;br&gt;
The fix isn't "write more tests" — it's "stop hand-maintaining the mapping between a table&lt;br&gt;
and a struct, and regenerate it from the table instead." A unit test only catches what you&lt;br&gt;
thought to test. A schema read catches what actually changed.&lt;/p&gt;

&lt;p&gt;I ended up formalizing this into &lt;a href="https://github.com/panedrone/sqldalmaker" rel="noopener noreferrer"&gt;SQL DAL Maker&lt;/a&gt;&lt;br&gt;
— it reads live JDBC metadata and generates the DAO/model layer for Go (and a few other&lt;br&gt;
languages) from it, so the struct-vs-schema gap can't quietly reopen every time someone&lt;br&gt;
runs a migration. But the generator isn't really the point of this post. The point is:&lt;br&gt;
if your models are hand-typed and your schema isn't frozen, you already have this bug&lt;br&gt;
somewhere. You just haven't hit it yet.&lt;/p&gt;

</description>
      <category>go</category>
      <category>mysql</category>
      <category>gorm</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
