| | 477 | def recreate_database(name) #:nodoc: |
|---|
| | 478 | drop_database(name) |
|---|
| | 479 | create_database(name) |
|---|
| | 480 | end |
|---|
| | 481 | |
|---|
| | 482 | # Create a new PostgreSQL database. Options include :owner, :template, |
|---|
| | 483 | # :encoding, :tablespace, and :connection_limit (note that MySQL uses |
|---|
| | 484 | # :charset while PostgreSQL uses :encoding). |
|---|
| | 485 | # |
|---|
| | 486 | # Example: |
|---|
| | 487 | # create_database config[:database], config |
|---|
| | 488 | # create_database 'foo_development', :encoding => 'unicode' |
|---|
| | 489 | def create_database(name, options = {}) |
|---|
| | 490 | options = options.reverse_merge(:encoding => "utf8") |
|---|
| | 491 | |
|---|
| | 492 | option_string = options.symbolize_keys.sum do |key, value| |
|---|
| | 493 | case key |
|---|
| | 494 | when :owner |
|---|
| | 495 | " OWNER = '#{value}'" |
|---|
| | 496 | when :template |
|---|
| | 497 | " TEMPLATE = #{value}" |
|---|
| | 498 | when :encoding |
|---|
| | 499 | " ENCODING = '#{value}'" |
|---|
| | 500 | when :tablespace |
|---|
| | 501 | " TABLESPACE = #{value}" |
|---|
| | 502 | when :connection_limit |
|---|
| | 503 | " CONNECTION LIMIT = #{value}" |
|---|
| | 504 | else |
|---|
| | 505 | "" |
|---|
| | 506 | end |
|---|
| | 507 | end |
|---|
| | 508 | |
|---|
| | 509 | execute "CREATE DATABASE #{name}#{option_string}" |
|---|
| | 510 | end |
|---|
| | 511 | |
|---|
| | 512 | # Drops a PostgreSQL database |
|---|
| | 513 | # |
|---|
| | 514 | # Example: |
|---|
| | 515 | # drop_database 'matt_development' |
|---|
| | 516 | def drop_database(name) #:nodoc: |
|---|
| | 517 | execute "DROP DATABASE IF EXISTS #{name}" |
|---|
| | 518 | end |
|---|
| | 519 | |
|---|
| | 520 | |
|---|