Repository navigation
Expand file tree
/
Copy pathschemaplan.go
More file actions
285 lines (267 loc) · 10.2 KB
/
Copy pathschemaplan.go
File metadata and controls
285 lines (267 loc) · 10.2 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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/codefly-dev/service-postgres/libs/go/schemaplan"
"io"
"os"
"path/filepath"
"sort"
"strings"
)
// schemaPlanContractVersion identifies the bootstrap artifact's plan format. A
// consumer reading bootstrap/plan.json checks it before interpreting the fields.
const schemaPlanContractVersion = schemaplan.ContractVersion
// schemaPlan is the packaged form of the resolved schema prerequisites: what the
// bootstrap image carries, and what it applies. Resolving and validating the
// declarations belongs to resolveSchemaPrerequisites, which the runtime and the
// build share; this adds only what packaging needs — a staged location per
// lineage, the files staged under it, and the content identity the artifact
// publishes — so the image applies the same schema contract the runtime does.
type schemaPlan struct {
database string
lineages []schemaLineage
extensions []extensionRequest
access schemaAccess
}
// schemaLineage is one resolved migration lineage together with where the build
// stages it and what it stages.
type schemaLineage struct {
// migrationSource is what the runtime applies: the lineage's root and the
// ledger it owns.
migrationSource
// stage is the lineage's directory under bootstrap/sources in the build
// context. The ordinal prefix makes it collision-free by construction.
stage string
files []schemaFile
digest string
}
// schemaFile is one staged file's identity within a lineage.
type schemaFile struct {
name string
digest string
}
// schemaAccess carries the runtime-role grant inputs. Both the local reconciler
// and the bootstrap image's runtime-access.sql are driven from these values.
type schemaAccess struct {
readOnlyRole string
readWriteRole string
schemas []string
readWriteRoles []string
}
// buildSchemaPlan packages already-resolved prerequisites for the build. It is
// pure: it reads the migration files' names, never a database.
//
// NoMigration packages no lineage at all, mirroring applySchema: extensions and
// runtime grants still apply, so the image does exactly what the runtime does.
//
// Every resolved source is packaged as it comes. resolveSchemaPrerequisites
// already excludes a lineage with no migration, and skipping one here as well
// would hide a lineage from the image that the runtime still tried to apply —
// the divergence this packaging exists to remove.
func buildSchemaPlan(prerequisites *schemaPrerequisites, settings *Settings) (*schemaPlan, error) {
access, err := resolveSchemaAccess(settings)
if err != nil {
return nil, err
}
plan := &schemaPlan{
database: settings.DatabaseName,
extensions: prerequisites.extensions,
access: access,
}
if settings.NoMigration {
return plan, nil
}
for _, source := range prerequisites.sources {
files, filesErr := stagedFiles(source.dir)
if filesErr != nil {
return nil, fmt.Errorf("cannot inventory migration source %q: %w", source.label(), filesErr)
}
plan.lineages = append(plan.lineages, schemaLineage{
migrationSource: source,
stage: fmt.Sprintf("%02d-%s", len(plan.lineages), source.label()),
files: files,
})
}
return plan, nil
}
// stagedFiles lists the files of one source directory that the build stages,
// sorted by name. A file is staged when migrationFileName recognizes it — the
// same single definition the runtime's source driver filters on — so an editor
// backup left beside a migration is neither applied locally nor carried into the
// image. Contents are not read here: digests are filled in by attest, so a plan
// can be assembled without reading every migration byte.
//
// A symlink is followed when its target stays inside the source root, so a
// layout that works locally keeps working. One that leaves the root is rejected:
// staging copies bytes, and content pulled from an arbitrary path would enter
// the image with no record of where it came from. Declare such content as its
// own migration source instead.
func stagedFiles(dir string) ([]schemaFile, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
files := make([]schemaFile, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() || !migrationFileName.MatchString(entry.Name()) {
continue
}
if entry.Type()&os.ModeSymlink != 0 {
contained, containedErr := symlinkStaysInside(dir, entry.Name())
if containedErr != nil {
return nil, containedErr
}
if !contained.Mode().IsRegular() {
continue
}
} else if !entry.Type().IsRegular() {
return nil, fmt.Errorf("entry %q is not a regular file", entry.Name())
}
files = append(files, schemaFile{name: entry.Name()})
}
sort.Slice(files, func(i, j int) bool { return files[i].name < files[j].name })
return files, nil
}
// symlinkStaysInside resolves one symlinked entry and reports its target's file
// information, rejecting a target that resolves outside the source root.
func symlinkStaysInside(dir, name string) (os.FileInfo, error) {
root, err := filepath.EvalSymlinks(dir)
if err != nil {
return nil, err
}
target, err := filepath.EvalSymlinks(filepath.Join(dir, name))
if err != nil {
return nil, fmt.Errorf("migration source entry %q does not resolve: %w", name, err)
}
relative, err := filepath.Rel(root, target)
if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return nil, fmt.Errorf(
"migration source entry %q links outside its source root; declare %s as its own migration source instead",
name, filepath.Dir(target))
}
return os.Stat(target)
}
// attest fills in the content identity the build artifact publishes: a digest
// per staged file and per lineage.
func (p *schemaPlan) attest() error {
for i := range p.lineages {
for j := range p.lineages[i].files {
digest, err := fileDigest(filepath.Join(p.lineages[i].dir, p.lineages[i].files[j].name))
if err != nil {
return fmt.Errorf("cannot digest migration source %q: %w", p.lineages[i].label(), err)
}
p.lineages[i].files[j].digest = digest
}
p.lineages[i].digest = lineageDigest(p.lineages[i])
}
return nil
}
// fileDigest streams the file through sha256 rather than buffering it whole, so
// digesting a large data migration does not read it into memory.
func fileDigest(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hasher := sha256.New()
if _, err = io.Copy(hasher, file); err != nil {
return "", err
}
return "sha256:" + hex.EncodeToString(hasher.Sum(nil)), nil
}
// digest is the plan's content identity: a deterministic function of every
// lineage's ledger, staged location and file contents, the required extensions,
// and the runtime-role grant inputs. Mutating one migration file changes it.
func (p *schemaPlan) digest() string { return p.artifactWithoutDigest().ContentDigest() }
func lineageDigest(l schemaLineage) string {
artifact := schemaLineageArtifact{Label: l.label(), Ledger: l.trackingTable()}
for _, f := range l.files {
artifact.Files = append(artifact.Files, schemaFileArtifact{Name: f.name, Digest: f.digest})
}
return artifact.ContentDigest()
}
// stagedChecksums renders the staged content as a sha256sum manifest, relative
// to the staging root. The bootstrap program verifies it before applying
// anything: the image is built from the service directory by a later, separate
// process, so a staged tree that drifted or was only partly written would
// otherwise apply fewer migrations and report success.
func (p *schemaPlan) stagedChecksums() []byte {
var manifest strings.Builder
for _, lineage := range p.lineages {
for _, file := range lineage.files {
manifest.WriteString(strings.TrimPrefix(file.digest, "sha256:"))
manifest.WriteString(" sources/")
manifest.WriteString(lineage.stage)
manifest.WriteString("/")
manifest.WriteString(file.name)
manifest.WriteString("\n")
}
}
return []byte(manifest.String())
}
func resolveSchemaAccess(settings *Settings) (schemaAccess, error) {
readOnlyRole, readWriteRole := runtimeRoleNames(settings.DatabaseName)
schemas, err := normalizedRuntimeSchemas(settings.RuntimeSchemas)
if err != nil {
return schemaAccess{}, err
}
readWriteRoles, err := normalizedRuntimeReadWriteRoles(settings.RuntimeReadWriteRoles, readOnlyRole, readWriteRole)
if err != nil {
return schemaAccess{}, err
}
return schemaAccess{
readOnlyRole: readOnlyRole,
readWriteRole: readWriteRole,
schemas: schemas,
readWriteRoles: readWriteRoles,
}, nil
}
// schemaPlanArtifact is the bootstrap image's evidence of what it will apply.
// It is deliberately machine-independent and secret-free: staged locations and
// content digests rather than filesystem provenance, role names rather than
// credentials. Two machines building one commit must publish identical bytes.
type schemaPlanArtifact = schemaplan.Plan
type schemaExtensionArtifact = schemaplan.Extension
type schemaLineageArtifact = schemaplan.Lineage
type schemaFileArtifact = schemaplan.File
type schemaAccessArtifact = schemaplan.Access
func (p *schemaPlan) artifact() schemaPlanArtifact {
a := p.artifactWithoutDigest()
a.Digest = a.ContentDigest()
return a
}
func (p *schemaPlan) artifactWithoutDigest() schemaPlanArtifact {
extensions := make([]schemaExtensionArtifact, 0, len(p.extensions))
for _, extension := range p.extensions {
extensions = append(extensions, schemaExtensionArtifact{Name: extension.name, Required: extension.required})
}
lineages := make([]schemaLineageArtifact, 0, len(p.lineages))
for _, lineage := range p.lineages {
files := make([]schemaFileArtifact, 0, len(lineage.files))
for _, file := range lineage.files {
files = append(files, schemaFileArtifact{Name: file.name, Digest: file.digest})
}
lineages = append(lineages, schemaLineageArtifact{
Label: lineage.label(),
Ledger: lineage.trackingTable(),
Stage: lineage.stage,
Digest: lineage.digest,
Files: files,
})
}
return schemaPlanArtifact{
ContractVersion: schemaPlanContractVersion,
Database: p.database,
Extensions: extensions,
Lineages: lineages,
Access: schemaAccessArtifact{
ReadOnlyRole: p.access.readOnlyRole,
ReadWriteRole: p.access.readWriteRole,
Schemas: p.access.schemas,
ReadWriteRoles: p.access.readWriteRoles,
},
}
}