136 lines
3.3 KiB
Ruby
Executable File
136 lines
3.3 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require_relative "./load_env"
|
|
|
|
def current_version
|
|
DB[:meta].first[:version] rescue 0
|
|
end
|
|
|
|
def migrate(version, message = nil, &block)
|
|
puts "Check migration #{version}".on_blue
|
|
if current_version < version
|
|
puts "Migrate #{version}".blue
|
|
begin
|
|
yield
|
|
DB[:meta].update(version: version)
|
|
puts "Successfuly set version #{version}".green
|
|
puts message.green if message
|
|
rescue => err
|
|
puts err.message.on_red
|
|
exit 1
|
|
end
|
|
else
|
|
puts "Already migrated #{version}".yellow
|
|
end
|
|
end
|
|
|
|
migrate 1, "Initialized database" do
|
|
DB.create_table :meta do
|
|
primary_key :id
|
|
Int :version
|
|
end
|
|
DB[:meta].insert(version: 1)
|
|
|
|
DB.create_table :pexs do
|
|
primary_key :id
|
|
String :name
|
|
Float :amount
|
|
end rescue puts "pexs already exists".yellow
|
|
|
|
DB.create_table :user_pexs do
|
|
primary_key :id
|
|
Int :user_id
|
|
Int :pex_id
|
|
Date :created_at
|
|
end rescue puts "user_pexs already exists".yellow
|
|
|
|
DB.create_table :users do
|
|
primary_key :id
|
|
String :username
|
|
String :hashed_password
|
|
end rescue puts "users already exists".yellow
|
|
end
|
|
|
|
migrate 2, "Add pex categories" do
|
|
DB.alter_table :pexs do
|
|
add_column :category, String
|
|
end rescue puts "pexs.category already exists".yellow
|
|
end
|
|
|
|
migrate 3, "Add pex user belonging" do
|
|
DB.alter_table :pexs do
|
|
add_column :user_id, :Int
|
|
end rescue puts "pexs.user_id already exists".yellow
|
|
DB[:pexs].update(user_id: 1)
|
|
end
|
|
|
|
migrate 4, "Fix pex category default behavior" do
|
|
DB.alter_table :pexs do
|
|
set_column_default(:category, '')
|
|
end rescue puts "pexs.category default already exists".yellow
|
|
DB[:pexs].where(category: nil).update(category: '')
|
|
end
|
|
|
|
migrate 5, "Add meta schema" do
|
|
DB.alter_table :meta do
|
|
add_column :code_version, String
|
|
end rescue puts "meta.code_version already exists".yellow
|
|
DB[:meta].update(code_version: "0")
|
|
end
|
|
|
|
migrate 6 do
|
|
DB.alter_table :meta do
|
|
add_column :code_date, String
|
|
end rescue puts "meta.code_date already exists".yellow
|
|
end
|
|
|
|
migrate 7, "Fix pex initial category" do
|
|
DB[:pexs].each do |pex|
|
|
DB[:pexs].where(id: pex[:id]).update(category: pex[:category].to_s.downcase)
|
|
end
|
|
end
|
|
|
|
migrate 8, "Add default hidden for pexs" do
|
|
DB.alter_table :pexs do
|
|
add_column :hidden, TrueClass
|
|
end rescue puts "pex.hidden already exists".yellow
|
|
DB[:pexs].each { |pex| DB[:pexs].where(id: pex[:id]).update(hidden: false) }
|
|
end
|
|
|
|
migrate 9, "Add achievements" do
|
|
DB.create_table :achievements do
|
|
primary_key :id
|
|
Int :user_id
|
|
String :name
|
|
String :success_name
|
|
String :parameters_json
|
|
String :icon
|
|
end
|
|
end
|
|
|
|
migrate 10, "Add generic flag to pexs, for bookmarking" do
|
|
DB.alter_table :pexs do
|
|
add_column :flag, :String
|
|
end rescue puts "pexs.flag already exists".yellow
|
|
end
|
|
|
|
migrate 11, "Add recalls" do
|
|
DB.create_table :recalls do
|
|
primary_key :id
|
|
Int :user_id
|
|
Int :pex_id
|
|
String :name
|
|
Int :span_duration
|
|
Int :repeated
|
|
end
|
|
puts "Migrated recalls".green
|
|
end
|
|
|
|
puts "End migration".on_blue
|
|
CODE_VERSION = `git tag`.split("\n").map{ |str| Utils::Semver.new(str) }.sort.last.to_s
|
|
CODE_DATE=`git show #{CODE_VERSION} --pretty="format:%as"`.split("\n").first
|
|
DB[:meta].update(code_version: CODE_VERSION)
|
|
DB[:meta].update(code_date: CODE_DATE)
|
|
puts "Set code version to #{CODE_VERSION}".green
|
|
puts "Set code date to #{CODE_DATE}".green
|