Skip to content

Instantly share code, notes, and snippets.

@riiamri23
Last active May 11, 2024 16:29
Show Gist options
  • Save riiamri23/51f3b63d6f18ec48dd31f1b3871555cd to your computer and use it in GitHub Desktop.
Save riiamri23/51f3b63d6f18ec48dd31f1b3871555cd to your computer and use it in GitHub Desktop.
Kampus GO With MySQL on My Mac
CREATE TABLE `employees` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`lastname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`contact_no` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`official_email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`personal_email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`identity_no` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date_of_birth` date DEFAULT NULL,
`gender` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`emergency_contact_relationship` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`emergency_contact` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`emergency_contact_address` text COLLATE utf8mb4_unicode_ci,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`current_address` text COLLATE utf8mb4_unicode_ci,
`permanent_address` text COLLATE utf8mb4_unicode_ci,
`city` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`designation` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'office' COMMENT 'work from remote/office',
`status` int NOT NULL DEFAULT '1',
`employment_status` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`joining_date` date DEFAULT NULL,
`exit_date` date DEFAULT NULL,
`gross_salary` int DEFAULT '0',
`bonus` int DEFAULT '0',
`branch_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`department_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `employees_official_email_unique` (`official_email`),
UNIQUE KEY `employees_personal_email_unique` (`personal_email`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1. Active MySQL on my local
- sudo mysql.server start
2. run golang
go run ./{lokasi file}
2b. run golang with nodemon for read file changes
npm i -g nodemon // (install nodemon if you haven't nodemon)
npx nodemon --exec "go run" ./[name file].go --signal SIGTERM
3. add package
go mod init example/hello
go get -u github.com/davecgh/go-spew/spew
go mod vendor
go mod tidy
1. get PID from terminal
ps aux | grep main.go
2. kill it
kill -9 PID
ex: kill -9 1758
https://www.freecodecamp.org/news/database-migration-golang-migrate/
1. install the tool
brew install golang-migrate
2. create file
migrate create -ext sql -dir database/migration/ -seq init_mg
3. description about migration
A migration typically consists of two distinct files, one for moving the database to a new state (referred to as "up") and another for reverting the changes made to the previous state (referred to as "down").
Example:
{version}_{title}.down.sql
{version}_{title}.up.sql
4. how to run migration for up
migrate -path database/migration/ -database "postgresql://username:secretkey@localhost:5432/database_name?sslmode=disable" -verbose up
5. how to run migration for down
migrate -path database/migration/ -database "postgresql://username:secretkey@localhost:5432/database_name?sslmode=disable" -verbose down
6. how to resolve error on migration
migrate -path database/migration/ -database "postgresql://username:secretkey@localhost:5432/database_name?sslmode=disable" force <VERSION>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment