package cli import ( "fmt" "git.unkin.net/unkin/logarchiver/internal/index" "github.com/spf13/cobra" ) func newInitSchemaCmd() *cobra.Command { var printOnly bool cmd := &cobra.Command{ Use: "init-schema", Short: "Create the ClickHouse archive-index database and table (idempotent)", Long: `init-schema creates the ClickHouse database and archive_index table. In-cluster the argocd bootstrap Job owns schema creation (like the logging stack's clickhouse-schema PostSync hook); this command is for local/dev use and for emitting the DDL (--print) to embed in that Job.`, RunE: func(cmd *cobra.Command, _ []string) error { cfg, err := loadConfig() if err != nil { return err } if printOnly { out := cmd.OutOrStdout() _, _ = fmt.Fprintln(out, index.CreateDatabaseSQL(cfg.Index.Database)+";") _, _ = fmt.Fprintln(out, index.CreateTableSQL(cfg.Index.Database, cfg.Index.Table)+";") return nil } ch, err := index.NewClickHouse(cmd.Context(), indexConfig(cfg.Index)) if err != nil { return err } defer func() { _ = ch.Close() }() if err := ch.InitSchema(cmd.Context()); err != nil { return err } _, _ = fmt.Fprintf(cmd.OutOrStdout(), "schema ready: %s.%s\n", cfg.Index.Database, cfg.Index.Table) return nil }, } cmd.Flags().BoolVar(&printOnly, "print", false, "print the DDL instead of executing it") return cmd }