Skip to content

Commit a2efef5

Browse files
committed
release 1.1.4
1 parent 61914b4 commit a2efef5

14 files changed

Lines changed: 78 additions & 105 deletions

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ def deps do
3030
end
3131
```
3232

33+
## Configuration
34+
```elixir
35+
config :ex_dbmigrate, ExDbmigrate.Repo,
36+
adapter: Ecto.Adapters.Postgres,
37+
username: "postgres",
38+
password: "postgres",
39+
database: "postgres",
40+
hostname: "localhost",
41+
pool_size: 10,
42+
primary_key_type: :uuid
43+
# port: 5543
44+
```
3345
## Usage:
3446
```bash
3547
mix ExDbmigrate.Gen.Migration

config/config.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@ import Config
2727
#
2828
# import_config "#{Mix.env()}.exs"
2929

30-
config :ex_dbmigrate, :ecto_repos, [ExDbmigrate.Repo]
31-
3230
import_config "#{Mix.env()}.exs"

config/dev.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import Config
22
# Do not include metadata nor timestamps in development logs
33
config :logger, :console, format: "[$level] $message\n"
44

5-
config :ex_dbmigrate, :ecto_repos, [ExDbmigrate.Repo]
6-
75
config :ex_dbmigrate, ExDbmigrate.Repo,
86
adapter: Ecto.Adapters.Postgres,
97
username: "postgres",

ex_dbmigrate.iml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,10 @@
126126
<orderEntry type="library" name="exflect" level="project" />
127127
<orderEntry type="library" name="inflex" level="project" />
128128
<orderEntry type="library" name="httpoison" level="project" />
129+
<orderEntry type="library" name="bypass" level="project" />
130+
<orderEntry type="library" name="meck" level="project" />
131+
<orderEntry type="library" name="mock" level="project" />
132+
<orderEntry type="library" name="sax_map" level="project" />
133+
<orderEntry type="library" name="saxy" level="project" />
129134
</component>
130135
</module>

lib/application.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ defmodule ExDbmigrate.Application do
55

66
use Application
77

