/ build / scripts / migrate_db.rb
migrate_db.rb
 1  require 'logger'
 2  require 'active_support/inflector'
 3  
 4  if $0 =~ /scripts[\/\\]rb[\/\\]migrate_db.rb$/
 5    # This script runs in two contexts: build/run as a part of development, and
 6    # setup-database.(sh|bat) from the distribution zip file.  Allow for both.
 7    require_relative '../../launcher/launcher_init'
 8  end
 9  
10  require 'config/config-distribution'
11  require "db/db_migrator"
12  
13  if AppConfig[:db_url] =~ /jdbc:mysql/
14    require "db/sequel_mysql_timezone_workaround"
15  end
16  
17  
18  if ARGV.length > 0 and ARGV[0] == "nuke"
19    if (AppConfig[:db_url] =~ /jdbc:derby:(.*?);.*aspacedemo=true$/)
20      dir = $1
21  
22      if File.directory?(dir) and File.exist?(File.join(dir, "seg0"))
23        puts "Nuking demo database: #{dir}"
24        sleep(5)
25        FileUtils.rm_rf(dir)
26        exit
27      end
28    end
29  end
30  
31  
32  begin
33    migration_logger = Logger.new($stderr)
34  
35    # Just log messages relating to the migration.  Otherwise we get a full SQL
36    # trace...
37    def migration_logger.error(*args)
38      unless args.to_s =~ /SCHEMA_INFO.*does not exist/
39        super
40      end
41    end
42  
43    def migration_logger.info(*args)
44      if args[0].is_a?(String) && args[0] =~ /applying migration/
45        super
46      end
47    end
48  
49    Sequel.connect(AppConfig[:db_url],
50                   :max_connections => 1,
51                   :loggers => [migration_logger]) do |db|
52      if ARGV.length > 0 and ARGV[0] == "nuke"
53        DBMigrator.nuke_database(db)
54  
55        indexer_state = File.join(AppConfig[:data_directory], "indexer_state")
56        if Dir.exist? (indexer_state)
57          FileUtils.rm_rf(indexer_state)
58        end
59      else
60        puts "Running migrations against #{AppConfig[:db_url_redacted]}"
61        DBMigrator.setup_database(db)
62        puts "All done."
63      end
64    end
65  rescue Sequel::AdapterNotFound => e
66  
67    if AppConfig[:db_url] =~ /mysql/
68      libdir = File.expand_path(File.join(File.dirname($0), "..", "..", "lib"))
69  
70      puts <<EOF
71  
72  You have configured ArchivesSpace to use MySQL but seem to be missing the MySQL
73  JDBC driver (#{e}).
74  
75  Please download the latest version of 'mysql-connector-j-X.Y.Z.jar' and place
76  it in the following directory:
77  
78    #{libdir}
79  
80  You can find the latest version at the following URL:
81  
82    https://mvnrepository.com/artifact/com.mysql/mysql-connector-j
83  
84  Once you have installed the MySQL connector, please re-run this script.
85  
86  EOF
87    else
88      raise $!
89    end
90  end