<?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>Forem: Luke Babich</title>
    <description>The latest articles on Forem by Luke Babich (@codewithluke).</description>
    <link>https://forem.com/codewithluke</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%2F62658%2Fd8a8e3d1-9f5b-43cf-88e8-825f09c03fbd.jpeg</url>
      <title>Forem: Luke Babich</title>
      <link>https://forem.com/codewithluke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/codewithluke"/>
    <language>en</language>
    <item>
      <title>Create and Seed Your Postgres Database in Minutes with Docker</title>
      <dc:creator>Luke Babich</dc:creator>
      <pubDate>Wed, 14 Dec 2022 09:38:05 +0000</pubDate>
      <link>https://forem.com/codewithluke/create-and-seed-your-postgres-database-in-minutes-with-docker-1i11</link>
      <guid>https://forem.com/codewithluke/create-and-seed-your-postgres-database-in-minutes-with-docker-1i11</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3lxh9x2txg5jywnmd90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3lxh9x2txg5jywnmd90.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;adventure time Golang Gopher character growing out of ground, chaotic composition, photorealistic - Created with Midjourney&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; What is Database Seeding and Why?&lt;/li&gt;
&lt;li&gt; Building Your Postgres Docker Container

&lt;ul&gt;
&lt;li&gt;  Prerequisites&lt;/li&gt;
&lt;li&gt;  Creating the Docker Container&lt;/li&gt;
&lt;li&gt;  Connecting to our DB&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  Structuring Your DB with Go Migrate

&lt;ul&gt;
&lt;li&gt;  Generating Our Migration Scripts&lt;/li&gt;
&lt;li&gt;  Running our Scripts&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  Populating Your DB with Go Faker

&lt;ul&gt;
&lt;li&gt;  Generating Fake Data&lt;/li&gt;
&lt;li&gt;  Inserting Fake Data into the Database&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Summary&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This article help us understand the importance of creating silo'ed development environments that allow us to quickly iterate, create scenarios and test. In this article we will focus on using Go as our language to programatically seed our data. However there are tools in other languages to achieve the same results. Whatever your preference, this article just hopes to achieve bringing light to how you can go about it.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Database Seeding and why
&lt;/h1&gt;

&lt;p&gt;Database seeding is the process of adding initial data to a database. This is often done for testing or development purposes, as it allows developers to work with a sample dataset without having to worry about corrupting real data. Database seeding can be useful for testing the functionality of a database, as well as for experimenting with different queries and data structures. In general, seeding a database with fake data can help ensure that the database is working properly and can be useful for a variety of purposes.&lt;/p&gt;

&lt;p&gt;One important aspect we want to ensure is that it is quick and easy to reboot and create these scenarios or get us back to a stable point.&lt;/p&gt;

&lt;h1&gt;
  
  
  1) Building Your Postgres Docker Container
&lt;/h1&gt;

&lt;p&gt;Before we can start seeding our database we first have to get our container up and running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before we begin with creating a container we need to ensure that we have &lt;a href="https://docs.docker.com/get-started/" rel="noopener noreferrer"&gt;Docker installed and running&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now you will not need to worry about getting too advanced with Docker features. So if you are not very well versed in Docker do not worry. &lt;/p&gt;

&lt;p&gt;In short we are going to use a predefined Docker image for Postgres and then modify and put some data into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the Docker Container
&lt;/h3&gt;

&lt;p&gt;So to start we want to create our bare bone Postgres container:&lt;/p&gt;

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

