on main help sign in
reshape/tests/create_table.rs
200 lines 5.9 kB history raw
1mod common;
2use common::Test;
4#[test]
5fn create_table() {
6 let mut test = Test::new("Create table");
8 test.first_migration(
9 r#"
10 name = "create_users_table"
12 [[actions]]
13 type = "create_table"
14 name = "users"
15 primary_key = ["id"]
17 [[actions.columns]]
18 name = "id"
19 type = "INTEGER"
20 generated = "ALWAYS AS IDENTITY"
22 [[actions.columns]]
23 name = "name"
24 type = "TEXT"
26 [[actions.columns]]
27 name = "created_at"
28 type = "TIMESTAMP"
29 nullable = false
30 default = "NOW()"
31 "#,
32 );
34 test.after_first(|db| {
35 // Ensure table was created
36 let result = db
37 .query_opt(
38 "
39 SELECT table_name
40 FROM information_schema.tables
41 WHERE table_name = 'users' AND table_schema = 'public'",
42 &[],
43 )
44 .unwrap();
45 assert!(result.is_some());
47 // Ensure table has the right columns
48 let result = db
49 .query(
50 "
51 SELECT column_name, column_default, is_nullable, data_type
52 FROM information_schema.columns
53 WHERE table_name = 'users' AND table_schema = 'public'
54 ORDER BY ordinal_position",
55 &[],
56 )
57 .unwrap();
59 // id column
60 let id_row = &result[0];
61 assert_eq!("id", id_row.get::<_, String>("column_name"));
62 assert!(id_row.get::<_, Option<String>>("column_default").is_none());
63 assert_eq!("NO", id_row.get::<_, String>("is_nullable"));
64 assert_eq!("integer", id_row.get::<_, String>("data_type"));
66 // name column
67 let name_row = &result[1];
68 assert_eq!("name", name_row.get::<_, String>("column_name"));
69 assert!(name_row
70 .get::<_, Option<String>>("column_default")
71 .is_none());
72 assert_eq!("YES", name_row.get::<_, String>("is_nullable"));
73 assert_eq!("text", name_row.get::<_, String>("data_type"));
75 // created_at column
76 let created_at_column = &result[2];
77 assert_eq!(
78 "created_at",
79 created_at_column.get::<_, String>("column_name")
80 );
81 assert!(created_at_column
82 .get::<_, Option<String>>("column_default")
83 .is_some());
84 assert_eq!("NO", created_at_column.get::<_, String>("is_nullable"));
85 assert_eq!(
86 "timestamp without time zone",
87 created_at_column.get::<_, String>("data_type")
88 );
90 // Ensure the primary key has the right columns
91 let primary_key_columns: Vec<String> = db
92 .query(
93 "
94 SELECT a.attname AS column
95 FROM pg_index i
96 JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
97 JOIN pg_class t ON t.oid = i.indrelid
98 WHERE t.relname = 'users' AND i.indisprimary
99 ",
100 &[],
101 )
102 .unwrap()
103 .iter()
104 .map(|row| row.get("column"))
105 .collect();
107 assert_eq!(vec!["id"], primary_key_columns);
108 });
110 test.run();
113#[test]
114fn create_table_with_foreign_keys() {
115 let mut test = Test::new("Create table");
117 test.first_migration(
118 r#"
119 name = "create_users_table"
121 [[actions]]
122 type = "create_table"
123 name = "users"
124 primary_key = ["id"]
126 [[actions.columns]]
127 name = "id"
128 type = "INTEGER"
129 generated = "ALWAYS AS IDENTITY"
131 [[actions.columns]]
132 name = "name"
133 type = "TEXT"
135 [[actions.columns]]
136 name = "created_at"
137 type = "TIMESTAMP"
138 nullable = false
139 default = "NOW()"
141 [[actions]]
142 type = "create_table"
143 name = "items"
144 primary_key = ["id"]
146 [[actions.columns]]
147 name = "id"
148 type = "INTEGER"
150 [[actions.columns]]
151 name = "user_id"
152 type = "INTEGER"
153 nullable = false
155 [[actions.foreign_keys]]
156 columns = ["user_id"]
157 referenced_table = "users"
158 referenced_columns = ["id"]
159 "#,
160 );
162 test.after_first(|db| {
163 let foreign_key_columns: Vec<(String, String, String)> = db
164 .query(
165 "
166 SELECT
167 kcu.column_name,
168 ccu.table_name AS foreign_table_name,
169 ccu.column_name AS foreign_column_name
170 FROM
171 information_schema.table_constraints AS tc
172 JOIN information_schema.key_column_usage AS kcu
173 ON tc.constraint_name = kcu.constraint_name
174 AND tc.table_schema = kcu.table_schema
175 JOIN information_schema.constraint_column_usage AS ccu
176 ON ccu.constraint_name = tc.constraint_name
177 AND ccu.table_schema = tc.table_schema
178 WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='items';
179 ",
180 &[],
181 )
182 .unwrap()
183 .iter()
184 .map(|row| {
185 (
186 row.get("column_name"),
187 row.get("foreign_table_name"),
188 row.get("foreign_column_name"),
189 )
190 })
191 .collect();
193 assert_eq!(
194 vec![("user_id".to_string(), "users".to_string(), "id".to_string())],
195 foreign_key_columns
196 );
197 });
199 test.run();