Class: Migrate::Storage::Postgres
- Inherits:
-
DB
- Object
- DB
- Migrate::Storage::Postgres
show all
- Defined in:
- lib/migrate/storage/postgres.rb
Instance Attribute Summary
Attributes inherited from DB
#config
Instance Method Summary
collapse
Methods inherited from DB
#current_version, #delete, #get_migration, #list_migrations, #log_down, #log_up, #lowest_version, #migrations_range, #new_migration, #next_version, #prev_version, #print, #type
Constructor Details
#initialize(*args) ⇒ Postgres
Returns a new instance of Postgres.
6
7
8
9
10
11
12
13
14
|
# File 'lib/migrate/storage/postgres.rb', line 6
def initialize(*args)
super
@conn = PG.connect({
dbname: @config.database,
host: @config.host,
user: @config.user,
password: @config.password,
})
end
|
Instance Method Details
#create_tables ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/migrate/storage/postgres.rb', line 16
def create_tables
Log.info("Creating version table")
self.exec_sql " CREATE TABLE \#{@config.version_info}\n (\n version SERIAL PRIMARY KEY NOT NULL,\n description TEXT,\n created_date TIMESTAMP WITH TIME ZONE NOT NULL,\n last_up TIMESTAMP WITH TIME ZONE,\n last_down TIMESTAMP WITH TIME ZONE\n );\n CREATE UNIQUE INDEX \#{@config.version_info}_version_uindex ON \#{@config.version_info} (version);\n\n CREATE TABLE \#{@config.version_number}\n (\n version INT PRIMARY KEY NOT NULL\n );\n\n INSERT INTO \#{@config.version_number} VALUES(0);\n eos\n Log.success(\"Version table created\")\nend\n"
|
#exec_sql(sql) ⇒ Object
48
49
50
|
# File 'lib/migrate/storage/postgres.rb', line 48
def exec_sql(sql)
@tx.exec sql
end
|
40
41
42
43
44
45
46
|
# File 'lib/migrate/storage/postgres.rb', line 40
def (results)
if results && results.count > 0
results[0]["version"]
else
raise VersionNotFound
end
end
|
#has_tx ⇒ Object
52
53
54
|
# File 'lib/migrate/storage/postgres.rb', line 52
def has_tx
@tx != nil
end
|
#tx ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/migrate/storage/postgres.rb', line 56
def tx
if has_tx
yield
else
begin
@conn.transaction do |tx|
@tx = tx
yield
end
ensure
@tx = nil
end
end
end
|