Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-06

Database migration

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Darius Blaszyk

Hi all,

After spending the last 12 months working intensively on a PHP based project, I moved back to FPC once again for another project. One of the more interesting concepts working in PHP was migrations. These are simple scripts that define the columns in a database table including indexes and foreign key constraints. Next to that also seeding and factories are integrated in this design. A trivial example is shown below;

        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });

I already did some tests in pascal and it seems quite doable to create a system like this. But before I proceed I would like to check here if something like this already exists and if other developers are interested in this. If so I would publish the sources on github.

Rgds, Darius

Reply by email

leledumbo
to Darius Blaszyk
But before I proceed I would like to check here if something like this

already exists

None that I know of for native Pascal. As I no longer use ORM, Sqitch does the job for me these days. Basically it's like a git for database schema (surely you can do CRUD as well there, but it's mainly for schema). I have an experience of building such a tool myself, not a good days. I'd rather use what others have already made and proven to be working well.

Reply by email

Michael Van Canneyt
to Jean SUZINEAU
Jean SUZINEAU wrote 44 lines
Hello, Le 19/06/2020 à 13:55, Darius Blaszyk via fpc-pascal a écrit : Schema::create('flights', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('airline'); $table->timestamps(); }); I have something that looks a bit like this, a kind of ORM (sorry, part of my identifiers are in French, "champ" means "field") ... in the class declaration //champs persistants public nUser: Integer; nProject: Integer; Beginning: TDateTime; cBeginning: TChamp; End_ : TDateTime; cEnd : TChamp; Description: String; ... in the implementation of the constructor : Champs.ChampDefinitions.NomTable:= 'Work'; //champs persistants Integer_from_ ( nUser , 'nUser' ); Integer_from_ ( nProject , 'nProject' ); cBeginning:= DateTime_from_( Beginning , 'Beginning' ); cBeginning.Definition.Format_DateTime:= 'yyyy/mm/dd" "hh:nn'; cEnd:= DateTime_from_( End_ , 'End' ); cEnd.Definition.Format_DateTime:= 'yyyy/mm/dd" "hh:nn'; (this is extracted from class TblWork from https://github.com/jsuzineau/pascal_o_r_mapping/blob/master/jsWorks/Elements/Work/ublWork.pas ) This class is for a row of the table "Work", most of the time, the instance is initialized by reading a row of dataset, but I've started some code to read from some other source, like android database or JSON data. I've made a source code generator which can query the structure of a database, and generate source codes from templates. Particularly it can create the skeleton of unit ublWork.pas which contains class TblWork

FPC contains this as well since many years, you can drop the components on a form, but they are exposed in the lazarus database desktop as well.

Michael.

Reply by email

Jean SUZINEAU
to Michael Van Canneyt

Le 21/06/2020 à 00:30, Michael Van Canneyt a écrit :

FPC contains this as well since many years, you can drop the components on a form, but they are exposed in the lazarus database desktop as well.

Very interesting, I didn't know this existed. I will have fact I developed my code around 2004 for Delphi 7 and migrated it to fpc/lazarus around 2013 to be able to run it on linux server.

Unfortunately my code is progressively abandoned now.

Trader en karma J'adore me regarder penser... ;-) http://jean.suzineau.pagesperso-orange.fr/ : mes pages perso http://www.mars42.com : mes pages professionnelles, astronomie & informatique

Reply by email

Michael Van Canneyt
to Jean SUZINEAU
Jean SUZINEAU wrote 11 lines
Le 21/06/2020 à 00:30, Michael Van Canneyt a écrit : > FPC contains this as well since many years, you can drop the > components on a form, but they > are exposed in the lazarus database desktop as well. Very interesting, I didn't know this existed. I will have look at it. In fact I developed my code around 2004 for Delphi 7 and migrated it to fpc/lazarus around 2013 to be able to run it on linux server. Unfortunately my code is progressively abandoned now.

I use the functionality in the database desktop actively. The generated code even runs in Delphi.

If you look at it and find ways to improve it, I'm all ears :-)

Michael.

Reply by email

Michael Van Canneyt
to Jean SUZINEAU
Jean SUZINEAU wrote 14 lines
Le 21/06/2020 à 09:55, Michael Van Canneyt a écrit : I use the functionality in the database desktop actively. The generated code even runs in Delphi. If you look at it and find ways to improve it, I'm all ears :-) I got a quick look at it (Lazarus 2.0.6, FPC 3.0.4). I saw there is a distinct generator for each case: fpcsrc/3.0.4/packages/fcl-db/src/codegen/fpcgdbcoll.pp fpcsrc/3.0.4/packages/fcl-db/src/codegen/fpcgcreatedbf.pp fpcsrc/3.0.4/packages/fcl-db/src/codegen/fpcgtiopf.pp In my case, there is a single generator which is  based on StringReplace with key/values stored in a StringList.

I've seen other code generators that use a similar approach as yours: use templates. I considered but discarded that approach early on because there is too much logic involved; I would end up with too many templates.

Michael.

Reply by email