pgslice 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a7f188ff426286b8fd13dbc359d7bbdf514de64
4
- data.tar.gz: bf65c37948a1cbd88e072f7c88b20e0cf4b75b1c
3
+ metadata.gz: 595fd1ef4439e8f5180ee6f84596c088124cf900
4
+ data.tar.gz: 6a7144e70f93c4edeb7b48be0efe28e6ccfd87f9
5
5
  SHA512:
6
- metadata.gz: 6cece21cd4d7bb10fcf510fa6ba6a1590ba24faaaf553315c1a0c2f2d734a39b77ac9fa419168eb75a09f6f7851bf1b62ab0f1cb59774daa6832b4a7e6ed3559
7
- data.tar.gz: 1a820b128546c65a697c32d928955734bac43123af7bc714839530e1bfcb3ce06c61ac7d454caf1e92a2d8a188267dc94910e5ceb25ce0db2b23eb0a0c130cc5
6
+ metadata.gz: ffb023c7ceacab8f0362808131a6a8d680455d756ab636f558257390c68b20214edf2ad4eefb3dd6f5fb46b9a9b7a8eb264fa070ffc4fca34e051c9b069ca66f
7
+ data.tar.gz: 44869b8f950334d8db95a9a081937966367e1cac30caa5ce03254b694f54f893f04a429f935e2f6a80487465c5283005f280512ac3b600f027a2f9fd87d4ec16
@@ -1,6 +1,11 @@
1
+ ## 0.1.2
2
+
3
+ - Added `--dry-run` option
4
+ - Print sql to stdout instead of stderr
5
+
1
6
  ## 0.1.1
2
7
 
3
- - Add sql commands to output
8
+ - Added sql commands to output
4
9
 
5
10
  ## 0.1.0
6
11
 
data/README.md CHANGED
@@ -84,6 +84,66 @@ To undo swap, use:
84
84
  pgslice unswap <table>
