Class: GitPusshuTen::Commands::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/gitpusshuten/modules/active_record/command.rb

Instance Attribute Summary

Attributes inherited from Base

#cli, #command, #configuration, #environment, #hooks, #perform_hooks

Instance Method Summary collapse

Methods inherited from Base

#c, #command_object, description, #e, #error, example, #g, #git, #help, #local, long_description, #message, #perform!, #perform_hooks!, #post_perform!, #pre_perform!, #prompt_for_root_password!, #prompt_for_user_password!, #r, #requires_user_existence!, #silent, #standard, usage, #validate!, #warning, #y, #yes?

Constructor Details

#initialize(*objects) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 20

def initialize(*objects)
  super
  
  ##
  # Set the command
  @command = cli.arguments.shift
  
  ##
  # Display the help screen if either the command
  # or environment name hasn't been set
  help if command.nil? or e.name.nil?
  
  ##
  # Default Configuration
  @local_configuration_dir    = File.join(local.gitpusshuten_dir, 'active_record')
  @local_configuration_file   = File.join(@local_configuration_dir, "#{e.name}.database.yml")
  @remote_configuration_dir   = File.join(c.path, 'modules', 'active_record')
  @remote_configuration_file  = File.join(@remote_configuration_dir, "#{e.sanitized_app_name}.#{e.name}.database.yml")
  
  ##
  # Ensure the directory is always available
  FileUtils.mkdir_p(@local_configuration_dir)
end

Instance Method Details

#choose_adapterObject

Prompts the user to choose a database adapter



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 119

def choose_adapter
  choose do |menu|
    menu.prompt = ''
    menu.choice('mysql', 'mysql2')
    menu.choice('postgresql')
    menu.choice('sqlite3')
    menu.choice('oracle')
    menu.choice('frontbase')
    menu.choice('ibm_db')
  end
end

#overwrite_if_exists?Boolean

If the local configuration file exists, it will ask the user whether he wants to overwrite it

Returns:

  • (Boolean)


212
213
214
215
216
217
218
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 212

def overwrite_if_exists?
  if File.exist?(@local_configuration_file)
    warning "Configuration file already exists in #{@local_configuration_file}."
    warning "Would you like to overwrite?"
    exit unless yes?
  end
end

#perform_create_config!Object

Alias to perform_create_configuration!



55
56
57
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 55

def perform_create_config!
  perform_create_configuration!
end

#perform_create_configuration!Object

Creates a template for the user to work with



46
47
48
49
50
51
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 46

def perform_create_configuration!
  overwrite_if_exists?
  
  render_database_yml
  message "Created #{y(@local_configuration_file)}."
end

#perform_download_config!Object

Alias to perform_download_configuration!



113
114
115
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 113

def perform_download_config!
  perform_download_configuration!
end

#perform_download_configuration!Object

Downloads the configuration file from the remote server.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 92

def perform_download_configuration!
  overwrite_if_exists?
  
  ##
  # Don't proceed unless the file exists
  if not e.file?(@remote_configuration_file)
    error "Configuration could not be found on the server in #{y(@remote_configuration_file)}."
    exit
  end
  
  ##
  # Ensure the remote active_record module directory exits
  # and upload the local file to the remote file location
  Spinner.return :message => "Downloading the database configuration file.." do
    e.scp_as_user(:download, @remote_configuration_file, @local_configuration_file)
    g('Done!')
  end        
end

#perform_upload_config!Object

Alias to perform_upload_configuration!



86
87
88
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 86

def perform_upload_config!
  perform_upload_configuration!
end

#perform_upload_configuration!Object

Uploads the local configuration file to the remote server.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 61

def perform_upload_configuration!
  requires_user_existence!
  prompt_for_user_password!
  
  if not File.exist?(@local_configuration_file)
    error "Could not find #{y(@local_configuration_file)}."
    error "Either download an existing one from your server with:"
    standard "\n\s\s#{y("heavenly active_record download-configuration from #{e.name}")}\n\n"
    error "Or create a new template with:"
    standard "\n\s\s#{y("heavenly active_record create-configuration for #{e.name}")}"
    exit
  end
  
  ##
  # Ensure the remote active_record module directory exits
  # and upload the local file to the remote file location
  Spinner.return :message => "Uploading the database configuration file.." do
    e.execute_as_user("mkdir -p '#{@remote_configuration_dir}'")
    e.scp_as_user(:upload, @local_configuration_file, @remote_configuration_file)
    g('Done!')
  end
end

#render_database_ymlObject

Render the chosen Active Record database template file



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/gitpusshuten/modules/active_record/command.rb', line 133

def render_database_yml
  config_content = case choose_adapter
  when 'mysql', 'mysql2'
<<-CONFIG
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: #{e.sanitized_app_name}_#{e.name}
  pool: 5
  username: #{c.user}
  password:
  host: localhost
  # socket: /tmp/mysql.sock
CONFIG
  when 'postgresql'
<<-CONFIG
production:
  adapter: postgresql
  encoding: unicode
  database: #{e.sanitized_app_name}_#{e.name}
  pool: 5
  username: #{c.user}
  password:
CONFIG
  when 'sqlite3'
<<-CONFIG
production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000
CONFIG
  when 'oracle'
<<-CONFIG
production:
  adapter: oracle
  database: #{e.sanitized_app_name}_#{e.name}
  username: #{c.user}
  password: 
CONFIG
  when 'frontbase'
<<-CONFIG
production:
  adapter: frontbase
  host: localhost
  database: #{e.sanitized_app_name}_#{e.name}
  username: #{c.user}
  password: 
CONFIG
  when 'ibm_db'
<<-CONFIG
production:
  adapter: ibm_db
  username: #{c.user}
  password:
  database: #{e.sanitized_app_name}_#{e.name}
  #schema: db2inst1
  #host: localhost
  #port: 50000
  #account: my_account
  #app_user: my_app_user
  #application: my_application
  #workstation: my_workstation
  #security: SSL
  #timeout: 10
  #authentication: SERVER
  #parameterized: false
CONFIG
  end # end case statement
  
  File.open(@local_configuration_file, 'w') do |file|
    file << config_content
  end
end