docker run &lt;span class="nt"&gt;--name&lt;/span&gt; my-db &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;database &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 5432:5432 postgres


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create a new Docker container named "my-db" using the latest version of the Postgres image from Docker Hub. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-e&lt;/code&gt; flag is used to set the password for the default user (default is &lt;code&gt;postgres&lt;/code&gt;) to mysecretpassword as well as creating us a database inside our container called 'database' (default is &lt;code&gt;postgres&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-d&lt;/code&gt; flag specifies that the container should run in detached mode, running in the background. &lt;/p&gt;

&lt;p&gt;Then the &lt;code&gt;-p&lt;/code&gt; flag is used to expose the port of our container and map it to our db, in this case it is a mapping of &lt;code&gt;5432 -&amp;gt; 5432&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To confirm it is running we can then run:&lt;/p&gt;

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

docker ps


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Connecting to our DB
&lt;/h3&gt;

&lt;p&gt;So now that our container is up and running you may be asking yourself how do I connect to it or how do I know it is working?&lt;/p&gt;

&lt;p&gt;We will now connect to our new Postgres container to then confirm that it is as we expect:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

docker container &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; my-db bash


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once connected to the container we can confirm all is running fine with:&lt;/p&gt;

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

psql &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;user&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;db&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;NOTE: to exit this psql &lt;code&gt;\q&lt;/code&gt; and hit enter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So if you have followed the above you can swap out user for &lt;code&gt;postgres&lt;/code&gt; and db for &lt;code&gt;database&lt;/code&gt; or whatever you have named your instance.&lt;/p&gt;

&lt;p&gt;Now you are able to execute and insert data into your new database.&lt;/p&gt;

&lt;p&gt;If you prefer to just connect to the container and run queries direct you can modify the above:&lt;/p&gt;

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

psql &lt;span class="nt"&gt;-U&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;user&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;db&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"{SQL_QUERY}"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  2) Structuring Your DB with Go Migrate
&lt;/h1&gt;

&lt;p&gt;Now that we have got our database running we want to now populate it with some tables. &lt;/p&gt;

&lt;p&gt;This involves specifying the schema, which defines the structure and organization of the data, as well as any constraints or rules for the data. To do this we are going to use &lt;a href="https://github.com/golang-migrate/migrate" rel="noopener noreferrer"&gt;Go Migrate&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Tools like Go migrate are typically used in database development to manage changes to the database schema over time. In our case we can make use of this tool to define our initial DB schema which will we then populate with the data we want.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating Our Migration Scripts
&lt;/h2&gt;

&lt;p&gt;Using the Go Migrate CLI we will first create our migration files in a folder of choice in our project.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

migrate create &lt;span class="nt"&gt;-ext&lt;/span&gt; sql &lt;span class="nt"&gt;-dir&lt;/span&gt; db/schema &lt;span class="nt"&gt;-seq&lt;/span&gt; schema_init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create an up and down file. I recommend reading into how migration tools work. In short however. &lt;em&gt;Up&lt;/em&gt; has to do with doing some form of change to your DB. Whereas &lt;em&gt;Down&lt;/em&gt; has to do with reverting the changes. &lt;/p&gt;

&lt;p&gt;When using the CLI this will add some numbers to the front. This is used for internal tracking of what SQL scripts have been executed and what has not. So make sure to keep these.&lt;/p&gt;

&lt;p&gt;Once you have generated your migration files we can then add our SQL scripts to structure our DB:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;

&lt;span class="c1"&gt;-- schema_init.up.sql&lt;/span&gt;

&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Create the users table&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;       &lt;span class="nb"&gt;SERIAL&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="n"&gt;username&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;handle&lt;/span&gt;   &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create 2 tables called &lt;em&gt;rooms&lt;/em&gt; and &lt;em&gt;users&lt;/em&gt;. You can create your structure however you want but this is just to show as an example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running our Scripts
&lt;/h2&gt;

&lt;p&gt;You can use the CLI to run a migration script while connecting to the DB directly. However in this example I am going to show you the code version.&lt;/p&gt;

&lt;p&gt;TLDR; this is the end code:&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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
   &lt;span class="s"&gt;"database/sql"&lt;/span&gt;  
   &lt;span class="s"&gt;"fmt"&lt;/span&gt;   &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4"&lt;/span&gt;   &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4/database/postgres"&lt;/span&gt;   &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4/source/file"&lt;/span&gt;  
   &lt;span class="s"&gt;"github.com/joho/godotenv"&lt;/span&gt;   &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/lib/pq"&lt;/span&gt;  
   &lt;span class="s"&gt;"log"&lt;/span&gt;   &lt;span class="s"&gt;"os"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"postgres://postgres:mysecretpassword@localhost:5432/database?sslmode=disable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WithInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="c"&gt;// NOTE: relative path to folder  &lt;/span&gt;
   &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewWithDatabaseInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
      &lt;span class="s"&gt;"file://./db/dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
      &lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&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;Over here I have my main package that I can run. Using the &lt;code&gt;database/sql&lt;/code&gt; and &lt;code&gt;golang-migrate&lt;/code&gt; packages.&lt;/p&gt;

