Saturday, August 8, 2020

Clojure Postgres Heroku

Nuke the heroku database!

  heroku pg:reset DATABASE_URL

Run migration or rollback:

 heroku run bash

then from the command line:

java -jar target/uberjar/<appname>.jar migrate

java -jar target/uberjar/<appname>.jar rollback

Friday, July 24, 2020

Clojure Database Testing

Set up a simple project: luminus +postgres. The sample database test called test-users passes. 

Added a game table using migrate then using hugsql a simple add to the game table, create-game, and a simple list rows in the table, list-games. Copy and pasted test-user, renamed it test-game and then replace the calls with my methods and the tests failed.

After a few hours of research and troubleshooting I found that I was only passing 2 arguments to the list-games function but it requires 3 arguments to (db/list-games t-conn {})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
(deftest game-test
  (jdbc/with-transaction [t-conn *db* {:rollback-only true}]
                         (db/create-game!
                           t-conn
                           {:first_name "test1"})
                         (db/create-game!
                           t-conn
                           {:first_name "test2"})
                         (prn (count (jdbc/execute! t-conn ["SELECT * FROM games"])))
                         (is (= '("test1" "test2") (map :games/first_name (jdbc/execute! t-conn ["SELECT * FROM games"]))))
                         (is (= '("test1" "test2") (map :first_name (db/list-games t-conn {}))))))

Along the way I learned several things:
1.  use  (jdbc/execute! t-conn ["SELECT * FROM games"]) to do anything you need to without opening a psql shell.

2. execute! -- executes the SQL statement and produces a vector of realized hash maps, that use qualified keywords for the column names, of the form :<table>/<column>

3. Everything can be deleted with: drop schema public cascade; create schema public.

4. use-fixtures is the clojure @setup/@teardown mechanisim.  https://clojuredocs.org/clojure.test/use-fixtures

Tuesday, November 27, 2018

Using git branches

There have been some questions about how to do things with gil now that we switched to gitlab.

Mostly rebase,  and some "oh crap my branch is hosed" moments.

First, whenever you are making a change not matter how big or small you need to make a branch (or at least push to a branch that is not develop, master or a releasex-x-x)

git checkout develop
git pull
git checkout -b xxxx/some-useful-name

where xxxx is feature or bug or nothing.  I am not so sure about the usefulness of grouping the branches as feature or bug right now so bug and feature would be nice but its not worth stressing over.

okay you are commiting new and wonderful changes to the new branch!

git push


This is your branch so you can
git push -f
and make the remote branch look just like your local branch.

As features are added to develop you will want to put those changes into your branch.  The way you do this is with rebase.  Rebase will remove your changes pull the specified branch's changes to your branch the "replay your work on top of" the changes from develop.
 git fetch
 git rebase origin/develop  
 First, rewinding head to replay your work on top of it...  
 Applying: added staged command  


more about rebasing here: git-rebase

Last word of advice.  Make yourself a safety net and try new things with git!

You can make a safety net like this:
 git branch copy-of-my-work  


now if you do a pull and get merges or try a `rebase -i` to squash things, or just want to change the code to something completely crazy you have a back up


you can either work on the copy or use it to restore your branch when everything fails and you wish you never switched to git.


A local copy is great because sometimes while collaborating on a branch, the remote doesn't match up perfectly and 'git pull --rebase` does unexpected things to the local branch.  Having that backup branch is nice because in the worst case you can `git push -f` back to the remote restore order to the universe.

Tuesday, October 30, 2018

More yak shaving in clojure

I got a fresh clone of my luminus project from version control and got this error:

Exception in thread "main" java.lang.RuntimeException: could not start [#'track-work.config/env] due to, compiling:(/tmp/form-init1269791463497202877.clj:1:73)
 at clojure.lang.Compiler.load(Compiler.java:7526)
 ... more
Caused by: java.lang.RuntimeException: could not start [#'track-work.config/env] due to
 at mount.core$up$fn__297.invoke(core.cljc:94)
 ... 28 more
Caused by: java.lang.RuntimeException: could not find a non empty configuration file to load. looked in the classpath (as a "resource") and on a file system via "conf" system property
 at cprop.core$load_config.invokeStatic(core.cljc:24)

 ... 34 more

could not find a non empty configuration file to load. is a strange way to say it could not find my config file.

lein is looking for my dev-config.edn which was never added to version control. The test-config.edn and dev-config.edn are automatically added to the .gitignore by the template.

I ran the lein template again to get a fresh copy of the dev-config.edn and test-config.edn


lein new luminus myproject +re-frame +postgres
Then used the files to create a bash script that will make a dev-config-sample.edn and test-config-sample.edn in case this ever happens again. Maybe there is a way to ask luminus to generate the files for you again.

Saturday, October 20, 2018

Spacemacs Cider Figwheel

Spacemacs Cider Figwheel
I had lots of weird issues getting my workflow going in spacemacs. Sometimes figwheel did not live update, sometimes I could not evaluate expressions in the editor. There are plenty of guides but many of them are old and some of them tell you to make changes you don’t need and then things break. Then I tried to fix what appeared broken only to make things worse.

I started over several times with new lein projects and worked through the tutorials. Everything clicked when I learned about the helper functions which are stored in the user.clj https://github.com/bhauman/lein-figwheel/wiki/Using-the-Figwheel-REPL-within-NRepl .
It turns out the luminus template I was using works with emacs/cider without any changes, if you know the correct things to do.

Make a new project:

lein new luminus myproject +re-frame +postgres

Setup your database:

Add your dev database to postgres
psql
create database myproject_dev
create database myproject_test
then follow http://www.luminusweb.net/docs/database.html to setup your edn files and migrate

Spacemacs

Open a clj file. I like to open project.clj (but don’t change anything).

Now use:, ' to start cider. Wait… you can always switch buffers to SPC b b and choose the buffer with nrepl in the name however eventually you will see a buffer with cider in the name and that is the one you will use.

Or if you left you PC and went to grab a cup of coffee when you get back you should be able to use , s s to show the cider repl buffer.

Troubleshooting: The cider hotkeys should work when you have any .clj file open. Mine stopped working which was very confusing. To fix it I deleted my .emacs.d directory and .spacemacs and re-cloned spacemacs. In this repl you can migrate your database if it changed, start the https server and figwheel.

(start)
(start-fw) 
(cljs)
Now you can evaluate any expression in a cljs file using , e f and as you change and save your cljs files you will see it update in the browser in real time.
If you need to do some work on the backend you can execute :cljs/exit to get back to the clj prompt.

Find stuff

Emacs

Sometimes I see what args a function takes in the editor, but I have to know the name of the function. I am still not sure how to get emacs to do code completions.

List everything in a namespace

(keys (ns-publics 'namespace))
That little "'" in front of your namespace is very important. otherwise you get an error that looks like

xxxxxxx

User.clj

This file contains the commands you can run while you are in the user namespace.

This is a good place to look for new commands when you are working with different templates or to add the convenience you got use to.

Wednesday, August 8, 2018

clojurescript tips 1.0

Clojurescript:

You can access the browser state in the repl.  use
<namespace>/<atom-name>.
or change to the namespace using

(ns example.core)

Note: @ needs to be before a fully qualified variable!!!
(defn process-data
  (let [app-state @example.core/app-state]
    (example.horror/mangle! app-state)))

Do not use js/console.log to display clojure objects because javascript will display strange things.  Turn on (enable-console-print!) and then use (println xxxxxx).

You should also load the clojure formatters in your browser: https://github.com/binaryage/cljs-devtools/blob/master/docs/installation.md

This makes things look even better.


To stop figwheel from loading stuff on every reload, add a defonce variable and call the functions you only want run once.  The trick here is to set the variable to a non null value after running your functions.



1
2
3
4
5
(defonce setup-app
  (do
    (add-three-js-scene)
    (initialize-app)
    :done))  ;;setting setup-app to :done


Code formatting on blogspot

This is the highlighter I thought I lost the link to!  http://hilite.me/
from: https://rusya7.blogspot.com/2015/02/how-to-insert-code-blocks-in-blogger.html


So I tried a new one with ok results.  Posting here so I don't lose it!
http://codeformatter.blogspot.com/

I didn't like this one much https://tohtml.com/c/