Class: PgSync::Init

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/pgsync/init.rb

Constant Summary

Constants included from Utils

Utils::COLOR_CODES

Instance Method Summary collapse

Methods included from Utils

#colorize, #confirm_tables_exist, #db_config_file, #deprecated, #escape, #first_schema, #friendly_name, #log, #output, #quote_ident, #quote_ident_full, #quote_string, #task_name, #warning

Constructor Details

#initialize(arguments, options) ⇒ Init

Returns a new instance of Init.



5
6
7
8
# File 'lib/pgsync/init.rb', line 5

def initialize(arguments, options)
  @arguments = arguments
  @options = options
end

Instance Method Details

#django?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/pgsync/init.rb', line 64

def django?
  file_exists?("manage.py", /django/i)
end

#file_exists?(path, contents = nil) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# File 'lib/pgsync/init.rb', line 80

def file_exists?(path, contents = nil)
  if contents
    File.read(path).match(contents)
  else
    File.exist?(path)
  end
rescue
  false
end

#heroku?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pgsync/init.rb', line 68

def heroku?
  `git remote -v 2>&1`.include?("git.heroku.com") rescue false
end

#laravel?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/pgsync/init.rb', line 72

def laravel?
  file_exists?("artisan")
end

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pgsync/init.rb', line 10

def perform
  if @arguments.size > 1
    raise Error, "Usage:\n    pgsync --init [db]"
  end

  file =
    if @options[:config]
      @options[:config]
    elsif @arguments.any?
      db_config_file(@arguments.first)
    elsif @options[:db]
      db_config_file(@options[:db])
    else
      ".pgsync.yml"
    end

  if File.exist?(file)
    raise Error, "#{file} exists."
  else
    exclude =
      if rails?
        <<~EOS
          exclude:
            - ar_internal_metadata
            - schema_migrations
        EOS
      elsif django?
        # TODO exclude other tables?
        <<~EOS
          exclude:
            - django_migrations
        EOS
      elsif laravel?
        <<~EOS
          exclude:
            - migrations
        EOS
      else
        <<~EOS
          # exclude:
          #   - table1
          #   - table2
        EOS
      end

    # create file
    contents = File.read(__dir__ + "/../../config.yml")
    contents.sub!("$(some_command)", "$(heroku config:get DATABASE_URL)") if heroku?
    File.write(file, contents % {exclude: exclude})

    log "#{file} created. Add your database credentials."
  end
end

#rails?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/pgsync/init.rb', line 76

def rails?
  file_exists?("bin/rails")
end