&lt;p&gt;First we open a connection to our container DB, which includes the username, password, host, and port of the database, as well as the database name and SSL mode. We then create a new instance of the Postgres driver and uses it to connect to the database instance.&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;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"postgres://postgres:mysecretpassword@localhost:5432/database?sslmode=disable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

   &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WithInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then we create a new instance of the migrate package, using the path to the migration files and the Postgres driver instance (In my case this is relative path to where I am executing this script). This allows the migrate package to access and manage the database schema using the provided migration files.&lt;/p&gt;

&lt;p&gt;Finally, the code calls the &lt;code&gt;Up()&lt;/code&gt; method on the migrate instance, which will apply any pending migration scripts to the database, updating the schema as necessary.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;

   &lt;span class="c"&gt;// NOTE: relative path to folder  &lt;/span&gt;
   &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewWithDatabaseInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
      &lt;span class="s"&gt;"file://./db/dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
      &lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After this your running Postgres Container will now have its relevant schema set up. If you are wanting to change this at all. Create a &lt;em&gt;Down&lt;/em&gt; script that removes these tables or simply delete you docker container and recreate it.&lt;/p&gt;

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

docker &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;container&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Populating Your DB with Go Faker
&lt;/h1&gt;

&lt;p&gt;Now that we have our DB and our Schema for our DB set and in place we now want to populate it with some fake data using &lt;a href="https://github.com/bxcodec/faker" rel="noopener noreferrer"&gt;Go Faker&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating Fake Data
&lt;/h2&gt;

&lt;p&gt;Go Faker is a handy little library that allows us to define a Struct, add some tags and it will then randomly generate data based on the tags we define.&lt;/p&gt;

&lt;p&gt;In the example above we defined a &lt;code&gt;users&lt;/code&gt; table. Now for simplicity we are going to generate ourselves a user and then insert that user into our DB.&lt;/p&gt;

&lt;p&gt;First let's go ahead and define a user Struct:&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;User&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;Username&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Handle&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see here there is nothing crazy going on. Just a simple struct with 2 properties. We can then dive into how faker can work in 2 different ways. Depending on what your needs are you may want to generate the data yourself or use aspects of faker to do it for us.&lt;/p&gt;

&lt;p&gt;Let's modify our User model slightly by adding a faker tag to generate a username:&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;User&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;Username&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`faker:"username"`&lt;/span&gt;
    &lt;span class="n"&gt;Handle&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above has now defined that when we use Faker to generate our data that we want to use it's inbuilt generation to create a random username for us. There are a few other ways that you can ensure that it is unique. But there are many &lt;a href="https://github.com/bxcodec/faker/blob/master/example_with_tags_test.go" rel="noopener noreferrer"&gt;tags you can use&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To then generate using Faker we then can add the following piece of code:&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;func&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;faker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FakeData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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="n"&gt;user&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This method over here will now generate our user with a random username and a random string for their handle (if you do not define a tag it will generate randomly based on the type).&lt;/p&gt;

&lt;p&gt;The alternative to the above is:&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;func&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;internal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;  
   &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;faker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With the above approach there is no need for the tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inserting Fake Data into the Database
&lt;/h2&gt;

&lt;p&gt;We now have a way of generating a user and inserting them into our DB. However how do we tie all this in together?&lt;/p&gt;

