<?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: Kyle Nickel</title>
    <description>The latest articles on DEV Community by Kyle Nickel (@nickelkr).</description>
    <link>https://dev.to/nickelkr</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%2F98885%2Fff11ad82-653e-45db-9f89-51f4f6dce61b.jpeg</url>
      <title>DEV Community: Kyle Nickel</title>
      <link>https://dev.to/nickelkr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nickelkr"/>
    <language>en</language>
    <item>
      <title>PlanetScale CLI Basics</title>
      <dc:creator>Kyle Nickel</dc:creator>
      <pubDate>Fri, 21 May 2021 03:48:37 +0000</pubDate>
      <link>https://dev.to/nickelkr/planetscale-cli-basics-21d8</link>
      <guid>https://dev.to/nickelkr/planetscale-cli-basics-21d8</guid>
      <description>&lt;p&gt;&lt;a href="https://www.planetscale.com/"&gt;PlanetScale&lt;/a&gt; is a new hosted database provider. Where it differs from the rest though is in it's branching/merging features for migrations.&lt;/p&gt;

&lt;p&gt;It allows developers to create branches off of the &lt;code&gt;main&lt;/code&gt; database schema and then apply their schema changes in isolation. &lt;/p&gt;

&lt;p&gt;After they're applied, a deploy request can be opened, approved, and merged seamlessly. It promises to be fast, non-blocking on schema changes, and easily scalable.&lt;/p&gt;

&lt;p&gt;Their free tier is generous and plenty to get started messing around with.&lt;/p&gt;

&lt;p&gt;This will be a quick crash course in how to use the CLI to accomplish some of the basic tasks through a  workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prereqs
&lt;/h2&gt;

&lt;p&gt;Installed the PlanetScale, &lt;code&gt;pscale&lt;/code&gt;, CLI utility:&lt;br&gt;
&lt;a href="https://docs.planetscale.com/reference/planetscale-environment-setup"&gt;CLI Install Instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installed the MySQL Shell:&lt;br&gt;
&lt;a href="https://docs.oracle.com/cd/E17952_01/mysql-shell-8.0-en/mysql-shell-install-linux-quick.html"&gt;MySQL Shell&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll be using Linux and ZSH for reference.&lt;/p&gt;
&lt;h2&gt;
  
  
  Logging In
&lt;/h2&gt;

&lt;p&gt;Logging into &lt;code&gt;pscale&lt;/code&gt; is easy, but interesting process. Start by running the &lt;code&gt;auth login&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale auth login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open a tab in your default browser which displays a code and a &lt;code&gt;confirm&lt;/code&gt; button.&lt;/p&gt;

&lt;p&gt;Looking back at your shell you should see the confirmation code listed on the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Confirmation Code: XXXXXXX

If something goes wrong, copy and paste this URL into your browser: https://auth.planetscale.com/oauth/device?user_code=XXXXXXX
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After verifying the code and accepting it on the page your terminal should show a &lt;code&gt;Successfully logged in.&lt;/code&gt; message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Database
&lt;/h2&gt;

&lt;p&gt;Starting out, you won't have any databases. The first step is to create one (I'm using &lt;code&gt;thunks&lt;/code&gt; as a database name, but obviously use whatever name you want):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale database create thunks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Connecting to the Database
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Note: The docs say we can just use &lt;code&gt;pscale shell thunks&lt;/code&gt;, however I get a error for a unknown option &lt;code&gt;--defaults-extra-file&lt;/code&gt;, so we'll be doing it manually-ish.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a database, let's connect to it and take a look at what we have. In your terminal create the tunnel to your database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale connect thunks
Secure connection to database thunks and branch main is established!.

Local address to connect your application: 127.0.0.1:3306 (press ctrl-c to quit)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give you a local address, default: &lt;code&gt;127.0.0.1&lt;/code&gt;, and the port, default: &lt;code&gt;3306&lt;/code&gt;. Use these to create your connection using the &lt;code&gt;mysqlsh&lt;/code&gt; shell. You can omit the port if it's the default and when prompted for a password hit &lt;code&gt;enter&lt;/code&gt; without entering one. You'll need to do this in a different shell instance as the connection process is blocking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; mysqlsh --sql -uroot -h127.0.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Taking a look at what we have for databases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; show databases;

