Class: Webhookdb::Tasks::DB

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/webhookdb/tasks/db.rb

Instance Method Summary collapse

Constructor Details

#initializeDB

Returns a new instance of DB.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webhookdb/tasks/db.rb', line 11

def initialize
  super()
  namespace :db do
    desc "Drop all tables in the public schema."
    task :drop_tables do
      require "webhookdb/postgres"
      Webhookdb::Postgres.load_superclasses
      Webhookdb::Postgres.each_model_superclass do |sc|
        sc.db[:pg_tables].where(schemaname: "public").each do |tbl|
          self.exec(sc.db, "DROP TABLE #{tbl[:tablename]} CASCADE")
        end
      end
    end

    desc "Remove all data from application schemas"
    task :wipe do
      require "webhookdb/postgres"
      Webhookdb::Postgres.load_superclasses
      Webhookdb::Postgres.each_model_class do |c|
        c.truncate(cascade: true)
      end
    end

    desc "Run migrations (rake db:migrate[<target>] to go to a specific version)"
    task :migrate, [:version] do |_, args|
      require "webhookdb/postgres"
      Webhookdb::Postgres.load_superclasses
      Webhookdb::Postgres.run_all_migrations(target: args[:version]&.to_i)
    end

    desc "Re-create the database tables. Drop tables and migrate."
    task reset: ["db:drop_tables", "db:migrate"]

    task :drop_replication_databases do
      require "webhookdb/postgres"
      Webhookdb::Postgres.load_superclasses
      Webhookdb::Postgres.each_model_superclass do |c|
        c.db[:pg_database].grep(:datname, "adb%").select(:datname).all.each do |row|
          c.db << "DROP DATABASE #{row[:datname]}"
        end
      end
    end

    task drop_tables_and_replication_databases: ["db:drop_tables", "db:drop_replication_databases"]

    task wipe_tables_and_drop_replication_databases: ["db:wipe", "db:drop_replication_databases"]

    task :lookup_org_admin_url, [:org_id] do |_, args|
      (orgid = args[:org_id]) or raise "Must provide org id as first argument"
      require "webhookdb"
      Webhookdb.load_app
      org_cond = orgid.match?(/^\d$/) ? orgid.to_i : {key: orgid}
      (org = Webhookdb::Organization[org_cond]) or raise "Org #{orgid} does not exist"
      u = org.admin_connection_url
      raise "Org #{orgid} has no connection url yet" if u.blank?
      print(u)
    end
  end
end

Instance Method Details

#exec(db, cmd) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/webhookdb/tasks/db.rb', line 71

def exec(db, cmd)
  print cmd
  begin
    db.execute(cmd)
    print "\n"
  rescue StandardError
    print " (error)\n"
    raise
  end
end