&lt;p&gt;TLDR;&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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
   &lt;span class="s"&gt;"database/sql"&lt;/span&gt;  
   &lt;span class="s"&gt;"fmt"&lt;/span&gt;   &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4"&lt;/span&gt;   &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4/database/postgres"&lt;/span&gt;   &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/golang-migrate/migrate/v4/source/file"&lt;/span&gt;  
   &lt;span class="s"&gt;"github.com/joho/godotenv"&lt;/span&gt;   &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/lib/pq"&lt;/span&gt;  
   &lt;span class="s"&gt;"log"&lt;/span&gt;   &lt;span class="s"&gt;"os"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"postgres://postgres:mysecretpassword@localhost:5432/database?sslmode=disable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WithInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="c"&gt;// NOTE: relative path to folder  &lt;/span&gt;
   &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewWithDatabaseInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
      &lt;span class="s"&gt;"file://./db/dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
      &lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

   &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&gt;}&lt;/span&gt;  

    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;insertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;faker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FakeData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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="n"&gt;user&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;insertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;internal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;sqlStatement&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"INSERT INTO users (username, handle) VALUES ($1, $2) RETURNING id;"&lt;/span&gt;  

   &lt;span class="n"&gt;lastInsertId&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;  
   &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QueryRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlStatement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lastInsertId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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="n"&gt;lastInsertId&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lets break down the new sections we just added to the above code:&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;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;insertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the main we then called our &lt;code&gt;createUser&lt;/code&gt; method and then handed this over to another method who will then insert that user into our DB.&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;func&lt;/span&gt; &lt;span class="n"&gt;insertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;internal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;sqlStatement&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"INSERT INTO users (username, handle) VALUES ($1, $2) RETURNING id;"&lt;/span&gt;  

   &lt;span class="n"&gt;lastInsertId&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;  
   &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QueryRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlStatement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lastInsertId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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="n"&gt;lastInsertId&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above is just taking our original connection when we did our schema setup and then executing a SQL query to insert into our users table.&lt;/p&gt;

&lt;p&gt;This proceeds to then return the index that was last inserted in case we wanted to perform another action after.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Hopefully all the above makes sense. To summarize everything:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We created a bare bones Postgres Docker container&lt;/li&gt;
&lt;li&gt;Created scripts to structure our DB for us (Users table) using Go Migrate&lt;/li&gt;
&lt;li&gt;We then generated a random user with fake data using Go Faker&lt;/li&gt;
&lt;li&gt;Inserted this new user and returned the insertion ID&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The example is not overly complicated however you can expand and add to it as you wish. Now as I said this is not something just for Go. Other languages have their own versions of these tools. The main takeaway is understanding their use cases and how they can be used together to create a development environment that is easy to modify and simple to reset should you need to! As always these things can be as complicated as you require.&lt;/p&gt;

&lt;p&gt;If you liked this article be sure to follow or check out some of my socials. I &lt;a href="https://www.twitch.tv/codingwithluke" rel="noopener noreferrer"&gt;also stream on Twitch&lt;/a&gt;  every Tuesday + Thursday at GMT+1.&lt;/p&gt;

</description>
      <category>go</category>
      <category>postgres</category>
      <category>docker</category>
      <category>database</category>
    </item>
    <item>
      <title>Dependency Injection in under 2minutes</title>
      <dc:creator>Luke Babich</dc:creator>
      <pubDate>Thu, 19 May 2022 08:53:34 +0000</pubDate>
      <link>https://forem.com/codewithluke/dependency-injection-in-under-2minutes-1pjn</link>
      <guid>https://forem.com/codewithluke/dependency-injection-in-under-2minutes-1pjn</guid>
      <description>&lt;p&gt;Hello Devs! Lets talk &lt;strong&gt;DI&lt;/strong&gt; or &lt;strong&gt;dependency injection&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  The easy to digest analogy:
&lt;/h3&gt;

&lt;p&gt;You work in a car factory. Each part of the car making process requires either multiple pieces of specialized machinery or a person to put the car together.&lt;/p&gt;

&lt;p&gt;Stage 1 of production requires a machine that cuts the frame, and another to lift the frame to stage 2. &lt;/p&gt;

&lt;p&gt;In stage 2 it then puts assembles the frame the car puts it together. It then uses the same machine to lift and take it to stage 3. &lt;/p&gt;

&lt;p&gt;In stage 3 we may have a person who comes and inspects the final product and then sends it on its way.&lt;/p&gt;

