Note: This project is built for Learning Purposes. It is not intended to be used in production environments
sql-mi is a tool designed to convert a schema for defining database tables into SQL code. This tool currently supports generating SQL code for SQLite databases. It enables you to define tables and their attributes in a simplified syntax and convert them into corresponding SQL statements.
To get started, download the specific version of sql-mi that matches your system from the Releases section. After downloading, follow these steps to set up the tool on your system:
-
Unzip the downloaded file.
-
Open a terminal window and navigate to the directory containing the unzipped files.
-
Make the binary executable:
chmod +x sql-mi
-
Optionally, move the binary to a directory in your system's
PATH
to make it accessible from anywhere.
To generate SQL code from your schema, follow this pattern:
./sql-mi -o output.sql input
Where:
output.sql
: The name of the output SQL file where the generated SQL code will be saved.input
: The name of the input file containing your schema.
Suppose you have a file named schema.txt
containing the following syntax:
table users
id int @id @auto_increment
created_at datetime @default(`CURRENT_TIMESTAMP`)
end
table contacts
name `varchar(255)` @default("text") @nullable
user_id int @reference("users", "id") @onDelete("RESTRICT") @onUpdate("CASCADE")
created_at datetime @default(`CURRENT_TIMESTAMP`)
end
You can generate the corresponding SQL code by running:
./sql-mi -o output.sql schema.txt
This will create an output.sql
file containing the generated SQL statements.
Sql-mi supports the following attributes for table columns:
@default
: Set the default value for the column. You can enter raw SQL like @default(`NOW()`) to use SQL functions.@id
: Mark the column as the primary key.@auto_increment
: Enable auto-increment for integer columns.@nullable
: Allow null values for the column.@reference
: Define a foreign key reference to another table.@onDelete
: Specify the behavior on delete (e.g., "RESTRICT", "CASCADE").@onUpdate
: Specify the behavior on update (e.g., "RESTRICT", "CASCADE").
Sql-mi supports the following data types:
int
string
datetime
bool
float
blob
Additionally, you can enter raw SQL data types like `varchar(255)`.
The project currently supports SQLite databases. You can specify the database provider by implementing the following syntax in your input file:
set provider sqlite
Contributions to this project are welcome! If you have ideas for improvements or new features, feel free to open an issue or submit a pull request.