8-
alias ExDbmigrate.Config
9-
108
@impl true
119
def start(_type, args) do
12-
repo = Config.repo()
13-
1410
children = [
15-
{repo, args},
11+
{ExDbmigrate.Repo, args},
1612
# Starts a worker by calling: ExDbmigrate.Worker.start_link(arg)
1713
{Registry, keys: :unique, name: :tables},
1814
{ExDbmigrate.Table.Supervisor, args}

lib/config.ex

Lines changed: 0 additions & 44 deletions
This file was deleted.

lib/ex_dbmigrate.ex

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ defmodule ExDbmigrate do
22
@moduledoc """
33
Documentation for `ExDbmigrate`.
44
"""
5-
6-
@repo ExDbmigrate.Config.repo()
5+
@repo ExDbmigrate.Repo
6+
@ignore ["information_schema", "pg_catalog"]
77

88
import Exflect
99

@@ -57,15 +57,15 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
5757
5858
## Examples
5959
60-
iex> ExDbmigrate.migration()
60+
iex> ExDbmigrate.migration("public")
6161
["mix phx.gen.migration CatalogMetas CatalogMeta catalog_metas key:string data:string product_id:uuid",
6262
"mix phx.gen.migration CatalogVideosToProducts CatalogVideosToProduct catalog_videos_to_product product_id:uuid video_id:uuid",
6363
"mix phx.gen.migration CatalogProducts CatalogProduct catalog_products name:string",
6464
"mix phx.gen.migration CatalogVideos CatalogVideo catalog_videos path:string"]
6565
6666
"""
67-
def migration() do
68-
results = fetch_results()
67+
def migration(schema \\ "public") do
68+
results = fetch_results(schema)
6969

7070
Enum.map(results.rows, fn r ->
7171
fetch_table_data(r)
@@ -79,14 +79,14 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
7979
## Examples
8080
8181
iex> ExDbmigrate.migration_relations()
82-
["mix phx.gen.schema CatalogMetas CatalogMeta catalog_metas product_id:references:catalog_products",
83-
"mix phx.gen.schema CatalogVideosToProducts CatalogVideosToProduct catalog_videos_to_product product_id:references:catalog_products video_id:references:catalog_videos",
84-
"mix phx.gen.schema CatalogProducts CatalogProduct catalog_products ",
85-
"mix phx.gen.schema CatalogVideos CatalogVideo catalog_videos "]
82+
["mix phx.gen.schema CatalogMetas.CatalogMeta catalog_metas product_id:references:catalog_products",
83+
"mix phx.gen.schema CatalogVideosToProducts.CatalogVideosToProduct catalog_videos_to_product product_id:references:catalog_products video_id:references:catalog_videos",
84+
"mix phx.gen.schema CatalogProducts.CatalogProduct catalog_products ",
85+
"mix phx.gen.schema CatalogVideos.CatalogVideo catalog_videos "]
8686
8787
"""
88-
def migration_relations() do
89-
results = fetch_results()
88+
def migration_relations(schema \\ "public") do
89+
results = fetch_results(schema)
9090

9191
Enum.map(results.rows, fn r ->
9292
list_foreign_keys(r)
@@ -100,14 +100,14 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
100100
## Examples
101101
102102
iex> ExDbmigrate.schema()
103-
["mix phx.gen.schema CatalogMetas CatalogMeta catalog_metas key:string data:string product_id:integer --no-migration",
104-
"mix phx.gen.schema CatalogVideosToProducts CatalogVideosToProduct catalog_videos_to_product product_id:integer video_id:integer --no-migration",
105-
"mix phx.gen.schema CatalogProducts CatalogProduct catalog_products name:string --no-migration",
106-
"mix phx.gen.schema CatalogVideos CatalogVideo catalog_videos path:string --no-migration"]
103+
["mix phx.gen.schema CatalogMetas.CatalogMeta catalog_metas key:string data:string product_id:integer --no-migration",
104+
"mix phx.gen.schema CatalogVideosToProducts.CatalogVideosToProduct catalog_videos_to_product product_id:integer video_id:integer --no-migration",
105+
"mix phx.gen.schema CatalogProducts.CatalogProduct catalog_products name:string --no-migration",
106+
"mix phx.gen.schema CatalogVideos.CatalogVideo catalog_videos path:string --no-migration"]
107107
108108
"""
109-
def schema() do
110-
results = fetch_results()
109+
def schema(schema \\ "public") do
110+
results = fetch_results(schema)
111111

112112
Enum.map(results.rows, fn r ->
113113
fetch_table_data(r)
@@ -128,8 +128,8 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
128128
]
129129
130130
"""
131-
def html() do
132-
results = fetch_results()
131+
def html(schema \\ "public") do
132+
results = fetch_results(schema)
133133

134134
Enum.map(results.rows, fn r ->
135135
fetch_table_data(r)
@@ -149,8 +149,8 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
149149
"mix phx.gen.json CatalogVideos CatalogVideo catalog_videos path:string"]
150150
151151
"""
152-
def json() do
153-
results = fetch_results()
152+
def json(schema \\ "public") do
153+
results = fetch_results(schema)
154154

155155
Enum.map(results.rows, fn r ->
156156
fetch_table_data(r)
@@ -170,26 +170,33 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='#{table}';
170170
"mix phx.gen.live CatalogVideos CatalogVideo catalog_videos path:string"]
171171
172172
"""
173-
def live() do
174-
results = fetch_results()
173+
def live(schema \\ "public") do
174+
results = fetch_results(schema)
175175

176176
Enum.map(results.rows, fn r ->
177177
fetch_table_data(r)
178178
|> generate_lives_command(r)
179179
end)
180180
end
181181

182-
def fetch_results() do
182+
def fetch_table_schemas(ignore \\ @ignore) do
183+
query = "SELECT schema_name
184+
FROM information_schema.schemata;"
185+
186+
Ecto.Adapters.SQL.query!(@repo, query, []) |> Enum.reject(fn x -> Enum.member?(ignore, x) end)
187+
end
188+
189+
def fetch_results(schema \\ "public") do
183190
query =
184-
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_name != 'schema_migrations';"
191+
"SELECT table_name FROM information_schema.tables WHERE table_schema = '#{schema}' and table_name != 'schema_migrations';"
185192

186193
Ecto.Adapters.SQL.query!(@repo, query, [])
187194
end
188195

189-
def fetch_table_data(r) do
196+
def fetch_table_data(r, schema \\ "public") do
190197
query =
191198
"SELECT column_name, is_nullable, data_type, ordinal_position, character_maximum_length FROM information_schema.columns
192-
WHERE table_schema = 'public'
199+
WHERE table_schema = '#{schema}'
193200
AND table_name = '#{r}'"
194201

195202
Ecto.Adapters.SQL.query!(@repo, query, [])
@@ -300,7 +307,7 @@ WHERE table_schema = 'public'
300307

301308
migration_name = String.downcase(migration_name <> "relations")
302309

303-
"mix phx.gen.schema #{module} #{module_name} #{table} #{migration_string}"
310+
"mix phx.gen.schema #{module}.#{module_name} #{table} #{migration_string}"
304311
end
305312

306313
def generate_schema_relations_command(fk_data, [migration_name]) do
@@ -324,7 +331,7 @@ WHERE table_schema = 'public'
324331

325332
module_name = String.downcase(module_name <> "relations")
326333

327-
"mix phx.gen.schema #{module} #{module_name} #{table} #{migration_string}"
334+
"mix phx.gen.schema #{module}.#{module_name} #{table} #{migration_string}"
328335
end
329336

330337
def generate_schemas_command(data, [migration_name]) do
@@ -338,7 +345,7 @@ WHERE table_schema = 'public'
338345

339346
module = migration_module |> pluralize()
340347

341-
"mix phx.gen.schema #{module} #{module_name} #{table} #{migration_string} --no-migration"
348+
"mix phx.gen.schema #{module}.#{module_name} #{table} #{migration_string} --no-migration"
342349
end
343350

344351
def type_select(t) do

lib/repo.ex

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ defmodule ExDbmigrate.Repo do
33
otp_app: :ex_dbmigrate,
44
adapter: Ecto.Adapters.Postgres
55

6-
@doc """
7-
Dynamically loads the repository url from the
8-
DATABASE_URL environment variable.
9-
"""
10-
def init(arg, nil) do
11-
init(arg, [])
12-
end
13-
14-
def init(_, opts) do
15-
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
16-
end
17-
186
@doc """
197
Empty the Database Table
208
"""

lib/table.ex

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule ExDbmigrate.Table.Server do
4343
def init(init_arg) do
4444
data = ExDbmigrate.list_foreign_keys(init_arg)
4545

46-
ref_type = ExDbmigrate.Config.key_type()
46+
ref_type = key_type()
4747

4848
timestamp_fields =
4949
Application.get_env(:ex_dbmigrate, :timestamp_fields, [:updated_at, :inserted_at])
@@ -145,7 +145,7 @@ defmodule ExDbmigrate.Table.Server do
145145
_from,
146146
state
147147
) do
148-
ref_type = ExDbmigrate.Config.key_type()
148+
ref_type = key_type()
149149

150150
assoc_type =
151151
case Enum.count(state.links) do
@@ -166,7 +166,7 @@ defmodule ExDbmigrate.Table.Server do
166166
) do
167167
link_data =
168168
Enum.map(data, fn x ->
169-
ref_type = ExDbmigrate.Config.key_type()
169+
ref_type = key_type()
170170

171171
%{
172172
column_name: x.column_name,
@@ -243,4 +243,18 @@ defmodule ExDbmigrate.Table.Server do
243243
def handle_info(_msg, state) do
244244
{:noreply, state}
245245
end
246+
247+
def key_type() do
248+
case Application.get_env(:ex_dbmigrate, ExDbmigrate.Repo)[:primary_key_type] do
249+
nil -> :integer
250+
_ -> :binary_id
251+
end
252+
end
253+
254+
def key_type(:migration) do
255+
case Application.get_env(:ex_dbmigrate, repo())[:primary_key_type] do
256+
nil -> :integer
257+
_ -> :uuid
258+
end
259+
end
246260
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule ExDbmigrate.MixProject do
22
use Mix.Project
3-
@version "1.1.3"
3+
@version "1.1.4"
44
@source_url "https://github.com/mithereal/ExDbmigrate"
55

66
def project do

0 commit comments

Comments
 (0)