Skip to content

Instantly share code, notes, and snippets.

@hez
Last active Jun 6, 2022
Embed
What would you like to do?
Testing Ecto handling of order_by for SQLite3
defmodule Migrations.Testtable do
use Ecto.Migration
def change do
create table(:testing) do
add :company, :string
add :name, :string
add :vintage, :string
add :drunk_at, :utc_datetime
timestamps()
end
create index(:testing, [:company])
end
end
defmodule TestTable do
use Ecto.Schema
import Ecto.Changeset
require Ecto.Query
schema "testing" do
field :company, :string
field :name, :string
field :vintage, :string
field :drunk_at, :utc_datetime
timestamps()
end
@doc false
def changeset(bottle, attrs) do
bottle
|> cast(attrs, [
:company,
:name,
:vintage,
:drunk_at
])
|> validate_required([:company, :name])
end
@initial """
company,name,vintage
10 Barrel Brewing Company,Fancy Beer Series: Rose Bois Grand Cru,
Ale Apothecary,El Cuatro,2016-04-07
Almanac Beer Co,Dogpatch Grand Cru,2016-01-01
Almanac Beer Company,Grand Cru Edition No. 2: GOLDEN Imperial Sour Red Ale,2016-01-01
Freigeist Bierkultur,Prussian Imperial Stout,2016-01-01
Freigeist Bierkultur,Salzspeicher,2018-01-01
Fremont Brewing,Bourbon Barrel Aged Dark Star,2016-01-01
Fremont Brewing,"Bourbon Barrel Aged Dark Star ""Coffee Edition""",2017-01-01
Goose Island,Bourbon County Stout,2017-01-01
Gueuzerie Tilquin,GueuzeTilquin a l'ancienne,2017-01-01
Gueuzerie Tilquin,Mure Tilquin a l'ancienne,2017-01-01
Gueuzerie Tilquin,Quetsche Tilquin a l'ancienne,2017-01-01
Gueuzerie Tilquin,GueuzeTilquin a l'ancienne,2017-01-01
Gueuzerie Tilquin,Mure Tilquin a l'ancienne,2017-01-01
Hair of the Dog Brewing,Adam from the Stone,2015-01-01
Hair of the Dog Brewing,Side by Side Collaborative Ale,2017-01-01
Hair of the Dog Brewing,Cherry Fred from the Wood,2017-01-01
Hair of the Dog Brewing,Putin,2015-01-01
Hornbeer,Viking Chili Stout,2016-01-01
Les Trois Mousquetaires,Grande Cuvée,2018-01-01
Les Trois Mousquetaires,Grande Cuvée,2017-01-01
Les Trois Mousquetaires,Porter Baltique,2015-01-01
Luppolo,Albicocchina,2018-01-01
Luppolo,Prima Forza,2018-01-01
Main Street Brewing (Vancouver),Stag and Pheasant,2016-01-01
Mikkeller,Dry Stout (Sauternes Edition),2016-04-01
Moody Ales,Bourbon Barrel Aged Russian Imperial Stout,2016-01-01
Moody Ales,Bourbon Barrel Aged Russian Imperial Stout,2017-01-01
Moylan's Brewing Company,Old Blarney Barleywine Style Ale,2016-01-01
Parallel 49 Brewing Company,Barley Wine ,2014-01-01
Parallel 49 Brewing Company,Barrel Aged Russian Imperial Stout,2014-01-01
Parallel 49 Brewing Company,Barrel Aged Russian Imperial Stout,2016-01-01
Pelican Brewing Company,Mother of all Storms,2015-01-01
Pfriem Family Brewers,Belgian Style Stout,2015-01-01
Pfriem Family Brewers,Flanders-Style Red Ale,2016-07-01
Russian River Brewing Company,Consecration,2016-01-08
Russian River Brewing Company,Defenestration,2016-07-01
Russian River Brewing Company,Sanctification,2015-06-01
Russian River Brewing Company,Supplication,2016-06-01
Russian River Brewing Company,Temptation,2016-05-01
Stone Brewery ,12th Anniversary Bitter Chocolate Stout,2016-01-01
Strange Fellows Brewing,Boris (Barrel Aged),2014-11-01
Strange Fellows Brewing,Little Red One II,2017-01-01
Strange Fellows Brewing,Kriek,2017-01-01
Temporal Artisan Ales,Imaginary Path,2019-01-01
Temporal Artisan Ales,Ultraviolet Frequency,2019-01-01
Temporal Artisan Ales,Luminous Flux,2019-01-01
The Bruery,Bois - Aged in Bourbon,2013-01-01
The Bruery,Cuivre,2015-03-05
The Bruery,"Cuivre, Anno 2015 7th Anniversary ale",2015-01-01
The Bruery,White Oak,2016-01-01
The Bruery Terreux,Gypsy Tart,2016-03-01
Three Floyds Brewing Company,Dark Lord,2014-01-01
Timmermans,Timmermans Oude Kriek Limited Edition,2016-01-01
Unibroue,Grande Reserve 17,2015-01-01
Unibroue,Grande Reserve 17 2016,2016-01-01
Vliegende Paard Brouwers,Prearis Grand Cru,2017-11-01
"""
def insert_initial_data_set(repo) do
@initial
|> StringIO.open()
|> elem(1)
|> IO.binstream(:line)
|> CSV.decode!(headers: true)
|> Enum.map(fn line ->
%{company: line["company"], name: line["name"], vintage: line["vintage"]}
end)
|> Enum.each(fn attrs ->
%__MODULE__{}
|> changeset(attrs)
|> repo.insert!()
end)
end
def all(repo) do
__MODULE__
|> Ecto.Query.order_by([:company, :name, :vintage])
|> repo.all()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment