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 +4 -4
- data/README.md +31 -3
- data/lib/blue-shift.rb +1 -0
- data/lib/blueshift/migration.rb +16 -5
- data/lib/blueshift/version.rb +1 -1
- data/lib/sequel/adapters/postgres_ext.rb +0 -1
- data/lib/tasks/schema.rake +12 -0
- metadata +3 -3
- data/db/migrations/dummy_migration.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 854cfe4464d33859954cd59b31e0aab3416ff3c3
|
4
|
+
data.tar.gz: 36e046e96ca8f013b9a1dcfed1017334f2a2c4a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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: :
|
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
|
-
|
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
|
|
data/lib/blue-shift.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'blueshift'
|
data/lib/blueshift/migration.rb
CHANGED
@@ -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
|
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
|
data/lib/blueshift/version.rb
CHANGED
data/lib/tasks/schema.rake
CHANGED
@@ -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.
|
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-
|
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
|
-
-
|
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
|