&lt;h3&gt;
  
  
  The summary:
&lt;/h3&gt;

&lt;p&gt;The above analogy can be thought of as you have a code base or group of functionality (the car factory) that is made up of different services or functionality (machines or people).  &lt;/p&gt;

&lt;p&gt;These pieces of functionality could be used in one or multiple places in your code base (the stages of production).&lt;/p&gt;

&lt;p&gt;Dependency injection is about providing your functionality to your different pieces of code. Without that code directly importing or using that piece of code (this is the responsibility of something called an injector to provide it). &lt;/p&gt;

&lt;p&gt;This makes it easy to swap it out and only have to make sure that whatever you are swapping it with conforms to the interface that piece of code requires (it is about making sure the contract of functionality is met).&lt;/p&gt;

&lt;p&gt;The benefit of dependency injection is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decreased coupling between pieces of code&lt;/li&gt;
&lt;li&gt;Increased flexibility in swapping functionality (what happens if you need a new machine to lift or move assemble the car?)&lt;/li&gt;
&lt;li&gt;Ease of testability of your code&lt;/li&gt;
&lt;li&gt;Less boilerplate&lt;/li&gt;
&lt;li&gt;It allows concurrent development. Multiple developers can work on different classes that use each other, while only needing to know the interface the classes will communicate through.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this has helped!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to identify and avoid burnout as a software developer</title>
      <dc:creator>Luke Babich</dc:creator>
      <pubDate>Mon, 06 Sep 2021 13:23:54 +0000</pubDate>
      <link>https://forem.com/codewithluke/how-to-identify-and-avoid-burnout-as-a-software-developer-1e4i</link>
      <guid>https://forem.com/codewithluke/how-to-identify-and-avoid-burnout-as-a-software-developer-1e4i</guid>
      <description>&lt;p&gt;Recently me and some friends have been working on a side project. Since we had been hanging out together a lot more you start to get some insight into how they manage their time, as well as see how they burnout.&lt;/p&gt;

&lt;p&gt;This started to get me thinking that in the current tech space there is this constant need for validation and push towards always learning. However it is not talked about how on top of your regular job these extra learnings, side projects and general life start to add up and contribute to burnout.&lt;/p&gt;

&lt;p&gt;I recently made a video that outlines the issues and how to deal with burnout. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/IZVZkVG283A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;However I will also discuss it here.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt; &lt;br&gt;
I will focus on one main point within this article and that has to do with how developers (the ones in the frontend space in particular) are swamped with a so much information, tools, frameworks etc.&lt;/p&gt;

&lt;p&gt;This is not a bad thing. However I am sure there are people that have reached a point where they have been reading articles or been in software long enough to see the infamous &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;x framework vs y framework&lt;/strong&gt;, &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Top 10 tools this week&lt;/strong&gt;, &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What you need to know to be a good developer&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once again these are not necessarily bad. I am more focusing on the mindset. The feeling newer developers start to experience when either coming into the industry or just starting. I know I felt it when I first started and that was when half the current tools didn't exist yet. This feeling of a massive amount of learning. &lt;/p&gt;

&lt;p&gt;If I had to list every single tool or framework I have worked with or learnt, either in my free time or work the list would be quite a nice chunky read. The frontend ecosystem is always growing and evolving. However there is always pressure on people to try and learn it all as quick as they can. There is a reason there is a meme behind "Have 3 years of experience in X framework" when it has only been around for a year. This pressure to learn the latest and newest things as fast as possible, causes a ripple effect across the industry.&lt;/p&gt;

&lt;p&gt;What does this result in? New and existing developers feeling pressure to dedicate what little time they have and this can lead to burnout and lifestyle imbalances.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Controversy
&lt;/h2&gt;

&lt;p&gt; &lt;br&gt;
This take might seem somewhat double edged. On one hand its important to grow your skills. However on the other it is also important to not just wither away learning something that may not even be relevant in a few years time. Or rather, just to live a little.&lt;/p&gt;

&lt;p&gt;Some might say that learning is what they enjoy. That is fine and maybe then this article is not something that would really appeal to you. The idea behind this is to identify unhealthy habits that some people have when it comes to learning in their free time, on top of a job, side project and just a general busy schedule.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what's the solution?
&lt;/h2&gt;

