db_schema-reader-postgres 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +6 -0
- data/README.md +14 -8
- data/lib/db_schema/reader/postgres.rb +28 -24
- data/lib/db_schema/reader/postgres/table.rb +1 -1
- data/lib/db_schema/reader/postgres/version.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f12b3dcea5dc1a301418825e2b8da015e078c520195e42517da991830d3bc22
|
4
|
+
data.tar.gz: 52778450688b800dccf4934525fc567f0d99a9b426095298f198728cb827a9e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c222884746207d2c46ab112ed40b7d78b4de10feef39dbeee650fa0b40c58bd00307c121ce1fa550391379aa61e80a4d78275e7c2782394f4e91153be986997
|
7
|
+
data.tar.gz: 7ec7a253c3ca0c576b6935df764a24456994b8a26a045d5614445164399dff84568c424d11955ab4560542eef9ec8d340c215ee82145c7b585b27c38b5cf003a
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# DbSchema::Reader::Postgres
|
1
|
+
# DbSchema::Reader::Postgres [![Build Status](https://travis-ci.org/db-schema/reader-postgres.svg?branch=master)](https://travis-ci.org/db-schema/reader-postgres)
|
2
2
|
|
3
3
|
DbSchema::Reader::Postgres is a library for reading the database
|
4
4
|
structure from PostgreSQL.
|
@@ -26,7 +26,7 @@ $ gem install db_schema-reader-postgres
|
|
26
26
|
|
27
27
|
First you need a Sequel connection object with `:pg_enum` and `:pg_array`
|
28
28
|
Sequel extensions enabled; once you have that object just pass it to
|
29
|
-
|
29
|
+
`DbSchema::Reader::Postgres.new` to construct the reader:
|
30
30
|
|
31
31
|
``` ruby
|
32
32
|
connection = Sequel.connect(adapter: 'postgres', database: 'db_schema_test').tap do |db|
|
@@ -34,24 +34,30 @@ connection = Sequel.connect(adapter: 'postgres', database: 'db_schema_test').tap
|
|
34
34
|
db.extension :pg_array
|
35
35
|
end
|
36
36
|
|
37
|
-
DbSchema::Reader::Postgres.
|
37
|
+
reader = DbSchema::Reader::Postgres.new(connection)
|
38
|
+
```
|
39
|
+
|
40
|
+
You can call `#read_schema` to get the full database schema definition:
|
41
|
+
|
42
|
+
``` ruby
|
43
|
+
reader.read_schema
|
38
44
|
# => #<DbSchema::Definitions::Schema ...>
|
39
45
|
```
|
40
46
|
|
41
|
-
Other useful methods are
|
47
|
+
Other useful methods are `#read_tables`, `#read_table`, `#read_enums` & `#read_extensions`;
|
42
48
|
they return definitions of respective parts of the database schema:
|
43
49
|
|
44
50
|
``` ruby
|
45
|
-
|
51
|
+
reader.read_tables
|
46
52
|
# => [#<DbSchema::Definitions::Table ...>, #<DbSchema::Definitions::Table ...>, ...]
|
47
53
|
|
48
|
-
|
54
|
+
reader.read_table(:users)
|
49
55
|
# => #<DbSchema::Definitions::Table name=:users ...>
|
50
56
|
|
51
|
-
|
57
|
+
reader.read_enums
|
52
58
|
# => [#<DbSchema::Definitions::Enum ...>, #<DbSchema::Definitions::Enum ...>, ...]
|
53
59
|
|
54
|
-
|
60
|
+
reader.read_extensions
|
55
61
|
# => [#<DbSchema::Definitions::Extension ...>, #<DbSchema::Definitions::Extension ...>, ...]
|
56
62
|
```
|
57
63
|
|
@@ -4,7 +4,7 @@ require_relative 'postgres/version'
|
|
4
4
|
|
5
5
|
module DbSchema
|
6
6
|
module Reader
|
7
|
-
|
7
|
+
class Postgres
|
8
8
|
ENUMS_QUERY = <<-SQL.freeze
|
9
9
|
SELECT t.typname AS name,
|
10
10
|
array_agg(e.enumlabel ORDER BY e.enumsortorder) AS values
|
@@ -20,35 +20,39 @@ SELECT extname
|
|
20
20
|
WHERE extname != 'plpgsql'
|
21
21
|
SQL
|
22
22
|
|
23
|
-
|
24
|
-
def read_schema(connection)
|
25
|
-
Definitions::Schema.new(
|
26
|
-
tables: read_tables(connection),
|
27
|
-
enums: read_enums(connection),
|
28
|
-
extensions: read_extensions(connection)
|
29
|
-
)
|
30
|
-
end
|
23
|
+
attr_reader :connection
|
31
24
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
25
|
+
def initialize(connection)
|
26
|
+
@connection = connection
|
27
|
+
end
|
28
|
+
|
29
|
+
def read_schema
|
30
|
+
Definitions::Schema.new(
|
31
|
+
tables: read_tables,
|
32
|
+
enums: read_enums,
|
33
|
+
extensions: read_extensions
|
34
|
+
)
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
37
|
+
def read_tables
|
38
|
+
connection.tables.map do |table_name|
|
39
|
+
read_table(table_name)
|
40
40
|
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def read_table(table_name)
|
44
|
+
Table.new(connection, table_name).read
|
45
|
+
end
|
41
46
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
47
|
+
def read_enums
|
48
|
+
connection[ENUMS_QUERY].map do |enum_data|
|
49
|
+
Definitions::Enum.new(enum_data[:name].to_sym, enum_data[:values].map(&:to_sym))
|
46
50
|
end
|
51
|
+
end
|
47
52
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
53
|
+
def read_extensions
|
54
|
+
connection[EXTENSIONS_QUERY].map do |extension_data|
|
55
|
+
Definitions::Extension.new(extension_data[:extname].to_sym)
|
52
56
|
end
|
53
57
|
end
|
54
58
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_schema-reader-postgres
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vsevolod Romashov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -207,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
207
207
|
version: '0'
|
208
208
|
requirements: []
|
209
209
|
rubyforge_project:
|
210
|
-
rubygems_version: 2.6
|
210
|
+
rubygems_version: 2.7.6
|
211
211
|
signing_key:
|
212
212
|
specification_version: 4
|
213
213
|
summary: Database schema reader for PostgreSQL
|