{"id":21476859,"url":"https://github.com/phogolabs/prana","last_synced_at":"2025-06-24T09:38:45.718Z","repository":{"id":31921391,"uuid":"130819465","full_name":"phogolabs/prana","owner":"phogolabs","description":"Golang Database Management and Code Generation","archived":false,"fork":false,"pushed_at":"2023-01-05T09:02:57.000Z","size":808,"stargazers_count":106,"open_issues_count":3,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-06-18T22:54:24.252Z","etag":null,"topics":["database-management","database-migrations","database-modeling","golang","sql-script","sqlx"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phogolabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-24T08:10:07.000Z","updated_at":"2024-06-07T22:05:34.000Z","dependencies_parsed_at":"2023-01-14T20:06:38.964Z","dependency_job_id":null,"html_url":"https://github.com/phogolabs/prana","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phogolabs%2Fprana","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phogolabs%2Fprana/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phogolabs%2Fprana/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phogolabs%2Fprana/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phogolabs","download_url":"https://codeload.github.com/phogolabs/prana/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226033262,"owners_count":17563125,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database-management","database-migrations","database-modeling","golang","sql-script","sqlx"],"created_at":"2024-11-23T11:10:42.711Z","updated_at":"2024-11-23T11:10:43.336Z","avatar_url":"https://github.com/phogolabs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prana\n\n[![Documentation][godoc-img]][godoc-url]\n[![License][license-img]][license-url]\n[![Build Status][travis-img]][travis-url]\n[![Coverage][codecov-img]][codecov-url]\n[![Go Report Card][report-img]][report-url]\n\n*Golang Database Manager*\n\n[![Prana][prana-img]][prana-url]\n\n## Overview\n\nPrana is a package for rapid application development with relational databases in\nGolang.  It has a command line interface that provides:\n\n- SQL Migrations\n- Embedded SQL Scripts\n- Model generation from SQL schema\n\n## Installation\n\n#### GitHub\n\n```console\n$ go get -u github.com/phogolabs/prana\n$ go install github.com/phogolabs/prana/cmd/prana\n```\n\n#### Homebrew (for Mac OS X)\n\n```console\n$ brew tap phogolabs/tap\n$ brew install prana\n```\n\n## Introduction\n\nNote that Prana is in BETA. We may introduce breaking changes until we reach\nv1.0.\n\n### SQL Migrations\n\nEach migration is a SQL script that contains two operations for upgrade and\nrollback. They are labelled with the following comments:\n\n- `-- name: up` for upgrade\n- `-- name: down` for revert\n\nIn order to prepare the project for migration, you have to set it up:\n\n```console\n$ prana migration setup\n```\n\nThen you can create a migration with the following command:\n\n```console\n$ prana migration create schema\n```\n\nThe command will create the following migration file in `/database/migration`:\n\n```console\n$ tree database\n\ndatabase/\n└── migration\n    ├── 00060524000000_setup.sql\n    └── 20180329162010_schema.sql\n```\n\nThe `20180329162010_schema.sql` migration has similar to example below format:\n\n```sql\n-- Auto-generated at Thu Mar 29 16:20:10 CEST 2018\n-- Please do not change the name attributes\n\n-- name: up\nCREATE TABLE users (\n  id INT PRIMARY KEY NOT NULL,\n  first_name TEXT NOT NULL,\n  last_name TEXT\n);\n\nGO; -- split execution of the migration\n\nINSERT INTO users (id, first_name, last_name) VALUES (1, 'John', 'Doe');\n\n-- name: down\nDROP TABLE IF EXISTS users;\n```\n\nYou can run the migration with the following command:\n\n```console\n$ prana migration run\n```\n\nIf you want to rollback the migration you have to revert it:\n\n```console\n$ prana migration revert\n```\n\nIf you have an SQL script that is compatible with particular database, you can\nappend the database's driver name suffix. For instance if you want to run part\nof a particular migration for MySQL, you should have the following directory\ntree:\n\n```console\n$ tree database\n\ndatabase/\n└── migration\n    ├── 00060524000000_setup.sql\n    ├── 20180406190015_users.sql\n    ├── 20180406190015_users_mysql.sql\n    └── 20180406190015_users_sqlite3.sql\n```\n\nPrana will execute the following migrations with `users` suffix, when MySQL\ndriver is used:\n\n- `20180406190015_users.sql`\n- `20180406190015_users_mysql.sql`\n\nPresently the following suffixes are supported:\n\n- `sqlite3`\n- `mysql`\n- `postgres`\n\n## SQL Schema and Code Generation\n\nLet's assume that we want to generate a mode for the `users` table.\n\nYou can use the `prana` command line interface to generate a package that\ncontains Golang structs, which represents each table from the desired schema.\n\nFor that purpose you should call the following subcommand:\n\n```bash\n$ prana model sync\n```\n\nBy default the command will place the generated code in single `schema.go` file\nin `$PWD/database/model` package for the default database schema. Any other\nschemas will be placed in the same package but in separate files. You can\ncontrol the behavior by passing `--keep-schema` flag which will\ncause each schema to be generated in own package under the\n`/$PWD/database/model` package.\n\nYou can print the source code without generating a package by executing the\nfollowing command:\n\n```bash\n$ prana model print\n```\n\nNote that you can specify the desired schema or tables by providing the correct\narguments.\n\nIf you pass `--extra-tag` argument, you can specify which tag to be included in\nyour final result. Supported extra tags are:\n\n- [json](https://golang.org/pkg/encoding/json/)\n- [xml](https://golang.org/pkg/encoding/xml/)\n- [validate](https://github.com/go-playground/validator/blob/v9/_examples/simple/main.go#L11) to validates fields by [validator](https://github.com/go-playground/validator) package\n\nThe model representation of the users table is:\n\n```golang\npackage model\n\nimport \"github.com/phogolabs/schema\"\n\n// User represents a data base table 'users'\ntype User struct {\n\t// ID represents a database column 'id' of type 'INT PRIMARY KEY NOT NULL'\n\tID int `db:\"id,primary_key,not_null\" json:\"id\" xml:\"id\" validate:\"required\"`\n\n\t// FirstName represents a database column 'first_name' of type 'TEXT NOT NULL'\n\tFirstName string `db:\"first_name,not_null\" json:\"first_name\" xml:\"first_name\" validate:\"required\"`\n\n\t// LastName represents a database column 'last_name' of type 'TEXT NULL'\n\tLastName schema.NullString `db:\"last_name,null\" json:\"last_name\" xml:\"last_name\" validate:\"-\"`\n}\n```\n\nNote that the code generation depends on two packages. In order to produce a\nsource code that compiles you should have in your `$GOPATH/src` directory\ninstalled:\n\n- [schema](https://github.com/phogolabs/schema) package\n\nThe generated `db` tag is recognized by\n[orm.Gateway](https://godoc.org/github.com/phogolabs/orm#Gateway) as well as\n[sqlx](https://github.com/jmoiron/sqlx).\n\nIf you wan to generate models for [gorm](http://gorm.io), you should\npass `--orm-tag gorm`. Note that constraints like unique or indexes are not\nincluded for now.\n\n```console\n$ prana model sync --orm-tag gorm -e json -e xml -e validate\n```\n\nThe command above will produce the following model:\n\n```golang\npackage model\n\nimport \"github.com/phogolabs/schema\"\n\n// User represents a data base table 'users'\ntype User struct {\n\t// ID represents a database column 'id' of type 'INT PRIMARY KEY NOT NULL'\n\tID int `gorm:\"column:id;type:int;primary_key;not null\" json:\"id\" xml:\"id\" validate:\"required\"`\n\n\t// FirstName represents a database column 'first_name' of type 'TEXT NOT NULL'\n\tFirstName string `gorm:\"column:first_name;type:text;not null\" json:\"first_name\" xml:\"first_name\" validate:\"required\"`\n\n\t// LastName represents a database column 'last_name' of type 'TEXT NULL'\n\tLastName schema.NullString `gorm:\"column:last_name;type:text;null\" json:\"last_name\" xml:\"last_name\" validate:\"-\"`\n}\n```\n\n### SQL Scripts and Commands\n\nAlso, it provides a way to work with embeddable SQL scripts which can be\nexecuted easily by [ORM][orm-url] as SQL Routines. You can see the\n[ORM example](https://github.com/phogolabs/orm/tree/master/example) to\nunderstand more about that. First of all you have create a script that contains\nyour SQL statements.\n\nThe easies way to generate a SQL script with correct format is by using `prana`\ncommand line interface:\n\n```console\n$ prana routine create show-sqlite-master\n```\n\nThe command above will generate a script in your `$PWD/database/routine`;\n\n```console\n$ tree database/\n\ndatabase/\n└── routine\n    └── 20180328184257.sql\n```\n\nYou can enable the script for particular type of database by adding the driver\nname as suffix: `20180328184257_slite3.sql`.\n\nIt has the following contents:\n\n```sql\n-- Auto-generated at Wed Mar 28 18:42:57 CEST 2018\n-- name: show-sqlite-master\nSELECT type,name,rootpage FROM sqlite_master;\n```\n\nThe `-- name: show-sqlite-master` comment define the name of the command in\nyour SQL script. The SQL statement afterwards is considered as the command\nbody. Note that the command must have only one statement.\n\nThen you can use the `prana` command line interface to execute the command:\n\n```console\n$ prana script run show-sqlite-master\n\nRunning command 'show-sqlite-master' from '$PWD/database/script'\n+-------+-------------------------------+----------+\n| TYPE  |             NAME              | ROOTPAGE |\n+-------+-------------------------------+----------+\n| table | migrations                    |        2 |\n| index | sqlite_autoindex_migrations_1 |        3 |\n+-------+-------------------------------+----------+\n```\n\nYou can also generate all CRUD operations for given table. The command below\nwill generate a SQL script that contains SQL queries for each table in the\ndefault schema:\n\n```console\n$ prana routine sync\n```\n\nIt will produce the following script in `$PWD/database/rotuine`:\n\n```sql\n-- name: select-all-users\nSELECT * FROM users;\n\n-- name: select-user\nSELECT * FROM users\nWHERE id = ?;\n\n-- name: insert-user\nINSERT INTO users (id, first_name, last_name)\nVALUES (?, ?, ?);\n\n-- name: update-user\nUPDATE users\nSET first_name = ?, last_name = ?\nWHERE id = ?;\n\n-- name: delete-user\nDELETE FROM users\nWHERE id = ?;\n```\n\n### Command Line Interface Advance Usage\n\nBy default the CLI work with `sqlite3` database called `prana.db` at your current\ndirectory.\n\nPrana supports:\n\n- PostgreSQL\n- MySQL\n- SQLite\n\nIf you want to change the default connection, you can pass it via command line\nargument:\n\n```bash\n$ prana --database-url [driver-name]://[connection-string] [command]\n```\n\nprana uses a URL schema to determines the right database driver. If you want to\npass the connection string via environment variable, you should export\n`PRANA_DB_URL`.\n\n### Help\n\nFor more information, how you can change the default behavior you can read the\nhelp documentation by executing:\n\n```console\n$ prana -h\n\nNAME:\n   prana - Golang Database Manager\n\nUSAGE:\n   prana [global options]\n\nVERSION:\n   1.0-beta-05\n\nCOMMANDS:\n     migration   A group of commands for generating, running, and reverting migrations\n     model       A group of commands for generating object model from database schema\n     repository  A group of commands for generating database repository from schema\n     routine     A group of commands for generating, running, and removing SQL commands\n     help, h     Shows a list of commands or help for one command\n\nGLOBAL OPTIONS:\n   --database-url value  Database URL (default: \"sqlite3://prana.db\") [$PRANA_DB_URL]\n   --log-format value    format of the logs [$PRANA_LOG_FORMAT]\n   --log-level value     level of logging (default: \"info\") [$PRANA_LOG_LEVEL]\n   --help, -h            show help\n   --version, -v         print the version\n```\n\n## Contributing\n\nWe are welcome to any contributions. Just fork the\n[project](https://github.com/phogolabs/prana).\n\n*logo made by [Free Pik][logo-author-url]*\n\n[report-img]: https://goreportcard.com/badge/github.com/phogolabs/prana\n[report-url]: https://goreportcard.com/report/github.com/phogolabs/prana\n[logo-author-url]: https://www.freepik.com/free-vector/abstract-cross-logo-template_1185919.htm\n[logo-license]: http://creativecommons.org/licenses/by/3.0/\n[prana-url]: https://github.com/phogolabs/prana\n[prana-img]: doc/img/logo.png\n[codecov-url]: https://codecov.io/gh/phogolabs/prana\n[codecov-img]: https://codecov.io/gh/phogolabs/prana/branch/master/graph/badge.svg\n[travis-img]: https://travis-ci.org/phogolabs/prana.svg?branch=master\n[travis-url]: https://travis-ci.org/phogolabs/prana\n[prana-url]: https://github.com/phogolabs/prana\n[godoc-url]: https://godoc.org/github.com/phogolabs/prana\n[godoc-img]: https://godoc.org/github.com/phogolabs/prana?status.svg\n[license-img]: https://img.shields.io/badge/license-MIT-blue.svg\n[license-url]: LICENSE\n[loukoum-url]: https://github.com/ulule/loukoum\n[orm-url]: https://github.com/phogolabs/orm\n[sqlx-url]: https://github.com/jmoiron/sqlx\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphogolabs%2Fprana","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphogolabs%2Fprana","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphogolabs%2Fprana/lists"}