Class: JustOneDB::Command::Heroku

Inherits:
Base
  • Object
show all
Defined in:
lib/justonedb/command/heroku/base.rb,
lib/justonedb/command/heroku/migrate.rb,
lib/justonedb/command/heroku/promote.rb,
lib/justonedb/command/heroku/purge-old.rb,
lib/justonedb/command/heroku/make-default.rb,
lib/justonedb/command/heroku/reset-default.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

commands, load, platforms

Class Method Details

.get_commandsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/justonedb/command/heroku/base.rb', line 14

def get_commands
	[
		{
			:cmd_class => self,
			:method => self.instance_method(:migrate),
			:platform => "heroku",
			:command => "migrate",
			:summary => "Migrate Data from current to 'NEW' database",
			:options => [
							{
								:option_id => "directory",
								:short => '-d',
								:long => '--directory TMP_DIR',
								:description => 'Write temporary data to TMP_DIR (default ".")'
							}
						],
		},
		{
			:cmd_class => self,
			:method => self.instance_method(:promote),
			:platform => "heroku",
			:command => "promote",
			:summary => "Promote JustOneDB 'NEW' database to live",
			:options => NIL
		},
		{
			:cmd_class => self,
			:method => self.instance_method(:purge_old),
			:platform => "heroku",
			:command => "purge-old",
			:summary => "Purge 'OLD' JustOneDB variables",
			:options => NIL
		},
		{
			:cmd_class => self,
			:method => self.instance_method(:make_justone_default_database),
			:platform => "heroku",
			:command => "make-default",
			:summary => "Make JustOneDB the default Heroku database",
			:options => NIL
		},
		{
			:cmd_class => self,
			:method => self.instance_method(:reset_default_database),
			:platform => "heroku",
			:command => "reset-default",
			:summary => "Reset the default Heroku database using DATABASE_URL_DEFAULT",
			:options => NIL
		},
	]
end

Instance Method Details

#make_justone_default_database(args, options) ⇒ Object

Make JustOneDB the default Rails database in heroku, by assigning JUSTONEDB_DBI_URL to DATABASE_URL

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/justonedb/command/heroku/make-default.rb', line 9

def make_justone_default_database(args, options)
	herokuapi = ::Heroku::Command::BaseWithApp.new(args, options)

	vars = herokuapi.heroku.config_vars(herokuapi.app)

	raise InvalidAppConfig, "JUSTONEDB_DBI_URL undefined" unless vars["JUSTONEDB_DBI_URL"]

	if vars["DATABASE_URL"] && vars["DATABASE_URL"] == vars["JUSTONEDB_DBI_URL"]
		raise InvalidAppConfig, "DATABASE_URL is already using the JustOne database"
	end

	puts "Making JUSTONEDB_DBI_URL the default Rails database"

	detected_app = herokuapi.app

	if vars["DATABASE_URL"]
		# Save the old one ...
		herokuapi.heroku.add_config_vars(detected_app, { "JUSTONEDB_SAVED_DATABASE_URL" => vars["DATABASE_URL"] })
	end

	herokuapi.heroku.add_config_vars(detected_app, { "DATABASE_URL" => vars["JUSTONEDB_DBI_URL"] })

	puts "DATABASE_URL now points directly to the JustOneDB database; application restarting"
end

#migrate(args, options) ⇒ Object

Migrate JustOne database data from JUSTONEDB_DBI_URL to JUSTONEDB_NEW_DBI_URL

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/justonedb/command/heroku/migrate.rb', line 11

def migrate(args, options)
	# Migrate data from JUSTONEDB_DBI_URL to JUSTONEDB_NEW_DBI_URL
	# -d, --directory # Specify temporary dump directory
	directory = options[:directory]
	directory = Dir::tmpdir unless directory

	herokuapi = ::Heroku::Command::BaseWithApp.new(args, options)

	vars = herokuapi.heroku.config_vars(herokuapi.app)

	# Check that we have an old and a new DBI entry
	raise InvalidAppConfig, "JUSTONEDB_DBI_URL undefined" unless vars["JUSTONEDB_DBI_URL"]
	raise InvalidAppConfig, "JUSTONEDB_NEW_DBI_URL undefined" unless vars["JUSTONEDB_NEW_DBI_URL"]

	super(vars["JUSTONEDB_DBI_URL"], vars["JUSTONEDB_NEW_DBI_URL"], directory)
end

#promote(args, options) ⇒ Object

Promote ‘NEW’ JustOne database to current

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/justonedb/command/heroku/promote.rb', line 10

def promote(args, options)
	herokuapi = ::Heroku::Command::BaseWithApp.new(args, options)

	vars = herokuapi.heroku.config_vars(herokuapi.app)

	# Check that we have an old and a new DBI entry
	raise InvalidAppConfig, "JUSTONEDB_NEW_DBI_URL undefined" unless vars["JUSTONEDB_NEW_DBI_URL"]
	raise InvalidAppConfig, "JUSTONEDB_NEW_REST_BASE undefined" unless vars["JUSTONEDB_NEW_REST_BASE"]
	raise InvalidAppConfig, "JUSTONEDB_NEW_REST_DB undefined" unless vars["JUSTONEDB_NEW_REST_DB"]
	raise InvalidAppConfig, "JUSTONEDB_NEW_SUPPORT_ID undefined" unless vars["JUSTONEDB_NEW_SUPPORT_ID"]

	puts "Promoting JustOneDB 'NEW' vars to current, and saving current to 'OLD'"

	detected_app = herokuapi.app

	new_vars = {}
	new_vars["JUSTONEDB_OLD_DBI_URL"] = vars["JUSTONEDB_DBI_URL"]
	new_vars["JUSTONEDB_OLD_REST_BASE"] = vars["JUSTONEDB_REST_BASE"]
	new_vars["JUSTONEDB_OLD_REST_DB"] = vars["JUSTONEDB_REST_DB"]
	new_vars["JUSTONEDB_OLD_SUPPORT_ID"] = vars["JUSTONEDB_SUPPORT_ID"]
	new_vars["JUSTONEDB_DBI_URL"] = vars["JUSTONEDB_NEW_DBI_URL"]
	new_vars["JUSTONEDB_REST_BASE"] = vars["JUSTONEDB_NEW_REST_BASE"]
	new_vars["JUSTONEDB_REST_DB"] = vars["JUSTONEDB_NEW_REST_DB"]
	new_vars["JUSTONEDB_SUPPORT_ID"] = vars["JUSTONEDB_NEW_SUPPORT_ID"]

	herokuapi.heroku.add_config_vars(detected_app, new_vars)

	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_NEW_DBI_URL")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_NEW_REST_BASE")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_NEW_REST_DB")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_NEW_SUPPORT_ID")

	puts "Variables modified, please check that your application works correctly, before running 'justonedb heroku purge-old'"
end

#purge_old(args, options) ⇒ Object

Purge ‘OLD’ JustOneDB environment variables

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/justonedb/command/heroku/purge-old.rb', line 10

def purge_old(args, options)
	herokuapi = ::Heroku::Command::BaseWithApp.new(args, options)

	vars = herokuapi.heroku.config_vars(herokuapi.app)

	# Check that we have the old entries
	raise InvalidAppConfig, "JUSTONEDB_OLD_DBI_URL undefined" unless vars["JUSTONEDB_OLD_DBI_URL"]
	raise InvalidAppConfig, "JUSTONEDB_OLD_REST_BASE undefined" unless vars["JUSTONEDB_OLD_REST_BASE"]
	raise InvalidAppConfig, "JUSTONEDB_OLD_REST_DB undefined" unless vars["JUSTONEDB_OLD_REST_DB"]
	raise InvalidAppConfig, "JUSTONEDB_OLD_SUPPORT_ID undefined" unless vars["JUSTONEDB_OLD_SUPPORT_ID"]

	puts "Purging JustOneDB 'OLD' configuration variables"

	detected_app = herokuapi.app

	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_OLD_DBI_URL")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_OLD_REST_BASE")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_OLD_REST_DB")
	herokuapi.heroku.remove_config_var(detected_app, "JUSTONEDB_OLD_SUPPORT_ID")
end

#reset_default_database(args, options) ⇒ Object

Set DATABASE_URL back to JUSTONEDB_SAVED_DATABASE_URL

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/justonedb/command/heroku/reset-default.rb', line 9

def reset_default_database(args, options)
	herokuapi = ::Heroku::Command::BaseWithApp.new(args, options)

	vars = herokuapi.heroku.config_vars(herokuapi.app)

	raise InvalidAppConfig, "JUSTONEDB_SAVED_DATABASE_URL undefined" unless vars["JUSTONEDB_SAVED_DATABASE_URL"]

	puts "Resetting DATABASE_URL from JUSTONEDB_SAVED_DATABASE_URL"

	detected_app = herokuapi.app

	herokuapi.heroku.add_config_vars(detected_app, { "DATABASE_URL" => vars["JUSTONEDB_SAVED_DATABASE_URL"] })

	puts "DATABASE_URL has now been reset; application restarting"
end