Skip to content

DAO implementation, accounts table, seed script #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2022

Conversation

lzap
Copy link
Member

@lzap lzap commented May 13, 2022

Also drop down-migrations, these do not appear to be very helpful in golang-migrate because when migration fails, it sets the "dirty" flag and database must be fixed manually anyway. We can add down migrations later if we need to.

The patch also adds seeds support, this will be helpful for setting up a working development setup. By deafult this is turned off, use DB_SEED_SCRIPT=dev_small to use it.

@lzap
Copy link
Member Author

lzap commented May 13, 2022

While working on DAO and REST, I find SshPublicKey hard to type, any objections to name the model as PubKey (table pubkeys) and REST endpoint /public_keys ?

@lzap
Copy link
Member Author

lzap commented May 13, 2022

I am pushing initial DAO and REST API (POC only) implementation for Accounts.

Do not merge, the implementation suffers from an interesting bug - I am getting all records pointing to the same struct for some reason :)

lzap@mone provisioning-backend % curl http://localhost:8000/accounts
[{"ID":4,"AccountNumber":"77","OrgID":"000077"},{"ID":4,"AccountNumber":"77","OrgID":"000077"},{"ID":4,"AccountNumber":"77","OrgID":"000077"},{"ID":4,"AccountNumber":"77","OrgID":"000077"}]

I will take a look on Monday, unless someone else can figure this one out.

@lzap
Copy link
Member Author

lzap commented May 13, 2022

Fixed the problem, lint will fail tho. I will add error wrapping to DAO, however, I will need to turn off statement close check for the dao package:

internal/dao/sqlx/account_dao.go:25:46: Rows/Stmt was not closed (sqlclosecheck)
	daoImpl.getById, err = db.DB.PreparexContext(ctx, getById)
	                                            ^
internal/dao/sqlx/account_dao.go:29:43: Rows/Stmt was not closed (sqlclosecheck)
	daoImpl.list, err = db.DB.PreparexContext(ctx, list)

As it is stated in the Go documentation: https://pkg.go.dev/database/sql#Stmt

If a Stmt is prepared on a DB, it will remain usable for the lifetime of the DB. When the Stmt needs to execute on a new underlying connection, it will prepare itself on the new connection automatically.

I am taking advantage of this in the DAO package, each DAO implementation prepares statements on the connection and when executed it the connection pool automatically creates them on the fly. They are never closed, that is what this lint is all about, but in this case it makes sense. We could close the statements at the end of each request but thats actually not needed.

@lzap lzap changed the title Create accounts and ssh_keys tables DAO implementation, accounts table, seed script May 16, 2022
@lzap
Copy link
Member Author

lzap commented May 16, 2022

Ok fixed all the problems, DAO is now clean API with error wrapping, vastly improved also service package error wrapping and rendering. Removes SSH keys from this PR, I will implement it separately.

@lzap
Copy link
Member Author

lzap commented May 16, 2022

Hmm the linter on CI errors out while its passing locally: Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: goanalysis_metalinter: execinquery: package "dao" (isInitialPkg: true, needAnalyzeSource: true): runtime error: invalid memory address or nil pointer dereference

Looks like an issue with master branch: golangci/golangci-lint#2851

@lzap lzap force-pushed the ssh-keys branch 2 times, most recently from fe7b1b0 to 1d9c036 Compare May 16, 2022 12:32
@lzap
Copy link
Member Author

lzap commented May 16, 2022

Fixed, I upgraded GHA actions to v3 where appropriate and also pinned the goloangci-lint action to version 1.45 which works fine.

Copy link
Member

@ezr-ondrej ezr-ondrej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks quite ready to me, but I got a domain question: Do we even want to expose Accounts as API endpoints? It seems to me we do not, but I'm happy to be proven wrong.

"net/http"
)

func ListAccounts(w http.ResponseWriter, r *http.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we even expose Accounts in API? it seems unecessary as the service should always run in a context of one account IMHO, right?

Copy link
Member Author

@lzap lzap May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know, but I wanted to create few example API calls with DAO so we have at least something, I had to start with Accounts because of dependencies. We will either delete REST operations completely or create an administrative role. It would be good if you could figure out if administrative roles are allowed by SRE or is something that is a good practice.

We can actually turn this into turnpike API which is only available from inside of our RH network.

END;
$empty$ LANGUAGE 'plpgsql';

COMMIT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's still quite a lot of end lines missing ^_^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with git ls-files -z | while IFS= read -rd '' f; do tail -c1 < "$f" | read -r _ || echo >> "$f"; done

Added editorconfig for go project.

@@ -0,0 +1,3 @@
// @no-log
GET http://localhost:8000/accounts/1 HTTP/1.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this file? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped, that is a test file for HTTP Client plugin for GoLand/VSCode and others.

CREATE TABLE accounts
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
account_number TEXT NOT NULL UNIQUE CHECK (NOT empty(account_number)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is wrong as account_numer is being phased out and I've read that there are already CUs that do not have account_number, so we might want to have this null or at least empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know for sure, if I have to choose between get that info and store it to only drop it later or ignore this and solve when we encounter problems, I choose one. There is a transition period planned and some services will only accept the account number for some time, although there is the translation service why not to keep those numbers in our database since it will come for free in requests anyway? Dropping a column is literally a DDL one-liner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not against keeping the account_number, I want to allow a blank value for it. There are already some CUs that will not have account_number and such CUs would not be able to use our service :)

@lzap
Copy link
Member Author

lzap commented May 17, 2022

Squashed into a single commit.

@adiabramovitch
Copy link
Member

Great work, really helped.
@ezr-ondrej Do you have any comments?

@ezr-ondrej
Copy link
Member

Yeah I still believe we should allow blank accounts 🤔 but we can talk about that later, but would be better not to change migrations ^_^

@lzap
Copy link
Member Author

lzap commented May 17, 2022

So I am assuming that NOT NULL will not work with UNIQUE, but let me try maybe NULLs are not taken into account.

@lzap
Copy link
Member Author

lzap commented May 17, 2022

You were right, NULL values are allowed regardless of the unique constraint, therefore let's go with that. Also I removed the ssh key table from the seed script - that was a leftover. Finally, I added go documentation to the account model type.

CREATE TABLE accounts
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
account_number TEXT UNIQUE CHECK (NOT empty(account_number)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧡

Copy link
Member

@ezr-ondrej ezr-ondrej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good, thanks @lzap and thanks @adiabramovitch for the review !

@ezr-ondrej ezr-ondrej merged commit 3df956c into RHEnVision:main May 17, 2022
@lzap lzap deleted the ssh-keys branch May 17, 2022 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants