-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
265 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000001_initialize_schema.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
drop table if exists "pastes"; | ||
drop table "pastes"; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000001_initialize_schema.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000002_rename_deletion_token.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" rename column "modificationToken" to "deletionToken"; | ||
alter table "pastes" rename column "modificationToken" to "deletionToken"; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000002_rename_deletion_token.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" rename column "deletionToken" to "modificationToken"; | ||
alter table "pastes" rename column "deletionToken" to "modificationToken"; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000003_add_metadata.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" drop column "metadata"; | ||
alter table "pastes" drop column "metadata"; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000003_add_metadata.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" add column "metadata" jsonb; | ||
alter table "pastes" add column "metadata" jsonb; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000004_remove_paste_specific_autodelete.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" add column "autoDelete" boolean; | ||
alter table "pastes" add column "autoDelete" boolean; | ||
|
||
commit; |
2 changes: 1 addition & 1 deletion
2
internal/storage/postgres/migrations/000004_remove_paste_specific_autodelete.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
begin; | ||
|
||
alter table if exists "pastes" drop column "autoDelete"; | ||
alter table "pastes" drop column "autoDelete"; | ||
|
||
commit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package sqlite | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"embed" | ||
"errors" | ||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/sqlite" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
"github.com/lus/pasty/internal/pastes" | ||
"github.com/lus/pasty/internal/storage" | ||
"github.com/rs/zerolog/log" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
//go:embed migrations/*.sql | ||
var migrations embed.FS | ||
|
||
type Driver struct { | ||
filePath string | ||
connPool *sql.DB | ||
pastes *pasteRepository | ||
} | ||
|
||
var _ storage.Driver = (*Driver)(nil) | ||
|
||
func New(filePath string) *Driver { | ||
return &Driver{ | ||
filePath: filePath, | ||
} | ||
} | ||
|
||
func (driver *Driver) Initialize(ctx context.Context) error { | ||
db, err := sql.Open("sqlite", driver.filePath) | ||
if err != nil { | ||
return err | ||
} | ||
if err := db.PingContext(ctx); err != nil { | ||
return err | ||
} | ||
|
||
log.Info().Msg("Performing SQLite database migrations...") | ||
source, err := iofs.New(migrations, "migrations") | ||
if err != nil { | ||
_ = db.Close() | ||
return err | ||
} | ||
defer func() { | ||
_ = source.Close() | ||
}() | ||
migrateDriver, err := sqlite.WithInstance(db, &sqlite.Config{ | ||
MigrationsTable: sqlite.DefaultMigrationsTable, | ||
DatabaseName: driver.filePath, | ||
NoTxWrap: false, | ||
}) | ||
if err != nil { | ||
_ = db.Close() | ||
return err | ||
} | ||
migrator, err := migrate.NewWithInstance("iofs", source, "sqlite", migrateDriver) | ||
if err != nil { | ||
_ = db.Close() | ||
return err | ||
} | ||
if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { | ||
_ = db.Close() | ||
return err | ||
} | ||
|
||
driver.connPool = db | ||
driver.pastes = &pasteRepository{ | ||
connPool: db, | ||
} | ||
return nil | ||
} | ||
|
||
func (driver *Driver) Close() error { | ||
driver.pastes = nil | ||
_ = driver.connPool.Close() | ||
driver.connPool = nil | ||
return nil | ||
} | ||
|
||
func (driver *Driver) Pastes() pastes.Repository { | ||
return driver.pastes | ||
} |
1 change: 1 addition & 0 deletions
1
internal/storage/sqlite/migrations/000001_initialize_schema.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE "pastes"; |
8 changes: 8 additions & 0 deletions
8
internal/storage/sqlite/migrations/000001_initialize_schema.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE "pastes" ( | ||
"id" TEXT NOT NULL, | ||
"content" TEXT NOT NULL, | ||
"modification_token" TEXT NOT NULL, | ||
"created" BIGINT NOT NULL, | ||
"metadata" TEXT NOT NULL, | ||
PRIMARY KEY ("id") | ||
); |
Oops, something went wrong.