Browse Source

script: improve db migration

develop
Arthur POULET 9 months ago
parent
commit
d1fae9bf63
Signed by: Sceptique GPG Key ID: C108A668349EE601
  1. 4
      README.md
  2. 13
      Rakefile
  3. 13
      init/load_env.rb
  4. 56
      init/migrate_db.rb

4
README.md

@ -46,7 +46,7 @@ Since I suck with SGDB administration, this is a sample of what you may do.
psql -U postgres postgres -c "CREATE USER root WITH PASSWORD 'toor' SUPERUSER;"
psql -U postgres postgres -c "CREATE DATABASE life_pex"
echo LIFEPEX_DB=postgres://root:toor@localhost/life_pex >> .env.local
./init/database.rb
rake db:migrate
```
### Start
@ -91,7 +91,7 @@ editor .env.test.local # don't forget to set a new database !!!
Then init the database
```
LIFEPEX_ENV=test ./init/database.rb
LIFEPEX_ENV=test rake db:migrate
```
Then if you want to run the test, simply type `rake test` (you will need the startup env variable to be set first).

13
Rakefile

@ -5,4 +5,17 @@ Rake::TestTask.new do |t|
t.pattern = "test/*_test.rb"
end
namespace "db" do
desc "Migrate the database to the lasted schema"
task "migrate" do
require_relative "./init/migrate_db"
end
desc "Reset all tables, schema, data"
task "reset" do
require_relative "./init/load_env"
DB.tables.each {|t| DB.drop_table t }
end
end
task default: :test

13
init/load_env.rb

@ -0,0 +1,13 @@
require "sequel"
require "colorize"
require "dotenv"
Dotenv.load(".env.local", ".env")
require_relative "../src/utils/semver.rb"
DB = Sequel.connect ENV["LIFEPEX_DB"]
if ENV["environment"] == "debug"
require "pry"
binding.pry
end

56
init/database.rb → init/migrate_db.rb

@ -1,41 +1,30 @@
#!/usr/bin/env ruby
require "sequel"
require "colorize"
require "dotenv"
Dotenv.load(".env.local", ".env")
require_relative "../src/utils/semver.rb"
DB = Sequel.connect ENV["LIFEPEX_DB"]
if ENV["environment"] == "debug"
require "pry"
binding.pry
end
require_relative "./load_env"
def current_version
DB[:meta].first[:version] rescue 0
end
def migrate(version, &block)
puts "Check migration #{version}"
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}"
puts "Successfuly set version #{version}".green
puts message.green if message
rescue => err
puts err.message.red
puts err.message.on_red
exit 1
end
else
puts "Already migrated #{version}".blue
puts "Already migrated #{version}".yellow
end
end
migrate 1 do
migrate 1, "Initialized database" do
DB.create_table :meta do
primary_key :id
Int :version
@ -60,33 +49,29 @@ migrate 1 do
String :username
String :hashed_password
end rescue puts "users already exists".yellow
puts "Initialized database".green
end
migrate 2 do
migrate 2, "Add pex categories" do
DB.alter_table :pexs do
add_column :category, String
end rescue puts "pexs.category already exists".yellow
puts "Migrated categories".green
end
migrate 3 do
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)
puts "Migrated pex belonging to user".green
end
migrate 4 do
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: '')
puts "Migrated pex default category".green
end
migrate 5 do
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
@ -99,18 +84,20 @@ migrate 6 do
end rescue puts "meta.code_date already exists".yellow
end
migrate 7 do
DB[:pexs].each { |pex| DB[:pexs].where(id: pex[:id]).update(category: pex[:category].to_s.downcase) }
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 do
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 do
migrate 9, "Add achievements" do
DB.create_table :achievements do
primary_key :id
Int :user_id
@ -121,14 +108,13 @@ migrate 9 do
end
end
migrate 10 do
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
puts "Migrated pex flag for bookmarks".green
end
migrate 11 do
migrate 11, "Add recalls" do
DB.create_table :recalls do
primary_key :id
Int :user_id
@ -140,7 +126,7 @@ migrate 11 do
puts "Migrated recalls".green
end
puts "End migration".green
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)
Loading…
Cancel
Save