action/README.md
bytebase-action helps to do common chores in database CI/CD with Bytebase.
This action provides several subcommands to interact with Bytebase.
checkUsage: bytebase-action check [global flags]
Checks the SQL files matching the --file-pattern. This is typically used for linting or pre-deployment validation within a CI pipeline. Use --output to save check results to a JSON file.
rolloutUsage: bytebase-action rollout [global flags] [rollout flags]
Creates a new release and initiates a rollout issue in the specified Bytebase --project.
If a --plan is specified, it rolls out that specific plan.
Otherwise, it applies the SQL files matching the --file-pattern to the defined --targets.
The rollout will proceed up to the specified --target-stage.
The release is named using the --release-id-template flag.
This action is configured via command-line flags. Global flags apply to all commands, while some commands have specific flags.
These flags apply to the main bytebase-action command and its subcommands (check, rollout).
--output: The output file location. The output file is a JSON file with the created resource names and check results.
"" (empty string)check command: outputs detailed check results including advices, affected rows, and risk levelsrollout command: outputs created resource names (release, plan, rollout)--url: The Bytebase instance URL.
https://demo.bytebase.com--service-account: The service account email.
"" (empty string). If not provided via flag, reads from the BYTEBASE_SERVICE_ACCOUNT environment variable.--service-account-secret: The service account password.
"" (empty string). If not provided via flag, reads from the BYTEBASE_SERVICE_ACCOUNT_SECRET environment variable.BYTEBASE_SERVICE_ACCOUNT_SECRET is the recommended way to handle the secret.--project: The target Bytebase project name.
projects/{project}projects/hr--targets: A comma-separated list or multiple uses of the flag specifying the target databases or database groups.
--plan is not specified for the rollout command.instances/{instance}/databases/{database}projects/{project}/databaseGroups/{databaseGroup}instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod--file-pattern: A glob pattern used to find SQL files.
check and rollout (when --plan is not specified) to locate relevant files."" (empty string)--declarative is false):
v1.2.3_description.sql, 1.0_initial_schema.sql, V2_add_users_table.sql^[vV]?(\d+(\.\d+)*)--declarative is true):
tables.sql, views.sql, indexes.sql)-- migration-type: ghostGhost, GHOST, ghost all work)ghost is supported for gh-ost migrations-- migration-type: ghost
ALTER TABLE large_table ADD COLUMN new_col VARCHAR(255);
MIGRATION_TYPE_UNSPECIFIED--declarative (experimental): Use declarative mode for SQL schema management instead of versioned migrations.
tables.sql, views.sql)YYYYMMDD.HHMMSSfalsecheck Command Specific FlagsThese flags are specific to the check subcommand (bytebase-action check).
--check-release: Determines whether to fail the command based on check results.
SKIP: Do not fail regardless of check results (default behavior).FAIL_ON_WARNING: Fail if there are warnings or errors in the check results.FAIL_ON_ERROR: Fail only if there are errors in the check results.SKIProllout Command Specific FlagsThese flags are specific to the rollout subcommand (bytebase-action rollout).
--target-stage: The target stage up to which the rollout should proceed. If not specified, the rollout will be created but will not wait for completion.
environments/{environment}environments/prod--plan: The specific plan to rollout.
projects/{project}/plans/{plan}--file-pattern and --targets flags, meaning they will be ignored.Declarative mode is an experimental feature currently in development that allows you to manage database schemas as desired state definitions rather than versioned migrations.
To enable declarative mode, add the --declarative flag to your command:
bytebase-action rollout --declarative --file-pattern="schema/*.sql" [other flags]
In declarative mode, your SQL files represent the complete desired state of your database schema. The system will automatically:
When using declarative mode, you must follow these steps:
Export Schema to download your current database schema.--declarative flag to apply your changes.Database Support: Currently only PostgreSQL is supported.
Supported SQL Statements: The following PostgreSQL statements are supported:
CREATE TABLECREATE INDEX / CREATE UNIQUE INDEXCREATE VIEWCREATE SEQUENCECREATE FUNCTIONALTER SEQUENCESchema Requirements: You must use fully qualified names (with schema prefix) for all database objects in your schema files:
-- Correct: fully qualified name
CREATE TABLE public.users (
id INTEGER NOT NULL,
name VARCHAR(100) NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (id)
);
-- Incorrect: unqualified name
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR(100) NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (id)
);
Constraint Requirements: PRIMARY KEY, UNIQUE, FOREIGN KEY, and CHECK constraints must be defined at table level with explicit names. Only NOT NULL, DEFAULT, and GENERATED constraints are allowed at column level:
-- Correct: table-level constraints with explicit names
CREATE TABLE public.users (
id INTEGER NOT NULL, -- NOT NULL is allowed at column level
email TEXT NOT NULL, -- NOT NULL is allowed at column level
created_at TIMESTAMP DEFAULT NOW(), -- DEFAULT is allowed at column level
CONSTRAINT pk_users PRIMARY KEY (id),
CONSTRAINT uk_users_email UNIQUE (email),
CONSTRAINT chk_users_email CHECK (email LIKE '%@%')
);
-- Incorrect: PRIMARY KEY, UNIQUE, CHECK at column level
CREATE TABLE public.users (
id INTEGER PRIMARY KEY, -- ERROR: PRIMARY KEY must be at table level
email TEXT UNIQUE, -- ERROR: UNIQUE must be at table level
age INTEGER CHECK (age >= 0), -- ERROR: CHECK must be at table level
CONSTRAINT pk_users PRIMARY KEY (id)
);
-- Incorrect: unnamed constraints
CREATE TABLE public.users (
id INTEGER NOT NULL,
email TEXT NOT NULL,
PRIMARY KEY (id), -- ERROR: constraint must have explicit name
UNIQUE (email), -- ERROR: constraint must have explicit name
CHECK (email LIKE '%@%') -- ERROR: constraint must have explicit name
);
Foreign Key References: Foreign key references must use fully qualified table names:
-- Correct: fully qualified reference
CREATE TABLE public.orders (
id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
CONSTRAINT pk_orders PRIMARY KEY (id),
CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
);
-- Incorrect: unqualified reference
CREATE TABLE public.orders (
id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
CONSTRAINT pk_orders PRIMARY KEY (id),
CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id)
);
Index Naming: All indexes must have explicit names:
-- Correct: named index
CREATE INDEX idx_users_email ON public.users(email);
-- Incorrect: unnamed index
CREATE INDEX ON public.users(email);