Sha256: 4001f1ae0c12fd82f752d7a92d50e6b041ed9b2e51b215c46853128f093dd16e

Contents?: true

Size: 1.91 KB

Versions: 13

Compression:

Stored size: 1.91 KB

Contents

module PgSync
  class Init
    include Utils

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

    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

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

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

    def laravel?
      file_exists?("artisan")
    end

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
pgsync-0.8.0 lib/pgsync/init.rb
pgsync-0.7.4 lib/pgsync/init.rb
pgsync-0.7.3 lib/pgsync/init.rb
pgsync-0.7.2 lib/pgsync/init.rb
pgsync-0.7.1 lib/pgsync/init.rb
pgsync-0.7.0 lib/pgsync/init.rb
pgsync-0.6.8 lib/pgsync/init.rb
pgsync-0.6.7 lib/pgsync/init.rb
pgsync-0.6.6 lib/pgsync/init.rb
pgsync-0.6.5 lib/pgsync/init.rb
pgsync-0.6.4 lib/pgsync/init.rb
pgsync-0.6.3 lib/pgsync/init.rb
pgsync-0.6.2 lib/pgsync/init.rb