An unofficial, mostly Bitwarden-compatible API server written in Ruby (Sinatra and ActiveRecord)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

migrate_to_ar: cope with the database path changing

Assume we just have a database at the old path
db/production.sqlite3, then dump that to the YAML file.

Then run the migration which will create the new, blank database at
db/production/production.sqlite3, which we will then import the YAML
file to.

Fixes #63

+56 -35
+16 -14
AR-MIGRATE.md
··· 7 7 you need to do the following steps to migrate the data and generate the new 8 8 table structures. 9 9 10 - Even though the migration script will create a backup of your database, it is 11 - probably best to create a backup yourself. 10 + Even though the migration script will import to a new database file at a 11 + different path, it is probably best to create a backup yourself. 12 12 You can also copy the `db/production.sqlite3` to your local machine and do the 13 13 migration there. 14 - 15 14 After a successful migration you'd have to copy the updated database file back 16 15 to the production machine. 17 16 ··· 27 26 28 27 bundle exec ruby tools/migrate_to_ar.rb -e production 29 28 30 - The -e switch allows you to select the correct database environment from 29 + The `-e` switch allows you to select the correct database environment from 31 30 `db/config.yml`. 31 + 32 32 The migration script will: 33 33 34 - - dump the contents of the database to a YAML file 35 - - rename the original database file to `production.sqlite3.#{Time.now.to_i}` 36 - - create the database using ActiveRecord migrations 37 - - load the contents from the dump file 34 + - dump the contents of the old database (most likely at 35 + `db/production.sqlite`) to a temporary YAML file 36 + - create the new database at `db/production/production.sqlite3` using 37 + ActiveRecord migrations 38 + - import the contents from the dump file 38 39 - remove the dump file 39 40 40 - Now your data is completely migrated and the library will now use ActiveRecord 41 - to handle anything database related. 41 + Now your data is completely migrated into a new database at the new recommended 42 + path, and the library will now use ActiveRecord to handle anything database 43 + related. 42 44 43 - Note: The ActiveRecord migration also defaults to putting the production 44 - database files in `db/production/` instead of just `db/`, which allows for 45 - a separate user to be able to write to the SQLite file without writing to 46 - `db/config.yml` and `db/migrate/` files. 45 + It is recommended to follow the 46 + [initial installation instructions](https://github.com/jcs/rubywarden#usage) 47 + to create a new, unprivileged user to own the new `db/production/` database 48 + and run the server.
+40 -21
tools/migrate_to_ar.rb
··· 1 - # Tool to migrate old. "manually managed" database to active-record + standalone_migrations 2 - ## Necessary steps 3 - # 1. Create backup of old database 4 - # 2. Initialize new database with AR 5 - # 3. Migrate data from old to ar-db 6 - # 4. Profit! 1 + # see https://github.com/jcs/rubywarden/blob/master/AR-MIGRATE.md 7 2 8 - require 'getoptlong' 3 + require "fileutils" 4 + require "getoptlong" 5 + require "tempfile" 6 + require "yaml_db" 9 7 10 8 def usage 11 9 puts "usage: #{$PROGRAM_NAME} -e development" ··· 28 26 29 27 usage unless environment 30 28 31 - 32 - require 'yaml_db' 33 - require 'fileutils' 34 29 require File.realpath(File.dirname(__FILE__) + "/../lib/rubywarden.rb") 30 + 35 31 ActiveRecord::Base.remove_connection 36 32 37 - data_file = "db/dump.yml" 33 + dbconfig = YAML.load(File.read(File.realpath(__dir__ + "/../db/config.yml"))) 34 + 35 + # if a file exists at the new path, some kind of migration has already been 36 + # done so bail out 37 + newdb = dbconfig[environment]["database"] 38 + if File.exists?(newdb) 39 + raise "a file already exists at #{newdb}, has a migration already taken place?" 40 + end 41 + 42 + olddb = File.realpath(__dir__ + "/../db/production.sqlite3") 43 + if !olddb || !File.exists?(olddb) 44 + raise "no file at #{olddb} to migrate" 45 + end 38 46 39 - dbconfig = YAML.load(File.read('db/config.yml')) 40 - ActiveRecord::Base.establish_connection dbconfig[environment] 47 + # point a temporary config at the old db path so we can dump it 48 + tmpconfig = dbconfig[environment].dup 49 + tmpconfig["database"] = olddb 50 + ActiveRecord::Base.establish_connection tmpconfig 41 51 42 52 # select only tables for defined models 43 53 class YamlDb::SerializationHelper::Dump 44 54 def self.tables 45 - #ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations', 'schema_version'].include?(table) }.sort 46 - ObjectSpace.each_object(Class).select {|k| k < DBModel}.map {|k| k.table_name } 55 + ObjectSpace.each_object(Class).select{|k| k < DBModel}.map{|k| k.table_name } 47 56 end 48 57 end 49 58 50 - YamlDb::SerializationHelper::Base.new(YamlDb::Helper).dump(data_file) 59 + dump_file = Tempfile.new("rubywarden-migrate") 51 60 61 + puts "dumping old database to #{dump_file.path}" 62 + YamlDb::SerializationHelper::Base.new(YamlDb::Helper).dump(dump_file.path) 52 63 ActiveRecord::Base.remove_connection 53 64 54 - FileUtils.mv dbconfig[environment]["database"], "#{dbconfig[environment]["database"]}.#{Time.now.to_i}" 55 - 56 - system "rake db:migrate RACK_ENV=#{environment}" 65 + puts "creating new database at #{dbconfig[environment]["database"]}" 66 + system("rake", "db:migrate", "RACK_ENV=#{environment}") 57 67 68 + puts "importing old database dump" 58 69 ActiveRecord::Base.establish_connection dbconfig[environment] 59 - YamlDb::SerializationHelper::Base.new(YamlDb::Helper).load(data_file) 60 - FileUtils.rm data_file 70 + YamlDb::SerializationHelper::Base.new(YamlDb::Helper).load(dump_file.path) 71 + 72 + puts "deleting dump file" 73 + dump_file.unlink 61 74 62 75 # reset created_at / updated_at from seconds since epoch to actual datetime for ar magic 63 76 DBModel.record_timestamps = false ··· 67 80 end 68 81 end 69 82 DBModel.record_timestamps = true 83 + 84 + newdb = File.realpath(__dir__ + "/../" + dbconfig[environment]["database"]) 85 + puts "you may wish to delete the old database at #{newdb}" 86 + 87 + puts "you may also wish to create a new, unprivileged user to run the" 88 + puts "rubywarden server and own the db/production/ directory"