Skip to content

Latest commit

 

History

History
226 lines (169 loc) · 13.7 KB

init.md

File metadata and controls

226 lines (169 loc) · 13.7 KB

Initialize a database

Run schemabot init in a terminal to bring an existing database into SchemaBot. Setup reads its live schema into files and verifies they match, without changing application tables or rewriting existing SQL scripts. An empty database follows the same flow. The wizard and explicit CLI flags use the same setup workflow.

SchemaBot setup, a schema edit, and the first plan

Try without a database

Run schemabot init in an empty project directory and choose Try a sample database, then choose MySQL, PostgreSQL, or Vitess. Docker must be running. SchemaBot starts a local container, seeds customers and orders, and uses the same import and verification flow. No connection string is needed.

A Vitess sample is two containers rather than one: a local cluster behind a PlanetScale-compatible API, and a MySQL server holding SchemaBot's own state. Its keyspace is split across two shards, so an apply reports progress per shard, and it needs no PlanetScale account.

The first Vitess sample downloads a multi-gigabyte cluster image, so allow several minutes on a machine that has never run one. Docker keeps the image, so later samples skip the download, but the cluster still takes a minute or so to come up. The cluster is rebuilt from the seed schema whenever the container restarts, while the state database keeps what was applied, so restart it to start the demonstration over rather than to carry work forward. See Vitess on PlanetScale.

Start a sample database, review and apply a change, then verify the result

For scripts, select the sample explicitly:

$ schemabot init --sample --type mysql --non-interactive --json
{"database":"shop","environment":"development","profile":"schemabot-sample-example","schema_dir":"/project/schema","plan_id":"plan-example","tables":2,"verified":true}

Use --type postgres for PostgreSQL, or --type vitess for a sharded Vitess cluster; init --sample --type vitess is how you set up a Vitess target. Names and paths vary by project. A sample's setup records its profile in schemabot.yaml, so plan and apply in that directory reach that sample without --profile, whatever your default profile is. The key belongs to the machine that runs the sample; a real project's init writes none, so its schemabot.yaml stays shareable. Each project gets its own sample and runtime; retrying setup keeps your data and never overwrites schema edits. Initialization only succeeds when the files match the live database; use plan to review edits you have not applied yet. Samples listen only on 127.0.0.1.

Docker retains sample data across restarts. To stop the database, run docker stop with the container name printed during setup. Start it again with docker start <container-name>, then continue with plan or apply. You do not need to rerun initialization to restart it. The container keeps its assigned port, so the saved connection continues to work. If another process takes that port while the sample is stopped, stop that process and retry docker start; do not remove the sample to resolve a port conflict. When finished, stop its SchemaBot runtime with schemabot local stop <container-name> before removing the disposable database with docker rm -v <container-name>. Removing the container permanently deletes its sample data. Your schema files remain in your project. After deleting a sample, start a new sample in a fresh project directory; the old project's saved connection belongs to the deleted database. Use make demo DEMO_DIR=/absolute/path/to/new-project to choose a fresh demo project.

Before you start

Use MySQL or PostgreSQL with an existing application database. You can paste a connection string or enter host, port, database, username, and password inside the wizard. Passwords and pasted strings are hidden. The final review asks you to confirm saving entered credentials in private, unencrypted files under ~/.schemabot/credentials, outside the project. These files persist across terminal and laptop restarts; keep them private and include them in your credential-management practices. Cancelling before final confirmation writes no credentials.

If DATABASE_URL is already set, the wizard shows its destination and offers to use it. Environment-variable and absolute file references remain available as an advanced option; a referenced file must contain only the connection string. References stay references, so environment variables must remain available to later CLI invocations. Schema files never contain credentials. OS credential-store integration is not implemented.

The wizard connects MySQL and PostgreSQL databases. It offers Vitess as a sample database, but not as an existing one: register a PlanetScale database in server configuration instead, since it is reached through an API and a service token rather than a connection string.

The wizard asks Where should SchemaBot store its own data?

  • Integrated: a simple setup for a single database project. SchemaBot creates a new schemabot database on your application's server, using the same credentials. Those credentials need permission to create a database. Your application's tables stay separate.
  • Standalone: provide SCHEMABOT_STORAGE_DSN for an existing, dedicated state database. A separate server is a good fit for teams managing multiple databases.

Integrated setup refuses to adopt an existing schemabot database silently. If you already prepared one for SchemaBot, choose Standalone and provide its connection explicitly.

You can move state to another server later, but this is an operator-managed transfer, not a wizard toggle: drain in-flight work, stop the runtime, transfer the complete state database, update its connection configuration, and verify it before restarting. Keep the original state until the new connection is verified. Re-running init never replaces existing state storage.

Setup initializes SchemaBot's metadata tables in the state database. Baseline planning also needs the engine's scratch privileges. Setup never applies application schema changes.

Private databases do not need a public endpoint. Run SchemaBot somewhere that can reach them: on your VPN, through an existing tunnel, or on a machine inside the private network. If a connection check fails, keep the wizard open, restore access, and retry.

Follow the wizard

The wizard detects an available connection and offers Use this connection, or lets you paste a connection string, enter details, or use a reference. It shows the host and database without credentials and checks access before continuing. Failed checks can be retried; Shift+Tab returns to connection choices without leaving the wizard.

Standalone setup uses the same adaptive connection flow. Integrated creates its database only after final confirmation. Connection checks run a read-only query and create no metadata. Environment, schema directory, and profile defaults remain editable in the final review.

