Skip to content

Commit a8da465

Browse files
author
swagfinger
committed
transactions + schema migrations
1 parent b1f1dad commit a8da465

File tree

3 files changed

+349
-124
lines changed

3 files changed

+349
-124
lines changed

01.SQL.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393

9494
[15. Materialized Views](#15-materialized-views)
9595

96+
[16. Transactions](#16-transactions)
97+
9698
---
9799
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
98100
## 1. Basics
@@ -1509,6 +1511,8 @@ syntax: CREATE OR REPLACE VIEW _ AS
15091511
DROP VIEW recent_posts;
15101512
```
15111513
---
1514+
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
1515+
15121516
## 15. Materialized views
15131517
Query that gets executed only at very specific times, but the results are saved and
15141518
can be referenced without rerunning the query.
@@ -1546,3 +1550,32 @@ modifying any data in materialized view (SQL statement), will require us to refr
15461550
```SQL
15471551
REFRESH MATERIALIZED VIEW weekly_likes;
15481552
```
1553+
1554+
---
1555+
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
1556+
1557+
## 16. Transactions
1558+
locks to updates to ensure execution succeeds in order.
1559+
Transactions ensure all execute successfully or none execute successfully (roll-back).
1560+
when opening up a transaction, database makes an 'isolated workspace'. any SQL statements run are isolated until transaction ends with COMMIT.
1561+
1562+
##### Opening a transaction / complete a transaction
1563+
```SQL
1564+
BEGIN; --executing starts a transaction
1565+
1566+
---some sql
1567+
1568+
COMMIT; -- to finish transaction
1569+
```
1570+
1571+
##### closing a transaction
1572+
If an error occurs after BEGIN call, manually run ROLLBACK needs to be executed.
1573+
if a crash occurs, postgres rolls back transaction
1574+
1575+
##### Simulating a crash / recover from error
1576+
(postgress-> dashboard -> server activity -> *(close connections that have 'CONN'))
1577+
if you check database, postgres did an auto rollback.
1578+
1579+
```SQL
1580+
ROLLBACK; -- revert
1581+
```

02.database_design.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
+ [Mention system](#mention-system)
1010
+ [Hashtag system](#hashtag-system)
1111
+ [follower system](#follower-system)
12+
+ [creating instagram database](#creating-instagram-database)
1213

1314
---
1415

@@ -488,3 +489,128 @@ Table followers {
488489
}
489490
```
490491

492+
---
493+
494+
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
495+
496+
## creating instagram database
497+
498+
1. servers -> localhost -> databases -> (right-click create)
499+
2. open query tool by right clicking on specific database
500+
501+
###### users table
502+
```SQL
503+
CREATE TABLE users (
504+
id SERIAL PRIMARY KEY,
505+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
506+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
507+
username VARCHAR(30) NOT NULL,
508+
bio VARCHAR(400),
509+
avatar VARCHAR(200),
510+
phone VARCHAR(25),
511+
email VARCHAR(40),
512+
password VARCHAR(50),
513+
status VARCHAR(15),
514+
CHECK(COALESCE(phone, email) IS NOT NULL)
515+
);
516+
```
517+
518+
###### posts table
519+
```SQL
520+
CREATE TABLE posts (
521+
id SERIAL PRIMARY KEY,
522+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
523+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
524+
url VARCHAR(200) NOT NULL,
525+
caption VARCHAR(240),
526+
lat REAL CHECK(lat IS NULL OR (lat >= -90 AND lat <= 90)),
527+
lng REAL CHECK(lng IS NULL OR (lng >= -180 AND lng <= 180)),
528+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
529+
);
530+
```
531+
532+
###### comments table
533+
```SQL
534+
CREATE TABLE comments (
535+
id SERIAL PRIMARY KEY,
536+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
537+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
538+
contents VARCHAR(240) NOT NULL,
539+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
540+
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE
541+
);
542+
```
543+
544+
###### likes table
545+
```SQL
546+
CREATE TABLE likes (
547+
id SERIAL PRIMARY KEY,
548+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
549+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
550+
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE,
551+
comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE,
552+
CHECK(
553+
COALESCE((post_id)::BOOLEAN::INTEGER, 0)
554+
+
555+
COALESCE((comment_id)::BOOLEAN::INTEGER, 0)
556+
= 1
557+
),
558+
UNIQUE(user_id, post_id, comment_id)
559+
);
560+
```
561+
562+
###### photo_tags
563+
```SQL
564+
CREATE TABLE photo_tags (
565+
id SERIAL PRIMARY KEY,
566+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
567+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
568+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
569+
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
570+
x INTEGER NOT NULL,
571+
y INTEGER NOT NULL,
572+
UNIQUE(user_id, post_id)
573+
);
574+
```
575+
576+
###### caption_tags
577+
```SQL
578+
CREATE TABLE caption_tags (
579+
id SERIAL PRIMARY KEY,
580+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
581+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
582+
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
583+
UNIQUE(user_id, post_id)
584+
);
585+
```
586+
587+
###### hashtags
588+
```SQL
589+
CREATE TABLE hashtags (
590+
id SERIAL PRIMARY KEY,
591+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
592+
title VARCHAR(20) NOT NULL UNIQUE
593+
);
594+
```
595+
596+
###### hashtags_post
597+
```SQL
598+
CREATE TABLE hashtags_posts (
599+
id SERIAL PRIMARY KEY,
600+
hashtag_id INTEGER NOT NULL REFERENCES hashtags(id) ON DELETE CASCADE,
601+
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
602+
UNIQUE(hashtag_id, post_id)
603+
);
604+
```
605+
606+
###### followers
607+
```SQL
608+
CREATE TABLE followers (
609+
id SERIAL PRIMARY KEY,
610+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
611+
leader_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
612+
follower_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
613+
UNIQUE(leader_id, follower_id)
614+
);
615+
```
616+

0 commit comments

Comments
 (0)