@@ -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
0 commit comments