&lt;p&gt; &lt;br&gt;
The solution is not to stop learning. The solution is to schedule your time accordingly. &lt;/p&gt;

&lt;p&gt;As an example I dedicate 30min in the morning and evening (this is more a time to wind down and relax) to reading. During my day job, at lunch I use the gym to stay in shape and ensure I stay healthy.&lt;/p&gt;

&lt;p&gt;After work it becomes a choice, dedicate an hour or two to studies/articles/videos or on my side project. The rest of my time is dedicated to what I enjoy, playing games with my friends or watching some TV.&lt;/p&gt;

&lt;p&gt;By scheduling this time it ensures that I am getting the right amount of sleep and balance. This balance allows not only for me to grow as a developer but also to grow as a person and enjoy the things I want. &lt;/p&gt;

&lt;p&gt;I just want to re-iterate. If what you find as fun is learning and doing little code projects this is 100% fine. The point behind the solution is not to stop coding and doing those things. It is about managing your time in order to ensure that you get sleep/exercise/eat well and stay hydrated. What you do for fun is up to you. However that fun should not make you feel pressured or induce anxiety and stress to do it. That fun time should be the stuff that allows you to unwind and reset mentally in order to avoid a prolonged or new burnout.&lt;/p&gt;

&lt;p&gt;I hope this was helpful to some as I have found this is what works best for me. Feel free to leave any questions and if you did check out that video that is much appreciated as well! &lt;/p&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript: File Naming Conventions</title>
      <dc:creator>Luke Babich</dc:creator>
      <pubDate>Mon, 10 Feb 2020 10:09:14 +0000</pubDate>
      <link>https://forem.com/codewithluke/javascript-file-naming-conventions-1fn7</link>
      <guid>https://forem.com/codewithluke/javascript-file-naming-conventions-1fn7</guid>
      <description>&lt;p&gt;&lt;code&gt;So first a little disclaimer, I don't feel how I write files to be better than the next person. This is more of an open forum to discuss how people go about naming theirs and how the manage files as a system grows.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So what exactly do I mean?
&lt;/h2&gt;

&lt;p&gt;So you are working in your company, personal project or whatever it may be and it starts to scale. You get lots of files, loads of components etc.&lt;/p&gt;

&lt;p&gt;So in general how do you make sure that you understand what something is? Coming from starting in Angular and reading &lt;a href="https://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=asc_df_0132350882/?tag=googshopuk-21&amp;amp;linkCode=df0&amp;amp;hvadid=310913487979&amp;amp;hvpos=1o1&amp;amp;hvnetw=g&amp;amp;hvrand=11954773612972842206&amp;amp;hvpone=&amp;amp;hvptwo=&amp;amp;hvqmt=&amp;amp;hvdev=c&amp;amp;hvdvcmdl=&amp;amp;hvlocint=&amp;amp;hvlocphy=1007268&amp;amp;hvtargid=aud-859821414841:pla-435472505264&amp;amp;psc=1&amp;amp;th=1&amp;amp;psc=1"&gt;Clean Code by Robert Martin&lt;/a&gt; I am very used to the approach of naming my files according to the thing that it is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Component&lt;/li&gt;
&lt;li&gt;Service&lt;/li&gt;
&lt;li&gt;Router&lt;/li&gt;
&lt;li&gt;Util&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(communication.service.ts or button.component as an example)&lt;/p&gt;

&lt;p&gt;The list goes on. Each has a reason for the name, each has a difference in its responsibility. Just like a function would have its single responsibility.&lt;/p&gt;

&lt;p&gt;Now coming into React/Vue most components stick to UpperPascal for their naming and a lot of people just name their files without saying what it is but sometimes what it does. Its just a different approach to file splitting.&lt;/p&gt;

&lt;p&gt;So the question comes in how do you go about naming and scaling your code base? What approaches did you take to ensure you don't get overwhelmed by files with hidden meaning behind their names?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>angular</category>
      <category>react</category>
    </item>
  </channel>
</rss>
