From 09585f94afacbd428fd28665ca87bcc480f5d5f5 Mon Sep 17 00:00:00 2001 From: Rob Conery Date: Wed, 13 Apr 2016 11:10:05 -0700 Subject: [PATCH] Fixed borken bilt --- lib/moebius.ex | 12 +++----- test/test_helper.exs | 73 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/lib/moebius.ex b/lib/moebius.ex index 51a563f..9f03715 100644 --- a/lib/moebius.ex +++ b/lib/moebius.ex @@ -15,17 +15,15 @@ defmodule Moebius do These functions hand off to PSQL because Postgrex can't run more than one command per query. """ - # def run_with_psql(sql, opts) do - # db = opts[:database] || opts[:db] - # args = ["-d", db, "-c", sql, "--quiet", "--set", "ON_ERROR_STOP=1", "--no-psqlrc"] - # IO.inspect args - # System.cmd "psql", args - # end + def run_with_psql(sql, opts) do + db = opts[:database] || opts[:db] + args = ["-d", db, "-c", sql, "--quiet", "--set", "ON_ERROR_STOP=1", "--no-psqlrc"] + System.cmd "psql", args + end def get_connection(), do: get_connection(:connection) def get_connection(key) when is_atom(key) do opts = Application.get_env(:moebius, key) - IO.inspect opts cond do Keyword.has_key?(opts, :url) -> Keyword.merge(opts, parse_connection(opts[:url])) true -> opts diff --git a/test/test_helper.exs b/test/test_helper.exs index dca6a70..ae648b1 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,11 +1,74 @@ ExUnit.start() defmodule TestDb, do: use Moebius.Database -import Moebius.Query -worker = Supervisor.Spec.worker(TestDb, [Moebius.get_connection(:test_db)]) + +worker = Supervisor.Spec.worker(TestDb, [Moebius.get_connection]) Supervisor.start_link [worker], strategy: :one_for_one -IO.inspect "Dropping and reloading db..." -sql_file(:test_schema) |> TestDb.first -IO.inspect "Here we go..." +schema_sql = """ +drop index if exists idx_docs; +drop table if exists user_docs; +drop table if exists logs; +drop table if exists users; +drop table if exists products; +drop table if exists date_night; + +create table users( + id serial primary key, + email varchar(50) unique not null, + first varchar(50), + last varchar(50), + order_count integer not null default 10, + profile jsonb +); + +create table products( + id serial primary key not null, + sku varchar(50) not null, + name varchar(255) not null, + price decimal(10,2) not null default 0, + description text, + search tsvector, + variants jsonb +); + +create table logs( + id serial primary key not null, + user_id integer references users(id), + log text +); + +create table user_docs( + id serial primary key not null, + body jsonb not null, + created_at timestamptz default now(), + updated_at timestamptz +); + +create index idx_docs on user_docs using GIN(body jsonb_path_ops); + +insert into users(email, first, last) values('rob@test.com','Rob','Blah'); +insert into users(email, first, last) values('jill@test.com','Jill','Gloop'); +insert into users(email, first, last) values('mary@test.com','Mary','Muggtler'); +insert into users(email, first, last) values('mike@test.com','Mike','Ghruoisl'); + + +create table date_night(id serial primary key, date timestamptz); +insert into date_night(date) values(now()); +insert into date_night(date) values(now() - '1 day' :: interval); +insert into date_night(date) values(now() + '2 days' :: interval); +insert into date_night(date) values(now() + '1 year' :: interval); + +drop table if exists sessions; +create table sessions( + id varchar(36) primary key not null, + body jsonb not null, + search tsvector, + created_at timestamptz not null default now(), + updated_at timestamptz +); +create index idx_sessions_search on sessions using GIN(search); +create index idx_sessions on sessions using GIN(body jsonb_path_ops); +""" +Moebius.run_with_psql(schema_sql, db: "meebuss")