<?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: Marcøs</title>
    <description>The latest articles on DEV Community by Marcøs (@ideamarcos).</description>
    <link>https://dev.to/ideamarcos</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%2F315686%2Fab86175e-89ca-4b29-8240-07be61171e45.jpg</url>
      <title>DEV Community: Marcøs</title>
      <link>https://dev.to/ideamarcos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ideamarcos"/>
    <language>en</language>
    <item>
      <title>Elixir - csv import</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Thu, 29 Oct 2020 03:08:08 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-csv-import-4jhf</link>
      <guid>https://dev.to/ideamarcos/elixir-csv-import-4jhf</guid>
      <description>&lt;p&gt;See Files on Github&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/priv/repo/country-codes.csv"&gt;country-codes.csv&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/priv/repo/migrations/20200113205300_create_country_table.exs"&gt;create_country_table.exs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post expands on &lt;a href="https://experimentingwithcode.com/using-the-postgres-copy-command-in-ecto-migrations/"&gt;this article from 'Experimenting with Code'&lt;/a&gt; to only import a subset of csv columns into Postgres using Ecto.&lt;/p&gt;

&lt;p&gt;In this case, we are only concerned with the &lt;code&gt;code&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; columns from &lt;code&gt;country-codes.csv&lt;/code&gt;. This example uses &lt;a href="https://hexdocs.pm/elixir/File.html#stream!/3"&gt;File.stream()&lt;/a&gt; and &lt;a href="https://hexdocs.pm/csv/CSV.html#decode!/2"&gt;CSV.decode!()&lt;/a&gt; to get just the two columns and pass it to the &lt;a href="https://www.postgresql.org/docs/current/sql-copy.html"&gt;COPY&lt;/a&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;CSV&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decode!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;headers:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;CSV&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;into&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>elixir</category>
    </item>
    <item>
      <title>Elixir - schema drop</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Fri, 07 Aug 2020 19:43:49 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-schema-drop-3jj0</link>
      <guid>https://dev.to/ideamarcos/elixir-schema-drop-3jj0</guid>
      <description>&lt;p&gt;See Files on Github&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/config/config.exs"&gt;config.exs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/mix.exs"&gt;mix.exs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/priv/repo/schema_drop.exs"&gt;schema_drop.exs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There may be cases when you share a database (but not a schema) with other applications. So using &lt;a href="https://hexdocs.pm/ecto/Mix.Tasks.Ecto.Drop.html"&gt;mix ecto.drop&lt;/a&gt; may not be an option. In this case, dropping the schema may be a better alternative.&lt;/p&gt;

&lt;p&gt;First configure your &lt;a href="https://hexdocs.pm/ecto_sql/Ecto.Migration.html#module-repo-configuration"&gt;ecto migrations settings&lt;/a&gt; in config.exs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:portishead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Portishead&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Repo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;migration_default_prefix:&lt;/span&gt; &lt;span class="s2"&gt;"portishead"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;migration_source:&lt;/span&gt; &lt;span class="s2"&gt;"portishead_schema_migrations"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;schema_drop.exs&lt;/code&gt; script to the &lt;code&gt;priv/repo&lt;/code&gt; folder. The script gets the schema and migration table from the config and runs raw sql to drop them. This script is using &lt;a href="https://en.wikipedia.org/wiki/PL/pgSQL"&gt;PL/pgSQL.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="no"&gt;Repo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DROP SCHEMA IF EXISTS &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; CASCADE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;Repo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DROP TABLE IF EXISTS &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;migration_table&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;mix.exs&lt;/code&gt;, add an alias to drop the schema&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;  &lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;aliases&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="s2"&gt;"ecto.schema.drop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"run priv/repo/schema_drop.exs"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the shell, you can now run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mix ecto.schema.drop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can add an aliases to use &lt;code&gt;ecto.schema.drop&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;  &lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;aliases&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="s2"&gt;"ecto.schema.reset"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"ecto.schema.drop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ecto.setup"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>elixir</category>
      <category>ecto</category>
    </item>
    <item>
      <title>Elixir - be sorted, not sordid</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Mon, 15 Jun 2020 20:47:50 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-tests-with-sorting-3be7</link>
      <guid>https://dev.to/ideamarcos/elixir-tests-with-sorting-3be7</guid>
      <description>&lt;p&gt;&lt;a href="https://gist.github.com/ideaMarcos/096f415e8c42b4db72d4251aaede78bd"&gt;See code on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I notice that when devs use libraries like &lt;a href="https://github.com/thoughtbot/ex_machina"&gt;ExMachina&lt;/a&gt; to generate fake data for tests, they forget to test for case insensitivity. So they end up with unexpected results like all the uppercase letters appearing before all of the lowercase letters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To help with testing, I've written a helper function to randomize the case of a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;random_case&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;is_binary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can be combined with the [sequence] function (&lt;a href="https://hexdocs.pm/ex_machina/ExMachina.html#sequence/1"&gt;https://hexdocs.pm/ex_machina/ExMachina.html#sequence/1&lt;/a&gt;) in a factory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;article_factory&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;title:&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Article Title"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;ExMachinaHelper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random_case&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is some overhead in changing the case of strings so only choose the fields related to sorting.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>sorting</category>
      <category>testing</category>
    </item>
    <item>
      <title>Elixir setup - being consistent with schema</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Fri, 12 Jun 2020 02:53:37 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-setup-being-consistent-with-schema-4g9j</link>
      <guid>https://dev.to/ideamarcos/elixir-setup-being-consistent-with-schema-4g9j</guid>
      <description>&lt;p&gt;See Files on Github&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/lib/domain/schema/common.ex"&gt;common.ex&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/lib/domain/schema/band.ex"&gt;band.ex&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's typical to see &lt;a href="https://hexdocs.pm/ecto/Ecto.Schema.html#timestamps/1"&gt;timestamps()&lt;/a&gt; in every schema file, but what if you want to have the same primary key, schema prefix, or subset of columns in all the schema files.&lt;/p&gt;

&lt;p&gt;First create your own &lt;code&gt;using&lt;/code&gt; macro to handle the use/import statements along with any &lt;a href="https://hexdocs.pm/ecto/Ecto.Schema.html#module-schema-attributes"&gt;schema attributes&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;  &lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;__using__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Ecto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;
      &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Ecto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Changeset&lt;/span&gt;
      &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Portishead&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Common&lt;/span&gt;

      &lt;span class="nv"&gt;@primary_key&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:binary_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;autogenerate:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nv"&gt;@schema_prefix&lt;/span&gt; &lt;span class="s2"&gt;"premarcos"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then define a macro that contains the common fields across all your schemas. You can use &lt;code&gt;timestamps()&lt;/code&gt; or define your own date fields instead like &lt;code&gt;created_at&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;  &lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;common_fields&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="c1"&gt;# field :created_at, :utc_datetime_usec, autogenerate: {Ecto.Schema, DateTime.utc_now(), []}&lt;/span&gt;
      &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:string&lt;/span&gt;
      &lt;span class="n"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, in your schema file, use the module that was just created and call the &lt;code&gt;common_fields&lt;/code&gt; macro.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Portishead&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Band&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Portishead&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Common&lt;/span&gt;

  &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="s2"&gt;"band"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;common_fields&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also worth reading&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://bernheisel.com/blog/ecto_changeset_tips/"&gt;https://bernheisel.com/blog/ecto_changeset_tips/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elixir</category>
      <category>setup</category>
      <category>phoenix</category>
    </item>
    <item>
      <title>Elixir project setup - config.exs</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Tue, 28 Jan 2020 04:27:55 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-project-setup-config-exs-3p8m</link>
      <guid>https://dev.to/ideamarcos/elixir-project-setup-config-exs-3p8m</guid>
      <description>&lt;p&gt;I'll be writing about typical changes I make to the default phoenix project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/config/config.exs"&gt;See file on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE&lt;/em&gt;: This article assumes you will using Ecto, and the database tables use UUID as the primary key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you plan on running &lt;a href="https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Gen.Model.html"&gt;mix phoenix.gen.model&lt;/a&gt; to generate Ecto models, set the &lt;code&gt;generators&lt;/code&gt; key to use &lt;code&gt;binary_id&lt;/code&gt; for schema’s primary key.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:portishead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;generators:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;binary_id:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For &lt;a href="https://hexdocs.pm/ecto_sql/Mix.Tasks.Ecto.Migrate.html"&gt;mix ecto.migrate&lt;/a&gt; with &lt;a href="https://hexdocs.pm/ecto_sql/Ecto.Migration.html#create/2"&gt;create table&lt;/a&gt; syntax, set the &lt;code&gt;migration_primary_key&lt;/code&gt; to use &lt;code&gt;uuid&lt;/code&gt;. &lt;code&gt;migration_default_prefix&lt;/code&gt; is the schema name of the table in postgresql.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:portishead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Portishead&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Repo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;migration_primary_key:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;name:&lt;/span&gt; &lt;span class="ss"&gt;:uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;type:&lt;/span&gt; &lt;span class="ss"&gt;:uuid&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="ss"&gt;migration_default_prefix:&lt;/span&gt; &lt;span class="s2"&gt;"portishead"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;migration_source:&lt;/span&gt; &lt;span class="s2"&gt;"portishead_schema_migrations"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>elixir</category>
      <category>setup</category>
      <category>ecto</category>
    </item>
    <item>
      <title>Elixir project setup - mix.exs</title>
      <dc:creator>Marcøs</dc:creator>
      <pubDate>Tue, 28 Jan 2020 04:27:43 +0000</pubDate>
      <link>https://dev.to/ideamarcos/elixir-project-setup-mix-exs-16fo</link>
      <guid>https://dev.to/ideamarcos/elixir-project-setup-mix-exs-16fo</guid>
      <description>&lt;p&gt;I'll be writing about typical changes I make to the default phoenix &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ideaMarcos/portishead/blob/main/mix.exs"&gt;See file on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the minimum elixir version allowed. If you're working with other devs, this will force them to keep their local environment up to date.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      elixir: "&amp;gt;= 1.9.0",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Override the default tasks for &lt;a href="https://hexdocs.pm/mix/Mix.Tasks.Compile.html"&gt;compile&lt;/a&gt; and &lt;a href="https://hexdocs.pm/ecto_sql/Mix.Tasks.Ecto.Migrate.html"&gt;ecto.migrate&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  defp aliases do
    [
      compile: ["compile --warnings-as-errors"],
      "ecto.migrate": ["ecto.migrate --log-sql"],
    ...
    ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;compile&lt;/code&gt; alias is useful make sure that the &lt;code&gt;warnings-as-errors&lt;/code&gt; flag is always enabled, even when running &lt;code&gt;mix test&lt;/code&gt; or &lt;code&gt;mix release&lt;/code&gt;. The &lt;code&gt;ecto.migrate&lt;/code&gt; flag is to display the raw sql for migration scripts, not just the &lt;a href="https://hexdocs.pm/ecto/Ecto.Query.html"&gt;Ecto Query DSL&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>setup</category>
      <category>phoenix</category>
    </item>
  </channel>
</rss>
