Sha256: a166505e905a9a84b7ea115df6adf5195af0bc70c29c228fcea582bb8e00f937

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 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.read("manage.py") =~ /django/i) rescue false
    end

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

    def laravel?
      File.exist?("artisan")
    end

    def rails?
      File.exist?("bin/rails")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pgsync-0.6.1 lib/pgsync/init.rb