# sqlite3def ``` Usage: sqlite3def [OPTION]... FILENAME --export sqlite3def [OPTION]... FILENAME --apply < desired.sql sqlite3def [OPTION]... FILENAME --dry-run < desired.sql sqlite3def [OPTION]... current.sql < desired.sql Application Options: -f, --file=FILENAME Read desired SQL from the file, rather than stdin (default: -) --dry-run Don't run DDLs but just show them --apply Apply DDLs to the database (default, but will require this flag in future versions) --export Just dump the current schema to stdout --enable-drop Enable destructive changes such as DROP for TABLE, SCHEMA, ROLE, USER, FUNCTION, PROCEDURE, TRIGGER, VIEW, INDEX, SEQUENCE, TYPE --config=PATH YAML configuration file (can be specified multiple times) --config-inline=YAML YAML configuration as inline string (can be specified multiple times) --help Show this help --version Show version information ``` ## Synopsis ```shell # Create SQLite database and tables $ sqlite3 mydb.db "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);" # Export current schema $ sqlite3def mydb.db --export CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT ); # Save it to edit $ sqlite3def mydb.db --export > schema.sql ``` Update schema.sql as follows: ```diff CREATE TABLE users ( id INTEGER PRIMARY KEY, - name TEXT + name TEXT, + email TEXT NOT NULL ); + +CREATE INDEX idx_users_email ON users(email); ``` And then run: ```shell # Preview migration plan (dry run) $ sqlite3def mydb.db --dry-run < schema.sql -- dry run -- BEGIN; ALTER TABLE users ADD COLUMN email TEXT NOT NULL; CREATE INDEX idx_users_email ON users(email); COMMIT; # Apply DDLs $ sqlite3def mydb.db --apply < schema.sql -- Apply -- BEGIN; ALTER TABLE users ADD COLUMN email TEXT NOT NULL; CREATE INDEX idx_users_email ON users(email); COMMIT; # Operations are idempotent - safe to run multiple times $ sqlite3def mydb.db --apply < schema.sql -- Nothing is modified -- # By default, DROP operations are skipped (safe mode) # To enable DROP TABLE, DROP COLUMN, etc., use --enable-drop $ sqlite3def mydb.db --apply --enable-drop < schema.sql -- Apply -- BEGIN; DROP TABLE old_users; COMMIT; # Use config file to filter tables $ cat > config.yml <