MCPcopy Index your code
hub / github.com/dnote/dnote / generateSchema

Function generateSchema

pkg/cli/database/schema/main.go:64–125  ·  view source on GitHub ↗

generateSchema creates a fresh database, runs all migrations, and extracts the schema

(tmpDir string)

Source from the content-addressed store, hash-verified

62
63// generateSchema creates a fresh database, runs all migrations, and extracts the schema
64func generateSchema(tmpDir string) (string, error) {
65 // Create dnote directory structure in temp dir
66 dnoteDir := filepath.Join(tmpDir, "dnote")
67 if err := os.MkdirAll(dnoteDir, 0755); err != nil {
68 return "", fmt.Errorf("creating dnote dir: %w", err)
69 }
70
71 // Use a file-based database
72 dbPath := filepath.Join(tmpDir, "schema.db")
73
74 // Create context
75 ctx := context.DnoteCtx{
76 Paths: context.Paths{
77 Home: tmpDir,
78 Config: tmpDir,
79 Data: tmpDir,
80 Cache: tmpDir,
81 },
82 Version: "schema-gen",
83 }
84
85 // Open database
86 db, err := database.Open(dbPath)
87 if err != nil {
88 return "", fmt.Errorf("opening database: %w", err)
89 }
90 defer db.Close()
91 ctx.DB = db
92
93 // Initialize database with base tables
94 if err := infra.InitDB(ctx); err != nil {
95 return "", fmt.Errorf("initializing database: %w", err)
96 }
97
98 // Initialize system data
99 if err := infra.InitSystem(ctx); err != nil {
100 return "", fmt.Errorf("initializing system: %w", err)
101 }
102
103 // Create minimal config file
104 if err := config.Write(ctx, config.Config{}); err != nil {
105 return "", fmt.Errorf("writing initial config: %w", err)
106 }
107
108 // Run all local migrations
109 if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil {
110 return "", fmt.Errorf("running migrations: %w", err)
111 }
112
113 // Extract schema before closing database
114 schema, err := extractSchema(db)
115 if err != nil {
116 return "", fmt.Errorf("extracting schema: %w", err)
117 }
118
119 // Add INSERT statements for migration versions.
120 systemData := "\n-- Migration version data.\n"
121 systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemSchema, len(migrate.LocalSequence))

Callers 1

runFunction · 0.85

Calls 7

OpenFunction · 0.92
InitDBFunction · 0.92
InitSystemFunction · 0.92
WriteFunction · 0.92
RunFunction · 0.92
extractSchemaFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected