Class: Complish::Database Private

Inherits:
Object
  • Object
show all
Defined in:
lib/complish/database.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The Complish Database class

Author:

Since:

  • 0.1.0-alpha

Version:

  • 1.2

Constant Summary collapse

MIGRATIONS_DIR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Complish migrations directory

Examples:

Return MIGRATIONS_DIR

Complish::Database::MIGRATIONS_DIR
 #=> '<path to complish>/db/migrations'

Since:

  • 0.1.0-alpha

File.join(ENV['PWD'], 'db', 'migrations').freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComplish::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Database

Examples:

Create a new instance of Database

Complish::Database.new
 #=> #<Complish::Database>

Raises:

  • (StandardError)

    if persisted configuration is not found

Since:

  • 0.1.0-alpha

Version:

  • 1.0


45
46
47
48
49
50
51
# File 'lib/complish/database.rb', line 45

def initialize
  config = configuration
  @name = config[:db_file]
  @location = config[:db_location]
  build_database
  @connection = Sequel.connect("sqlite://#{database_file}")
end

Instance Attribute Details

#connectionSequel::SQLite::Database (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the database connection

Examples:

Return the Database connection

Complish::Database.new.connection
 #=> <#Sequel::SQLite::Database>

Since:

  • 0.1.0-alpha


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/complish/database.rb', line 27

