Skip to content

Commit

Permalink
Another diesel attempt: using the diesel_cli this time
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Sep 1, 2024
1 parent 392dcf7 commit 2c91e27
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 91 deletions.
1 change: 0 additions & 1 deletion src/lib/diesel_schemas/custom_to_fro.rs

This file was deleted.

66 changes: 0 additions & 66 deletions src/lib/diesel_schemas/mod.rs

This file was deleted.

9 changes: 9 additions & 0 deletions src/lib/diesel_specific/diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]

[migrations_directory]
dir = "src/lib/diesel_specific/migrations"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/lib/diesel_specific/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[path = "src/schema.rs"]
pub mod schema;
pub use crate::diesel_specific;
63 changes: 63 additions & 0 deletions src/lib/diesel_specific/src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @generated automatically by Diesel CLI.

diesel::table! {
execution_plans (id) {
id -> Int4,
steps -> Jsonb,
steps_to_run -> Jsonb,
name -> Text,
}
}

diesel::table! {
steps (id) {
id -> Int4,
name -> Text,
config -> Jsonb,
}
}

diesel::table! {
tasks (id) {
id -> Int4,
clear -> Nullable<Bool>,
description -> Nullable<Text>,
category -> Nullable<Text>,
disabled -> Nullable<Bool>,
private -> Nullable<Bool>,
deprecated -> Nullable<Jsonb>,
extend -> Nullable<Text>,
workspace -> Nullable<Bool>,
plugin -> Nullable<Text>,
watch -> Nullable<Jsonb>,
condition -> Nullable<Jsonb>,
condition_script -> Nullable<Jsonb>,
condition_script_runner_args -> Nullable<Jsonb>,
ignore_errors -> Nullable<Bool>,
force -> Nullable<Bool>,
env_files -> Nullable<Jsonb>,
env -> Nullable<Jsonb>,
cwd -> Nullable<Text>,
alias -> Nullable<Text>,
linux_alias -> Nullable<Text>,
windows_alias -> Nullable<Text>,
mac_alias -> Nullable<Text>,
install_crate -> Nullable<Jsonb>,
install_crate_args -> Nullable<Jsonb>,
install_script -> Nullable<Jsonb>,
command -> Nullable<Text>,
args -> Nullable<Jsonb>,
script -> Nullable<Jsonb>,
script_runner -> Nullable<Text>,
script_runner_args -> Nullable<Jsonb>,
script_extension -> Nullable<Text>,
run_task -> Nullable<Jsonb>,
dependencies -> Nullable<Jsonb>,
toolchain -> Nullable<Jsonb>,
linux -> Nullable<Jsonb>,
windows -> Nullable<Jsonb>,
mac -> Nullable<Jsonb>,
}
}

diesel::allow_tables_to_appear_in_same_query!(execution_plans, steps, tasks,);
4 changes: 3 additions & 1 deletion src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ mod command;
mod condition;
pub mod config;
mod descriptor;

#[cfg(feature = "diesel")]
pub mod diesel_schemas;
pub mod diesel_specific;

mod environment;
pub mod error;
mod execution_plan;
Expand Down
48 changes: 25 additions & 23 deletions src/lib/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#[path = "types_test.rs"]
mod types_test;

use crate::legacy;
use crate::plugin::types::Plugins;
use crate::{diesel_schemas, legacy};
use ci_info::types::CiInfo;
use git_info::types::GitInfo;
use indexmap::{IndexMap, IndexSet};
Expand All @@ -18,9 +18,6 @@ use std::collections::HashMap;
#[cfg(feature = "diesel")]
use diesel::dsl::*;

#[cfg(feature = "diesel")]
use diesel_schemas::*;

