blue-shift 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0bb5cec279a6d30fcd817b2a267e87d1e32cac3
4
- data.tar.gz: 4af59446089a1b34753495df831bcb804f2d190c
3
+ metadata.gz: 854cfe4464d33859954cd59b31e0aab3416ff3c3
4
+ data.tar.gz: 36e046e96ca8f013b9a1dcfed1017334f2a2c4a1
5
5
  SHA512:
6
- metadata.gz: eaa9e257e759e9bd4041f6d49335d4f6e46fac9b8f114ef152ce05fcea98dcf71b5d11c53375dfce50d9a75320159d52e88eb89cb2cf2b0a88ff4bf0fa8ee23e
7
- data.tar.gz: 11b6e0785d57ad8cbd8482f47f4090d90ebc9fbe17d064b7d37206cc6e3c2e2ae437708ff06ec2f63aa166012c8c91e58384908e9e5d044fcd56db1c7d2a61a9
6
+ metadata.gz: 5a3dacb4ab8e472434905142c1f1ffe344a269cc80e1e8194524bc10b80c1dddeab08677be3ff2961f463b8da3de0a36170a12a6614b771759d5c1b0da959e32
7
+ data.tar.gz: f3969bf286f9e125c1d621e8bcab8e4ada926757d688844ee6888b1b7ac6b601c8436fb442a20e5887d9a6a5fef6216b5dee71af5f5b2518659f15eaebb9c72a
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  - `:diststyle` => `:even` (implicit default), `:key`, or `:all`
30
30
 
31
- The Distribution Style. When `:distkey` is also specified, only `:key` DISTSTYLE is supported by Redshift.
31
+ The Distribution Style. When `:distkey` is also specified, only `:key` DISTSTYLE is supported by Redshift and will be ignored by Postgres
32
32
 
33
33
  - `:sortkeys` => a list of column names
34
34
 
@@ -43,7 +43,7 @@ Or install it yourself as:
43
43
  For example:
44
44
 
45
45
  ```ruby
46
- create_table :chocolates, distkey: :region, diststyle: :all, sortkeys: [:richness, :organic], sortstyle: :interleaved do
46
+ create_table :chocolates, distkey: :region, diststyle: :key, sortkeys: [:richness, :organic], sortstyle: :interleaved do
47
47
  String :region
48
48
  Integer :richness
49
49
  boolean :organic
@@ -51,8 +51,14 @@ create_table :chocolates, distkey: :region, diststyle: :all, sortkeys: [:richnes
51
51
  end
52
52
  ```
53
53
 
54
+ You can also redeclare the sortkeys for an existing table by using the `optimize_table` method. This is reconstructive, no additive. For example:
54
55
 
55
- ### Migrations (coming soon)
56
+ ```ruby
57
+ optimize_table :chocolates, sortkeys: [:organic, :region], distkey: :region
58
+ ```
59
+
60
+
61
+ ### Migrations
56
62
 
57
63
  Blueshift unifies migrations for your Postgres and Redshift databases into one file. Postgres migrations and Redshift migrations use Sequel.
58
64
 
@@ -93,6 +99,28 @@ Blueshift.migration do
93
99
  reddown {}
94
100
  end
95
101
  ```
102
+
103
+ #### Transactions
104
+
105
+ By default, each migration runs within a transaction.
106
+ You can manually specify to disable transactions on a per migration basis. For example, if you want to not force
107
+ transaction use for a particular migration, call the `no_transaction` method in the `Blueshift.migration` block:
108
+
109
+ ```ruby
110
+ Blueshift.migration do
111
+ no_transaction
112
+ up do
113
+ # ...
114
+ end
115
+ end
116
+ ```
117
+
118
+ This is necessary in some cases, such as when attempting to use `CREATE INDEX CONCURRENTLY`
119
+ on PostgreSQL (which supports transactional schema, but not that statement inside a transaction).
120
+
121
+ Also, because each `optimize_table` call gets run within its own transaction, you should probably
122
+ use no_transaction in migrations that use that in order to prevent starting multiple transactions
123
+ within one another.
96
124
 
97
125
  ## Development
98
126
 
@@ -0,0 +1 @@
1
+ require_relative 'blueshift'
@@ -5,9 +5,11 @@ require 'logger'
5
5
  module Blueshift
6
6
  REDSHIFT_DB = Sequel.connect(ENV.fetch('REDSHIFT_URL', 'redshift://'), logger: Logger.new('redshift.log'))
7
7
  POSTGRES_DB = Sequel.connect(ENV.fetch('DATABASE_URL', 'postgres://'), logger: Logger.new('postgres.log'))
8
+ DBS = { pg: POSTGRES_DB, redshift: REDSHIFT_DB }
8
9
 
9
10
  class Migration
10
- attr_reader :postgres_migration, :redshift_migration, :use_transactions
11
+ attr_reader :postgres_migration, :redshift_migration
12
+ attr_accessor :use_transactions
11
13
  MIGRATION_DIR = File.join(Dir.pwd, 'db/migrations')
12
14
  SCHEMA_TABLE = :schema_migrations
13
15
  SCHEMA_COLUMN = :filename
@@ -38,6 +40,10 @@ module Blueshift
38
40
  redshift_migration.down = block
39
41
  end
40
42
 
43
+ def no_transaction
44
+ self.use_transactions = false
45
+ end
46
+
41
47
  def apply(db, direction)
42
48
  if db.is_a?(Sequel::Redshift::Database)
43
49
  redshift_migration.apply(db, direction)
@@ -47,12 +53,12 @@ module Blueshift
47
53
  end
48
54
 
49
55
  class << self
50
- def run_pg!
51
- Sequel::Migrator.run(POSTGRES_DB, MIGRATION_DIR)
56
+ def run_pg!(options = {})
57
+ Sequel::Migrator.run(POSTGRES_DB, MIGRATION_DIR, options)
52
58
  end
53
59
 
54
- def run_redshift!
55
- Sequel::Migrator.run(REDSHIFT_DB, MIGRATION_DIR)
60
+ def run_redshift!(options = {})
61
+ Sequel::Migrator.run(REDSHIFT_DB, MIGRATION_DIR, options)
56
62
  end
57
63
 
58
64
  def run_both!
@@ -60,6 +66,11 @@ module Blueshift
60
66
  run_redshift!
61
67
  end
62
68
 
69
+ def rollback!(db_type)
70
+ target = Sequel::TimestampMigrator.new(DBS[db_type], MIGRATION_DIR).applied_migrations[-2].to_i
71
+ public_send("run_#{db_type}!", target: target)
72
+ end
73
+
63
74
  def insert_into_schema_migrations(db)
64
75
  ds = schema_dataset(db)
65
76
  ds.delete
@@ -1,3 +1,3 @@
1
1
  module Blueshift
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -7,7 +7,6 @@ module Sequel
7
7
  def type_literal_generic_suuid(column)
8
8
  column[:fixed] = true
9
9
  column[:size] = 36
10
- column[:null] ||= false
11
10
  type_literal_generic_string(column)
12
11
  end
13
12
  end
@@ -34,6 +34,12 @@ namespace :pg do
34
34
  Blueshift::Migration.run_pg!
35
35
  Rake::Task['pg:schema:dump'].invoke
36
36
  end
37
+
38
+ desc 'Rollback the latest applied migration for Postgres'
39
+ task :rollback do
40
+ Blueshift::Migration.rollback!(:pg)
41
+ Rake::Task['pg:schema:dump'].invoke
42
+ end
37
43
  end
38
44
 
39
45
 
@@ -61,6 +67,12 @@ namespace :redshift do
61
67
  Blueshift::Migration.run_redshift!
62
68
  Rake::Task['redshift:schema:dump'].invoke
63
69
  end
70
+
71
+ desc 'Rollback the latest applied migration for Redshift'
72
+ task :rollback do
73
+ Blueshift::Migration.rollback!(:redshift)
74
+ Rake::Task['pg:schema:dump'].invoke
75
+ end
64
76
  end
65
77
 
66
78
  namespace :blueshift do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blue-shift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Mansour
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-01 00:00:00.000000000 Z
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -114,7 +114,7 @@ files:
114
114
  - bin/console
115
115
  - bin/setup
116
116
  - blueshift.gemspec
117
- - db/migrations/dummy_migration.rb
117
+ - lib/blue-shift.rb
118
118
  - lib/blueshift.rb
119
119
  - lib/blueshift/migration.rb
120
120
  - lib/blueshift/railtie.rb
File without changes