85
85
  ```
86
86
 
87
+ ## Sample Output
88
+
89
+ `pgslice` prints the SQL commands that were executed on the server. To print without executing, use the `--dry-run` option.
90
+
91
+ ```console
92
+ $ pgslice prep locations created_at day
93
+ BEGIN;
94
+
95
+ CREATE TABLE locations_intermediate (
96
+ LIKE locations INCLUDING INDEXES INCLUDING DEFAULTS
97
+ );
98
+
99
+ CREATE FUNCTION locations_insert_trigger()
100
+ RETURNS trigger AS $$
101
+ BEGIN
102
+ EXECUTE 'INSERT INTO public.locations_' || to_char(NEW.created_at, 'YYYYMMDD') || ' VALUES ($1.*)' USING NEW;
103
+ RETURN NULL;
104
+ END;
105
+ $$ LANGUAGE plpgsql;
106
+
107
+ CREATE TRIGGER locations_insert_trigger
108
+ BEFORE INSERT ON locations_intermediate
109
+ FOR EACH ROW EXECUTE PROCEDURE locations_insert_trigger();
110
+
111
+ COMMIT;
112
+ ```
113
+
114
+ ```console
115
+ $ pgslice add_partitions locations --intermediate --past 1 --future 1
116
+ BEGIN;
117
+
118
+ CREATE TABLE locations_20160423 (
119
+ LIKE locations_intermediate INCLUDING INDEXES INCLUDING DEFAULTS,
120
+ CHECK (created_at >= '2016-04-23'::date AND created_at < '2016-04-24'::date)
121
+ ) INHERITS (locations_intermediate);
122
+
123
+ CREATE TABLE locations_20160424 (
124
+ LIKE locations_intermediate INCLUDING INDEXES INCLUDING DEFAULTS,
125
+ CHECK (created_at >= '2016-04-24'::date AND created_at < '2016-04-25'::date)
126
+ ) INHERITS (locations_intermediate);
127
+
128
+ CREATE TABLE locations_20160425 (
129
+ LIKE locations_intermediate INCLUDING INDEXES INCLUDING DEFAULTS,
130
+ CHECK (created_at >= '2016-04-25'::date AND created_at < '2016-04-26'::date)
131
+ ) INHERITS (locations_intermediate);
132
+
133
+ COMMIT;
134
+ ```
135
+
136
+ ```console
137
+ $ pgslice swap locations
138
+ BEGIN;
139
+
140
+ ALTER TABLE locations RENAME TO locations_retired;
141
+
142
+ ALTER TABLE locations_intermediate RENAME TO locations;
143
+
144
+ COMMIT;
145
+ ```
146
+
87
147
  ## Upgrading
88
148
 
89
149
  Run:
@@ -59,8 +59,6 @@ module PgSlice
59
59
  abort "Column not found: #{column}" unless columns(table).include?(column)
60
60
  abort "Invalid period: #{period}" unless SQL_FORMAT[period.to_sym]
61
61
 
62
- log "Creating #{intermediate_table} from #{table}"
63
-
64
62
  queries = []
65
63
 
66
64
  queries << <<-SQL
@@ -97,8 +95,6 @@ FOR EACH ROW EXECUTE PROCEDURE #{trigger_name}();
97
95
  abort "Usage: pgslice unprep <table>" if arguments.length != 1
98
96
  abort "Table not found: #{intermediate_table}" unless table_exists?(intermediate_table)
99
97
 
100
- log "Dropping #{intermediate_table}"
101
-
102
98
  queries = [
103
99
  "DROP TABLE #{intermediate_table} CASCADE;",
104
100
  "DROP FUNCTION #{trigger_name}();"
@@ -130,7 +126,6 @@ FOR EACH ROW EXECUTE PROCEDURE #{trigger_name}();
130
126
  partition_name = "#{original_table}_#{day.strftime(name_format)}"
131
127
  next if table_exists?(partition_name)
132
128
 
133
- log "Creating #{partition_name} from #{table}"
134
129
  date_format = "%Y-%m-%d"
135
130
 
136
131
  queries << <<-SQL
@@ -175,22 +170,24 @@ CREATE TABLE #{partition_name} (
175
170
  fields = columns(source_table).join(", ")
176
171
  batch_size = options[:batch_size]
177
172
 
178
- if starting_id < max_source_id
179
- log "#{primary_key}: #{starting_id} -> #{max_source_id}"
180
- log "time: #{starting_time.to_date} -> #{ending_time.to_date}"
173
+ log "Overview"
174
+ log "#{source_table} max #{primary_key}: #{max_source_id}"
175
+ log "#{dest_table} max #{primary_key}: #{max_dest_id}"
176
+ log "time period: #{starting_time.to_date} -> #{ending_time.to_date}"
177
+ log
181
178
 
182
- while starting_id < max_source_id
183
- log "#{starting_id}..#{[starting_id + batch_size - 1, max_source_id].min}"
179
+ log "Batches"
180
+ while starting_id <= max_source_id
181
+ log "#{starting_id}..#{[starting_id + batch_size - 1, max_source_id].min}"
184
182
 
185
- query = "INSERT INTO #{dest_table} (#{fields}) SELECT #{fields} FROM #{source_table} WHERE #{primary_key} >= #{starting_id} AND #{primary_key} < #{starting_id + batch_size} AND #{field} >= '#{starting_time.strftime(date_format)}'::date AND #{field} < '#{ending_time.strftime(date_format)}'::date"
186
- log query if options[:debug]
187
- execute(query)
183
+ query = "INSERT INTO #{dest_table} (#{fields}) SELECT #{fields} FROM #{source_table} WHERE #{primary_key} >= #{starting_id} AND #{primary_key} < #{starting_id + batch_size} AND #{field} >= '#{starting_time.strftime(date_format)}'::date AND #{field} < '#{ending_time.strftime(date_format)}'::date"
184
+ log query if options[:debug]
185
+ execute(query)
188
186
 
189
- starting_id += batch_size
187
+ starting_id += batch_size
190
188
 
191
- if options[:sleep] && starting_id < max_source_id
192
- sleep(options[:sleep])
193
- end
189
+ if options[:sleep] && starting_id <= max_source_id
190
+ sleep(options[:sleep])
194
191
  end
195
192
  end
196
193
  end
@@ -205,9 +202,6 @@ CREATE TABLE #{partition_name} (
205
202
  abort "Table not found: #{intermediate_table}" unless table_exists?(intermediate_table)
206
203
  abort "Table already exists: #{retired_table}" if table_exists?(retired_table)
207
204
 
208
- log "Renaming #{table} to #{retired_table}"
209
- log "Renaming #{intermediate_table} to #{table}"
210
-
211
205
  queries = [
212
206
  "ALTER TABLE #{table} RENAME TO #{retired_table};",
213
207
  "ALTER TABLE #{intermediate_table} RENAME TO #{table};"
@@ -225,9 +219,6 @@ CREATE TABLE #{partition_name} (
225
219
  abort "Table not found: #{retired_table}" unless table_exists?(retired_table)
226
220
  abort "Table already exists: #{intermediate_table}" if table_exists?(intermediate_table)
227
221
 
228
- log "Renaming #{table} to #{intermediate_table}"
229
- log "Renaming #{retired_table} to #{table}"
230
-
231
222
  queries = [
232
223
  "ALTER TABLE #{table} RENAME TO #{intermediate_table};",
233
224
  "ALTER TABLE #{retired_table} RENAME TO #{table};"
@@ -246,6 +237,7 @@ CREATE TABLE #{partition_name} (
246
237
  o.integer "--future", default: 3
247
238
  o.integer "--past", default: 3
248
239
  o.integer "--batch-size", default: 10000
240
+ o.boolean "--dry-run", default: false
249
241
  o.on "-v", "--version", "print the version" do
250
242
  log PgSlice::VERSION
251
243
  @exit = true
@@ -263,6 +255,10 @@ CREATE TABLE #{partition_name} (
263
255
  $stderr.puts message
264
256
  end
265
257
 
258
+ def log_sql(message = nil)
259
+ $stdout.puts message
260
+ end
261
+
266
262
  def abort(message)
267
263
  raise PgSlice::Error, message
268
264
  end
@@ -294,13 +290,14 @@ CREATE TABLE #{partition_name} (
294
290
  def run_queries(queries)
295
291
  connection.transaction do
296
292
  execute("SET client_min_messages TO warning")
297
- log
298
- log "============================== SQL =============================="
293
+ log_sql "BEGIN;"
294
+ log_sql
299
295
  queries.each do |query|
300
- log
301
- log query
302
- execute(query)
296
+ log_sql query
297
+ log_sql
298
+ execute(query) unless options[:dry_run]
303
299
  end
300
+ log_sql "COMMIT;"
304
301
  end
305
302
  end
306
303
 
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgslice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane