db-migrate 0.1.3 → 0.2.0

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: 116ab5a81c362901cdd816c8dfce0052a8b988c0
4
- data.tar.gz: e91ded66c26efca89c6f53bb7fc11a0a79c18d65
3
+ metadata.gz: 7c9d9e26ef60000e8df08e69c64785f48916b3cf
4
+ data.tar.gz: d0ee951a80e3c977887261eeaa309893b6613e7a
5
5
  SHA512:
6
- metadata.gz: 4cb51474c633de2e333bb25d586136b0ccc6428f7bcd0edb4e9132fd94a353d6f7f4f7c9e09811913aaace1de8af80b06547b1c61bcb0117996eda36df78ba53
7
- data.tar.gz: 56a5bf6ba365179d7b86298874dffbe9f3a28e512313ab69b23d1dbc4f740de52143f52a4ca9821c6afdc25c735c93350546674b092fc0c72219dc74d830ce1c
6
+ metadata.gz: ff194194be430978206223312659d01aec0d1f0e9edac2936e56119992f4cb49bab9c82e016c2979610f71bdf215196748d0cd418174c5388ec0b2153264550d
7
+ data.tar.gz: 717dfe6e6efa4f7f8c604447b4cd585bd37d4351de73efef85a96f232aa194f81e9e169883240022a71f116eddea57f15e95dd8c9102b909946bf9e276cf952e
@@ -7,6 +7,9 @@ rvm:
7
7
 
8
8
  script: "bundle exec rspec"
9
9
 
10
+ addons:
11
+ postgresql: "9.4"
12
+
10
13
  services:
11
14
  - mysql
12
15
  - postgresql
@@ -14,3 +17,5 @@ services:
14
17
  before_script:
15
18
  - mysql -e 'create database migrate_test;'
16
19
  - psql -c 'create database migrate_test;' -U postgres
20
+ - psql -c 'CREATE SCHEMA IF NOT EXISTS public;' -U postgres
21
+ - psql -c 'SET search_path = public;' -U postgres
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- db-migrate (0.1.3)
4
+ db-migrate (0.2.0)
5
5
  colorize (= 0.7.7)
6
6
  highline (= 1.7.8)
7
7
  json (= 1.8.3)
data/README.md CHANGED
@@ -9,6 +9,8 @@ Tool for managing and executing your database migrations.
9
9
  gem install db-migrate
10
10
  ```
11
11
 
12
+ If you are going to use for example Postgres with **migrate**, make sure that you have Postgresql server running.
13
+
12
14
  ## Demo
13
15
  ![img](http://i.giphy.com/26tPaeasgQYU2mCoE.gif)
14
16
 
@@ -36,7 +38,7 @@ It supports multiple databases and multiple languages for executing migrations.
36
38
  ##### --help
37
39
  ```
38
40
  Commands:
39
- migrate init # make configuration file
41
+ migrate init # initialize tables and create config file if necessary
40
42
  migrate new [DESCRIPTION] # generate files for new migration
41
43
  migrate up # Upgrade database schema
42
44
  migrate down # Downgrade database schema
@@ -81,7 +83,9 @@ version_number=version_number_table_name
81
83
  If configuration file does not exist, it will run interactive configuration file creation process. You will answer few questions about your database, and **migrate** will create configuration file for you.
82
84
 
83
85
  #### new
84
- After that you can start generating migrations by using **migrate new** command. This will generate migration script for you based on your prefered language.
86
+ After that you can start generating migrations by using **migrate new** command. This will generate migration scripts for you based on your prefered language.
87
+
88
+ You will get new directory in format `vXXX-YYYY`, where `XXX` is version number, and `YYY` is short description you provide. Inside generated directory there will be two files. `up.LANG` and `down.LANG`, where `LANG` is language you use for writing migration scripts.
85
89
 
86
90
  #### up
87
91
  When you are done with writing your `up` and `down` migration script, you can execute **migrate up** to run up migration script for new version. You can also execute multiple migrations in single call by providing `--to n` argument, where `n` is highest version where you want to navigate.
@@ -114,6 +114,7 @@ class CLI < Thor
114
114
  end
115
115
 
116
116
  @migrator.init
117
+ @migrator.recover
117
118
  rescue Exception => e
118
119
  Log.error("Error while initialization.", e)
119
120
  if generated_config
@@ -123,7 +124,7 @@ class CLI < Thor
123
124
  end
124
125
 
125
126
  desc "new [DESCRIPTION]", "generate files for new migration"
126
- def new(description="")
127
+ def new(description="migration")
127
128
  @migrator.new(description)
128
129
  end
129
130
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'db-migrate'
3
- s.version = '0.1.3'
3
+ s.version = '0.2.0'
4
4
  s.licenses = ['MIT']
5
5
  s.summary = "Tool for managing and executing your database migrations."
6
6
  s.description = "#{s.summary} It supports multiple databases and multiple languages for writing migration scripts."
@@ -17,23 +17,55 @@ module Migrate
17
17
 
18
18
  def init
19
19
  @db.tx do
20
- @db.create_tables
20
+ if @db.tables_exists?
21
+ Log.info("Version tables already exist.")
22
+ else
23
+ @db.create_tables
24
+ end
25
+
26
+ self.recover
21
27
  end
22
28
  end
23
29
 
30
+ def recover
31
+ @db.tx do
32
+ directory = @config.root
33
+ migrations = Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
34
+ migrations.each do |migration|
35
+ match = migration.match(/v(\d*)-(.*)/i)
36
+ if match != nil
37
+ v, desc = match.captures
38
+ unless @db.version_exists?(v)
39
+ self.new(desc, v)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
24
47
  def migration_dir(migration)
25
48
  date = DateTime.parse(migration["created_date"].to_s)
26
- "#{@config.root}/v#{migration["version"]}-#{date.strftime("%Y-%m-%d")}"
49
+ "#{@config.root}/v#{migration["version"]}-#{migration["description"]}"
27
50
  end
28
51
 
29
- def new(desc)
52
+ def new(desc, version=nil)
30
53
  @db.tx do
31
54
  Log.info("Creating new migration...")
32
55
 
33
- migration = @db.new_migration(desc)
56
+ if version == nil
57
+ version = @db.highest_version.to_i + 1
58
+ end
59
+
60
+ migration = @db.new_migration(version, desc)
34
61
  migration_dir = self.migration_dir(migration)
35
- Dir.mkdir migration_dir
36
- @lang.create_migration(migration_dir)
62
+
63
+ if Dir.exists? migration_dir
64
+ Log.info("Migration directory '#{migration_dir}' already exists.")
65
+ else
66
+ Dir.mkdir migration_dir
67
+ @lang.create_migration(migration_dir)
68
+ end
37
69
 
38
70
  Log.success("Migration for version #{migration["version"]} created.")
39
71
  migration_dir
@@ -13,10 +13,10 @@ module Migrate
13
13
  @config.storage
14
14
  end
15
15
 
16
- def new_migration(description="")
16
+ def new_migration(version=0, description="")
17
17
  self.exec_sql <<-eos