Before setting up a runtime, SchemaBot reads the target catalog to discover namespaces. One result is selected automatically; multiple results appear in a searchable list. Use Space to select namespaces and Enter to continue. MySQL stays within the database named in the DSN; PostgreSQL lists accessible application schemas. No results or a failed connection stops here with a chance to retry, edit the connection, or press m to enter namespaces manually. Explicit --namespace flags also bypass discovery. Discovery uses only the application connection. State metadata is initialized only after the final review.

Press Shift+Tab to go back and change your choices, or Escape to cancel.

SchemaBot imports into a temporary directory and verifies a no-change plan before publishing the files. A successful setup ends with:

  ✓ Your schema is ready

  1 table · schema
  Baseline plan: no changes.

  Make your first edit, then review the plan:

    schemabot plan -s 'schema' -e 'development'

The command names the profile with --profile only when the connection was saved under a profile that is not your default, so the next step works as printed. A sample project needs no flag: setup records its profile in schemabot.yaml, and --profile or SCHEMABOT_PROFILE still selects another one. When a reused sample schemabot.yaml named a different profile, setup says which one it replaced.

A table its schema file cannot describe is left out and listed in ignore_tables, so SchemaBot leaves it alone. The headline then counts what setup imported, such as Schema ready for 3 of 5 tables, and the tables left out are listed with their reasons. When that is every table setup found, it says so instead of reporting success:

  No tables imported

  0 of 2 tables · SchemaBot manages none of them yet.

  /path/to/schema/
  └── public/
      └── schema.sql

  ⚠️  Not managed: 2 tables have no schema file, so schemabot.yaml lists them in ignore_tables:
    public.audit_log: unlogged tables cannot be rendered as a desired schema
    public.events_raw: unlogged tables cannot be rendered as a desired schema

  SchemaBot leaves them alone. To manage a new table, add its schema file to its namespace directory above, then review the plan:

    schemabot plan -s schema -e development

A PostgreSQL index whose concurrent build did not finish is invalid, and its table does not deliver it. Setup leaves it out of the table's schema file and lists it under Left out: with its fix. To keep it, declare it in the file once any rows with duplicate keys are resolved, and the apply builds it again.

Views, functions, triggers, partitions and the other objects a schema file cannot declare stay in the database as they are. Setup lists them so you know a database built from the files won't have them:

  ⚠️  Not in the schema files: 2 objects SchemaBot doesn't manage. It leaves them as they are, but a database built from these files won't have them:
    public: 1 trigger: orders_touch on orders
    public: 1 view: open_orders

An empty namespace gets an explicit marker in schema.sql that preserves its scope. Keep that marker until you add your first table declaration there when you are ready; setup does not invent a sample table.

If schema files already exist, the review screen explains that they will be verified and reused. It checks their database, engine, and namespace scope, and preserves their contents. Differences from the live database stop setup with the plan details; they are not applied automatically.

Connect GitHub

When the schema is ready, setup asks whether to connect GitHub so SchemaBot plans every pull request. Press Enter to continue in the same terminal with the steps of schemabot github create-app: create the App in your browser, optionally create a private test repository holding the schema files setup just wrote, and install the App. Answer n to skip; setup prints the command to run later.

With --sample, the database is a local sample, and the GitHub App and private test repository are there to try pull request plans. The App stays saved in ~/.schemabot/credentials after the sample is gone: a later init, sample or not, names it instead of offering a new one, and schemabot serve uses it. To start over with a new App, delete the App (and the test repository) on GitHub, then remove the App's github-app-* directory from ~/.schemabot/credentials.

Pull request plans also need a SchemaBot server that runs with the App and a webhook URL GitHub can reach. The local server that setup starts does not take GitHub webhooks, so run one as Configure SchemaBot and Start SchemaBot describe, and expose it as Webhook Ingress describes. Until then, a pull request in the test repository gets no plan.

Setup does not ask when a GitHub App is already saved, when it runs with --non-interactive, or when its input or output is not a terminal, as in a script or a pipe; it prints the command instead. --json output is unchanged and never includes the question.

Use an agent or script

Supply the same decisions as flags. Both --non-interactive and --json suppress prompts; --json also returns a structured result. For example, against a supported database with one table:

$ schemabot init --non-interactive --json --type mysql \
    --database shop --environment development --namespace shop \
    --dsn env:DATABASE_URL --storage-dsn env:SCHEMABOT_STORAGE_DSN \
    --schema-dir schema
{"database":"shop","environment":"development","profile":"default","schema_dir":"/project/schema","plan_id":"plan-example","tables":1,"verified":true}

Use --integrated instead of --storage-dsn to create SchemaBot’s own database on the application server. The flags are mutually exclusive.

Paths and plan IDs vary. Existing schema directories with a valid schemabot.yaml are verified and reused automatically, including with flags. --reuse-schema is also accepted. Select a named connection with --profile; existing profiles and the default connection are preserved.

Missing inputs are explicit and exit unsuccessfully:

$ schemabot init --non-interactive --json
{"error":{"code":"missing_inputs","message":"Provide the missing flags or run init interactively."},"missing":["database","environment","type","dsn","storage-dsn","namespace"]}

A failed setup preserves existing files and retains any runtime registration and state already created. Correct the reported issue and retry with the same inputs. An identical import can be reused; conflicting files or connections are never overwritten.