table_saw 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 023e631a9a6fcad3f3ea21aada1e8cc8431414a0e3b94b4d550e74e2ece0bfcf
4
+ data.tar.gz: e24b92a73712e5e23293c3d1d7386469ffe63efbd80a6090a9c8ddd7df38ac8c
5
+ SHA512:
6
+ metadata.gz: 3c91ce9ff2760cb5594e301044a150168761255aac47e6790e01b91fdd8dd1d0797d1415e46391bb12645f9b3bce20bffb0b3c7684b67678cb59d9251a62f91e
7
+ data.tar.gz: 19d3d436ab6dce541123d7fabed4f8a9e05516e1277bf0859e7944e867e7e6120fdea91af34bb5815f8591d3017156d79a460db38f64de7061d9544e4151f34e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --order rand
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/EmptyMethod:
11
+ EnforcedStyle: expanded
12
+
13
+ Style/FormatStringToken:
14
+ EnforcedStyle: template
15
+
16
+ Style/PercentLiteralDelimiters:
17
+ PreferredDelimiters:
18
+ default: ()
19
+ '%w': '()'
20
+ '%W': '()'
21
+ '%i': '()'
22
+ '%I': '()'
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hasghari@g2crowd.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in table_saw.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,105 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ table_saw (0.1.0)
5
+ connection_pool
6
+ pg
7
+ thor
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (5.2.3)
13
+ actionview (= 5.2.3)
14
+ activesupport (= 5.2.3)
15
+ rack (~> 2.0)
16
+ rack-test (>= 0.6.3)
17
+ rails-dom-testing (~> 2.0)
18
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
19
+ actionview (5.2.3)
20
+ activesupport (= 5.2.3)
21
+ builder (~> 3.1)
22
+ erubi (~> 1.4)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
25
+ activemodel (5.2.3)
26
+ activesupport (= 5.2.3)
27
+ activerecord (5.2.3)
28
+ activemodel (= 5.2.3)
29
+ activesupport (= 5.2.3)
30
+ arel (>= 9.0)
31
+ activesupport (5.2.3)
32
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
+ i18n (>= 0.7, < 2)
34
+ minitest (~> 5.1)
35
+ tzinfo (~> 1.1)
36
+ arel (9.0.0)
37
+ builder (3.2.3)
38
+ combustion (1.1.0)
39
+ activesupport (>= 3.0.0)
40
+ railties (>= 3.0.0)
41
+ thor (>= 0.14.6)
42
+ concurrent-ruby (1.1.5)
43
+ connection_pool (2.2.1)
44
+ crass (1.0.4)
45
+ database_cleaner (1.7.0)
46
+ diff-lcs (1.3)
47
+ erubi (1.8.0)
48
+ i18n (1.6.0)
49
+ concurrent-ruby (~> 1.0)
50
+ loofah (2.2.3)
51
+ crass (~> 1.0.2)
52
+ nokogiri (>= 1.5.9)
53
+ method_source (0.9.2)
54
+ mini_portile2 (2.4.0)
55
+ minitest (5.11.3)
56
+ nokogiri (1.10.3)
57
+ mini_portile2 (~> 2.4.0)
58
+ pg (1.1.4)
59
+ rack (2.0.7)
60
+ rack-test (1.1.0)
61
+ rack (>= 1.0, < 3)
62
+ rails-dom-testing (2.0.3)
63
+ activesupport (>= 4.2.0)
64
+ nokogiri (>= 1.6)
65
+ rails-html-sanitizer (1.0.4)
66
+ loofah (~> 2.2, >= 2.2.2)
67
+ railties (5.2.3)
68
+ actionpack (= 5.2.3)
69
+ activesupport (= 5.2.3)
70
+ method_source
71
+ rake (>= 0.8.7)
72
+ thor (>= 0.19.0, < 2.0)
73
+ rake (10.5.0)
74
+ rspec (3.8.0)
75
+ rspec-core (~> 3.8.0)
76
+ rspec-expectations (~> 3.8.0)
77
+ rspec-mocks (~> 3.8.0)
78
+ rspec-core (3.8.0)
79
+ rspec-support (~> 3.8.0)
80
+ rspec-expectations (3.8.3)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.8.0)
83
+ rspec-mocks (3.8.0)
84
+ diff-lcs (>= 1.2.0, < 2.0)
85
+ rspec-support (~> 3.8.0)
86
+ rspec-support (3.8.0)
87
+ thor (0.19.4)
88
+ thread_safe (0.3.6)
89
+ tzinfo (1.2.5)
90
+ thread_safe (~> 0.1)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ activerecord (~> 5.2)
97
+ bundler (~> 2.0)
98
+ combustion (~> 1.1)
99
+ database_cleaner (~> 1.7)
100
+ rake (~> 10.0)
101
+ rspec (~> 3.0)
102
+ table_saw!
103
+
104
+ BUNDLED WITH
105
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Hamed Asghari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # TableSaw
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/table_saw`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'table_saw'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install table_saw
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/table_saw. 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.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the TableSaw project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/table_saw/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'table_saw'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/table-saw ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require 'table_saw'
8
+ require 'table_saw/build_dependency_graph'
9
+ require 'table_saw/create_dump_file'
10
+ require 'table_saw/manifest'
11
+ require 'thor'
12
+
13
+ class CLI < Thor
14
+ def self.exit_on_failure?
15
+ true
16
+ end
17
+
18
+ desc 'dump', 'Create a postgres dump file'
19
+ method_option :url, aliases: '-u', default: ENV['DATABASE_URL']
20
+ method_option :dbname, aliases: '-d', default: ENV['PGDATABASE']
21
+ method_option :host, aliases: '-h', default: ENV['PGHOST']
22
+ method_option :port, aliases: '-p', default: ENV['PGPORT']
23
+ method_option :user, aliases: '-U', default: ENV['PGUSER']
24
+ method_option :password, default: ENV['PGPASSWORD']
25
+ method_option :manifest, aliases: '-m', required: true
26
+ method_option :output, aliases: '-o', default: 'psql.dump'
27
+ def dump
28
+ TableSaw.configure(options.to_hash)
29
+ records = TableSaw::BuildDependencyGraph.new(TableSaw::Manifest.instance).call
30
+ TableSaw::CreateDumpFile.new(records, options[:output]).call
31
+ end
32
+ end
33
+
34
+ CLI.start
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_saw/connection'
4
+ require 'table_saw/queries/foreign_key_relationships'
5
+ require 'table_saw/queries/no_id_tables'
6
+
7
+ module TableSaw
8
+ class BuildDependencyGraph
9
+ attr_reader :manifest, :records
10
+
11
+ def initialize(manifest)
12
+ @manifest = manifest
13
+ @records = Hash.new { |h, k| h[k] = Set.new }
14
+ end
15
+
16
+ def call
17
+ manifest.tables.each_value do |table|
18
+ perform_query(table.query).each do |row|
19
+ add(table.name, row['id'])
20
+ end
21
+ end
22
+
23
+ records
24
+ end
25
+
26
+ def add(table_name, id)
27
+ return if id.nil? || records[table_name].include?(id)
28
+
29
+ records[table_name].add id
30
+ fetch_belongs_to_associations(table_name, id)
31
+ fetch_has_many_associations(table_name, id)
32
+ end
33
+
34
+ def fetch_belongs_to_associations(table_name, id)
35
+ associations = belongs_to[table_name]
36
+ return if associations.nil? || associations.empty?
37
+
38
+ perform_query(
39
+ format('select %{columns} from %{table_name} where id = %{id}',
40
+ columns: associations.keys.join(','), table_name: table_name, id: id)
41
+ ).each do |row|
42
+ associations.each do |from_column, to_table|
43
+ add(to_table, row[from_column])
44
+ end
45
+ end
46
+ end
47
+
48
+ def fetch_has_many_associations(table_name, id)
49
+ has_many.fetch(table_name, []).each do |table, column|
50
+ next if tables_with_no_ids.include?(table)
51
+ next unless has_many_mapping.fetch(table_name, []).include?(table)
52
+
53
+ perform_query(
54
+ format('select id from %{table} where %{column} = %{id}', table: table, column: column, id: id)
55
+ ).each do |row|
56
+ add(table, row['id'])
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def perform_query(sql)
64
+ TableSaw::Connection.with do |conn|
65
+ conn.exec(sql)
66
+ end
67
+ end
68
+
69
+ def belongs_to
70
+ foreign_key_relationships.belongs_to
71
+ end
72
+
73
+ # rubocop:disable Naming/PredicateName
74
+ def has_many
75
+ foreign_key_relationships.has_many
76
+ end
77
+
78
+ def has_many_mapping
79
+ @has_many_mapping ||= manifest.tables.transform_values(&:has_many)
80
+ end
81
+ # rubocop:enable Naming/PredicateName
82
+
83
+ def foreign_key_relationships
84
+ @foreign_key_relationships ||= TableSaw::Queries::ForeignKeyRelationships.new
85
+ end
86
+
87
+ def tables_with_no_ids
88
+ @tables_with_no_ids ||= TableSaw::Queries::NoIdTables.new.call
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableSaw
4
+ class Configuration
5
+ attr_accessor :dbname, :host, :port, :user, :password, :manifest, :output
6
+
7
+ def connection
8
+ { dbname: dbname, host: host, port: port, user: user, password: password }
9
+ end
10
+
11
+ def url=(value)
12
+ URI.parse(value).tap do |uri|
13
+ self.dbname = uri.path[1..-1]
14
+ self.host = uri.host
15
+ self.port = uri.port
16
+ self.user = uri.user
17
+ self.password = uri.password
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require 'connection_pool'
5
+
6
+ module TableSaw
7
+ module Connection
8
+ def self.with
9
+ raise ArgumentError, 'requires a block' unless block_given?
10
+
11
+ pool.with { |conn| yield conn }
12
+ end
13
+
14
+ def self.pool
15
+ @pool ||= ConnectionPool.new(size: 2) { PG::Connection.new(TableSaw.configuration.connection) }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_saw/queries/table_columns'
4
+
5
+ module TableSaw
6
+ class CreateDumpFile
7
+ attr_reader :records, :file
8
+
9
+ def initialize(records, file = 'psql.dump')
10
+ @records = records
11
+ @file = file
12
+ end
13
+
14
+ # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
15
+ def call
16
+ write_to_file <<~SQL
17
+ BEGIN;
18
+
19
+ SET session_replication_role = replica;
20
+ SET statement_timeout = 0;
21
+ SET lock_timeout = 0;
22
+ SET client_encoding = 'UTF8';
23
+ SET standard_conforming_strings = on;
24
+ SET check_function_bodies = false;
25
+ SET client_min_messages = warning;
26
+
27
+ SET search_path = public, pg_catalog;
28
+ SQL
29
+
30
+ records.each do |name, ids|
31
+ write_to_file <<~COMMENT
32
+ --
33
+ -- Data for Name: #{name}; Type: TABLE DATA
34
+ --
35
+
36
+ COMMENT
37
+
38
+ write_to_file <<~SQL
39
+ COPY #{name} (#{quoted_columns(name)}) FROM STDIN;
40
+ SQL
41
+
42
+ TableSaw::Connection.with do |conn|
43
+ conn.copy_data "COPY (select * from #{name} where id in (#{ids.to_a.join(',')})) TO STDOUT" do
44
+ while (row = conn.get_copy_data)
45
+ write_to_file(row)
46
+ end
47
+ end
48
+ end
49
+
50
+ write_to_file('\.')
51
+ write_to_file("\n")
52
+ end
53
+
54
+ write_to_file 'COMMIT;'
55
+ end
56
+ # rubocop:enable Metrics/MethodLength,Metrics/AbcSize
57
+
58
+ private
59
+
60
+ def write_to_file(data)
61
+ File.open(file, 'ab') { |f| f.puts(data) }
62
+ end
63
+
64
+ def quoted_columns(table)
65
+ TableSaw::Queries::TableColumns.new(table).call.map { |c| "\"#{c}\"" }.join(', ')
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'table_saw/connection'
5
+
6
+ module TableSaw
7
+ class Manifest
8
+ class Table
9
+ attr_reader :variables, :config
10
+
11
+ def initialize(variables, config)
12
+ @variables = variables
13
+ @config = config
14
+ end
15
+
16
+ def table
17
+ config['table']
18
+ end
19
+
20
+ alias name table
21
+
22
+ def query
23
+ if config['query']
24
+ format(config['query'], variables.transform_keys(&:to_sym))
25
+ else
26
+ "select id from #{table}"
27
+ end
28
+ end
29
+
30
+ # rubocop:disable Naming/PredicateName
31
+ def has_many
32
+ config.fetch('has_many', [])
33
+ end
34
+ # rubocop:enable Naming/PredicateName
35
+ end
36
+
37
+ def self.instance
38
+ raise ArgumentError, 'Could not find manifest file' unless File.exist?(TableSaw.configuration.manifest)
39
+
40
+ new(YAML.safe_load(File.read(TableSaw.configuration.manifest)))
41
+ end
42
+
43
+ attr_reader :config
44
+
45
+ def initialize(config)
46
+ @config = config
47
+ end
48
+
49
+ def variables
50
+ config['variables']
51
+ end
52
+
53
+ def tables
54
+ @tables ||= config['tables'].map { |entry| Table.new(variables, entry) }.each_with_object({}) do |t, memo|
55
+ memo[t.name] = t
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_saw/connection'
4
+
5
+ module TableSaw
6
+ module Queries
7
+ class ForeignKeyRelationships
8
+ QUERY = <<~SQL
9
+ select
10
+ tc.constraint_name,
11
+ tc.table_name as from_table,
12
+ tc.constraint_type,
13
+ kcu.column_name as from_column,
14
+ ccu.table_name as to_table,
15
+ ccu.column_name as to_column
16
+ from information_schema.table_constraints tc
17
+ join information_schema.key_column_usage kcu on tc.constraint_name = kcu.constraint_name
18
+ join information_schema.constraint_column_usage ccu on tc.constraint_name = ccu.constraint_name
19
+ where tc.constraint_type = 'FOREIGN KEY'
20
+ SQL
21
+
22
+ def belongs_to
23
+ result.each_with_object(Hash.new { |h, k| h[k] = {} }) do |row, memo|
24
+ memo[row['from_table']][row['from_column']] = row['to_table']
25
+ end
26
+ end
27
+
28
+ # rubocop:disable Naming/PredicateName
29
+ def has_many
30
+ @has_many ||= result.each_with_object(Hash.new { |h, k| h[k] = [] }) do |row, memo|
31
+ memo[row['to_table']].push([row['from_table'], row['from_column']])
32
+ end
33
+ end
34
+ # rubocop:enable Naming/PredicateName
35
+
36
+ private
37
+
38
+ def result
39
+ @result ||= TableSaw::Connection.with do |conn|
40
+ conn.exec(QUERY)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableSaw
4
+ module Queries
5
+ class NoIdTables
6
+ QUERY = <<~SQL
7
+ select t.table_name
8
+ from information_schema.tables t left outer join information_schema.table_constraints tc
9
+ on t.table_name = tc.table_name and tc.constraint_type = 'PRIMARY KEY'
10
+ where t.table_schema = 'public' and table_type = 'BASE TABLE' and tc.table_name IS NULL
11
+ SQL
12
+
13
+ def call
14
+ TableSaw::Connection.with do |conn|
15
+ conn.exec(QUERY)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_saw/connection'
4
+
5
+ module TableSaw
6
+ module Queries
7
+ class TableColumns
8
+ QUERY = <<~SQL
9
+ select attname as colname
10
+ from pg_catalog.pg_attribute
11
+ where
12
+ attrelid = $1::regclass
13
+ and attnum > 0
14
+ and attisdropped = false
15
+ order by attnum
16
+ SQL
17
+
18
+ attr_reader :table
19
+
20
+ def initialize(table)
21
+ @table = table
22
+ end
23
+
24
+ def call
25
+ TableSaw::Connection.with { |conn| conn.exec_params(QUERY, [table]) }.map { |r| r['colname'] }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableSaw
4
+ VERSION = '0.1.0'
5
+ end
data/lib/table_saw.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'table_saw/version'
4
+ require 'table_saw/configuration'
5
+
6
+ module TableSaw
7
+ def self.configuration
8
+ @configuration ||= TableSaw::Configuration.new
9
+ end
10
+
11
+ def self.configure(args = {})
12
+ if block_given?
13
+ yield configuration
14
+ else
15
+ args.each do |key, value|
16
+ configuration.public_send("#{key}=", value)
17
+ end
18
+ end
19
+ end
20
+ end
data/table_saw.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'table_saw/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'table_saw'
9
+ spec.version = TableSaw::VERSION
10
+ spec.authors = ['Hamed Asghari']
11
+ spec.email = ['hasghari@gmail.com']
12
+
13
+ spec.summary = 'Create a postgres dump file from a subset of tables'
14
+ spec.homepage = 'https://github.com/hasghari/table_saw'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = ['table-saw']
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'connection_pool'
27
+ spec.add_dependency 'pg'
28
+ spec.add_dependency 'thor'
29
+
30
+ spec.add_development_dependency 'activerecord', '~> 5.2'
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
+ spec.add_development_dependency 'combustion', '~> 1.1'
33
+ spec.add_development_dependency 'database_cleaner', '~> 1.7'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_saw
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hamed Asghari
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: combustion
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ description:
140
+ email:
141
+ - hasghari@gmail.com
142
+ executables:
143
+ - table-saw
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".travis.yml"
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - exe/table-saw
160
+ - lib/table_saw.rb
161
+ - lib/table_saw/build_dependency_graph.rb
162
+ - lib/table_saw/configuration.rb
163
+ - lib/table_saw/connection.rb
164
+ - lib/table_saw/create_dump_file.rb
165
+ - lib/table_saw/manifest.rb
166
+ - lib/table_saw/queries/foreign_key_relationships.rb
167
+ - lib/table_saw/queries/no_id_tables.rb
168
+ - lib/table_saw/queries/table_columns.rb
169
+ - lib/table_saw/version.rb
170
+ - table_saw.gemspec
171
+ homepage: https://github.com/hasghari/table_saw
172
+ licenses:
173
+ - MIT
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubyforge_project:
191
+ rubygems_version: 2.7.7
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: Create a postgres dump file from a subset of tables
195
+ test_files: []