Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 940f588

Browse files
committed
Add automatic db migration and creation in home directory
1 parent 2be30c1 commit 940f588

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

db/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
development:
22
adapter: sqlite
3-
database: db/development.db
3+
database:
44
max_connections: 5
55
pool_timeout: 5000
66

77
production:
88
adapter: sqlite
9-
database: db/production.db
9+
database:
1010
max_connections: 5
1111
pool_timeout: 5000
1212

1313
test: &test
1414
adapter: sqlite
15-
database: db/test.db
15+
database:
1616
max_connections: 5
1717
pool_timeout: 5000

db/env.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# frozen_string_literal: true
22

3-
require 'sequel'
4-
require 'yaml'
5-
63
ENV['WPXF_ENV'] = 'development' unless ENV['WPXF_ENV']
74

85
db_config_path = File.join(__dir__, 'config.yml')
96
db_config = YAML.load_file(db_config_path)[ENV['WPXF_ENV']]
107

8+
if db_config['database'].nil?
9+
db_config['database'] = File.join(Wpxf.databases_path, "#{ENV['WPXF_ENV']}.db")
10+
end
11+
1112
Sequel::Model.plugin :timestamps
1213
Sequel::Model.db = Sequel.connect(db_config)
14+
15+
Sequel.extension :migration
16+
Sequel::Migrator.run(Sequel::Model.db, File.join(__dir__, 'migrations'))

lib/wpxf.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'date'
4+
require 'fileutils'
5+
require 'json'
6+
require 'time'
7+
require 'yaml'
8+
require 'zip'
9+
10+
Gem.loaded_specs['wpxf'].dependencies.each do |d|
11+
require d.name unless d.type == :development || d.name == 'rubyzip'
12+
end
13+
14+
# The root namespace.
15+
module Wpxf
16+
def self.data_directory
17+
File.join(app_path, 'data')
18+
end
19+
20+
def self.app_path
21+
File.expand_path(File.dirname(__dir__))
22+
end
23+
24+
def self.version
25+
Gem.loaded_specs['wpxf'].version
26+
end
27+
28+
def self.home_directory
29+
File.join(Dir.home, '.wpxf')
30+
end
31+
32+
def self.databases_path
33+
path = File.join(home_directory, 'db')
34+
FileUtils.mkdir_p(path) unless File.directory?(path)
35+
path
36+
end
37+
38+
def self.change_stdout_sync(enabled)
39+
original_setting = STDOUT.sync
40+
STDOUT.sync = true
41+
yield(enabled)
42+
STDOUT.sync = original_setting
43+
end
44+
end
45+
46+
require_relative '../db/env'

lib/wpxf/core.rb

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
# frozen_string_literal: true
22

3-
# The root namespace.
4-
module Wpxf
5-
def self.data_directory=(val)
6-
@@data_directory = val
7-
end
8-
9-
def self.data_directory
10-
@@data_directory
11-
end
12-
13-
def self.app_path=(val)
14-
@@app_path = val
15-
end
16-
17-
def self.app_path
18-
@@app_path
19-
end
20-
21-
def self.version
22-
File.read(File.join(Wpxf.app_path, 'VERSION')).strip
23-
end
24-
25-
def self.change_stdout_sync(enabled)
26-
original_setting = STDOUT.sync
27-
STDOUT.sync = true
28-
yield(enabled)
29-
STDOUT.sync = original_setting
30-
end
31-
end
32-
333
require 'wpxf/db'
344
require 'wpxf/utility'
355

0 commit comments

Comments
 (0)