Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/import sql schema #12

Merged
merged 11 commits into from
May 13, 2023
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chrono = "0.4.24"
chrono-human-duration = "0.1.1"
clap = { version = "4.1.8", features = ["derive"] }
cli-table = "0.4.7"
convert_case = "0.6.0"
diffy = "0.3.0"
fs_extra = "1.3.0"
include_dir = "0.7.3"
Expand All @@ -28,10 +29,12 @@ rust-ini = "0.18"
serde_json = "1.0"
serde = { version = "1.0.144", features = ["derive"] }
tokio = { version = "1.26.0", features = ["full"] }
sqlparser = "0.33.0"
surrealdb = "1.0.0-beta.9"

[dev-dependencies]
assert_cmd = "2.0.10"
dir-diff = "0.3.2"
pretty_assertions = "1.1.0"
serial_test = "2.0.0"
tokio-test = "0.4.2"
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ A migration file represents a change in SurrealDB data. It can be a change in th
You can start a migration project by scaffolding a new project using the following command line:

```
surrealdb-migrations scaffold empty
surrealdb-migrations scaffold template empty
```

This will create the necessary folders and files in order to perform migrations. The `empty` template should look like this:
Expand Down Expand Up @@ -144,7 +144,7 @@ To help you get started quickly, there is a list of predefined templates you can
You can scaffold a project using any of these templates using the following command line:

```
surrealdb-migrations scaffold <TEMPLATE>
surrealdb-migrations scaffold template <TEMPLATE>
```

## Configuration
Expand Down
Empty file added schema-files/empty.sql
Empty file.
7 changes: 7 additions & 0 deletions schema-files/mssql/create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE Post (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Title] NVARCHAR(255),
[Content] TEXT,
[Status] NVARCHAR(50),
[CreatedAt] DATETIME,
)
7 changes: 7 additions & 0 deletions schema-files/mssql/create_table_with_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE DailySales (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Value] DECIMAL(10, 2) NOT NULL,
[Date] DATE NOT NULL,
);

CREATE INDEX IX_DailySales_Sales ON DailySales (Date);
20 changes: 20 additions & 0 deletions schema-files/mssql/create_table_with_many_types.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE Test (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Char] CHAR,
[NChar] CHAR,
[Varchar] VARCHAR(255),
[NVarchar] NVARCHAR(255),
[Text] TEXT,
[Bit] BIT,
[TinyInt] TINYINT,
[SmallInt] SMALLINT,
[Int] INT,
[BigInt] BIGINT,
[Decimal] DECIMAl(10, 2),
[Numeric] NUMERIC,
[Float] FLOAT,
[DateTime] DATETIME,
[Timestamp] TIMESTAMP,
[Json] JSON,
[Variant] SQL_VARIANT,
)
8 changes: 8 additions & 0 deletions schema-files/mssql/create_table_with_multi_column_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE Product (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Name] NVARCHAR(255),
[Color] NVARCHAR(255),
[Size] NVARCHAR(255),
);

CREATE INDEX Vote_Name_Color_Size ON Product (Name, Color, Size);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE Vote (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Username] NVARCHAR(255),
[Movie] NVARCHAR(255),
);

CREATE UNIQUE INDEX Vote_Username_Movie_Unique ON Vote (Username, Movie);
7 changes: 7 additions & 0 deletions schema-files/mssql/create_table_with_not_null.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE Post (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Title] NVARCHAR(255) NOT NULL,
[Content] TEXT NOT NULL,
[Status] NVARCHAR(50) NOT NULL,
[CreatedAt] DATETIME NOT NULL,
)
25 changes: 25 additions & 0 deletions schema-files/mssql/create_table_with_relations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE Post (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Title] NVARCHAR(255) NOT NULL,
[Content] TEXT NOT NULL,
[Status] NVARCHAR(50) NOT NULL,
[CreatedAt] DATETIME NOT NULL,
);

