Automatic schema detection that creates the migrations for your Go project.
Write your schema in the Go struct format and migrate it to the database. This project is to help your database migration and automate the writing of migration file code.
To install this project on your machine you should have Go installed in your machine, if not follow this official step and install it in your machine.
Now installing Gobase:
go install github.com/Vikuuu/gobase/cmd/gobase@1.0.1
To use the gobase you must define a Yaml file in the root of your project directory named gobase.yml
. The content of the yaml file will be as follows:
database: "sqlite3"
schema_data: "./schema/users.go"
migration: "./migrations"
- Database: It will tell what database to use.
- Schema Data: This will point to the file in which you have defined your schema in form of struct.
- Migration: The directory in which all your migration files will be stored.
After defining the yaml file and creating a struct, to migrate up to the database use the following command:
gobase migrate
This command will create a migration file in your defined directory of migration, and migrate it to the database.
If your file that defines the struct that you want to convert it to database table, your file will look like this:
package database
type users struct {
ID int
Name string
}
Then the corresponding Migration file will look like this:
-- Up Migration
CREATE TABLE users (
id INTEGER,
name TEXT
);
-- Down Migration
DROP TABLE users;
Now if you make any changes to your struct, like:
package database
import "time"
type users struct {
ID int
Name string
CreatedAt time.Time
}
And then again run the migrate
command like:
gobase migrate
Then the new migration file will look like this:
-- Up Migration
ALTER TABLE users
ADD COLUMN created_at TIMESTAMP;
-- Down Migration
ALTER TABLE users DROP COLUMN created_at;
To migrate down use the following command:
gobase migrate down
Current the project only supports:
- Sqlite3
Currently the project has many limitations:
- Works only on the single table or struct.
- No table relations.
- Supports only a single file in the schema directory.
- Cannot migrate down or up to any desired version, can only migrate down only 1 step.
- Fork the repository.
- Create a new branch:
git checkout -b feature-name
- Make your changes.
- Push your branch:
git push origin feature-name
- Create a pull request.
This project in licensed under the MIT License