| 
									
										
										
										
											2016-01-10 11:29:30 +00:00
										 |  |  | defmodule Mix.Tasks.Isn.Gen.Migration do | 
					
						
							|  |  |  |   use Mix.Task | 
					
						
							|  |  |  |   import Mix.Ecto | 
					
						
							|  |  |  |   import Mix.Generator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @migration_name "CreateISNExtension" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @shortdoc "Generate an Ecto migration to add the `isn` extension" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @moduledoc """
 | 
					
						
							|  |  |  |   Generate an Ecto migration to add the `isn` extension to your database. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ## Example | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       mix isn.gen.migration | 
					
						
							|  |  |  |   """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @doc false | 
					
						
							|  |  |  |   def run(args) do | 
					
						
							|  |  |  |     Mix.Task.run "app.start", args | 
					
						
							|  |  |  |     [repo] = parse_repo(args) | 
					
						
							| 
									
										
										
										
											2017-01-22 21:20:57 +00:00
										 |  |  |     filename = "#{timestamp()}_create_isn_extension.exs" | 
					
						
							| 
									
										
										
										
											2016-01-10 11:29:30 +00:00
										 |  |  |     path = Path.relative_to(migrations_path(repo), Mix.Project.app_path) | 
					
						
							|  |  |  |     file = Path.join(path, filename) | 
					
						
							|  |  |  |     create_directory(path) | 
					
						
							|  |  |  |     mod = Module.concat([repo, Migrations, @migration_name]) | 
					
						
							|  |  |  |     create_file(file, migration_template(mod: mod)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   defp timestamp do | 
					
						
							|  |  |  |     {{y, m, d}, {hh, mm, ss}} = :calendar.universal_time() | 
					
						
							|  |  |  |     "#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 11:47:32 +00:00
										 |  |  |   defp pad(s), do: s |> to_string() |> String.rjust(2, ?0) | 
					
						
							| 
									
										
										
										
											2016-01-10 11:29:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   embed_template :migration, """
 | 
					
						
							|  |  |  |   defmodule <%= inspect @mod %> do | 
					
						
							|  |  |  |     use Ecto.Migration | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def up do | 
					
						
							|  |  |  |       execute "CREATE EXTENSION IF NOT EXISTS isn;" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def down do | 
					
						
							|  |  |  |       execute "DROP EXTENSION IF EXISTS isn;" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   """
 | 
					
						
							|  |  |  | end |