Skip to content

Commit

Permalink
use in-memory database to convert package manifest directory
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <[email protected]>
  • Loading branch information
joelanford committed Aug 19, 2021
1 parent a413d20 commit 558f9ed
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions internal/action/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ func (r Render) renderReference(ctx context.Context, ref string) (*declcfg.Decla
if !r.AllowedRefMask.Allowed(RefSqliteFile) {
return nil, fmt.Errorf("cannot render sqlite file: %w", ErrNotAllowed)
}
return sqliteToDeclcfg(ctx, ref)

db, err := sqlite.Open(ref)
if err != nil {
return nil, err
}
defer db.Close()
return sqliteToDeclcfg(ctx, db)
}

func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.DeclarativeConfig, error) {
Expand All @@ -170,7 +176,12 @@ func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.D
if !r.AllowedRefMask.Allowed(RefSqliteImage) {
return nil, fmt.Errorf("cannot render sqlite image: %w", ErrNotAllowed)
}
cfg, err = sqliteToDeclcfg(ctx, filepath.Join(tmpDir, dbFile))
db, err := sqlite.Open(filepath.Join(tmpDir, dbFile))
if err != nil {
return nil, err
}
defer db.Close()
cfg, err = sqliteToDeclcfg(ctx, db)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -222,13 +233,7 @@ func checkDBFile(ref string) error {
return nil
}

func sqliteToDeclcfg(ctx context.Context, dbFile string) (*declcfg.DeclarativeConfig, error) {
db, err := sqlite.Open(dbFile)
if err != nil {
return nil, err
}
defer db.Close()

func sqliteToDeclcfg(ctx context.Context, db *sql.DB) (*declcfg.DeclarativeConfig, error) {
migrator, err := sqlite.NewSQLLiteMigrator(db)
if err != nil {
return nil, err
Expand Down Expand Up @@ -419,26 +424,17 @@ func isPackageManifest(entries []os.DirEntry) bool {
}

func renderPackageManifest(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) {
tmpDB, err := os.CreateTemp("", "opm-render-pm-")
if err != nil {
return nil, err
}
if err := tmpDB.Close(); err != nil {
return nil, err
}

db, err := sqlite.Open(tmpDB.Name())
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?mode=memory&cache=shared&_foreign_keys=on", ref))
if err != nil {
return nil, err
}
defer db.Close()
defer os.RemoveAll(tmpDB.Name())

dbLoader, err := sqlite.NewSQLLiteLoader(db)
if err != nil {
return nil, err
}
if err := dbLoader.Migrate(context.TODO()); err != nil {
if err := dbLoader.Migrate(ctx); err != nil {
return nil, err
}

Expand All @@ -447,5 +443,5 @@ func renderPackageManifest(ctx context.Context, ref string) (*declcfg.Declarativ
return nil, fmt.Errorf("error loading manifests from directory: %s", err)
}

return sqliteToDeclcfg(ctx, tmpDB.Name())
return sqliteToDeclcfg(ctx, db)
}

0 comments on commit 558f9ed

Please sign in to comment.