6 let mut test = Test::new("Create table");
10 name = "create_users_table"
20 generated = "ALWAYS AS IDENTITY"
34 test.after_first(|db| {
35 // Ensure table was created
40 FROM information_schema.tables
41 WHERE table_name = 'users' AND table_schema = 'public'",
45 assert!(result.is_some());
47 // Ensure table has the right columns
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",
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"));
67 let name_row = &result[1];
68 assert_eq!("name", name_row.get::<_, String>("column_name"));
70 .get::<_, Option<String>>("column_default")
72 assert_eq!("YES", name_row.get::<_, String>("is_nullable"));
73 assert_eq!("text", name_row.get::<_, String>("data_type"));
76 let created_at_column = &result[2];
79 created_at_column.get::<_, String>("column_name")
81 assert!(created_at_column
82 .get::<_, Option<String>>("column_default")
84 assert_eq!("NO", created_at_column.get::<_, String>("is_nullable"));
86 "timestamp without time zone",
87 created_at_column.get::<_, String>("data_type")
90 // Ensure the primary key has the right columns
91 let primary_key_columns: Vec<String> = db
94 SELECT a.attname AS column
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
104 .map(|row| row.get("column"))
107 assert_eq!(vec!["id"], primary_key_columns);
114fn create_table_with_foreign_keys() {
115 let mut test = Test::new("Create table");
117 test.first_migration(
119 name = "create_users_table"
122 type = "create_table"
129 generated = "ALWAYS AS IDENTITY"
142 type = "create_table"
155 [[actions.foreign_keys]]
156 columns = ["user_id"]
157 referenced_table = "users"
158 referenced_columns = ["id"]
162 test.after_first(|db| {
163 let foreign_key_columns: Vec<(String, String, String)> = db
168 ccu.table_name AS foreign_table_name,
169 ccu.column_name AS foreign_column_name
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';
186 row.get("column_name"),
187 row.get("foreign_table_name"),
188 row.get("foreign_column_name"),
194 vec![("user_id".to_string(), "users".to_string(), "id".to_string())],
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list