<?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: Sho Minagawa</title>
    <description>The latest articles on DEV Community by Sho Minagawa (@msh5).</description>
    <link>https://dev.to/msh5</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F119319%2F0d5cffd2-d2d2-4a16-8ec1-31bb58fd2eb8.jpg</url>
      <title>DEV Community: Sho Minagawa</title>
      <link>https://dev.to/msh5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/msh5"/>
    <language>en</language>
    <item>
      <title>Start DB Schema Migration with "goose"</title>
      <dc:creator>Sho Minagawa</dc:creator>
      <pubDate>Wed, 12 Dec 2018 02:26:38 +0000</pubDate>
      <link>https://dev.to/msh5/start-db-schema-migration-with-goose-bhg</link>
      <guid>https://dev.to/msh5/start-db-schema-migration-with-goose-bhg</guid>
      <description>&lt;p&gt;&lt;em&gt;This is the post assigned as "&lt;a href="https://qiita.com/advent-calendar/2018/lob-inc"&gt;LOB advent calender&lt;/a&gt;" 12 day, have fun !&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Today, let me share a small tool "goose". It is a schema migration tool and it brings the management of schema migrations into the project. To tell the truth, I wasn't familiar with schema migration until I know this tool. In this post, I'll explain that "goose" is what tool, and also denote what is schema migration for newbies like past me.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is "Schema Migration" ?
&lt;/h2&gt;

&lt;p&gt;In the world, there are a lot of online services for web, messaging, finance, advertising and so on, and much of them depend on database system. Also, much of them faces with the need to change the schema in the service lifetime, for example creating a table, altering table to add/del columns and so on. It is called "&lt;a href="https://en.wikipedia.org/wiki/Schema_migration"&gt;schema migration&lt;/a&gt;" to change the schema specifically for online database. Also, not only that, it means to manage such changes to keep traceability. &lt;em&gt;rails migrate&lt;/em&gt; is very famous tool to provide schema migration. But it is the command embedded on Rails framework, so the non-Rails project will welcome the more portable tool. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is "goose" ?
&lt;/h2&gt;

&lt;p&gt;Goose is one of the schema migration tools, and it is small but very usual. It is written in Go, and you can install with "go get" or bringing a prebuilt binary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go get -u github.com/pressly/goose/cmd/goose
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By the way, there are two variations of goose. Of the following two, the second is original and the first is forked from the original. The tool I introduce today is the first one, because I think the forked one is superior in terms of some features have been added.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/pressly/goose"&gt;github.com/pressly/goose&lt;/a&gt; (forked from the original)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://bitbucket.org/liamstask/goose"&gt;bitbucket.org/liamstask/goose&lt;/a&gt; (original)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demonstration
&lt;/h2&gt;

&lt;p&gt;First of all, we need to create a migration file. we can generate it with &lt;code&gt;goose create&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;goose DRIVER DBSTRING create NAME TYPE

- DRIVER: A name of driver to query db.
- DBSTRING: A string passed to the driver as parameter.
- NAME: A name of the generated migration file.
- TYPE: "sql" or "go" as the type whose the generated migration file.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And, you can use as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ goose sqlite3 ~/database.db create 00001_create_first_table sql
2018/12/11 18:55:01 Created new file: 20181211185501_00001_create_first_table.sql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It means that the tool creates new migration file "&amp;lt;timestamp&amp;gt;_00001_create_first_table" where we write the steps to change schema for sqlite3 database that is located on "~/database.db". And we need to update the migration file. The migration file is generated as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- +goose Up
-- SQL in this section is executed when the migration is applied.

-- +goose Down
-- SQL in this section is executed when the migration is rolled back.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The migration file has two sections. The first is "+goose Up", the area where we can write the migration steps. The second is "+goose Down", the area where we can write the step to invert the changes for "+goose Up" steps. The primaly example is as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- +goose Up
CREATE TABLE post (
    id int NOT NULL,
    title text,
    body text,
    PRIMARY KEY(id)
);

-- +goose Down
DROP TABLE post;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can write migration steps even in Go, not only SQL. But if using Go, &lt;em&gt;we need to make custom "goose" binary&lt;/em&gt; built with migration step Go sources. So it takes a little effort.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "database/sql"

    "github.com/pressly/goose"
)

func init() {
    goose.AddMigration(Up00002, Down00002)
}

func Up00002(tx *sql.Tx) error {
    _, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
    if err != nil {
        return err
    }
    return nil
}

func Down00002(tx *sql.Tx) error {
    _, err := tx.Exec("UPDATE users SET username='root' WHERE username='admin';")
    if err != nil {
        return err
    }
    return nil
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that we create migration files every time to change schema, and it is recommended to be placed on the same directory. Do you think that then many migration files are accumulated ? That's right, and it is very expected because many migration files just stand for migration history. So you can set up a database with either latest schema and the schema at the specified point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ls migrations
20181211185501_00001_create_tables.sql
20190506082527_00002_add_columns_for_new_feature.sql
20191020225109_00003_alter_field_type_for_bugfix.sql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Actually, to apply the database changes that are written in migration files use &lt;code&gt;goose up&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// apply changes up to latest
goose up

// apply changes up to the specified point
goose up-to 20181211102811
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And you always revert the schema changes with &lt;code&gt;goose down&lt;/code&gt; command unless the steps of "goose Down" on each files works correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// revert previous change
goose down

// revert changes up to the specified point
goose down-to 20181211102811
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Goose is a schema migration tool.&lt;/li&gt;
&lt;li&gt;Goose changes schema of DB and keeps tracability of that.&lt;/li&gt;
&lt;li&gt;Goose is portable, not depending on the project's framework.&lt;/li&gt;
&lt;li&gt;We can write even in Go, but it takes a little effort.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, this is my debut post on dev.to, and also my debut post written in English. You might find the strange expression in this post, but then please forgive me and point it out to me for English learning if you can ;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;WE ARE HIRING:&lt;/em&gt;&lt;/strong&gt; We are "LOB, inc", Japanese ad-tech start-up company in Rakuten Group and building the new global ad platform ! You don't need to hesitate even if you don't speak Japanese. In Rakuten Group the common language is English and many foreign engineer are working. If you are interested, please contact me or apply an entry from the following recruit page.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LOB, inc: &lt;a href="https://lob-inc.com/recruit/"&gt;https://lob-inc.com/recruit/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;About Rakuten group: &lt;a href="https://global.rakuten.com/corp/"&gt;https://global.rakuten.com/corp/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>mysql</category>
      <category>rdb</category>
    </item>
  </channel>
</rss>
