forked from github/github-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_setup.rb
More file actions
51 lines (45 loc) · 1.49 KB
/
Copy pathexample_setup.rb
File metadata and controls
51 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
require "pp"
require "pathname"
root_path = Pathname(__FILE__).dirname.join("..").expand_path
lib_path = root_path.join("lib")
$:.unshift(lib_path)
require "active_record"
attempts = 0
begin
ActiveRecord::Base.establish_connection({
adapter: "mysql2",
username: "root",
database: "github_ds_test",
})
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
# remove db tree if present so we can start fresh
db_path = root_path.join("db")
db_path.rmtree if db_path.exist?
# use generator to create the migration
require "rails/generators"
require "generators/github/ds/active_record_generator"
Rails::Generators.invoke "github:ds:active_record"
# require migration and run it so we have the key values table
require db_path.join("migrate").children.first.to_s
ActiveRecord::Migration.verbose = false
CreateKeyValuesTable.up
rescue
raise if attempts >= 1
ActiveRecord::Base.establish_connection({
adapter: "mysql2",
username: "root",
})
ActiveRecord::Base.connection.execute("CREATE DATABASE IF NOT EXISTS `github_ds_test`")
attempts += 1
retry
end
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `example_key_values`")
ActiveRecord::Base.connection.execute(<<-SQL)
CREATE TABLE `example_key_values` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`key` varchar(255) NOT NULL,
`value` blob NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_key_values_on_key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SQL