### Install sql-migrate Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Installs the sql-migrate library and command-line program using 'go get' or 'go install'. ```bash go get -v github.com/rubenv/sql-migrate/... # For Go version 1.18+ go install github.com/rubenv/sql-migrate/...@latest ``` -------------------------------- ### sql-migrate HttpFileSystemMigrationSource (http.FileSystem) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source from any type that implements `http.FileSystem`. ```go // OR: Read migrations from a `http.FileSystem` migrationSource := &migrate.HttpFileSystemMigrationSource{ FileSystem: httpFS, } ``` -------------------------------- ### Install sql-migrate with Oracle (godror) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Installs the sql-migrate library and command-line program with support for the Oracle godror driver. This requires Oracle Instant Client. ```bash go get -tags godror -v github.com/rubenv/sql-migrate/... ``` -------------------------------- ### sql-migrate HttpFileSystemMigrationSource (pkger) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source using pkger, which implements `http.FileSystem`. ```go // OR: Use pkger which implements `http.FileSystem` migrationSource := &migrate.HttpFileSystemMigrationSource{ FileSystem: pkger.Dir("/db/migrations"), } ``` -------------------------------- ### Install sql-migrate with Oracle (oci8) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Installs the sql-migrate library and command-line program with support for the Oracle oci8 driver. This requires Oracle Instant Client. ```bash go get -tags oracle -v github.com/rubenv/sql-migrate/... ``` -------------------------------- ### sql-migrate FileMigrationSource Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source by reading migration files from a specified directory. ```go // OR: Read migrations from a folder: migrations := &migrate.FileMigrationSource{ Dir: "db/migrations", } ``` -------------------------------- ### sql-migrate AssetMigrationSource Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source from bindata using Asset and AssetDir functions. ```go // OR: Use migrations from bindata: migrations := &migrate.AssetMigrationSource{ Asset: Asset, AssetDir: AssetDir, Dir: "migrations", } ``` -------------------------------- ### sql-migrate Configuration File Example Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Provides an example of a dbconfig.yml file used by sql-migrate to configure database connections and migration directories for different environments. ```yml development: dialect: sqlite3 datasource: test.db dir: migrations/sqlite3 production: dialect: postgres datasource: dbname=myapp sslmode=disable dir: migrations/postgres table: migrations ``` -------------------------------- ### Oracle Instant Client Download (macOS) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example command to download the Oracle Instant Client for macOS. Users on other systems should refer to the Oracle Instant Client download page. ```bash wget https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basic-macos.x64-19.3.0.0.0dbru.zip ``` -------------------------------- ### sql-migrate MemoryMigrationSource Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source from hardcoded strings in memory using sql-migrate. Each migration has an ID, Up SQL, and Down SQL. ```go // Hardcoded strings in memory: migrations := &migrate.MemoryMigrationSource{ Migrations: []*migrate.Migration{ &migrate.Migration{ Id: "123", Up: []string{"CREATE TABLE people (id int)"}, Down: []string{"DROP TABLE people"}, }, }, } ``` -------------------------------- ### sql-migrate PackrMigrationSource (Deprecated) Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example of creating a migration source using Packr. Note that Packr is no longer supported, and the `embed` package is recommended. ```go // OR: Use migrations from a packr box // Note: Packr is no longer supported, your best option these days is [embed](https://pkg.go.dev/embed) migrations := &migrate.PackrMigrationSource{ Box: packr.New("migrations", "./migrations"), } ``` -------------------------------- ### Usage with sqlx Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Explains how to use the `migrate.Exec` function with a `sqlx.DB` object. The example shows dereferencing the underlying `sql.DB` from the `*sqlx.DB` pointer to pass it to the migration function. ```go n, err := migrate.Exec(db.DB, "sqlite3", migrations, migrate.Up) // ^^^ <-- Here db is a *sqlx.DB, the db.DB field is the plain sql.DB if err != nil { // Handle errors! } ``` -------------------------------- ### Oracle (oci8) Configuration Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example configuration for using sql-migrate with the oci8 Oracle driver. Specifies the dialect, data source, migration directory, and migration table. ```yml development: dialect: oci8 datasource: user/password@localhost:1521/sid dir: migrations/oracle table: migrations ``` -------------------------------- ### Oracle (godror) Configuration Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Example configuration for using sql-migrate with the godror Oracle driver. Specifies the dialect, data source, migration directory, and migration table. ```yml development: dialect: godror datasource: user/password@localhost:1521/sid dir: migrations/oracle table: migrations ``` -------------------------------- ### sql-migrate 'up' Command Options Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Details the available options for the 'sql-migrate up' command, including configuration file, environment, limit, version, and dryrun. ```APIDOC sql-migrate up --help Usage: sql-migrate up [options] ... Migrates the database to the most recent version available. Options: -config=dbconfig.yml Configuration file to use. -env="development" Environment. -limit=0 Limit the number of migrations (0 = unlimited). -version Run migrate up to a specific version, eg: the version number of migration 1_initial.sql is 1. -dryrun Don't apply migrations, just print them. ``` -------------------------------- ### Execute Migrations with sql-migrate Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Demonstrates how to use the `Exec` function to apply migrations to a database. It takes the database connection, dialect, migration source, and direction (Up/Down) as arguments. ```go db, err := sql.Open("sqlite3", filename) if err != nil { // Handle errors! } n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up) if err != nil { // Handle errors! } fmt.Printf("Applied %d migrations!\n", n) ``` -------------------------------- ### Import sql-migrate Package Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Demonstrates how to import the sql-migrate package into a Go application. ```go import "github.com/rubenv/sql-migrate" ``` -------------------------------- ### Embedding Migrations with http.FileSystem Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Demonstrates embedding migrations using any library that implements the `http.FileSystem` interface, such as `vfsgen`, `parcello`, or `go-resources`. This provides flexibility in how migration files are managed and accessed. ```go migrationSource := &migrate.HttpFileSystemMigrationSource{ FileSystem: httpFS, } ``` -------------------------------- ### sql-migrate CLI Commands Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Lists the available commands for the sql-migrate command-line interface, including down, new, redo, status, and up. ```APIDOC sql-migrate --help usage: sql-migrate [--version] [--help] [] Available commands are: down Undo a database migration new Create a new migration redo Reapply the last migration status Show migration status up Migrates the database to the most recent version available ``` -------------------------------- ### Implementing a Custom Migration Source Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Shows the interface definition for a custom `MigrationSource`. Implementing this interface allows for custom logic in finding and providing migrations, enabling integration with various storage or discovery mechanisms. ```go type MigrationSource interface { FindMigrations() ([]*Migration, error) } // The resulting slice of migrations will be executed in the given order, so it should usually be sorted by the `Id` field. ``` -------------------------------- ### Embedding Migrations with Go Embed Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Illustrates how to embed SQL migration files into a Go application using the `embed` package. This allows for a single binary distribution. The `embed.FS` is then used with `EmbedFileSystemMigrationSource` to locate the migrations. ```go import "embed" //go:embed migrations/* var dbMigrations embed.FS // ... later in your code ... migrations := migrate.EmbedFileSystemMigrationSource{ FileSystem: dbMigrations, Root: "migrations", } ``` -------------------------------- ### Configure Oracle Instant Client Environment Variable Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Sets the LD_LIBRARY_PATH environment variable to point to the Oracle Instant Client directory, which is necessary for the driver to function. ```bash export LD_LIBRARY_PATH=your_oracle_office_path/instantclient_19_3 ``` -------------------------------- ### sql-migrate Status Output Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Shows the expected output format for the 'sql-migrate status' command, displaying applied migrations and their applied dates. ```bash $ sql-migrate status +---------------+----------------------------------------+ | MIGRATION | APPLIED | +---------------+----------------------------------------+ | 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC | | 2_record.sql | no | +---------------+----------------------------------------+ ``` -------------------------------- ### Configuration with Environment Variables Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Demonstrates how to use environment variables within the 'datasource' field of the dbconfig.yml file for secure credential management. ```yml production: dialect: postgres datasource: host=prodhost dbname=proddb user=${DB_USER} password=${DB_PASSWORD} sslmode=require dir: migrations table: migrations ``` -------------------------------- ### Basic SQL Migration Structure Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Defines the basic structure of an SQL migration file using special comments to delineate 'Up' and 'Down' sections. The 'Up' section contains SQL statements applied during migration, and the 'Down' section contains SQL statements for rollback. ```sql -- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE people (id int); -- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back DROP TABLE people; ``` -------------------------------- ### SQL Migration with Statement Delimiters Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Demonstrates how to handle complex SQL statements that contain semicolons by using `-- +migrate StatementBegin` and `-- +migrate StatementEnd` comments to define the boundaries of such statements. ```sql -- +migrate Up CREATE TABLE people (id int); -- +migrate StatementBegin CREATE OR REPLACE FUNCTION do_something() returns void AS $$ DECLARE create_query text; BEGIN -- Do something here END; $$ language plpgsql; -- +migrate StatementEnd -- +migrate Down DROP FUNCTION do_something(); DROP TABLE people; ``` -------------------------------- ### MySQL Datasource Configuration Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Explains the necessary modification for MySQL connections in the dbconfig.yml file, requiring '?parseTime=true' in the datasource string. ```yml production: dialect: mysql datasource: root@/dbname?parseTime=true dir: migrations/mysql table: migrations ``` -------------------------------- ### SQL Migration with No Transaction Source: https://github.com/rubenv/sql-migrate/blob/master/README.md Shows how to execute a migration statement outside of a transaction using the `notransaction` option. This is useful for SQL commands that cannot be run within a transaction, such as creating an index concurrently in PostgreSQL. ```sql -- +migrate Up notransaction CREATE UNIQUE INDEX CONCURRENTLY people_unique_id_idx ON people (id); -- +migrate Down DROP INDEX people_unique_id_idx; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.