/// Returns the platform name
pub fn get_platform_name() -> String {
if cfg!(windows) {
Expand Down Expand Up @@ -1136,8 +1133,13 @@ pub enum ConditionScriptValue {
Text(Vec<String>),
}

#[cfg(feature = "diesel")]
fn foo() {
crate::diesel_specific::schema::execution_plans;
}

#[cfg_attr(feature = "diesel", derive(diesel::Queryable, diesel::Insertable))]
#[cfg_attr(feature = "diesel", diesel(table_name = task))]
#[cfg_attr(feature = "diesel", diesel(table_name = crate::diesel_specific::schema::task))]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds a single task configuration such as command and dependencies list
pub struct Task {
Expand All @@ -1163,7 +1165,7 @@ pub struct Task {

/// if not false, this task is defined as deprecated
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub deprecated: Option<DeprecationInfo>,

/// Extend any task based on the defined name
Expand All @@ -1180,22 +1182,22 @@ pub struct Task {

/// set to true to watch for file changes and invoke the task operation
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub watch: Option<TaskWatchOptions>,

/// if provided all condition values must be met in order for the task to be invoked (will not stop dependencies)
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub condition: Option<TaskCondition>,

/// if script exit code is not 0, the command/script of this task will not be invoked, dependencies however will be
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub condition_script: Option<ConditionScriptValue>,

/// The script runner arguments before the script file path
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub condition_script_runner_args: Option<Vec<String>>,

/// if true, any error while executing the task will be printed but will not break the build
Expand All @@ -1208,12 +1210,12 @@ pub struct Task {

/// The env files to setup before running the task commands
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub env_files: Option<Vec<EnvFile>>,

/// The env vars to setup before running the task commands
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub env: Option<IndexMap<String, EnvValue>>,

/// The working directory for the task to execute its command/script
Expand All @@ -1238,7 +1240,7 @@ pub struct Task {

/// if defined, the provided crate will be installed (if needed) before running the task
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub install_crate: Option<InstallCrate>,

/// additional cargo install arguments
Expand All @@ -1259,7 +1261,7 @@ pub struct Task {

/// If command is not defined, and script is defined, the provided script will be executed
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub script: Option<ScriptValue>,

/// The script runner (defaults to cmd in windows and sh for other platforms)
Expand All @@ -1268,7 +1270,7 @@ pub struct Task {

/// The script runner arguments before the script file path
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub script_runner_args: Option<Vec<String>>,

/// The script file extension
Expand All @@ -1277,32 +1279,32 @@ pub struct Task {

/// The task name to execute
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub run_task: Option<RunTaskInfo>,

/// A list of tasks to execute before this task
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub dependencies: Option<Vec<DependencyIdentifier>>,

/// The rust toolchain used to invoke the command or install the needed crates/components
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub toolchain: Option<ToolchainSpecifier>,

/// override task if runtime OS is Linux (takes precedence over alias)
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub linux: Option<PlatformOverrideTask>,

/// override task if runtime OS is Windows (takes precedence over alias)
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub windows: Option<PlatformOverrideTask>,

/// override task if runtime OS is Mac (takes precedence over alias)
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::pg::types::sql_types::Jsonb))]
#[cfg_attr(feature = "diesel", diesel(deserialize_as = diesel::sql_types::Jsonb))]
pub mac: Option<PlatformOverrideTask>,
}

Expand Down Expand Up @@ -2549,7 +2551,7 @@ impl ExternalConfig {
}

#[cfg_attr(feature = "diesel", derive(diesel::Queryable, diesel::Insertable))]
#[cfg_attr(feature = "diesel", diesel(table_name = step))]
#[cfg_attr(feature = "diesel", diesel(table_name = crate::diesel_specific::schema::step))]
#[derive(Serialize, Deserialize, Clone, Debug)]
/// Execution plan step to execute
pub struct Step {
Expand All @@ -2561,7 +2563,7 @@ pub struct Step {
}

#[cfg_attr(feature = "diesel", derive(diesel::Queryable, diesel::Insertable))]
#[cfg_attr(feature = "diesel", diesel(table_name = execution_plan))]
#[cfg_attr(feature = "diesel", diesel(table_name = crate::diesel_specific::schema::execution_plan))]
#[derive(Serialize, Deserialize, Clone, Debug)]
/// Execution plan which defines all steps to run and the order to run them
pub struct ExecutionPlan {
Expand Down

0 comments on commit 2c91e27

Please sign in to comment.