baza 0.0.28 → 0.0.29

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: a62941ce9303dc76e57c84b8db5045e9ffb060a6
4
- data.tar.gz: 88ab7cd45d1f8caf0fdb1139edb27931a3f0dfbc
3
+ metadata.gz: fb0c9dc47499fc957724e7051425231c423e40be
4
+ data.tar.gz: 8b5e83c0d0d78e16c16462b338d3e6cd0150fabf
5
5
  SHA512:
6
- metadata.gz: e8797dfcd1e18c7d0de33f048e7de2e903caa48c6abb10be47cf0b4e075ebc7607f27373497633ab23e79c5b3064d9ecf110d20a07a063c2904e1b6f0fdf23e1
7
- data.tar.gz: 59a1951ceea82bc29831ceeea9751f0755de1bc1ffa7ce89970c2492e51d2bd933eb7c730c190d141468e0ad75f7b10a1137e50aed030e0d3a26808726a54edf
6
+ metadata.gz: 11bb9bb3e0d6c1da04cc4881b354db53e2477607d4b272dc2c416447678fee26c7fa4aaa68b62c49f856e60b3c638d0091f5c47f4c574841040aeff03b5023d8
7
+ data.tar.gz: 0ff63d05d3441aa25dbc726a3fe86b5beed0910923db5187892f31a852925aaffc242a9df347cd8031fb59c6c59b2e2a44a0832eb993eb64a4a8e41db7c7850f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.28
1
+ 0.0.29
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: baza 0.0.28 ruby lib
5
+ # stub: baza 0.0.29 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza".freeze
9
- s.version = "0.0.28"
9
+ s.version = "0.0.29"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Kasper Johansen".freeze]
14
- s.date = "2017-07-03"
14
+ s.date = "2017-10-11"
15
15
  s.description = "A database abstraction layer, model framework and database framework.".freeze
16
16
  s.email = "kj@gfish.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -1,10 +1,10 @@
1
1
  class Baza::Database
2
2
  include Baza::DatabaseModelFunctionality
3
3
 
4
- attr_reader :db, :driver, :name_was
5
- attr_accessor :name
4
+ attr_reader :db, :driver, :name
6
5
 
7
6
  def initialize(args)
7
+ @changes = {}
8
8
  @db = args.fetch(:db)
9
9
  @driver = args.fetch(:driver)
10
10
  @name = args.fetch(:name)
@@ -19,6 +19,19 @@ class Baza::Database
19
19
  end
20
20
  end
21
21
 
22
+ def name=(new_name)
23
+ @changes[:name] = new_name
24
+ @name = new_name
25
+ end
26
+
27
+ def name_changed?
28
+ @changes.key?(:name) && @changes.fetch(:name).to_s != name.to_s
29
+ end
30
+
31
+ def name_was
32
+ @changes[:name]
33
+ end
34
+
22
35
  def tables(args = {})
23
36
  list_args = {database: name}
24
37
  list_args[:name] = args.fetch(:name) if args[:name]
@@ -5,16 +5,9 @@ class Baza::Driver::Pg::Database < Baza::Database
5
5
  end
6
6
 
7
7
  def drop
8
- other_db = @db.databases.list.find { |database| database.name != @db.current_database }
9
-
10
- # Drop database through a cloned connection, because Postgres might bug up if dropping the current
11
- @db.clone_conn(db: other_db.name) do |cloned_conn|
12
- # Close existing connections to avoid 'is being accessed by other users' errors
13
- cloned_conn.query("REVOKE CONNECT ON DATABASE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database} FROM public")
14
- cloned_conn.query("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = #{@db.sep_val}#{@db.esc(name)}#{@db.sep_val} AND pid != pg_backend_pid()")
15
-
8
+ with_cloned_conn_and_terminated_connections do |cloned_conn|
16
9
  # Drop the database
17
- cloned_conn.query("DROP DATABASE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database}")
10
+ cloned_conn.query("DROP DATABASE #{db.sep_database}#{db.escape_database(name)}#{db.sep_database}")
18
11
  end
19
12
 
20
13
  self
@@ -36,9 +29,9 @@ class Baza::Driver::Pg::Database < Baza::Database
36
29
  where_args[:table_name] = args.fetch(:name) if args[:name]
37
30
 
38
31
  use do
39
- @db.select([:information_schema, :tables], where_args, orderby: :table_name) do |table_data|
32
+ db.select([:information_schema, :tables], where_args, orderby: :table_name) do |table_data|
40
33
  table = Baza::Driver::Pg::Table.new(
41
- driver: @db.driver,
34
+ driver: db.driver,
42
35
  data: table_data
43
36
  )
44
37
 
@@ -56,7 +49,7 @@ class Baza::Driver::Pg::Database < Baza::Database
56
49
  end
57
50
 
58
51
  def use(&blk)
59
- @db.with_database(name, &blk)
52
+ db.with_database(name, &blk)
60
53
  self
61
54
  end
62
55
 
@@ -64,13 +57,29 @@ class Baza::Driver::Pg::Database < Baza::Database
64
57
  # Creates a new table by the given name and data.
65
58
  def create_table(table_name, data, args = nil)
66
59
  use do
67
- @db.tables.create(table_name, data, args)
60
+ db.tables.create(table_name, data, args)
68
61
  end
69
62
  end
70
63
 
71
64
  def rename(new_name)
72
- @db.query("ALTER DATABASE #{@db.sep_database}#{@db.escape_database(name_was)}#{@db.sep_database} RENAME TO #{@db.sep_database}#{@db.escape_database(new_name)}#{@db.sep_database}")
65
+ with_cloned_conn_and_terminated_connections do |cloned_conn|
66
+ cloned_conn.query("ALTER DATABASE #{db.sep_database}#{db.escape_database(name_was)}#{db.sep_database} RENAME TO #{db.sep_database}#{db.escape_database(new_name)}#{db.sep_database}")
67
+ end
68
+
73
69
  @name = new_name.to_s
74
70
  self
75
71
  end
72
+
73
+ def with_cloned_conn_and_terminated_connections
74
+ other_db = db.databases.list.find { |database| database.name != @db.current_database }
75
+
76
+ # Drop database through a cloned connection, because Postgres might bug up if dropping the current
77
+ db.clone_conn(db: other_db.name) do |cloned_conn|
78
+ # Close existing connections to avoid 'is being accessed by other users' errors
79
+ cloned_conn.query("REVOKE CONNECT ON DATABASE #{db.sep_database}#{db.escape_database(name)}#{db.sep_database} FROM public") unless name_changed?
80
+ cloned_conn.query("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = #{db.sep_val}#{@db.esc(name)}#{db.sep_val} AND pid != pg_backend_pid()")
81
+
82
+ yield cloned_conn
83
+ end
84
+ end
76
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-03 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator