Class: ProconBypassMan::Web::Db
- Inherits:
-
Object
- Object
- ProconBypassMan::Web::Db
- Defined in:
- lib/procon_bypass_man/web/db.rb
Class Method Summary collapse
- .db ⇒ Object
- .migrate_if_pending_migration(migrations_path: File.join(ProconBypassMan::Web.gem_root, 'lib', 'procon_bypass_man/web', 'migration', "/*.sql")) ⇒ Object
- .recreate! ⇒ Object
Class Method Details
.db ⇒ Object
4 5 6 7 |
# File 'lib/procon_bypass_man/web/db.rb', line 4 def self.db # TODO connection cache SQLite3::Database.new(ProconBypassMan::Web.config.db_path) end |
.migrate_if_pending_migration(migrations_path: File.join(ProconBypassMan::Web.gem_root, 'lib', 'procon_bypass_man/web', 'migration', "/*.sql")) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/procon_bypass_man/web/db.rb', line 14 def self.migrate_if_pending_migration(migrations_path: File.join(ProconBypassMan::Web.gem_root, 'lib', 'procon_bypass_man/web', 'migration', "/*.sql")) db.execute " create table if not exists \"schema_migrations\" (\"version\" varchar not null primary key)\n SQL\n\n Dir.glob(migrations_path).each do |path|\n if /^(\\d+)_[\\w.]+$/ =~ Pathname.new(path).basename.to_s\n version = $1\n rows = db.execute(\"select * from schema_migrations where version = ?\", version)\n if rows.size == 0\n sql = File.read(path)\n db.execute(sql)\n db.execute(\"insert into schema_migrations (version) values (?)\", [version])\n end\n end\n end\nend\n" |
.recreate! ⇒ Object
9 10 11 12 |
# File 'lib/procon_bypass_man/web/db.rb', line 9 def self.recreate! FileUtils.rm_rf(ProconBypassMan::Web.config.db_path) SQLite3::Database.new(ProconBypassMan::Web.config.db_path) end |