Fix schema
This commit is contained in:
		| @@ -15,7 +15,6 @@ type AccountData struct { | ||||
| } | ||||
|  | ||||
| func (h *Handler) account(c *gin.Context) { | ||||
|  | ||||
| 	accountID := c.Param("accountid") | ||||
| 	accountUUID, err := uuid.Parse(accountID) | ||||
| 	if err != nil { | ||||
|   | ||||
							
								
								
									
										10
									
								
								postgres/schema/0002_budgets.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								postgres/schema/0002_budgets.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE budgets ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     name text NOT NULL, | ||||
|     last_modification timestamp with time zone, | ||||
|     income_category_id uuid NOT NULL REFERENCES categories (id) DEFERRABLE | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE budgets; | ||||
							
								
								
									
										11
									
								
								postgres/schema/0003_users.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								postgres/schema/0003_users.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE users ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     email text NOT NULL, | ||||
|     name text NOT NULL, | ||||
|     password text NOT NULL, | ||||
|     last_login timestamp with time zone | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE users; | ||||
							
								
								
									
										8
									
								
								postgres/schema/0004_user_budgets.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								postgres/schema/0004_user_budgets.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE user_budgets ( | ||||
|     user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, | ||||
|     budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE user_budgets; | ||||
							
								
								
									
										10
									
								
								postgres/schema/0005_accounts.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								postgres/schema/0005_accounts.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE accounts ( | ||||
|         id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|         budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|         name varchar(50) NOT NULL, | ||||
|         on_budget boolean DEFAULT TRUE NOT NULL | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE accounts; | ||||
							
								
								
									
										9
									
								
								postgres/schema/0006_payees.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								postgres/schema/0006_payees.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE payees ( | ||||
|         id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|         budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|         name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE payees; | ||||
							
								
								
									
										9
									
								
								postgres/schema/0007_category-groups.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								postgres/schema/0007_category-groups.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE category_groups ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|     name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE category_groups; | ||||
							
								
								
									
										9
									
								
								postgres/schema/0008_categories.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								postgres/schema/0008_categories.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE categories ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     category_group_id uuid NOT NULL REFERENCES category_groups (id) ON DELETE CASCADE, | ||||
|     name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE categories; | ||||
							
								
								
									
										16
									
								
								postgres/schema/0009_transactions.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								postgres/schema/0009_transactions.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE transactions ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     date date NOT NULL, | ||||
|     memo text NOT NULL, | ||||
|     amount decimal(12,2) NOT NULL, | ||||
|     account_id uuid NOT NULL REFERENCES accounts (id), | ||||
|     category_id uuid REFERENCES categories (id), | ||||
|     payee_id uuid REFERENCES payees (id) | ||||
| ); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id"); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id"); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE transactions; | ||||
| @@ -1,66 +0,0 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE budgets ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     name text NOT NULL, | ||||
|     last_modification timestamp with time zone | ||||
| ); | ||||
|  | ||||
| CREATE TABLE users ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     email text NOT NULL, | ||||
|     name text NOT NULL, | ||||
|     password text NOT NULL, | ||||
|     last_login timestamp with time zone | ||||
| ); | ||||
|  | ||||
| CREATE TABLE user_budgets ( | ||||
|     user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, | ||||
|     budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE | ||||
| ); | ||||
|  | ||||
| CREATE TABLE accounts ( | ||||
|         id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|         budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|         name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| CREATE TABLE payees ( | ||||
|         id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|         budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|         name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| CREATE TABLE category_groups ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE, | ||||
|     name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| CREATE TABLE categories ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     category_group_id uuid NOT NULL REFERENCES category_groups (id) ON DELETE CASCADE, | ||||
|     name varchar(50) NOT NULL | ||||
| ); | ||||
|  | ||||
| CREATE TABLE transactions ( | ||||
|     id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||
|     date date NOT NULL, | ||||
|     memo text NOT NULL, | ||||
|     amount decimal(12,2) NOT NULL, | ||||
|     account_id uuid NOT NULL REFERENCES accounts (id), | ||||
|     category_id uuid REFERENCES categories (id), | ||||
|     payee_id uuid REFERENCES payees (id) | ||||
| ); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id"); | ||||
| ALTER TABLE "transactions" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id"); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE transactions; | ||||
| DROP TABLE accounts; | ||||
| DROP TABLE payees; | ||||
| DROP TABLE categories; | ||||
| DROP TABLE category_groups; | ||||
| DROP TABLE user_budgets; | ||||
| DROP TABLE budgets; | ||||
| DROP TABLE users; | ||||
| @@ -1,14 +0,0 @@ | ||||
| -- +goose Up | ||||
| ALTER TABLE budgets ADD COLUMN income_category_id uuid NULL REFERENCES categories (id); | ||||
| UPDATE budgets | ||||
| SET income_category_id = ( | ||||
|         SELECT categories.id | ||||
|         FROM categories | ||||
|         LEFT JOIN category_groups ON category_groups.id = categories.category_group_id | ||||
|         WHERE categories.name = 'Ready to Assign' | ||||
|         AND category_groups.budget_id = budgets.id | ||||
| ); | ||||
| ALTER TABLE budgets ALTER COLUMN income_category_id SET NOT NULL; | ||||
|  | ||||
| -- +goose Down | ||||
| ALTER TABLE budgets DROP COLUMN income_category_id; | ||||
| @@ -1,5 +0,0 @@ | ||||
| -- +goose Up | ||||
| ALTER TABLE accounts ADD COLUMN on_budget boolean DEFAULT TRUE NOT NULL; | ||||
|  | ||||
| -- +goose Down | ||||
| ALTER TABLE accounts DROP COLUMN on_budget; | ||||
		Reference in New Issue
	
	Block a user