pgsync 0.6.2 → 0.6.3
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.
Potentially problematic release.
This version of pgsync might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +7 -7
- data/lib/pgsync/client.rb +2 -1
- data/lib/pgsync/table_sync.rb +31 -2
- data/lib/pgsync/task.rb +1 -1
- data/lib/pgsync/version.rb +1 -1
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0d3ff88829338544bfe3434a16a00640e6496b7b89b1ef5dd43e66f844ea51ce
         | 
| 4 | 
            +
              data.tar.gz: d5457e8c2596fdda651c9739712fe0934af14028295ea9cd6addfc70438ced16
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6f9549d0d85cb502b5b88a3178fe617bf94f032d72787ab6dcf8f6097dc256ca2e969e2c4a434ebf33e4de17a4f11c590c57ef8cae420b20fe4f39bfed795baa
         | 
| 7 | 
            +
              data.tar.gz: b10ac04af7dc26c3067c2ff73f163c69d73dfb5c1f559585adcf774312ac78ac07dcd790e1706cc571a90f0e4573060cedf01fc6ebd8b1732fbaa16b486213dd
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -198,20 +198,20 @@ Rules starting with `unique_` require the table to have a single column primary | |
| 198 198 |  | 
| 199 199 | 
             
            Foreign keys can make it difficult to sync data. Three options are:
         | 
| 200 200 |  | 
| 201 | 
            -
            1.  | 
| 202 | 
            -
            2.  | 
| 203 | 
            -
            3. Disable foreign key triggers, which can silently break referential integrity
         | 
| 201 | 
            +
            1. Defer constraints (recommended)
         | 
| 202 | 
            +
            2. Manually specify the order of tables
         | 
| 203 | 
            +
            3. Disable foreign key triggers, which can silently break referential integrity (not recommended)
         | 
| 204 204 |  | 
| 205 | 
            -
             | 
| 205 | 
            +
            To defer constraints, use:
         | 
| 206 206 |  | 
| 207 207 | 
             
            ```sh
         | 
| 208 | 
            -
            pgsync  | 
| 208 | 
            +
            pgsync --defer-constraints-v2
         | 
| 209 209 | 
             
            ```
         | 
| 210 210 |  | 
| 211 | 
            -
             | 
| 211 | 
            +
            To manually specify the order of tables, use `--jobs 1` so tables are synced one-at-a-time.
         | 
| 212 212 |  | 
| 213 213 | 
             
            ```sh
         | 
| 214 | 
            -
            pgsync -- | 
| 214 | 
            +
            pgsync table1,table2,table3 --jobs 1
         | 
| 215 215 | 
             
            ```
         | 
| 216 216 |  | 
| 217 217 | 
             
            To disable foreign key triggers and potentially break referential integrity, use:
         | 
    
        data/lib/pgsync/client.rb
    CHANGED
    
    | @@ -67,7 +67,8 @@ Options:} | |
| 67 67 | 
             
                  o.integer "--batch-size", "batch size", default: 10000, help: false
         | 
| 68 68 | 
             
                  o.float "--sleep", "sleep", default: 0, help: false
         | 
| 69 69 | 
             
                  o.boolean "--fail-fast", "stop on the first failed table", default: false
         | 
| 70 | 
            -
                  o.boolean "--defer-constraints", "defer constraints", default: false
         | 
| 70 | 
            +
                  o.boolean "--defer-constraints", "defer constraints", default: false, help: false
         | 
| 71 | 
            +
                  o.boolean "--defer-constraints-v2", "defer constraints", default: false
         | 
| 71 72 | 
             
                  o.boolean "--disable-user-triggers", "disable non-system triggers", default: false
         | 
| 72 73 | 
             
                  o.boolean "--disable-integrity", "disable foreign key triggers", default: false
         | 
| 73 74 | 
             
                  # private, for testing
         | 
    
        data/lib/pgsync/table_sync.rb
    CHANGED
    
    | @@ -141,7 +141,7 @@ module PgSync | |
| 141 141 | 
             
                  options = {start: start, finish: finish}
         | 
| 142 142 |  | 
| 143 143 | 
             
                  jobs = opts[:jobs]
         | 
| 144 | 
            -
                  if opts[:debug] || opts[:in_batches] || opts[:defer_constraints]
         | 
| 144 | 
            +
                  if opts[:debug] || opts[:in_batches] || opts[:defer_constraints] || opts[:defer_constraints_v2] || opts[:disable_integrity] || opts[:disable_integrity_v2]
         | 
| 145 145 | 
             
                    warning "--jobs ignored" if jobs
         | 
| 146 146 | 
             
                    jobs = 0
         | 
| 147 147 | 
             
                  end
         | 
| @@ -172,8 +172,23 @@ module PgSync | |
| 172 172 | 
             
                end
         | 
| 173 173 |  | 
| 174 174 | 
             
                def maybe_defer_constraints
         | 
| 175 | 
            -
                  if opts[: | 
| 175 | 
            +
                  if opts[:disable_integrity] || opts[:disable_integrity_v2]
         | 
| 176 | 
            +
                    # create a transaction on the source
         | 
| 177 | 
            +
                    # to ensure we get a consistent snapshot
         | 
| 178 | 
            +
                    source.transaction do
         | 
| 179 | 
            +
                      yield
         | 
| 180 | 
            +
                    end
         | 
| 181 | 
            +
                  elsif opts[:defer_constraints] || opts[:defer_constraints_v2]
         | 
| 176 182 | 
             
                    destination.transaction do
         | 
| 183 | 
            +
                      if opts[:defer_constraints_v2]
         | 
| 184 | 
            +
                        table_constraints = non_deferrable_constraints(destination)
         | 
| 185 | 
            +
                        table_constraints.each do |table, constraints|
         | 
| 186 | 
            +
                          constraints.each do |constraint|
         | 
| 187 | 
            +
                            destination.execute("ALTER TABLE #{quote_ident_full(table)} ALTER CONSTRAINT #{quote_ident(constraint)} DEFERRABLE")
         | 
| 188 | 
            +
                          end
         | 
| 189 | 
            +
                        end
         | 
| 190 | 
            +
                      end
         | 
| 191 | 
            +
             | 
| 177 192 | 
             
                      destination.execute("SET CONSTRAINTS ALL DEFERRED")
         | 
| 178 193 |  | 
| 179 194 | 
             
                      # create a transaction on the source
         | 
| @@ -181,6 +196,20 @@ module PgSync | |
| 181 196 | 
             
                      source.transaction do
         | 
| 182 197 | 
             
                        yield
         | 
| 183 198 | 
             
                      end
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                      # set them back
         | 
| 201 | 
            +
                      # there are 3 modes: DEFERRABLE INITIALLY DEFERRED, DEFERRABLE INITIALLY IMMEDIATE, and NOT DEFERRABLE
         | 
| 202 | 
            +
                      # we only update NOT DEFERRABLE
         | 
| 203 | 
            +
                      # https://www.postgresql.org/docs/current/sql-set-constraints.html
         | 
| 204 | 
            +
                      if opts[:defer_constraints_v2]
         | 
| 205 | 
            +
                        destination.execute("SET CONSTRAINTS ALL IMMEDIATE")
         | 
| 206 | 
            +
             | 
| 207 | 
            +
                        table_constraints.each do |table, constraints|
         | 
| 208 | 
            +
                          constraints.each do |constraint|
         | 
| 209 | 
            +
                            destination.execute("ALTER TABLE #{quote_ident_full(table)} ALTER CONSTRAINT #{quote_ident(constraint)} NOT DEFERRABLE")
         | 
| 210 | 
            +
                          end
         | 
| 211 | 
            +
                        end
         | 
| 212 | 
            +
                      end
         | 
| 184 213 | 
             
                    end
         | 
| 185 214 | 
             
                  else
         | 
| 186 215 | 
             
                    yield
         | 
    
        data/lib/pgsync/task.rb
    CHANGED
    
    | @@ -156,7 +156,7 @@ module PgSync | |
| 156 156 | 
             
                    destination.execute("INSERT INTO #{quoted_table} (SELECT * FROM #{quote_ident_full(temp_table)}) ON CONFLICT (#{on_conflict}) DO #{action}")
         | 
| 157 157 | 
             
                  else
         | 
| 158 158 | 
             
                    # use delete instead of truncate for foreign keys
         | 
| 159 | 
            -
                    if opts[:defer_constraints]
         | 
| 159 | 
            +
                    if opts[:defer_constraints] || opts[:defer_constraints_v2]
         | 
| 160 160 | 
             
                      destination.execute("DELETE FROM #{quoted_table}")
         | 
| 161 161 | 
             
                    else
         | 
| 162 162 | 
             
                      destination.truncate(table)
         | 
    
        data/lib/pgsync/version.rb
    CHANGED