@@ -21,11 +21,11 @@ const (
2121 GrantUsageSchemaSQLTemplate = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
2222 GrantAllTablesSQLTemplate = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
2323 DefaultPrivsSchemaSQLTemplate = `ALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
24- GetTablesFromSchemaSQLTemplate = `SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'`
24+ GetTablesFromSchemaSQLTemplate = `SELECT tablename,tableowner FROM pg_tables WHERE schemaname = '%s'`
2525 ChangeTableOwnerSQLTemplate = `ALTER TABLE IF EXISTS "%s" OWNER TO "%s"`
2626 ChangeTypeOwnerSQLTemplate = `ALTER TYPE "%s"."%s" OWNER TO "%s"`
2727 // Got and edited from : https://stackoverflow.com/questions/3660787/how-to-list-custom-types-using-postgres-information-schema
28- GetTypesFromSchemaSQLTemplate = `SELECT t.typname as type
28+ GetTypesFromSchemaSQLTemplate = `SELECT t.typname as type, pg_catalog.pg_get_userbyid(t.typowner) as owner
2929FROM pg_type t
3030LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
3131WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
@@ -34,6 +34,16 @@ AND n.nspname = '%s';`
3434 DuplicateDatabaseErrorCode = "42P04"
3535)
3636
37+ type TableOwnership struct {
38+ TableName string
39+ Owner string
40+ }
41+
42+ type TypeOwnership struct {
43+ TypeName string
44+ Owner string
45+ }
46+
3747func (c * pg ) IsDatabaseExist (dbname string ) (bool , error ) {
3848 err := c .connect (c .defaultDatabase )
3949 if err != nil {
@@ -114,7 +124,7 @@ func (c *pg) CreateSchema(db, role, schema string) error {
114124 return nil
115125}
116126
117- func (c * pg ) GetTablesInSchema (db , schema string ) ([]string , error ) {
127+ func (c * pg ) GetTablesInSchema (db , schema string ) ([]* TableOwnership , error ) {
118128 err := c .connect (db )
119129 if err != nil {
120130 return nil , err
@@ -127,18 +137,18 @@ func (c *pg) GetTablesInSchema(db, schema string) ([]string, error) {
127137
128138 defer rows .Close ()
129139
130- res := []string {}
140+ res := []* TableOwnership {}
131141
132142 for rows .Next () {
133- tableName := ""
143+ it := & TableOwnership {}
134144 // Scan
135- err = rows .Scan (& tableName )
145+ err = rows .Scan (& it . TableName , & it . Owner )
136146 // Check error
137147 if err != nil {
138148 return nil , err
139149 }
140150 // Save
141- res = append (res , tableName )
151+ res = append (res , it )
142152 }
143153
144154 // Rows error
@@ -165,7 +175,7 @@ func (c *pg) ChangeTableOwner(db, table, owner string) error {
165175 return nil
166176}
167177
168- func (c * pg ) GetTypesInSchema (db , schema string ) ([]string , error ) {
178+ func (c * pg ) GetTypesInSchema (db , schema string ) ([]* TypeOwnership , error ) {
169179 err := c .connect (db )
170180 if err != nil {
171181 return nil , err
@@ -178,18 +188,18 @@ func (c *pg) GetTypesInSchema(db, schema string) ([]string, error) {
178188
179189 defer rows .Close ()
180190
181- res := []string {}
191+ res := []* TypeOwnership {}
182192
183193 for rows .Next () {
184- typeName := ""
194+ it := & TypeOwnership {}
185195 // Scan
186- err = rows .Scan (& typeName )
196+ err = rows .Scan (& it . TypeName , & it . Owner )
187197 // Check error
188198 if err != nil {
189199 return nil , err
190200 }
191201 // Save
192- res = append (res , typeName )
202+ res = append (res , it )
193203 }
194204
195205 // Rows error
0 commit comments