-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.go
227 lines (192 loc) · 6.29 KB
/
pg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* pg2sqlite - Migrate tables from PostgresQL to SQLite
Copyright (C) Louis Brauer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"strings"
"github.com/jackc/pgx/v5"
"github.com/samber/lo"
)
var (
pgConn *pgx.Conn
)
type TableSchema struct {
Name string
Schema string
Cols []TableColumn
}
type TableColumn struct {
Name string
Type string
Ignored bool
PrimaryKey bool
FK bool
FKTable string
FKColumn string
}
func ValidatePG(connStr string) error {
var err error
pgConn, err = pgx.Connect(context.Background(), connStr)
if err != nil {
return fmt.Errorf("unable to connect to Postgres database: %w", err)
}
return nil
}
func FetchSchema(schemaname string, tablename string, ignoredColumns []string) (*TableSchema, error) {
// Fetch table columns
rows, err := pgConn.Query(context.Background(),
"SELECT column_name, data_type FROM information_schema.columns "+
"WHERE table_name = $1 "+
"AND table_schema = $2 "+
"ORDER BY ordinal_position", tablename, schemaname)
if err != nil {
return nil, fmt.Errorf("unable to fetch columns from Postgres table: %w", err)
}
defer rows.Close()
tableSchema := TableSchema{
Name: tablename,
Schema: schemaname,
}
colCount := 0
for rows.Next() {
var columnName string
var dataType string
if err := rows.Scan(&columnName, &dataType); err != nil {
return nil, fmt.Errorf("unable to scan columns from Postgres table: %w", err)
}
tableSchema.Cols = append(tableSchema.Cols, TableColumn{
Name: columnName,
Type: dataType,
Ignored: lo.Contains(ignoredColumns, columnName),
})
colCount++
}
if colCount == 0 {
return nil, fmt.Errorf("table %s doesn't exist in Postgres", tablename)
}
// Fetch tables primary key
pkcols, err := pgConn.Query(context.Background(),
`SELECT c.column_name, c.data_type
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
WHERE constraint_type = 'PRIMARY KEY' and tc.table_name = $1`, tablename)
if err != nil {
return nil, fmt.Errorf("unable to fetch primary key from Postgres table: %w", err)
}
defer pkcols.Close()
for pkcols.Next() {
var columnName string
var dataType string
if err := pkcols.Scan(&columnName, &dataType); err != nil {
return nil, fmt.Errorf("unable to scan primary key from Postgres table: %w", err)
}
for i, col := range tableSchema.Cols {
if col.Name == columnName {
tableSchema.Cols[i].PrimaryKey = true
}
}
}
// Fetch table foreign keys
fkcols, err := pgConn.Query(context.Background(),
`SELECT
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name=$1
AND tc.table_schema = $2`, tablename, schemaname)
if err != nil {
return nil, fmt.Errorf("unable to fetch foreign keys from Postgres table: %w", err)
}
defer fkcols.Close()
for fkcols.Next() {
var columnName string
var foreignTableName string
var foreignColumnName string
if err := fkcols.Scan(&columnName, &foreignTableName, &foreignColumnName); err != nil {
return nil, fmt.Errorf("unable to scan foreign key from Postgres table: %w", err)
}
for i, col := range tableSchema.Cols {
if col.Name == columnName {
tableSchema.Cols[i].FK = true
tableSchema.Cols[i].FKTable = foreignTableName
tableSchema.Cols[i].FKColumn = foreignColumnName
}
}
}
return &tableSchema, nil
}
func LoadData(schema *TableSchema, out chan []interface{}) error {
colListArray := lo.FilterMap(schema.Cols, func(col TableColumn, index int) (string, bool) {
if col.Ignored {
return "", false
}
fmt.Println(col.Type)
return lo.
// JSON[b] columns
If(strings.HasPrefix(col.Type, "json") || col.Type == "uuid", fmt.Sprintf(`"%s"::text`, col.Name)).
// ARRAY columns
ElseIf(strings.ToLower(col.Type) == "array", fmt.Sprintf(`array_to_json("%s")::text`, col.Name)).
// Everything else
Else(fmt.Sprintf(`"%s"`, col.Name)), true
})
sqlStmt := fmt.Sprintf("SELECT %s FROM %s.%s T", strings.Join(colListArray, ", "), formatTableName(schema.Schema), formatTableName(schema.Name))
fmt.Println("Loading data with this statement:")
fmt.Println(sqlStmt)
fmt.Println()
rows, err := pgConn.Query(context.Background(), sqlStmt)
if err != nil {
return fmt.Errorf("unable to load data: %w", err)
}
defer rows.Close()
rowCounter := 0
for rows.Next() {
vals, err := rows.Values()
if err != nil {
return fmt.Errorf("unable to get row values: %w", err)
}
out <- vals
rowCounter++
}
close(out)
if rowCounter == 0 {
return fmt.Errorf("no rows in source table found")
}
return nil
}
func EstimateRows(tablename string) (uint64, error) {
rows, err := pgConn.Query(context.Background(), "SELECT reltuples AS estimate FROM pg_class where relname = $1 LIMIT 1", tablename)
if err != nil {
return 0, fmt.Errorf("unable to estimate rows in postgres table: %w", err)
}
defer rows.Close()
if !rows.Next() {
return 0, fmt.Errorf("unable to estimate rows, no estimate returned")
}
var rowcount uint64
if err := rows.Scan(&rowcount); err != nil {
return 0, fmt.Errorf("unable to estimate rows: %w", err)
}
return rowcount, nil
}