pretty

Sunday 2 July 2023

Running Docker MySQL container on MacOS

1. Launch docker desktop server on MacOS via Launchpad icon.

Docker desktop application can be downloaded from the docker web site.
After launch the docker icon should appear on the right top icon tray of the menu. Docker can be used from command line now, lets check the docker is running and the version.

docker --version
Docker version 23.0.5, build bc4487a

1.1 Get docker mysql image.
docker run mysql:latest

2. Launch MySQL docker container.
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql
Arguments:
-p publish port from container and map it to host OS port, this is to connect to MySQL from the outside environment later.
-e env variables, used only MYSQL_ROOT_PASSWORD in this case and set a new 'pwd' for the root user. Optionally, we can create more users with corresponding passwords on this step, providing more env parameters like this:
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd MYSQL_USER=newuser MYSQL_PASSWORD newpwd -d mysql
-d daemon mode
-'mysql' would be a new container name

2.1 Check that container is running and get its id from table CONTAINER ID.
docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
7fc7ee05859d   mysql     "docker-entrypoint.s…"   26 minutes ago   Up 26 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   angry_fermi

3. Connect to mysql container by CONTAINER ID (get it from previous step) using docker shell.
docker exec -it 7fc7ee05859d bash
Arguments:
-it interactive mode (shell)
-'7fc7ee05859d' is a container id
Now, inside the container, we can launch mysql console providing the root user name and actual password that was set on step 2.
mysql -uroot -p       
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

3.1 Lets check that MySQL is really available to the outside of the container, as we mapped internal port of the container to OS port 3306 on step 2.

One of the options to do this is to install MySQL workbench application from Oracle on the host computer. When launched it allows to create a new MySQL Connection. Supplying 127.0.0.1:3306 for this new connection (that is localhost:port, that we mapped to on step 2), we should be able to connect to the DB inside the docker container.

Saturday 15 April 2023

Inspect sqlite database file from iOS emulator

1. View sqlite file location.

Launch XCode and follow these steps:
- XCode Menu Bar > Product > Scheme > Edit Scheme
- Run option
- Add or modify the Arguments passed on launch to contain -com.apple.CoreData.SQLDebug 1
- Launch the application and wait for Output to have a string with db location, like:

CoreData: annotation: Connecting to sqlite database file at "/Users/user/Library/Developer/CoreSimulator/Devices/DA54BPCB-39F1-4D19-888A-FA146477606DD/data/Containers/Data/Application/567443-7273-4B75-BFDA-86756/Library/Application Support/Data.sqlite"

2. Knowing the location, open Terminal and cd into SQlite db location. It may look like this:

cd "/Users/user/Library/Developer/CoreSimulator/Devices/DA54BPCB-39F1-4D19-888A-FA146477606DD/data/Containers/Data/Application/567443-7273-4B75-BFDA-86756/Library/Application Support/"

Copy 3 database files from this directory to desktop, or some other convenient directory:
cp Data.sqlite /Users/user/Desktop
cp Data.sqlite-shm /Users/user/Desktop
cp Data.sqlite-wal /Users/user/Desktop

3. Open the copied Data.sqlite from /Desktop with apps from AppStore, like Ridill SQLite or SQLiteFlow.

Monday 6 February 2023

Kotlin return statement in anonymous function vs inlined lambda functions

Having a return statement in an anonymous function in Kotlin acts like a break statement. The return statement without a @label always returns from the nearest function declared with a 'fun' keyword (Anonymous function doc).

On the other hand, return statement inside a lambda function, that was passed to an inline function, exits the enclosing outer function. The lambda is inlined, so the return inside it is actually treated as a return from outer function scope.