Module: Postshift::Schema
- Defined in:
- lib/postshift/schema.rb
Constant Summary collapse
- FILENAME =
'postshift_schema.sql'.freeze
- ADMIN_UTILITIES =
%w(v_generate_tbl_ddl v_generate_view_ddl).freeze
Class Method Summary collapse
- .admin_uilities_exists? ⇒ Boolean
- .create_admin_utilities! ⇒ Object
- .ddl_results(ddl_sql) ⇒ Object
- .dump ⇒ Object
- .dump_sql ⇒ Object
- .ensure_admin_schema ⇒ Object
- .generate_ddl_sql(table_name) ⇒ Object
- .output_location ⇒ Object
- .remove_admin_utilities! ⇒ Object
- .restore ⇒ Object
- .schemas ⇒ Object
- .tbl_ddl_sql ⇒ Object
- .view_ddl_sql ⇒ Object
Class Method Details
.admin_uilities_exists? ⇒ Boolean
24 25 26 27 28 29 30 31 32 |
# File 'lib/postshift/schema.rb', line 24 def self.admin_uilities_exists? ensure_admin_schema ADMIN_UTILITIES.each do |table_name| Postshift.connection.exec("SELECT 'admin.#{table_name}'::regclass;") end true rescue PG::UndefinedTable false end |
.create_admin_utilities! ⇒ Object
10 11 12 13 14 15 |
# File 'lib/postshift/schema.rb', line 10 def self.create_admin_utilities! ensure_admin_schema ADMIN_UTILITIES.each do |table_name| Postshift.connection.exec(generate_ddl_sql(table_name)) end end |
.ddl_results(ddl_sql) ⇒ Object
64 65 66 |
# File 'lib/postshift/schema.rb', line 64 def self.ddl_results(ddl_sql) Postshift.connection.exec(ddl_sql, schemas) end |
.dump ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/postshift/schema.rb', line 39 def self.dump File.open(output_location, 'w+') do |file| ddl_results(tbl_ddl_sql).each_row do |row| file.puts(row) end ddl_results(view_ddl_sql).each_row do |row| file.puts(row) end end end |
.dump_sql ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/postshift/schema.rb', line 50 def self.dump_sql if File.exist?(output_location) File.read(output_location) else puts 'Postshift Schema Dump file does not exist. Run task postshift:schema:dump' false end end |
.ensure_admin_schema ⇒ Object
6 7 8 |
# File 'lib/postshift/schema.rb', line 6 def self.ensure_admin_schema Postshift.connection.exec('CREATE SCHEMA IF NOT EXISTS admin') end |
.generate_ddl_sql(table_name) ⇒ Object
34 35 36 37 |
# File 'lib/postshift/schema.rb', line 34 def self.generate_ddl_sql(table_name) path = File.join(Postshift.root, 'lib', 'tasks', "#{table_name}.sql") File.open(path).read end |
.output_location ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/postshift/schema.rb', line 68 def self.output_location if defined?(Rails) File.join(Rails.root, 'db', FILENAME) else base_path = File.join(Postshift.root, 'tmp') Dir.mkdir(base_path) unless Dir.exist?(base_path) File.join(base_path, FILENAME) end end |
.remove_admin_utilities! ⇒ Object
17 18 19 20 21 22 |
# File 'lib/postshift/schema.rb', line 17 def self.remove_admin_utilities! ensure_admin_schema ADMIN_UTILITIES.each do |table_name| Postshift.connection.exec("DROP VIEW IF EXISTS admin.#{table_name}") end end |
.restore ⇒ Object
59 60 61 62 |
# File 'lib/postshift/schema.rb', line 59 def self.restore sql = dump_sql Postshift.connection.exec(sql) if sql.present? end |
.schemas ⇒ Object
78 79 80 |
# File 'lib/postshift/schema.rb', line 78 def self.schemas %w(public) end |
.tbl_ddl_sql ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/postshift/schema.rb', line 82 def self.tbl_ddl_sql " SELECT ddl\n FROM admin.v_generate_tbl_ddl\n WHERE schemaname IN ($1)\n ORDER BY tablename ASC, seq ASC\n SQL\nend\n" |
.view_ddl_sql ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/postshift/schema.rb', line 91 def self.view_ddl_sql " SELECT ddl\n FROM admin.v_generate_view_ddl\n WHERE schemaname IN ($1)\n ORDER BY viewname ASC\n SQL\nend\n" |