+--------------------+
| Database           |
+--------------------+
| thunks             |
| information_schema |
| mysql              |
| sys                |
| performance_schema |
+--------------------+
5 rows in set (0.0279 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see our new database &lt;code&gt;thunks&lt;/code&gt; listed. It should be empty though:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; show tables;

Empty set (0.0245 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as we expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Branches
&lt;/h2&gt;

&lt;p&gt;A empty database isn't of much use, so let's create a new branch to start a migration on.&lt;/p&gt;

&lt;p&gt;PlanetScale uses branches much like &lt;code&gt;git&lt;/code&gt;. So, we can create a new branch, perform our changes, and then ask the maintainer to merge/deploy those changes in. This is one of the most interesting features of PlanetScale. It allows changes to occur in isolation and deploys to be rolled out seamlessly.&lt;/p&gt;

&lt;p&gt;By default a database is created with a branch of &lt;code&gt;main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To create a new branch we want to run the &lt;code&gt;branch&lt;/code&gt; command with the desired name. We'll start by adding a users table so we can get some people into our data set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale branch create thunks add-users-table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then check that it was successfully created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale branch list thunks

  NAME              STATUS   PARENT BRANCH   CREATED AT       UPDATED AT       NOTES  
 ----------------- -------- --------------- ---------------- ---------------- ------- 
  main                       n/a             14 minutes ago   13 minutes ago          
  add-users-table            main            1 minute ago     47 seconds ago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Making Changes
&lt;/h2&gt;

&lt;p&gt;The connection we created earlier was to our &lt;code&gt;main&lt;/code&gt; branch of the database, but the changes we want to make will be on &lt;code&gt;add-users-table&lt;/code&gt;. Close the &lt;code&gt;mysqlsh&lt;/code&gt; session and kill the connection process.&lt;/p&gt;

&lt;p&gt;Now, reconnect using the our new branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale connect thunks add-users-table      
Secure connection to database thunks and branch add-users-table is established!.

Local address to connect your application: 127.0.0.1:3306 (press ctrl-c to quit)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reconnect the MySQL Shell the same way as before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; mysqlsh --sql --uroot -h127.0.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can make our changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; CREATE TABLE users (
  login VARCHAR(50),
  name  VARCHAR(50)
);

&amp;gt; describe users;

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| login | varchar(50) | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.0183 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have made the changes on our branch. Let's verify that these haven't been propagated to our &lt;code&gt;main&lt;/code&gt; branch yet. Do the disconnect/reconnect dance and select the &lt;code&gt;main&lt;/code&gt; branch. Then check the tables in the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; show tables;

Empty set (0.0247 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perfect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy Requests
&lt;/h2&gt;

&lt;p&gt;Now that the changes we want are on our own branch, we need to create a deploy request to get them merged into the &lt;code&gt;main&lt;/code&gt; branch. This is very similar to a PR/MR in GitHub or GitLab. &lt;/p&gt;

&lt;p&gt;We do this by running the &lt;code&gt;deploy-request&lt;/code&gt; command with the database name and the branch we want merged in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale deploy-request create thunks add-users-table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we're able to list the deploy requests that are pending:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale deploy-request list thunks
  ID             NUMBER   BRANCH            INTO BRANCH   APPROVED   STATE   DEPLOY STATE   DEPLOYABLE   QUEUED AT      STARTED AT     FINISHED AT   CREATED AT   UPDATED AT   CLOSED AT  
 -------------- -------- ----------------- ------------- ---------- ------- -------------- ------------ -------------- -------------- ------------- ------------ ------------ ----------- 
  zdsty5vxjfin   1        add-users-table   main          No         open    ready          Yes          1 minute ago   1 minute ago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like a PR/MR, we'll need approval before we can deploy our changes to &lt;code&gt;main&lt;/code&gt;. This uses the &lt;code&gt;number&lt;/code&gt; listed above as the identifier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale deploy-request review --approve thunks 1
Deploy request thunks/1 is approved.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying Changes
&lt;/h2&gt;

&lt;p&gt;Now that our changes have been approved, we can deploy them out to main and get out database migrated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale deploy-request deploy thunks 1
Successfully deployed zdsty5vxjfin from add-users-table to main.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should verify that our changes have been made before cleaning up. Connect to the main branch and open up a MySQL Shell then check the schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; describe users;

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| login | varchar(50) | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.0213 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, our changes are there on main. Now for some finishing tasks.&lt;/p&gt;

&lt;p&gt;Let's start out by closing the deploy request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale deploy-request close thunks 1
Deploy request thunks/1 was successfully closed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can delete the branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; pscale branch delete thunks add-users-table
? Please type thunks/add-users-table to confirm: thunks/add-users-table
Branch add-users-table was successfully deleted from thunks.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've migrated our database and cleaned up the request and branch.&lt;/p&gt;

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

&lt;p&gt;PlanetScale is definitely cool. It brings the very familiar feel of branching, request, and merging to live databases.&lt;/p&gt;

&lt;p&gt;It promises to be fast, non-blocking on schema changes, and easily scalable.&lt;/p&gt;

&lt;p&gt;I do wish the CLI documentation was better. It's pretty sparse after the top-level commands, so the &lt;code&gt;help&lt;/code&gt; messages will be your friend.&lt;/p&gt;

&lt;p&gt;Now that I've gotten local the local development and CLI down, I'm excited to give it a go with a app and deployment.&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Auto-alias Your Repos</title>
      <dc:creator>Kyle Nickel</dc:creator>
      <pubDate>Sat, 25 Jan 2020 03:04:21 +0000</pubDate>
      <link>https://dev.to/nickelkr/auto-alias-your-repos-5epo</link>
      <guid>https://dev.to/nickelkr/auto-alias-your-repos-5epo</guid>
      <description>&lt;p&gt;If you have all your code repos under a common directory, you can make it so changing your working directory to any of the repos is just entering the name of the repo.&lt;/p&gt;

&lt;p&gt;For example, all my code repos are under &lt;code&gt;~/code&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~ &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; ~/code
bar    foo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding a loop for the sub-directories of the common parent inside your shell config, &lt;code&gt;~/.zshrc&lt;/code&gt; in my case, you can automatically create a alias for each of the repo directories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;repo &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; ~/code&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;alias&lt;/span&gt; &lt;span class="nv"&gt;$repo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cd ~/code/&lt;/span&gt;&lt;span class="nv"&gt;$repo&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you need to reload your config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~ &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you're in one repo, lets say &lt;code&gt;foo&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/code/foo/app/models &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can easily swap over to bar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/code/foo/app/models &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; bar
~/code/bar &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>linux</category>
      <category>productivity</category>
      <category>tips</category>
    </item>
  </channel>
</rss>
