forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.go
More file actions
514 lines (420 loc) · 13.3 KB
/
package.go
File metadata and controls
514 lines (420 loc) · 13.3 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package pgpkg
import (
"fmt"
"github.com/lib/pq"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
const migrationFilename = "@migration.pgpkg"
// Package represents a set of schemas in a database. pgpkg
// keeps track of and maintains the objects declared in the
// package, but doesn't touch anything else.
//
// Packages are divided into three bundles, called schema,
// MOB and tests. Each bundle operates in a unique way.
//
// The database structure is represented by a list of upgrade
// files, which are always executed in order. These files can contain
// any SQL code, but generally contain tables and data type definitions.
//
// The MOB to the schema is represented by files which contain
// functions, views and triggers. These are managed by pgpkg and
// may be created in any order. pgpkg works out the dependencies between
// them.
//
// Tests are files containing SQL functions, that are executed in order. Tests
// that produce exceptions cause the upgrade to be rolled back.
//
// The structure of a Package is:
//
// Package -> Bundles (structure, app, tests) -> Units (files) -> Statements
//
type Package struct {
Project *Project
Name string // canonical, unique name of the pgpkg package
Location string // Location of this package
Source Source // Source of the package (dir, zip, embedded, ...)
SchemaNames []string // Packages participate in one or more schemas
RoleName string // Associated role name
StatFuncCount int // Stat showing the number of functions in the package
StatViewCount int // Stat showing the number of views in the package
StatTriggerCount int // Stat showing the number of triggers in the package
StatMigrationCount int // Stat showing how many migration scripts were run
StatTestCount int // Stat showing how many tests there are.
Schema *Schema
MOB *MOB
Tests *Tests
IsDependency bool // This package was loaded from .pgpkg cache
bootstrapSchema bool // migrate without checking migration table. Allows pgpkg to bootstrap itself.
config *configType
}
func (p *Package) newBundle() *Bundle {
return &Bundle{
Path: "",
Package: p,
Index: make(map[string]*Unit),
}
}
func (p *Package) setRole(tx *PkgTx) {
_, err := tx.Exec(fmt.Sprintf("set role \"%s\"", Sanitize(rolePattern, p.RoleName)))
if err != nil {
panic(fmt.Errorf("unable to change to role %s: %w", p.RoleName, err))
}
}
func (p *Package) resetRole(tx *PkgTx) {
_, err := tx.Exec(fmt.Sprintf("reset role"))
if err != nil {
panic(fmt.Errorf("unable to reset to role %s: %w", p.RoleName, err))
}
}
func (p *Package) hasRole(tx *PkgTx) bool {
var roleCount int
row := tx.QueryRow("select count(*) from pg_roles where rolname=$1", p.RoleName)
err := row.Scan(&roleCount)
if err != nil {
panic(err)
}
return roleCount == 1
}
func (p *Package) createSchema(tx *PkgTx) error {
LogQuieter()
defer LogLouder()
if !p.hasRole(tx) {
_, err := tx.Exec(fmt.Sprintf("create role \"%s\"", Sanitize(rolePattern, p.RoleName)))
if err != nil {
return fmt.Errorf("unable to create role %s: %w", p.RoleName, err)
}
// The user running these scripts may not be a superuser (but must have create role),
// so we need to extend access to the new role.
_, err = tx.Exec(fmt.Sprintf("grant \"%s\" to current_user", Sanitize(rolePattern, p.RoleName)))
if err != nil {
return fmt.Errorf("unable to grant role %s to current_user: %w", p.RoleName, err)
}
}
for _, schemaName := range p.SchemaNames {
_, err := tx.Exec(fmt.Sprintf("create schema if not exists \"%s\" authorization \"%s\"",
Sanitize(schemaPattern, schemaName), Sanitize(rolePattern, p.RoleName)))
if err != nil {
return fmt.Errorf("unable to create schema %s: %w", schemaName, err)
}
}
exts := p.config.Extensions
if exts != nil {
for _, ext := range p.config.Extensions {
if _, err := tx.Exec(fmt.Sprintf("create extension if not exists \"%s\" with schema public", Sanitize(extensionPattern, ext))); err != nil {
return fmt.Errorf("unable to create package extension %s: %w", ext, err)
}
}
}
return nil
}
// Register this package in the pgpkg.pkg table.
func (p *Package) register(tx *PkgTx) error {
_, err := tx.Exec("insert into pgpkg.pkg (pkg, schema_names, uses) values ($1, $2, $3) "+
"on conflict (pkg) do update set schema_names=excluded.schema_names, uses=excluded.uses",
p.Name, pq.Array(p.SchemaNames), pq.Array(p.config.Uses))
return err
}
func (p *Package) grantPackage(tx *PkgTx, pkgName string) error {
var schemaNames []string
r := tx.QueryRow("select schema_names from pgpkg.pkg where pkg=$1", pkgName)
if err := r.Scan(pq.Array(&schemaNames)); err != nil {
return fmt.Errorf("unable to grant access to package %s: %w", pkgName, err)
}
for _, schemaName := range schemaNames {
if _, err := tx.Exec(fmt.Sprintf(`grant usage on schema "%s" to "%s"`,
Sanitize(schemaPattern, schemaName), Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
if _, err := tx.Exec(fmt.Sprintf(`grant execute on all functions in schema "%s" to "%s"`,
Sanitize(schemaPattern, schemaName), Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
if _, err := tx.Exec(fmt.Sprintf(`grant select, update, insert, references on all tables in schema "%s" to "%s"`,
Sanitize(schemaPattern, schemaName), Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
if _, err := tx.Exec(fmt.Sprintf(`grant usage on all sequences in schema "%s" to "%s"`,
Sanitize(schemaPattern, schemaName), Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
}
return nil
}
// grant access to certain parts of the pgpkg package.
func (p *Package) grantPgpkg(tx *PkgTx) error {
if _, err := tx.Exec(fmt.Sprintf(`grant usage on schema "pgpkg" to "%s"`,
Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
if _, err := tx.Exec(fmt.Sprintf(`grant execute on all functions in schema "pgpkg" to "%s"`,
Sanitize(rolePattern, p.RoleName))); err != nil {
return err
}
return nil
}
// Allow this package to access the packages in the Uses clause of the definition.
func (p *Package) grantUses(tx *PkgTx) error {
if p.config.Uses == nil {
return nil
}
for _, pkg := range p.config.Uses {
if err := p.grantPackage(tx, pkg); err != nil {
return err
}
}
return nil
}
func (p *Package) Apply(tx *PkgTx) error {
// Stop any other pgpkg process from running simultaneously.
if _, err := tx.Exec("select pg_advisory_xact_lock(hashtext('pgpkg'))"); err != nil {
return fmt.Errorf("pgpkg: unable to obtain package lock: %w", err)
}
err := p.createSchema(tx)
if err != nil {
return err
}
if p.MOB != nil && p.MOB.HasUnits() {
err = p.MOB.Parse()
if err != nil {
return err
}
// This runs as pgpkg user since it's accessing pgpkg tables
// and deleting stuff from the schema.
err = p.MOB.purge(tx)
if err != nil {
return err
}
} else {
if Options.Verbose {
fmt.Fprintf(os.Stderr, "note: %s: no MOBs defined\n", p.Name)
}
}
// Grant access to functions in pgpkg, e.g. the assertions
if err = p.grantPgpkg(tx); err != nil {
return err
}
// Grant access to any schema declared in the Uses section of the TOML.
if err = p.grantUses(tx); err != nil {
return err
}
if p.Schema != nil && p.Schema.HasUnits() {
// Load the migration state outside the schema role.
if err = p.Schema.loadMigrationState(tx); err != nil {
return err
}
p.setRole(tx)
if err = p.Schema.Apply(tx); err != nil {
return err
}
p.resetRole(tx)
// Save the migrated state, also outside the schema role
if err = p.Schema.saveMigrationState(tx); err != nil {
return err
}
} else {
if Options.Verbose {
fmt.Fprintf(os.Stderr, "note: %s: no schema defined\n", p.Name)
}
}
if p.MOB != nil && p.MOB.HasUnits() {
p.setRole(tx)
if err = p.MOB.Apply(tx); err != nil {
return err
}
p.resetRole(tx)
if err = p.MOB.updateState(tx); err != nil {
return err
}
}
if err = p.register(tx); err != nil {
return err
}
if p.Tests != nil && p.Tests.HasUnits() && !Options.SkipTests {
p.setRole(tx)
if err := p.Tests.Run(tx); err != nil {
return err
}
p.resetRole(tx)
}
if Options.Verbose || Options.Summary {
Verbose.Printf("%s: installed %d function(s), %d view(s) and %d trigger(s). %d migration(s) needed. %d test(s) run\n",
p.Name, p.StatFuncCount, p.StatViewCount, p.StatTriggerCount, p.StatMigrationCount, p.StatTestCount)
}
return nil
}
// Read the configuration TOML file and update the package accordingly.
// If the package is already configured, it's an error.
func (p *Package) parseConfig(tomlPath string) error {
if p.config != nil {
return fmt.Errorf("duplicate configuration found: %s", tomlPath)
}
pkgConfigReader, err := p.Source.Open(tomlPath)
if err != nil {
return err
}
defer pkgConfigReader.Close()
config, err := parseConfig(pkgConfigReader)
if err != nil {
return err
}
p.Name = config.Package
p.SchemaNames = SanitizeSlice(schemaPattern, config.Schemas)
p.config = config
if Options.ForceRole != "" {
p.RoleName = Sanitize(rolePattern, Options.ForceRole)
} else {
p.RoleName = Sanitize(rolePattern, "$"+p.Name)
}
return nil
}
var validNames = regexp.MustCompile("[^#]*")
func (p *Package) addUnit(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
name := d.Name()
// Ignore the pgpkg.toml file.
if name == "pgpkg.toml" {
return nil
}
// Ignore dot-files other than "." itself
if name != "." && name[0] == '.' {
if d.IsDir() {
return fs.SkipDir
} else {
return nil
}
}
if d.IsDir() {
// If this is a directory, and it contains migrations, then
// process it with a separate walk().
if _, err = fs.Stat(p.Source, filepath.Join(path, migrationFilename)); err == nil {
if err = p.Schema.loadMigrationDir(path); err != nil {
return err
}
return fs.SkipDir
}
}
// If this file is part of a migration, ignore it.
if p.Schema.migrationPaths[path] {
return nil
}
if strings.HasSuffix(name, "_test.sql") {
return p.Tests.addUnit(path)
}
if strings.HasSuffix(name, ".sql") {
return p.MOB.addUnit(path)
}
// Files that aren't recognised are just ignored. This lets us mix pgpkg sql with
// other files.
return nil
}
// Load the project details - the TOML file - without reading the rest of the schema data.
func readPackage(project *Project, source Source, dir string) (*Package, error) {
var err error
if dir != "" {
if source, err = source.Sub(dir); err != nil {
return nil, fmt.Errorf("unable to read package: source %s: %w", source, err)
}
}
pkg := &Package{
Project: project,
Source: source,
Location: source.Location(),
}
if err := pkg.parseConfig("pgpkg.toml"); err != nil {
return nil, err
}
return pkg, nil
}
func (p *Package) readSchema() error {
p.Schema = NewSchema(p)
p.MOB = &MOB{Bundle: p.newBundle()}
p.Tests = &Tests{Bundle: p.newBundle()}
// if the package config explicitly lists migrations, then you can't have a @migrations.pgpkg file.
if len(p.config.Migrations) > 0 {
if err := p.Schema.loadMigrations(p.config.Migrations); err != nil {
return fmt.Errorf("unable to load schema migrations: %w", err)
}
}
// Only walk the directory in which the toml file was found, rather than
// the entire filesystem provided in pkgFS.
if err := fs.WalkDir(p.Source, ".", p.addUnit); err != nil {
return fmt.Errorf("unable to read schema for package %s: %w", p.Name, err)
}
return nil
}
func (p *Package) isValidSchema(search string) bool {
for _, schema := range p.SchemaNames {
if schema == search {
return true
}
}
return false
}
// AddUses adds the given package name to the Uses clause of the package.
// Returns false if the package already exists in the Uses clause.
// Note that this does not update the config file; to do this, see WriteConfig.
func (p *Package) AddUses(pkg string) bool {
uses := p.config.Uses
// check that it doesn't already exist
if uses != nil {
for _, u := range uses {
if u == pkg {
return false
}
}
}
p.config.Uses = append(p.config.Uses, pkg)
return true
}
func (p *Package) WriteConfig() error {
// We can only write to this package if it came from a directory.
dirFS, ok := p.Source.(*DirSource)
if !ok {
return fmt.Errorf("package was not loaded from filesystem")
}
tempFile := path.Join(dirFS.Path(), "pgpkg-new.toml")
pkgConfigWriter, err := os.Create(tempFile)
if err != nil {
return fmt.Errorf("unable to create config file %s: %w", tempFile, err)
}
if err := p.config.writeConfig(pkgConfigWriter); err != nil {
return fmt.Errorf("unable to write config file %s: %w", tempFile, err)
}
if err := pkgConfigWriter.Close(); err != nil {
return fmt.Errorf("unable to complete config file write to %s: %w", tempFile, err)
}
tomlFile := path.Join(dirFS.Path(), "pgpkg.toml")
if err := os.Rename(tempFile, tomlFile); err != nil {
return fmt.Errorf("unable to replace existing pgpkg.toml: %w", err)
}
return nil
}
func (p *Package) PrintInfo(w InfoWriter) {
w.Print("Package name", p.Name)
w.Print("Location", p.Location)
w.Print("Source", p.Source)
w.Print("SchemaNames", p.SchemaNames)
w.Print("RoleName", p.RoleName)
if p.Schema != nil {
p.Schema.PrintInfo(w)
} else {
w.Println("no schema")
}
if p.MOB != nil {
p.MOB.PrintInfo(w)
} else {
w.Println("no managed objects")
}
if p.Tests != nil {
p.Tests.PrintInfo(w)
} else {
w.Println("no tests")
}
}