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

Refinery migration support #32

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ members = [
"snowflake-api",
"snowflake-api/examples/tracing",
"snowflake-api/examples/polars",
"snowflake-api/examples/migrate",
]
27 changes: 23 additions & 4 deletions snowflake-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ version = "0.7.0"

[features]
default = ["cert-auth"]
all = ["cert-auth", "polars"]
all = ["cert-auth", "polars", "refinery"]
cert-auth = ["dep:snowflake-jwt"]
# support for conversion of arrow and json payloads to dataframes
polars = ["dep:polars-core", "dep:polars-io"]
refinery = [
"dep:refinery-core",
"dep:time",
"dep:once_cell",
"dep:itertools",
"arrow/prettyprint",
]


[dependencies]
arrow = "50"
Expand All @@ -39,16 +47,27 @@ thiserror = "1"
url = "2"
uuid = { version = "1", features = ["v4"] }
snowflake-jwt = { version = "0.3.0", optional = true }
tap = "1"

# polars-support
polars-io = { version = ">=0.32", features = ["json", "ipc_streaming"], optional = true}
polars-core = { version = ">=0.32", optional = true}
polars-io = { version = ">=0.32", features = [
"json",
"ipc_streaming",
], optional = true }
polars-core = { version = ">=0.32", optional = true }

# put request support
object_store = { version = "0.9", features = ["aws"] }
glob = { version = "0.3"}
glob = { version = "0.3" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

refinery-core = { version = "0.8.14", optional = true } # migrations
time = { version = "0.3.5", features = [
"parsing",
"formatting",
], optional = true }
once_cell = { version = "1", optional = true }
itertools = { version = "0.12", optional = true }

[dev-dependencies]
anyhow = "1"
Expand Down
16 changes: 16 additions & 0 deletions snowflake-api/examples/migrate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "migrate-example"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.79"
dotenv = "0.15.0"
tokio = { version = "1.35.1", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3"

refinery = { version = "0.8.14" }
snowflake-api = { path = "../../../snowflake-api", features = ["refinery"] }
18 changes: 18 additions & 0 deletions snowflake-api/examples/migrate/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use anyhow::Result;
use snowflake_api::SnowflakeApi;

mod embedded {
use refinery::embed_migrations;
embed_migrations!("./tests/sql_migrations");
}

#[tokio::main]
async fn main() -> Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();

let mut conn = SnowflakeApi::from_env()?;
embedded::migrations::runner().run_async(&mut conn).await?;

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- create a dummy snowflake table
CREATE
OR REPLACE TABLE snowflake (
id INT,
NAME STRING,
age INT
);
-- insert some data into the table
INSERT INTO
snowflake
VALUES
(
1,
'John',
25
),
(
2,
'Jane',
30
),
(
3,
'Jim',
35
),
(
4,
'Jill',
40
),
(
5,
'Jack',
45
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- SET previous_role = CURRENT_ROLE();
-- SET previous_database = CURRENT_DATABASE();


USE ROLE SYSADMIN;
CREATE OR REPLACE DATABASE test_db;

-- Assume Snowflake ACCOUNTADMIN role
USE ROLE ACCOUNTADMIN;

-- Create a new role 'test_role'
CREATE OR REPLACE ROLE test_role;

-- Grant some privileges to 'test_role'
GRANT USAGE ON DATABASE test_db TO ROLE test_role;
GRANT USAGE ON SCHEMA test_db.public TO ROLE test_role;


-- Create a file format for CSV files
CREATE OR REPLACE FILE FORMAT my_csv_format
TYPE = 'CSV'
FIELD_DELIMITER = ','
SKIP_HEADER = 1;

/*
USE ROLE IDENTIFIER($previous_role);
USE DATABASE IDENTIFIER($previous_database);
*/
1 change: 0 additions & 1 deletion snowflake-api/examples/polars/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Result;
use polars::frame::DataFrame;

use snowflake_api::SnowflakeApi;

#[tokio::main]
Expand Down
2 changes: 2 additions & 0 deletions snowflake-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use crate::responses::{ExecResponseRowType, SnowflakeType};
use crate::session::AuthError::MissingEnvArgument;

pub mod connection;
#[cfg(feature = "refinery")]
mod migration;
#[cfg(feature = "polars")]
mod polars;
mod put;
Expand Down
Loading
Loading