18
- INSERT INTO #{@config.version_info} (description, created_date)
19
- VALUES('#{description}', now())
18
+ INSERT INTO #{@config.version_info} (version, description, created_date)
19
+ VALUES(#{version}, '#{description}', now())
20
20
  eos
21
21
 
22
22
  res = self.exec_sql <<-eos
@@ -26,7 +26,7 @@ module Migrate
26
26
  end
27
27
 
28
28
  def list_migrations(selects, limit)
29
- self.exec_sql <<-eos
29
+ self.exec_sql <<-eos
30
30
  SELECT #{(selects == nil ? "*" : selects)} FROM #{@config.version_info}
31
31
  ORDER BY last_up, version
32
32
  #{limit != nil ? "LIMIT #{limit}" : ""}
@@ -57,9 +57,19 @@ module Migrate
57
57
  eos
58
58
  end
59
59
 
60
+ def highest_version
61
+ self.extract_version self.exec_sql <<-eos
62
+ SELECT version FROM #{@config.version_info}
63
+ ORDER BY version DESC
64
+ LIMIT 1
65
+ eos
66
+ rescue VersionNotFound => e
67
+ 0
68
+ end
69
+
60
70
  def next_version
61
71
  self.extract_version self.exec_sql <<-eos
62
- SELECT version FROM #{@config.version_info}
72
+ SELECT version FROM #{@config.version_info}
63
73
  WHERE version > (SELECT version FROM #{@config.version_number} LIMIT 1)
64
74
  ORDER BY version
65
75
  LIMIT 1
@@ -75,8 +85,8 @@ module Migrate
75
85
 
76
86
  def prev_version
77
87
  self.extract_version self.exec_sql <<-eos
78
- SELECT version FROM #{@config.version_info}
79
- WHERE version < (SELECT version FROM #{@config.version_number} LIMIT 1)
88
+ SELECT version FROM #{@config.version_info}
89
+ WHERE version < (SELECT version FROM #{@config.version_number} LIMIT 1)
80
90
  ORDER BY version DESC
81
91
  LIMIT 1
82
92
  eos
@@ -89,7 +99,7 @@ module Migrate
89
99
 
90
100
  def log_down(version)
91
101
  self.exec_sql "UPDATE #{@config.version_info} SET last_down=now() WHERE version=#{version}"
92
-
102
+
93
103
  lowest_version = self.lowest_version
94
104
  version_to_save = lowest_version.to_i < version.to_i ? self.prev_version().to_i : 0
95
105
  self.exec_sql "UPDATE #{@config.version_number} SET version=#{version_to_save}"
@@ -104,6 +114,13 @@ module Migrate
104
114
  end
105
115
  end
106
116
 
117
+ def version_exists?(version)
118
+ self.get_migration(version)
119
+ true
120
+ rescue VersionNotFound
121
+ false
122
+ end
123
+
107
124
  def delete(version)
108
125
  self.exec_sql "DELETE FROM #{@config.version_info} WHERE version=#{version}"
109
126
  end
@@ -136,6 +153,10 @@ module Migrate
136
153
  raise "Implementation for creating tables not found"
137
154
  end
138
155
 
156
+ def tables_exists?
157
+ raise "Implementation for checking if version tables already exists not found"
158
+ end
159
+
139
160
  # Executes SQL
140
161
  def exec_sql(sql)
141
162
  raise "Implementation for executing SQL script not found"
@@ -40,6 +40,13 @@ module Migrate
40
40
  Log.success("Version table created")
41
41
  end
42
42
 
43
+ def tables_exists?
44
+ vi = self.exec_sql("SHOW TABLES LIKE '#{@config.version_info}'")
45
+ vn = self.exec_sql("SHOW TABLES LIKE '#{@config.version_number}'")
46
+
47
+ vi.length > 0 && vn.length > 0
48
+ end
49
+
43
50
  def exec_sql(sql)
44
51
  results = []
45
52
  result = @tx.query sql
@@ -1,4 +1,5 @@
1
1
  require "pg"
2
+ require 'wannabe_bool'
2
3
 
3
4
  module Migrate
4
5
  module Storage
@@ -13,6 +14,26 @@ module Migrate
13
14
  })
14
15
  end
15
16
 
17
+ def tables_exists?
18
+ vi = self.exec_sql <<-eos
19
+ SELECT EXISTS (
20
+ SELECT 1
21
+ FROM information_schema.tables
22
+ WHERE table_name = '#{@config.version_info}'
23
+ );
24
+ eos
25
+
26
+ vn = self.exec_sql <<-eos
27
+ SELECT EXISTS (
28
+ SELECT 1
29
+ FROM information_schema.tables
30
+ WHERE table_name = '#{@config.version_number}'
31
+ );
32
+ eos
33
+
34
+ vi[0]["exists"].to_b && vn[0]["exists"].to_b
35
+ end
36
+
16
37
  def create_tables
17
38
  Log.info("Creating version table")
18
39
  self.exec_sql <<-eos
@@ -1,6 +1,5 @@
1
1
  describe "Storage" do
2
2
  $dbs.each do |db|
3
-
4
3
  around(:each) do |test|
5
4
  db.tx do
6
5
  test.run
@@ -9,7 +8,8 @@ describe "Storage" do
9
8
 
10
9
  context db.type do
11
10
  it "should create new migration" do
12
- created = db.new_migration("this is description")
11
+ created = db.new_migration(200, "this is description")
12
+ expect(created["version"].to_i).to eq(200)
13
13
  expect(created).not_to eq(nil)
14
14
  expect(db.exec_sql(
15
15
  "SELECT * FROM #{$version_info} WHERE version=#{created["version"]}")[0])
@@ -53,6 +53,11 @@ describe "Storage" do
53
53
  end
54
54
  end
55
55
 
56
+ it "should be able to check if version table already exists" do
57
+ exists = db.tables_exists?
58
+ expect(exists).to be true
59
+ end
60
+
56
61
  it "should get lowest version" do
57
62
  version = db.lowest_version
58
63
  expect(version.to_s).to eq("1")
@@ -18,7 +18,7 @@ $pg_config_hash = $config_base.merge({
18
18
  :password => ""
19
19
  })
20
20
 
21
- def load_pg_config
21
+ def load_pg_config
22
22
  config = Conf.new("spec/lib/fixtures", "example_pg.config")
23
23
  config.init($pg_config_hash)
24
24
  config.load!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-migrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Pusic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-13 00:00:00.000000000 Z
11
+ date: 2016-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor