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, #highest_version, #list_migrations, #log_down, #log_up, #lowest_version, #migrations_from, #migrations_range, #new_migration, #next_version, #prev_version, #print, #type, #version_exists?
Constructor Details
#initialize(*args) ⇒ Postgres
Returns a new instance of Postgres.
7
8
9
10
11
12
13
14
15
|
# File 'lib/migrate/storage/postgres.rb', line 7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/migrate/storage/postgres.rb', line 37
def create_tables
Log.info("Creating version table")
self.exec_sql <<-eos
CREATE TABLE #{@config.version_info}
(
version SERIAL PRIMARY KEY NOT NULL,
description TEXT,
created_date TIMESTAMP WITH TIME ZONE NOT NULL,
last_up TIMESTAMP WITH TIME ZONE,
last_down TIMESTAMP WITH TIME ZONE
);
CREATE UNIQUE INDEX #{@config.version_info}_version_uindex ON #{@config.version_info} (version);
CREATE TABLE #{@config.version_number}
(
version INT PRIMARY KEY NOT NULL
);
INSERT INTO #{@config.version_number} VALUES(0);
eos
Log.success("Version table created")
end
|
#exec_sql(sql) ⇒ Object
69
70
71
|
# File 'lib/migrate/storage/postgres.rb', line 69
def exec_sql(sql)
@tx.exec sql
end
|
61
62
63
64
65
66
67
|
# File 'lib/migrate/storage/postgres.rb', line 61
def (results)
if results && results.count > 0
results[0]["version"]
else
raise VersionNotFound
end
end
|
#has_tx ⇒ Object
73
74
75
|
# File 'lib/migrate/storage/postgres.rb', line 73
def has_tx
@tx != nil
end
|
#tables_exists? ⇒ Boolean
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/migrate/storage/postgres.rb', line 17
def tables_exists?
vi = self.exec_sql <<-eos
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = '#{@config.version_info}'
);
eos
vn = self.exec_sql <<-eos
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = '#{@config.version_number}'
);
eos
vi[0]["exists"].to_b && vn[0]["exists"].to_b
end
|
#tx ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/migrate/storage/postgres.rb', line 77
def tx
if has_tx
yield
else
begin
@conn.transaction do |tx|
@tx = tx
yield
end
ensure
@tx = nil
end
end
end
|