Skip to content

Commit 1563c6a

Browse files
author
Jakub Matraszek
committed
rust-lang-nursery#8: Create project in db
1 parent 320c00a commit 1563c6a

File tree

9 files changed

+95
-2
lines changed

9 files changed

+95
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE contributors;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE contributors (
2+
id SERIAL NOT NULL,
3+
name VARCHAR,
4+
email VARCHAR
5+
);
6+
7+
ALTER TABLE ONLY contributors
8+
ADD CONSTRAINT contributors_pkey PRIMARY KEY (id);
9+
10+
CREATE UNIQUE INDEX contributors_id_idx ON contributors USING btree (id);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE projects;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE projects (
2+
id SERIAL NOT NULL,
3+
name VARCHAR NOT NULL,
4+
path VARCHAR NOT NULL,
5+
github_link VARCHAR NOT NULL
6+
);
7+
8+
ALTER TABLE ONLY projects
9+
ADD CONSTRAINT projects_pkey PRIMARY KEY (id);
10+
11+
CREATE UNIQUE INDEX projects_id_idx ON projects USING btree (id);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE ONLY releases
2+
DROP COLUMN project_id;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE ONLY releases
2+
ADD COLUMN project_id integer NOT NULL;
3+
4+
ALTER TABLE ONLY releases
5+
ADD CONSTRAINT projects
6+
FOREIGN KEY (project_id)
7+
REFERENCES projects (id)
8+
ON DELETE CASCADE;

src/bin/populate.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ fn main() {
3838
.arg(Arg::with_name("filepath")
3939
.short("p")
4040
.long("path")
41-
.help("filepath of the rust source code")
41+
.help("filepath of the source code")
42+
.takes_value(true)
43+
.required(true))
44+
.arg(Arg::with_name("name")
45+
.short("n")
46+
.long("name")
47+
.help("name of the project")
48+
.takes_value(true)
49+
.required(true))
50+
.arg(Arg::with_name("github_link")
51+
.short("l")
52+
.long("link")
53+
.help("GitHub link of the project")
4254
.takes_value(true)
4355
.required(true))
4456
.get_matches();
@@ -67,9 +79,22 @@ fn main() {
6779
}
6880
}
6981

82+
// get name
83+
let name = matches.value_of("name").unwrap();
84+
println!("Project name: {}", name);
85+
7086
// get path to git repo
7187
let path = matches.value_of("filepath").unwrap();
72-
println!("Path to rust repo: {}", path);
88+
println!("Path to project's repo: {}", path);
89+
90+
// get github link
91+
let link = matches.value_of("github_link").unwrap();
92+
println!("GitHub link: {}", link);
93+
94+
// create project
95+
contributors::create_project(&connection, name, path, link);
96+
97+
panic!("Project created!");
7398

7499
// create releases
75100

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::process::Command;
2020
pub mod schema;
2121
pub mod models;
2222

23+
use self::models::{Project, NewProject};
2324
use self::models::{Commit, NewCommit};
2425
use self::models::{Release, NewRelease};
2526

@@ -49,6 +50,20 @@ pub fn create_commit<'a>(conn: &PgConnection, sha: &'a str, author_name: &'a str
4950
.expect("Error saving new commit")
5051
}
5152

53+
pub fn create_project(conn: &PgConnection, name: &str, path: &str, github_link: &str) -> Project {
54+
use schema::projects;
55+
56+
let new_project = NewProject {
57+
name: name,
58+
path: path,
59+
github_link: github_link
60+
};
61+
62+
diesel::insert(&new_project).into(projects::table)
63+
.get_result(conn)
64+
.expect("Error saving new project")
65+
}
66+
5267

5368
pub fn create_release(conn: &PgConnection, version: &str) -> Release {
5469
use schema::releases;

src/models.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#[derive(Debug,Identifiable,Queryable,Associations)]
2+
#[has_many(releases)]
3+
pub struct Project {
4+
pub id: i32,
5+
pub name: String,
6+
pub path: String,
7+
pub github_link: String,
8+
}
9+
110
#[derive(Debug,Identifiable,Queryable,Associations)]
211
#[belongs_to(Release)]
312
pub struct Commit {
@@ -10,11 +19,22 @@ pub struct Commit {
1019

1120
#[derive(Debug,Identifiable,Queryable,Associations)]
1221
#[has_many(commits)]
22+
#[belongs_to(Project)]
1323
pub struct Release {
1424
pub id: i32,
1525
pub version: String,
26+
pub project_id: i32,
1627
}
1728

29+
use super::schema::projects;
30+
31+
#[derive(Insertable)]
32+
#[table_name="projects"]
33+
pub struct NewProject<'a> {
34+
pub name: &'a str,
35+
pub path: &'a str,
36+
pub github_link: &'a str,
37+
}
1838
use super::schema::commits;
1939

2040
#[derive(Insertable)]

0 commit comments

Comments
 (0)