<commit_before># PgParty

[![Gem Version](https://badge.fury.io/rb/pg_party.svg)][rubygems]
[![Build Status](https://circleci.com/gh/rkrage/pg_party.svg?&style=shield)][circle]
[![Code Climate](https://codeclimate.com/github/rkrage/pg_party/badges/gpa.svg)][climate]

[rubygems]: https://rubygems.org/gems/pg_party
[circle]:   https://circleci.com/gh/rkrage/pg_party
[climate]:  https://codeclimate.com/github/rkrage/pg_party

Active Record migrations and model helpers for creating and managing PostgreSQL 10 partitions!

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'pg_party'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install pg_party

## Usage

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rkrage/pg_party. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the PgParty project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rkrage/pg_party/blob/master/CODE_OF_CONDUCT.md).<commit_msg>usage examples / development instructions<commit_after># PgParty

[![Gem Version](https://badge.fury.io/rb/pg_party.svg)][rubygems]
[![Build Status](https://circleci.com/gh/rkrage/pg_party.svg?&style=shield)][circle]
[![Code Climate](https://codeclimate.com/github/rkrage/pg_party/badges/gpa.svg)][climate]

[rubygems]: https://rubygems.org/gems/pg_party
[circle]:   https://circleci.com/gh/rkrage/pg_party
[climate]:  https://codeclimate.com/github/rkrage/pg_party

[Active Record](http://guides.rubyonrails.org/active_record_basics.html) migrations and model helpers for creating and managing [PostgreSQL 10 partitions](https://www.postgresql.org/docs/10/static/ddl-partitioning.html)!

Features:
  - migration methods for partition specific database operations
  - model methods for querying partitioned data
  - model methods for creating adhoc partitions

Note: PostgreSQL 10 is currently in beta. This gem should not be used in production environments (yet).

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'pg_party'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install pg_party

## Usage

Full API documentation is in progress.

In the meantime, take a look at the [Combustion](https://github.com/pat/combustion) schema definition and integration specs:
  - https://github.com/rkrage/pg_party/blob/master/spec/internal/db/schema.rb
  - https://github.com/rkrage/pg_party/tree/master/spec/integration

### Migration Examples

Create range partition on `created_at::date` with two child partitions:

```ruby
class CreateSomeRangeRecord < ActiveRecord::Migration[5.1]
  def up
    current_date = Date.current

    create_range_partition :some_range_records, partition_key: "created_at::date" do |t|
      t.text :some_value
      t.timestamps
    end

    create_range_partition_of \
      :some_range_records,
      partition_key: "created_at::date",
      start_range: current_date,
      end_range: current_date + 1.day

     create_range_partition_of \
       :some_range_records,
       partition_key: "created_at::date",
       start_range: current_date + 1.day,
       end_range: current_date + 2.days
  end
end
```

Create list partition on `id` with two child partitions:

```ruby
class CreateSomeListRecord < ActiveRecord::Migration[5.1]
  def up
    create_list_partition :some_list_records, partition_key: :id do |t|
      t.text :some_value
      t.timestamps
    end

    create_list_partition_of \
      :some_list_records,
      partition_key: :id,
      values: (1..100).to_a

     create_list_partition_of \
       :some_list_records,
       partition_key: :id,
       values: (100..200).to_a
  end
end
```

Attach an existing table to a range partition:

```ruby
class AttachRangePartition < ActiveRecord::Migration[5.1]
  def up
    attach_range_partition("parent_table", "child_table")
  end
end
```

Attach an existing table to a list partition:

```ruby
class AttachListPartition < ActiveRecord::Migration[5.1]
  def up
    attach_list_partition("parent_table", "child_table")
  end
end
```

Detach a child table from any partition:

```ruby
class DetachPartition < ActiveRecord::Migration[5.1]
  def up
    detach_partition("parent_table", "child_table")
  end
end
```

### Model Examples

Define model that is backed by a range partition:

```ruby
class SomeRangeRecord < ApplicationRecord
  range_partition_by "created_at::date"
end
 ```

Define model that is backed by a list partition:

```ruby
class SomeListRecord < ApplicationRecord
  list_partition_by :id
end
```

Create child partition from range partition model:

```ruby
current_date = Date.current

SomeRangeRecord.create_partition(start_range: current_date + 1.day, end_range: current_date + 2.days)
```

Create child partition from list partition model:

```ruby
SomeListRecord.create_partition(values: (200..300).to_a)
```

Query for records within partition range:

```ruby
SomeRangeRecord.partition_key_in("2017-01-01".to_date, "2017-02-01".to_date)
```

Query for records in partition list:

```ruby
SomeListRecord.partition_key_in(1, 2, 3, 4)
```

Query for records matching partition key:

```ruby
SomeRangeRecord.partition_key_eq(Date.current)

SomeListRecord.partition_key_eq(100)
```

Query for records by partition name:

```ruby
# returns a collection of anonymous ActiveRecord::Base subclassed instances
SomeRangeRecord.in_partition("some_range_records_partition_name")

# returns a collection of anonymous ActiveRecord::Base subclassed instances
SomeListRecord.in_partition("some_list_records_partition_name")
```

## Development

The development / test environment relies heavily on [Docker](https://docs.docker.com).

Start the containers in the background:

    $ docker-compose up -d

Install dependencies:

    $ bin/de bundle
    $ bin/de appraisal

Run the tests:

    $ bin/de rake

Open a Pry console to play around with the sample Rails app:

    $ bin/de console

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rkrage/pg_party. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the PgParty project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rkrage/pg_party/blob/master/CODE_OF_CONDUCT.md).
{'fraction_non_alphanumeric': 0.10338983050847457, 'fraction_numerical': 0.008757062146892656, 'mean_word_length': 4.610935023771791, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 21, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1283171', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2146, 'n_words': 627}