Class: Communard::Commands
- Inherits:
-
Object
- Object
- Communard::Commands
- Extended by:
- Forwardable
- Defined in:
- lib/communard/commands.rb
Defined Under Namespace
Classes: Status
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #check_current ⇒ Object
- #create_database ⇒ Object
- #drop_database ⇒ Object
- #dump_schema ⇒ Object
-
#initialize(configuration) ⇒ Commands
constructor
A new instance of Commands.
- #load_schema ⇒ Object
- #migrate(target: nil) ⇒ Object
- #migrations_dir ⇒ Object
- #rollback(step: 1) ⇒ Object
- #run_without_database(query) ⇒ Object
- #schema_file ⇒ Object
- #seed ⇒ Object
- #seeds_file ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Commands
Returns a new instance of Commands.
17 18 19 20 |
# File 'lib/communard/commands.rb', line 17 def initialize(configuration) @configuration = configuration Sequel.extension :migration, :core_extensions end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/communard/commands.rb', line 8 def configuration @configuration end |
Instance Method Details
#check_current ⇒ Object
52 53 54 |
# File 'lib/communard/commands.rb', line 52 def check_current Sequel::Migrator.check_current(connection, migrations_dir) end |
#create_database ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/communard/commands.rb', line 22 def create_database return if adapter == "sqlite" run_without_database("CREATE DATABASE %{database_name}") rescue Sequel::DatabaseError => error if error..to_s =~ /database (.*) already exists/ configuration.default_logger.warn "Database #{$1} already exists." else raise end end |
#drop_database ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/communard/commands.rb', line 33 def drop_database if adapter == "sqlite" file = database_name File.rm(file) if File.exist?(file) else run_without_database("DROP DATABASE IF EXISTS %{database_name}") end end |
#dump_schema ⇒ Object
76 77 78 79 80 81 |
# File 'lib/communard/commands.rb', line 76 def dump_schema conn = configuration.silent_connection conn.extension :schema_dumper schema = conn.dump_schema_migration(same_db: configuration.same_db) schema_file.open("w") { |f| f.puts schema.gsub(/^\s+$/m, "").gsub(/:(\w+)=>/, '\1: ') } end |
#load_schema ⇒ Object
70 71 72 73 74 |
# File 'lib/communard/commands.rb', line 70 def load_schema migration = instance_eval(schema_file.read, schema_file..to_s, 1) conn = configuration.silent_connection migration.apply(conn, :up) end |
#migrate(target: nil) ⇒ Object
42 43 44 45 46 |
# File 'lib/communard/commands.rb', line 42 def migrate(target: nil) target = target.to_i if target.to_s.match?(/\A\d+\z/) migrator(target: target, current: nil).run dump_schema if target.nil? && configuration. end |
#migrations_dir ⇒ Object
119 120 121 |
# File 'lib/communard/commands.rb', line 119 def migrations_dir db_path.join("migrate") end |
#rollback(step: 1) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/communard/commands.rb', line 56 def rollback(step: 1) available = applied_migrations if available.size == 1 migrate(target: 0) else target = available[-step - 1] if target migrate(target: target.split(/_/, 2).first) else fail ArgumentError, "Cannot roll back to #{step}" end end end |
#run_without_database(query) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/communard/commands.rb', line 100 def run_without_database(query) opts = .dup database_name = opts.delete("database") if adapter == "postgres" opts["database"] = "postgres" opts["schema_search_path"] = "public" end conn = Sequel.connect(opts) conn.run(query % { database_name: database_name }) end |
#schema_file ⇒ Object
111 112 113 |
# File 'lib/communard/commands.rb', line 111 def schema_file db_path.join("schema.rb") end |
#seed ⇒ Object
48 49 50 |
# File 'lib/communard/commands.rb', line 48 def seed load seeds_file if seeds_file.exist? end |
#seeds_file ⇒ Object
115 116 117 |
# File 'lib/communard/commands.rb', line 115 def seeds_file db_path.join("seeds.rb") end |
#status ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/communard/commands.rb', line 83 def status results = Hash.new { |h, k| h[k] = Status.new(k, false, false) } available = Pathname.glob(migrations_dir.join("*.rb")).map(&:basename).map(&:to_s) available.each { |migration| results[migration].available = true } applied_migrations.each { |migration| results[migration].applied = true } $stdout.puts $stdout.puts "database: #{connection.opts.fetch(:database)}" $stdout.puts $stdout.puts " Status Migration ID Migration Name" $stdout.puts "--------------------------------------------------" results.values.sort.each do |result| $stdout.puts " %-7s %-15s %s" % [ result.status, result.id, result.name ] end $stdout.puts end |