CREATE TABLE User (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Username] NVARCHAR(255) NOT NULL UNIQUE,
[Email] NVARCHAR(255) NOT NULL UNIQUE,
[Password] NVARCHAR(255) NOT NULL,
[RegisteredAt] DATETIME NOT NULL,
);

CREATE TABLE Comment (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Content] TEXT NOT NULL,
[CreatedAt] DATETIME NOT NULL,
[User] UNIQUEIDENTIFIER NOT NULL,
[Post] UNIQUEIDENTIFIER NOT NULL,
FOREIGN KEY (User) REFERENCES User(Id),
FOREIGN KEY (Post) REFERENCES Post(Id),
);
3 changes: 3 additions & 0 deletions schema-files/mssql/create_table_with_script_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE ScriptMigration (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY
)
7 changes: 7 additions & 0 deletions schema-files/mssql/create_table_with_unique_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE User (
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
[Username] NVARCHAR(255) NOT NULL UNIQUE,
[Email] NVARCHAR(255) NOT NULL UNIQUE,
[Password] NVARCHAR(255) NOT NULL,
[RegisteredAt] DATETIME NOT NULL,
);
55 changes: 45 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use clap::{Parser, Subcommand};

#[derive(clap::ValueEnum, Debug, Clone)]
pub enum ScaffoldTemplate {
Empty,
Blog,
Ecommerce,
}

#[derive(Parser, Debug)]
#[clap(name = "surrealdb-migrations", version, author = "Odonno")]
/// An awesome CLI for SurrealDB migrations (provides commands to scaffold, create and apply migrations).
/// An awesome CLI for SurrealDB migrations
/// (provides commands to scaffold, create and apply migrations).
pub struct Args {
#[command(subcommand)]
pub command: Action,
Expand All @@ -20,8 +14,8 @@ pub enum Action {
/// Scaffold a new SurrealDB project (with migrations)
#[clap(aliases = vec!["s"])]
Scaffold {
/// Type of migration project to create
template: ScaffoldTemplate,
#[command(subcommand)]
command: ScaffoldAction,
},
/// Create a new migration file
#[clap(aliases = vec!["c"])]
Expand Down Expand Up @@ -90,6 +84,47 @@ pub enum Action {
},
}

#[derive(Subcommand, Debug)]
pub enum ScaffoldAction {
/// Scaffold a new project from a predefined template
Template {
/// Predefined template used to scaffold the project
template: ScaffoldTemplate,
},
/// Scaffold a new project from an existing SQL schema file
Schema {
/// Path to the SQL schema file
schema: String,
/// Type of the database used in the SQL schema file
#[clap(long)]
db_type: ScaffoldSchemaDbType,
/// Preserve casing of the table and column names instead of converting them to snake_case
#[clap(long)]
preserve_casing: bool,
},
}

#[derive(clap::ValueEnum, Debug, Clone)]
pub enum ScaffoldTemplate {
Empty,
Blog,
Ecommerce,
}

#[derive(clap::ValueEnum, Debug, Clone)]
#[clap(rename_all = "lower")]
pub enum ScaffoldSchemaDbType {
BigQuery,
ClickHouse,
Hive,
MsSql,
MySql,
PostgreSql,
Redshift,
SQLite,
Snowflake,
}

#[derive(Subcommand, Debug)]
pub enum CreateAction {
#[clap(aliases = vec!["s"])]
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use cli::{Action, Args, CreateAction};
use cli::{Action, Args, CreateAction, ScaffoldAction};
use create::CreateOperation;

mod apply;
Expand All @@ -20,7 +20,14 @@ async fn main() -> Result<()> {
let args = Args::parse();

match args.command {
Action::Scaffold { template } => scaffold::main(template),
Action::Scaffold { command } => match command {
ScaffoldAction::Template { template } => scaffold::template::main(template),
ScaffoldAction::Schema {
schema,
db_type,
preserve_casing,
} => scaffold::schema::main(schema, db_type, preserve_casing),
},
Action::Create { command, name } => match name {
Some(name) => create::main(name, CreateOperation::Migration, None, false),
None => match command {
Expand Down
Loading