class Database
  attr_reader :name, :location, :connection
  Sequel.extension :migration

  # The Complish migrations directory
  # @example Return MIGRATIONS_DIR
  #  Complish::Database::MIGRATIONS_DIR
  #   #=> '<path to complish>/db/migrations'
  # @return [String] the file path of the Complish migrations directory
  MIGRATIONS_DIR = File.join(ENV['PWD'], 'db', 'migrations').freeze

  # Creates a new instance of Database
  # @version 1.0
  # @example Create a new instance of Database
  #  Complish::Database.new
  #   #=> #<Complish::Database>
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def initialize
    config = configuration
    @name = config[:db_file]
    @location = config[:db_location]
    build_database
    @connection = Sequel.connect("sqlite://#{database_file}")
  end

  # Creates a new instance of Database and invokes .migrate
  # @version 1.0
  # @example Create a new instance and run migrations
  #  Complish::Database.migrate
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] version the version to migrate the database to
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.migrate(version = nil)
    new.tap { |i| i.migrate(version) }
  end

  # Runs migrations to specified version
  # @version 1.1
  # @example Run migrations on an instance of Database to a specific version
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.migrate(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided migrations will be run to latest
  # @param [Fixnum, nil] version the version to migrate the database to
  # @return [Complish::Database] an instance of Database
  def migrate(version = nil)
    return self if Dir["#{MIGRATIONS_DIR}/*"].empty?
    opts = version.nil? ? {} : { target: version }
    run_migrations(opts) unless version.nil? && current?
    self
  end

  # Creates a new instance of Database and invokes .rollback
  # @version 1.0
  # @example Create a new instance and rollback
  #  Complish::Database.rollback
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.rollback(steps = 1)
    new.tap { |i| i.rollback(steps) }
  end

  # Rolls the database migrations back the specified amount of steps
  # @version 1.1
  # @example Run rollback on an instance of Database to specified amount of steps
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.rollback(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided the database will only be rolled back one version
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @return [Complish::Database] an instance of Database
  def rollback(steps = 1)
    return self if current_version.zero?
    steps ||= 1 # override to default if nil is explicitly is passed.
    migrate_to = (current_version - steps) < 0 ? 0 : current_version - steps
    migrate(migrate_to)
    self
  end

  # Delete the database
  # @version 1.0
  # @example Delete the database
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.delete!
  #   #=> true
  # @return [Boolean] success of database file deletion
  def delete!
    File.delete(database_file) if File.exist?(database_file)
    !File.exist?(database_file)
  end

  # The current version of the database
  # @version 1.0
  # @example Get the current version of the database instance
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.current_version
  #   #=> 3
  # @note will return 0 if database has not been migrated or if database version is actually 0
  # @return [Fixnum] the version of the database
  def current_version
    connection[:schema_info].select(:version).first[:version].to_i
  rescue
    0
  end

  # Whether or not the database is migrated to the latest version
  # @version 1.0
  # @example Check if migrations are current
  #  Complish::Database.new.current?
  #   #=> true
  # @return [Boolean] whether the migrations are current
  def current?
    Sequel::Migrator.is_current?(connection, MIGRATIONS_DIR)
  end

  private

  def configuration
    raise StandardError, 'No configuration found' unless Complish::Configuration.exist?
    Complish::Configuration.from_file
  end

  def build_database
    FileUtils.mkdir_p(location) unless Dir.exist?(location)
    FileUtils.touch database_file
  end

  def database_file
    File.join(location, name)
  end

  def run_migrations(opts = {})
    Sequel::Migrator.run(connection, MIGRATIONS_DIR, opts)
  end
end

#locationString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the directory or url of the database file

Examples:

Return the Database location

Complish::Database.new.location
 #=> '/Users/<username>'

Since:

  • 0.1.0-alpha


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/complish/database.rb', line 27

class Database
  attr_reader :name, :location, :connection
  Sequel.extension :migration

  # The Complish migrations directory
  # @example Return MIGRATIONS_DIR
  #  Complish::Database::MIGRATIONS_DIR
  #   #=> '<path to complish>/db/migrations'
  # @return [String] the file path of the Complish migrations directory
  MIGRATIONS_DIR = File.join(ENV['PWD'], 'db', 'migrations').freeze

  # Creates a new instance of Database
  # @version 1.0
  # @example Create a new instance of Database
  #  Complish::Database.new
  #   #=> #<Complish::Database>
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def initialize
    config = configuration
    @name = config[:db_file]
    @location = config[:db_location]
    build_database
    @connection = Sequel.connect("sqlite://#{database_file}")
  end

  # Creates a new instance of Database and invokes .migrate
  # @version 1.0
  # @example Create a new instance and run migrations
  #  Complish::Database.migrate
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] version the version to migrate the database to
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.migrate(version = nil)
    new.tap { |i| i.migrate(version) }
  end

  # Runs migrations to specified version
  # @version 1.1
  # @example Run migrations on an instance of Database to a specific version
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.migrate(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided migrations will be run to latest
  # @param [Fixnum, nil] version the version to migrate the database to
  # @return [Complish::Database] an instance of Database
  def migrate(version = nil)
    return self if Dir["#{MIGRATIONS_DIR}/*"].empty?
    opts = version.nil? ? {} : { target: version }
    run_migrations(opts) unless version.nil? && current?
    self
  end

  # Creates a new instance of Database and invokes .rollback
  # @version 1.0
  # @example Create a new instance and rollback
  #  Complish::Database.rollback
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.rollback(steps = 1)
    new.tap { |i| i.rollback(steps) }
  end

  # Rolls the database migrations back the specified amount of steps
  # @version 1.1
  # @example Run rollback on an instance of Database to specified amount of steps
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.rollback(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided the database will only be rolled back one version
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @return [Complish::Database] an instance of Database
  def rollback(steps = 1)
    return self if current_version.zero?
    steps ||= 1 # override to default if nil is explicitly is passed.
    migrate_to = (current_version - steps) < 0 ? 0 : current_version - steps
    migrate(migrate_to)
    self
  end

  # Delete the database
  # @version 1.0
  # @example Delete the database
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.delete!
  #   #=> true
  # @return [Boolean] success of database file deletion
  def delete!
    File.delete(database_file) if File.exist?(database_file)
    !File.exist?(database_file)
  end

  # The current version of the database
  # @version 1.0
  # @example Get the current version of the database instance
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.current_version
  #   #=> 3
  # @note will return 0 if database has not been migrated or if database version is actually 0
  # @return [Fixnum] the version of the database
  def current_version
    connection[:schema_info].select(:version).first[:version].to_i
  rescue
    0
  end

  # Whether or not the database is migrated to the latest version
  # @version 1.0
  # @example Check if migrations are current
  #  Complish::Database.new.current?
  #   #=> true
  # @return [Boolean] whether the migrations are current
  def current?
    Sequel::Migrator.is_current?(connection, MIGRATIONS_DIR)
  end

  private

  def configuration
    raise StandardError, 'No configuration found' unless Complish::Configuration.exist?
    Complish::Configuration.from_file
  end

  def build_database
    FileUtils.mkdir_p(location) unless Dir.exist?(location)
    FileUtils.touch database_file
  end

  def database_file
    File.join(location, name)
  end

  def run_migrations(opts = {})
    Sequel::Migrator.run(connection, MIGRATIONS_DIR, opts)
  end
end

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the database file

Examples:

Return the Database name

Complish::Database.new.name
 #=> '.complish.db'

Since:

  • 0.1.0-alpha


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/complish/database.rb', line 27

class Database
  attr_reader :name, :location, :connection
  Sequel.extension :migration

  # The Complish migrations directory
  # @example Return MIGRATIONS_DIR
  #  Complish::Database::MIGRATIONS_DIR
  #   #=> '<path to complish>/db/migrations'
  # @return [String] the file path of the Complish migrations directory
  MIGRATIONS_DIR = File.join(ENV['PWD'], 'db', 'migrations').freeze

  # Creates a new instance of Database
  # @version 1.0
  # @example Create a new instance of Database
  #  Complish::Database.new
  #   #=> #<Complish::Database>
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def initialize
    config = configuration
    @name = config[:db_file]
    @location = config[:db_location]
    build_database
    @connection = Sequel.connect("sqlite://#{database_file}")
  end

  # Creates a new instance of Database and invokes .migrate
  # @version 1.0
  # @example Create a new instance and run migrations
  #  Complish::Database.migrate
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] version the version to migrate the database to
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.migrate(version = nil)
    new.tap { |i| i.migrate(version) }
  end

  # Runs migrations to specified version
  # @version 1.1
  # @example Run migrations on an instance of Database to a specific version
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.migrate(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided migrations will be run to latest
  # @param [Fixnum, nil] version the version to migrate the database to
  # @return [Complish::Database] an instance of Database
  def migrate(version = nil)
    return self if Dir["#{MIGRATIONS_DIR}/*"].empty?
    opts = version.nil? ? {} : { target: version }
    run_migrations(opts) unless version.nil? && current?
    self
  end

  # Creates a new instance of Database and invokes .rollback
  # @version 1.0
  # @example Create a new instance and rollback
  #  Complish::Database.rollback
  #   #=> #<Complish::Database>
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @raise [StandardError] if persisted configuration is not found
  # @return [Complish::Database] an instance of Database
  def self.rollback(steps = 1)
    new.tap { |i| i.rollback(steps) }
  end

  # Rolls the database migrations back the specified amount of steps
  # @version 1.1
  # @example Run rollback on an instance of Database to specified amount of steps
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.rollback(2)
  #   #=> #<Complish::Database>
  # @note if argument is not provided the database will only be rolled back one version
  # @param [Fixnum, nil] steps the amount of steps to rollback
  # @return [Complish::Database] an instance of Database
  def rollback(steps = 1)
    return self if current_version.zero?
    steps ||= 1 # override to default if nil is explicitly is passed.
    migrate_to = (current_version - steps) < 0 ? 0 : current_version - steps
    migrate(migrate_to)
    self
  end

  # Delete the database
  # @version 1.0
  # @example Delete the database
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.delete!
  #   #=> true
  # @return [Boolean] success of database file deletion
  def delete!
    File.delete(database_file) if File.exist?(database_file)
    !File.exist?(database_file)
  end

  # The current version of the database
  # @version 1.0
  # @example Get the current version of the database instance
  #  db = Complish::Database.new
  #   #=> #<Complish::Database>
  #  db.current_version
  #   #=> 3
  # @note will return 0 if database has not been migrated or if database version is actually 0
  # @return [Fixnum] the version of the database
  def current_version
    connection[:schema_info].select(:version).first[:version].to_i
  rescue
    0
  end

  # Whether or not the database is migrated to the latest version
  # @version 1.0
  # @example Check if migrations are current
  #  Complish::Database.new.current?
  #   #=> true
  # @return [Boolean] whether the migrations are current
  def current?
    Sequel::Migrator.is_current?(connection, MIGRATIONS_DIR)
  end

  private

  def configuration
    raise StandardError, 'No configuration found' unless Complish::Configuration.exist?
    Complish::Configuration.from_file
  end

  def build_database
    FileUtils.mkdir_p(location) unless Dir.exist?(location)
    FileUtils.touch database_file
  end

  def database_file
    File.join(location, name)
  end

  def run_migrations(opts = {})
    Sequel::Migrator.run(connection, MIGRATIONS_DIR, opts)
  end
end

Class Method Details

.migrate(version = nil) ⇒ Complish::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Database and invokes .migrate

Examples:

Create a new instance and run migrations

Complish::Database.migrate
 #=> #<Complish::Database>

Raises:

  • (StandardError)

    if persisted configuration is not found

Since:

  • 0.1.0-alpha

Version:

  • 1.0


61
62
63
# File 'lib/complish/database.rb', line 61

def self.migrate(version = nil)
  new.tap { |i| i.migrate(version) }
end

.rollback(steps = 1) ⇒ Complish::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Database and invokes .rollback

Examples:

Create a new instance and rollback

Complish::Database.rollback
 #=> #<Complish::Database>

Raises:

  • (StandardError)

    if persisted configuration is not found

Since:

  • 0.1.0-alpha

Version:

  • 1.0


90
91
92
# File 'lib/complish/database.rb', line 90

def self.rollback(steps = 1)
  new.tap { |i| i.rollback(steps) }
end

Instance Method Details

#current?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether or not the database is migrated to the latest version

Examples:

Check if migrations are current

Complish::Database.new.current?
 #=> true

Since:

  • 0.1.0-alpha

Version:

  • 1.0


146
147
148
# File 'lib/complish/database.rb', line 146

def current?
  Sequel::Migrator.is_current?(connection, MIGRATIONS_DIR)
end

#current_versionFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

will return 0 if database has not been migrated or if database version is actually 0

The current version of the database

Examples:

Get the current version of the database instance

db = Complish::Database.new
 #=> #<Complish::Database>
db.current_version
 #=> 3

Since:

  • 0.1.0-alpha

Version:

  • 1.0


134
135
136
137
138
# File 'lib/complish/database.rb', line 134

def current_version
  connection[:schema_info].select(:version).first[:version].to_i
rescue
  0
end

#delete!Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete the database

Examples:

Delete the database

db = Complish::Database.new
 #=> #<Complish::Database>
db.delete!
 #=> true

Since:

  • 0.1.0-alpha

Version:

  • 1.0


120
121
122
123
# File 'lib/complish/database.rb', line 120

def delete!
  File.delete(database_file) if File.exist?(database_file)
  !File.exist?(database_file)
end

#migrate(version = nil) ⇒ Complish::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

if argument is not provided migrations will be run to latest

Runs migrations to specified version

Examples:

Run migrations on an instance of Database to a specific version

db = Complish::Database.new
 #=> #<Complish::Database>
db.migrate(2)
 #=> #<Complish::Database>

Since:

  • 0.1.0-alpha

Version:

  • 1.1


75
76
77
78
79
80
# File 'lib/complish/database.rb', line 75

def migrate(version = nil)
  return self if Dir["#{MIGRATIONS_DIR}/*"].empty?
  opts = version.nil? ? {} : { target: version }
  run_migrations(opts) unless version.nil? && current?
  self
end

#rollback(steps = 1) ⇒ Complish::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

if argument is not provided the database will only be rolled back one version

Rolls the database migrations back the specified amount of steps

Examples:

Run rollback on an instance of Database to specified amount of steps

db = Complish::Database.new
 #=> #<Complish::Database>
db.rollback(2)
 #=> #<Complish::Database>

Since:

  • 0.1.0-alpha

Version:

  • 1.1


104
105
106
107
108
109
110
# File 'lib/complish/database.rb', line 104

def rollback(steps = 1)
  return self if current_version.zero?
  steps ||= 1 # override to default if nil is explicitly is passed.
  migrate_to = (current_version - steps) < 0 ? 0 : current_version - steps
  migrate(migrate_to)
  self
end