Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ankane/pgsync
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: CaperAi/pgsync
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Aug 1, 2018

  1. tenant support

    jungsoo woo authored and jungsoo woo committed Aug 1, 2018
    Copy the full SHA
    30e1aaa View commit details
  2. Merge pull request #1 from QueueHopTech/tenant_support

    tenant support
    wjs9715 authored Aug 1, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    2ead09a View commit details
Showing with 37 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +19 −1 lib/pgsync/client.rb
  3. +6 −0 lib/pgsync/data_source.rb
  4. +11 −0 lib/pgsync/table_list.rb
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -8,3 +8,4 @@
/spec/reports/
/tmp/
/.pgsync.yml
.idea
20 changes: 19 additions & 1 deletion lib/pgsync/client.rb
Original file line number Diff line number Diff line change
@@ -79,6 +79,15 @@ def sync(args, opts)

pretty_list list_items
else
if opts[:tenant_schema_first] || opts[:tenant_schema_only]
if opts[:preserve]
raise PgSync::Error, "Cannot use --preserve with --schema-first or --schema-only"
end

log "* Dumping Tenant schema schema"
sync_tenant_schema(source, destination, ['public', 'foodcellar_47th_rd'])
end

if opts[:schema_first] || opts[:schema_only]
if opts[:preserve]
raise PgSync::Error, "Cannot use --preserve with --schema-first or --schema-only"
@@ -88,7 +97,7 @@ def sync(args, opts)
sync_schema(source, destination, tables)
end

unless opts[:schema_only]
unless opts[:schema_only] || opts[:tenant_schema_only]
confirm_tables_exist(destination, tables, "destination")

in_parallel(tables) do |table, table_opts|
@@ -151,6 +160,12 @@ def sync_schema(source, destination, tables)
system("#{dump_command} | #{restore_command}")
end

def sync_tenant_schema(source, destination, tenants)
dump_command = source.dump_tenant_command(tenants)
restore_command = destination.restore_command
system("#{dump_command} | #{restore_command}")
end

def parse_args(args)
opts = Slop.parse(args) do |o|
o.banner = %{Usage:
@@ -159,6 +174,7 @@ def parse_args(args)
Options:}
o.string "-d", "--db", "database"
o.string "-t", "--tables", "tables to sync"
o.string "-T", "--tenants", "tenants to sync"
o.string "-g", "--groups", "groups to sync"
o.string "--schemas", "schemas to sync"
o.string "--from", "source"
@@ -176,6 +192,8 @@ def parse_args(args)
o.boolean "--truncate", "truncate existing rows", default: false
o.boolean "--schema-first", "schema first", default: false
o.boolean "--schema-only", "schema only", default: false
o.boolean "--tenant-schema-only", "tenant schema only", default: false
o.boolean "--tenant-schema-first", "tenant schema first", default: false
o.boolean "--all-schemas", "all schemas", default: false
o.boolean "--no-rules", "do not apply data rules", default: false
o.boolean "--setup", "setup", default: false
6 changes: 6 additions & 0 deletions lib/pgsync/data_source.rb
Original file line number Diff line number Diff line change
@@ -112,9 +112,15 @@ def close

def dump_command(tables)
tables = tables.keys.map { |t| "-t #{Shellwords.escape(quote_ident_full(t))}" }.join(" ")
# tables = ""
"pg_dump -Fc --verbose --schema-only --no-owner --no-acl #{tables} -d #{@url}"
end

def dump_tenant_command(tenants)
tenants = tenants.map { |t| "-n #{Shellwords.escape(quote_ident_full(t))}" }.join(" ")
"pg_dump -Fc --verbose --schema-only --no-owner --no-acl #{tenants} -d #{@url}"
end

def restore_command
psql_version = `psql --version`.lines[0].chomp.split(" ")[-1].split(/[^\d.]/)[0]
if_exists = Gem::Version.new(psql_version) >= Gem::Version.new("9.4.0")
11 changes: 11 additions & 0 deletions lib/pgsync/table_list.rb
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ def initialize(args, options, source, config)

def tables
tables = nil
if opts[:tenants]
tables ||= Hash.new { |hash, key| hash[key] = {} }
specified_groups = to_arr(opts[:tenants])
specified_groups.map do |tag|
if (t = (config["tenants"] || {})[tag])
t.each { |tables_to_sync| tables["#{tag}.#{tables_to_sync}"] = {} }
else
raise PgSync::Error, "Tenant not found: #{tag}"
end
end
end

if opts[:groups]
tables ||= Hash.new { |hash, key| hash[key] = {} }